- Release bridge interface's serializer when configuring member interface
[dragonfly.git] / sys / net / bridge / if_bridge.c
blob83a095c526de300493be07065129b8fcb315e0b3
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.34 2008/06/17 11:49:11 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 *, bridge_ctl_t, void *);
238 static int bridge_ioctl_add(struct bridge_softc *, void *);
239 static int bridge_ioctl_del(struct bridge_softc *, void *);
240 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
241 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
242 static int bridge_ioctl_scache(struct bridge_softc *, void *);
243 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
244 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
245 static int bridge_ioctl_rts(struct bridge_softc *, void *);
246 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
247 static int bridge_ioctl_sto(struct bridge_softc *, void *);
248 static int bridge_ioctl_gto(struct bridge_softc *, void *);
249 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
250 static int bridge_ioctl_flush(struct bridge_softc *, void *);
251 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
252 static int bridge_ioctl_spri(struct bridge_softc *, void *);
253 static int bridge_ioctl_ght(struct bridge_softc *, void *);
254 static int bridge_ioctl_sht(struct bridge_softc *, void *);
255 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
256 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
257 static int bridge_ioctl_gma(struct bridge_softc *, void *);
258 static int bridge_ioctl_sma(struct bridge_softc *, void *);
259 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
260 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
261 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
262 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
263 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
264 int);
265 static int bridge_ip_checkbasic(struct mbuf **mp);
266 #ifdef INET6
267 static int bridge_ip6_checkbasic(struct mbuf **mp);
268 #endif /* INET6 */
269 static int bridge_fragment(struct ifnet *, struct mbuf *,
270 struct ether_header *, int, struct llc *);
271 static void bridge_enqueue_internal(struct ifnet *, struct mbuf *m,
272 netisr_fn_t);
273 static void bridge_enqueue_handler(struct netmsg *);
274 static void bridge_pfil_enqueue_handler(struct netmsg *);
275 static void bridge_pfil_enqueue(struct ifnet *, struct mbuf *, int);
276 static void bridge_handoff_notags(struct ifnet *, struct mbuf *);
277 static void bridge_handoff(struct ifnet *, struct mbuf *);
279 SYSCTL_DECL(_net_link);
280 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
282 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
283 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
284 static int pfil_member = 1; /* run pfil hooks on the member interface */
285 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
286 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
287 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
288 &pfil_bridge, 0, "Packet filter on the bridge interface");
289 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
290 &pfil_member, 0, "Packet filter on the member interface");
292 struct bridge_control {
293 bridge_ctl_t bc_func;
294 int bc_argsize;
295 int bc_flags;
298 #define BC_F_COPYIN 0x01 /* copy arguments in */
299 #define BC_F_COPYOUT 0x02 /* copy arguments out */
300 #define BC_F_SUSER 0x04 /* do super-user check */
302 const struct bridge_control bridge_control_table[] = {
303 { bridge_ioctl_add, sizeof(struct ifbreq),
304 BC_F_COPYIN|BC_F_SUSER },
305 { bridge_ioctl_del, sizeof(struct ifbreq),
306 BC_F_COPYIN|BC_F_SUSER },
308 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
309 BC_F_COPYIN|BC_F_COPYOUT },
310 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
311 BC_F_COPYIN|BC_F_SUSER },
313 { bridge_ioctl_scache, sizeof(struct ifbrparam),
314 BC_F_COPYIN|BC_F_SUSER },
315 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
316 BC_F_COPYOUT },
318 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
319 BC_F_COPYIN|BC_F_COPYOUT },
320 { bridge_ioctl_rts, sizeof(struct ifbaconf),
321 BC_F_COPYIN|BC_F_COPYOUT },
323 { bridge_ioctl_saddr, sizeof(struct ifbareq),
324 BC_F_COPYIN|BC_F_SUSER },
326 { bridge_ioctl_sto, sizeof(struct ifbrparam),
327 BC_F_COPYIN|BC_F_SUSER },
328 { bridge_ioctl_gto, sizeof(struct ifbrparam),
329 BC_F_COPYOUT },
331 { bridge_ioctl_daddr, sizeof(struct ifbareq),
332 BC_F_COPYIN|BC_F_SUSER },
334 { bridge_ioctl_flush, sizeof(struct ifbreq),
335 BC_F_COPYIN|BC_F_SUSER },
337 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
338 BC_F_COPYOUT },
339 { bridge_ioctl_spri, sizeof(struct ifbrparam),
340 BC_F_COPYIN|BC_F_SUSER },
342 { bridge_ioctl_ght, sizeof(struct ifbrparam),
343 BC_F_COPYOUT },
344 { bridge_ioctl_sht, sizeof(struct ifbrparam),
345 BC_F_COPYIN|BC_F_SUSER },
347 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
348 BC_F_COPYOUT },
349 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
350 BC_F_COPYIN|BC_F_SUSER },
352 { bridge_ioctl_gma, sizeof(struct ifbrparam),
353 BC_F_COPYOUT },
354 { bridge_ioctl_sma, sizeof(struct ifbrparam),
355 BC_F_COPYIN|BC_F_SUSER },
357 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
358 BC_F_COPYIN|BC_F_SUSER },
360 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
361 BC_F_COPYIN|BC_F_SUSER },
363 { bridge_ioctl_addspan, sizeof(struct ifbreq),
364 BC_F_COPYIN|BC_F_SUSER },
365 { bridge_ioctl_delspan, sizeof(struct ifbreq),
366 BC_F_COPYIN|BC_F_SUSER },
368 const int bridge_control_table_size =
369 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
371 LIST_HEAD(, bridge_softc) bridge_list;
373 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
374 bridge_clone_create,
375 bridge_clone_destroy, 0, IF_MAXUNIT);
377 static int
378 bridge_modevent(module_t mod, int type, void *data)
380 switch (type) {
381 case MOD_LOAD:
382 LIST_INIT(&bridge_list);
383 if_clone_attach(&bridge_cloner);
384 bridge_input_p = bridge_input;
385 bridge_output_p = bridge_output;
386 bridge_detach_cookie = EVENTHANDLER_REGISTER(
387 ifnet_detach_event, bridge_ifdetach, NULL,
388 EVENTHANDLER_PRI_ANY);
389 #if notyet
390 bstp_linkstate_p = bstp_linkstate;
391 #endif
392 break;
393 case MOD_UNLOAD:
394 if (!LIST_EMPTY(&bridge_list))
395 return (EBUSY);
396 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
397 bridge_detach_cookie);
398 if_clone_detach(&bridge_cloner);
399 bridge_input_p = NULL;
400 bridge_output_p = NULL;
401 #if notyet
402 bstp_linkstate_p = NULL;
403 #endif
404 break;
405 default:
406 return (EOPNOTSUPP);
408 return (0);
411 static moduledata_t bridge_mod = {
412 "if_bridge",
413 bridge_modevent,
417 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
421 * bridge_clone_create:
423 * Create a new bridge instance.
425 static int
426 bridge_clone_create(struct if_clone *ifc, int unit)
428 struct bridge_softc *sc;
429 struct ifnet *ifp;
430 u_char eaddr[6];
432 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
433 ifp = sc->sc_ifp = &sc->sc_if;
435 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
436 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
437 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
438 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
439 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
440 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
441 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
443 /* Initialize our routing table. */
444 bridge_rtable_init(sc);
446 callout_init(&sc->sc_brcallout);
447 callout_init(&sc->sc_bstpcallout);
449 LIST_INIT(&sc->sc_iflist);
450 LIST_INIT(&sc->sc_spanlist);
452 ifp->if_softc = sc;
453 if_initname(ifp, ifc->ifc_name, unit);
454 ifp->if_mtu = ETHERMTU;
455 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
456 ifp->if_ioctl = bridge_ioctl;
457 ifp->if_start = bridge_start;
458 ifp->if_init = bridge_init;
459 ifp->if_type = IFT_BRIDGE;
460 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
461 ifp->if_snd.ifq_maxlen = ifqmaxlen;
462 ifq_set_ready(&ifp->if_snd);
463 ifp->if_hdrlen = ETHER_HDR_LEN;
466 * Generate a random ethernet address and use the private AC:DE:48
467 * OUI code.
470 int rnd = karc4random();
471 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
472 rnd = karc4random();
473 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
475 eaddr[0] &= ~1; /* clear multicast bit */
476 eaddr[0] |= 2; /* set the LAA bit */
478 ether_ifattach(ifp, eaddr, NULL);
479 /* Now undo some of the damage... */
480 ifp->if_baudrate = 0;
481 ifp->if_type = IFT_BRIDGE;
483 crit_enter();
484 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
485 crit_exit();
487 return (0);
491 * bridge_clone_destroy:
493 * Destroy a bridge instance.
495 static void
496 bridge_clone_destroy(struct ifnet *ifp)
498 struct bridge_softc *sc = ifp->if_softc;
499 struct bridge_iflist *bif;
501 lwkt_serialize_enter(ifp->if_serializer);
503 bridge_stop(ifp);
504 ifp->if_flags &= ~IFF_UP;
506 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
507 bridge_delete_member(sc, bif, 0);
509 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
510 bridge_delete_span(sc, bif);
513 callout_stop(&sc->sc_brcallout);
514 callout_stop(&sc->sc_bstpcallout);
516 lwkt_serialize_exit(ifp->if_serializer);
518 crit_enter();
519 LIST_REMOVE(sc, sc_list);
520 crit_exit();
522 ether_ifdetach(ifp);
524 /* Tear down the routing table. */
525 bridge_rtable_fini(sc);
527 kfree(sc, M_DEVBUF);
531 * bridge_ioctl:
533 * Handle a control request from the operator.
535 static int
536 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
538 struct bridge_softc *sc = ifp->if_softc;
539 union {
540 struct ifbreq ifbreq;
541 struct ifbifconf ifbifconf;
542 struct ifbareq ifbareq;
543 struct ifbaconf ifbaconf;
544 struct ifbrparam ifbrparam;
545 } args;
546 struct ifdrv *ifd = (struct ifdrv *) data;
547 const struct bridge_control *bc;
548 int error = 0;
550 ASSERT_SERIALIZED(ifp->if_serializer);
552 switch (cmd) {
553 case SIOCADDMULTI:
554 case SIOCDELMULTI:
555 break;
557 case SIOCGDRVSPEC:
558 case SIOCSDRVSPEC:
559 if (ifd->ifd_cmd >= bridge_control_table_size) {
560 error = EINVAL;
561 break;
563 bc = &bridge_control_table[ifd->ifd_cmd];
565 if (cmd == SIOCGDRVSPEC &&
566 (bc->bc_flags & BC_F_COPYOUT) == 0) {
567 error = EINVAL;
568 break;
569 } else if (cmd == SIOCSDRVSPEC &&
570 (bc->bc_flags & BC_F_COPYOUT) != 0) {
571 error = EINVAL;
572 break;
575 if (bc->bc_flags & BC_F_SUSER) {
576 error = suser_cred(cr, NULL_CRED_OKAY);
577 if (error)
578 break;
581 if (ifd->ifd_len != bc->bc_argsize ||
582 ifd->ifd_len > sizeof(args)) {
583 error = EINVAL;
584 break;
587 memset(&args, 0, sizeof(args));
588 if (bc->bc_flags & BC_F_COPYIN) {
589 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
590 if (error)
591 break;
594 error = bridge_control(sc, bc->bc_func, &args);
595 if (error)
596 break;
598 if (bc->bc_flags & BC_F_COPYOUT)
599 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
600 break;
602 case SIOCSIFFLAGS:
603 if (!(ifp->if_flags & IFF_UP) &&
604 (ifp->if_flags & IFF_RUNNING)) {
606 * If interface is marked down and it is running,
607 * then stop it.
609 bridge_stop(ifp);
610 } else if ((ifp->if_flags & IFF_UP) &&
611 !(ifp->if_flags & IFF_RUNNING)) {
613 * If interface is marked up and it is stopped, then
614 * start it.
616 ifp->if_init(sc);
618 break;
620 case SIOCSIFMTU:
621 /* Do not allow the MTU to be changed on the bridge */
622 error = EINVAL;
623 break;
625 default:
626 error = ether_ioctl(ifp, cmd, data);
627 break;
629 return (error);
633 * bridge_mutecaps:
635 * Clear or restore unwanted capabilities on the member interface
637 static void
638 bridge_mutecaps(struct bridge_iflist *bif, int mute)
640 struct ifnet *ifp = bif->bif_ifp;
641 struct ifreq ifr;
642 int error;
644 if (ifp->if_ioctl == NULL)
645 return;
647 bzero(&ifr, sizeof(ifr));
648 ifr.ifr_reqcap = ifp->if_capenable;
650 if (mute) {
651 /* mask off and save capabilities */
652 bif->bif_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
653 if (bif->bif_mutecap != 0)
654 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
655 } else {
656 /* restore muted capabilities */
657 ifr.ifr_reqcap |= bif->bif_mutecap;
660 if (bif->bif_mutecap != 0) {
661 lwkt_serialize_enter(ifp->if_serializer);
662 error = ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
663 lwkt_serialize_exit(ifp->if_serializer);
668 * bridge_lookup_member:
670 * Lookup a bridge member interface.
672 static struct bridge_iflist *
673 bridge_lookup_member(struct bridge_softc *sc, const char *name)
675 struct bridge_iflist *bif;
676 struct ifnet *ifp;
678 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
679 ifp = bif->bif_ifp;
680 if (strcmp(ifp->if_xname, name) == 0)
681 return (bif);
684 return (NULL);
688 * bridge_lookup_member_if:
690 * Lookup a bridge member interface by ifnet*.
692 static struct bridge_iflist *
693 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
695 struct bridge_iflist *bif;
697 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
698 if (bif->bif_ifp == member_ifp)
699 return (bif);
702 return (NULL);
706 * bridge_delete_member:
708 * Delete the specified member interface.
710 static void
711 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
712 int gone)
714 struct ifnet *ifs = bif->bif_ifp;
716 if (!gone) {
717 switch (ifs->if_type) {
718 case IFT_ETHER:
719 case IFT_L2VLAN:
721 * Take the interface out of promiscuous mode.
723 (void) ifpromisc(ifs, 0);
724 bridge_mutecaps(bif, 0);
725 break;
727 case IFT_GIF:
728 break;
730 default:
731 #ifdef DIAGNOSTIC
732 panic("bridge_delete_member: impossible");
733 #endif
734 break;
738 ifs->if_bridge = NULL;
739 LIST_REMOVE(bif, bif_next);
741 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
743 kfree(bif, M_DEVBUF);
745 if (sc->sc_ifp->if_flags & IFF_RUNNING)
746 bstp_initialization(sc);
750 * bridge_delete_span:
752 * Delete the specified span interface.
754 static void
755 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
757 KASSERT(bif->bif_ifp->if_bridge == NULL,
758 ("%s: not a span interface", __func__));
760 LIST_REMOVE(bif, bif_next);
761 kfree(bif, M_DEVBUF);
764 static int
765 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
767 struct ifbreq *req = arg;
768 struct bridge_iflist *bif = NULL;
769 struct ifnet *ifs, *bifp;
770 int error = 0;
772 bifp = sc->sc_ifp;
773 ASSERT_SERIALIZED(bifp->if_serializer);
775 ifs = ifunit(req->ifbr_ifsname);
776 if (ifs == NULL)
777 return (ENOENT);
779 /* If it's in the span list, it can't be a member. */
780 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
781 if (ifs == bif->bif_ifp)
782 return (EBUSY);
784 /* Allow the first Ethernet member to define the MTU */
785 if (ifs->if_type != IFT_GIF) {
786 if (LIST_EMPTY(&sc->sc_iflist)) {
787 bifp->if_mtu = ifs->if_mtu;
788 } else if (bifp->if_mtu != ifs->if_mtu) {
789 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
790 return (EINVAL);
794 if (ifs->if_bridge == sc)
795 return (EEXIST);
797 if (ifs->if_bridge != NULL)
798 return (EBUSY);
800 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
801 bif->bif_ifp = ifs;
802 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
803 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
804 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
806 switch (ifs->if_type) {
807 case IFT_ETHER:
808 case IFT_L2VLAN:
810 * Release bridge interface's serializer to
811 * avoid possible dead lock.
813 lwkt_serialize_exit(bifp->if_serializer);
816 * Place the interface into promiscuous mode.
818 error = ifpromisc(ifs, 1);
819 if (error) {
820 lwkt_serialize_enter(bifp->if_serializer);
821 goto out;
824 bridge_mutecaps(bif, 1);
826 lwkt_serialize_enter(bifp->if_serializer);
827 break;
829 case IFT_GIF: /* :^) */
830 break;
832 default:
833 error = EINVAL;
834 goto out;
837 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
839 if (bifp->if_flags & IFF_RUNNING)
840 bstp_initialization(sc);
841 else
842 bstp_stop(sc);
845 * Everything has been setup, so let the member interface
846 * deliver packets to this bridge on its input/output path.
848 ifs->if_bridge = sc;
849 out:
850 if (error) {
851 if (bif != NULL)
852 kfree(bif, M_DEVBUF);
854 return (error);
857 static int
858 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
860 struct ifbreq *req = arg;
861 struct bridge_iflist *bif;
863 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
864 if (bif == NULL)
865 return (ENOENT);
867 bridge_delete_member(sc, bif, 0);
869 return (0);
872 static int
873 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
875 struct ifbreq *req = arg;
876 struct bridge_iflist *bif;
878 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
879 if (bif == NULL)
880 return (ENOENT);
882 req->ifbr_ifsflags = bif->bif_flags;
883 req->ifbr_state = bif->bif_state;
884 req->ifbr_priority = bif->bif_priority;
885 req->ifbr_path_cost = bif->bif_path_cost;
886 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
888 return (0);
891 static int
892 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
894 struct ifbreq *req = arg;
895 struct bridge_iflist *bif;
897 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
898 if (bif == NULL)
899 return (ENOENT);
901 if (req->ifbr_ifsflags & IFBIF_SPAN)
902 /* SPAN is readonly */
903 return (EINVAL);
905 if (req->ifbr_ifsflags & IFBIF_STP) {
906 switch (bif->bif_ifp->if_type) {
907 case IFT_ETHER:
908 /* These can do spanning tree. */
909 break;
911 default:
912 /* Nothing else can. */
913 return (EINVAL);
917 bif->bif_flags = req->ifbr_ifsflags;
919 if (sc->sc_ifp->if_flags & IFF_RUNNING)
920 bstp_initialization(sc);
922 return (0);
925 static int
926 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
928 struct ifbrparam *param = arg;
930 sc->sc_brtmax = param->ifbrp_csize;
931 bridge_rttrim(sc);
933 return (0);
936 static int
937 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
939 struct ifbrparam *param = arg;
941 param->ifbrp_csize = sc->sc_brtmax;
943 return (0);
946 static int
947 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
949 struct ifbifconf *bifc = arg;
950 struct bridge_iflist *bif;
951 struct ifbreq breq;
952 int count, len, error = 0;
954 count = 0;
955 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
956 count++;
957 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
958 count++;
960 if (bifc->ifbic_len == 0) {
961 bifc->ifbic_len = sizeof(breq) * count;
962 return (0);
965 count = 0;
966 len = bifc->ifbic_len;
967 memset(&breq, 0, sizeof breq);
968 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
969 if (len < sizeof(breq))
970 break;
972 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
973 sizeof(breq.ifbr_ifsname));
974 breq.ifbr_ifsflags = bif->bif_flags;
975 breq.ifbr_state = bif->bif_state;
976 breq.ifbr_priority = bif->bif_priority;
977 breq.ifbr_path_cost = bif->bif_path_cost;
978 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
979 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
980 if (error)
981 break;
982 count++;
983 len -= sizeof(breq);
985 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
986 if (len < sizeof(breq))
987 break;
989 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
990 sizeof(breq.ifbr_ifsname));
991 breq.ifbr_ifsflags = bif->bif_flags;
992 breq.ifbr_state = bif->bif_state;
993 breq.ifbr_priority = bif->bif_priority;
994 breq.ifbr_path_cost = bif->bif_path_cost;
995 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
996 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
997 if (error)
998 break;
999 count++;
1000 len -= sizeof(breq);
1003 bifc->ifbic_len = sizeof(breq) * count;
1004 return (error);
1007 static int
1008 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1010 struct ifbaconf *bac = arg;
1011 struct bridge_rtnode *brt;
1012 struct ifbareq bareq;
1013 int count = 0, error = 0, len;
1015 if (bac->ifbac_len == 0)
1016 return (0);
1018 len = bac->ifbac_len;
1019 memset(&bareq, 0, sizeof(bareq));
1020 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1021 if (len < sizeof(bareq))
1022 goto out;
1023 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1024 sizeof(bareq.ifba_ifsname));
1025 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1026 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1027 time_second < brt->brt_expire)
1028 bareq.ifba_expire = brt->brt_expire - time_second;
1029 else
1030 bareq.ifba_expire = 0;
1031 bareq.ifba_flags = brt->brt_flags;
1033 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1034 if (error)
1035 goto out;
1036 count++;
1037 len -= sizeof(bareq);
1039 out:
1040 bac->ifbac_len = sizeof(bareq) * count;
1041 return (error);
1044 static int
1045 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1047 struct ifbareq *req = arg;
1048 struct bridge_iflist *bif;
1049 int error;
1051 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1052 if (bif == NULL)
1053 return (ENOENT);
1055 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1056 req->ifba_flags);
1058 return (error);
1061 static int
1062 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1064 struct ifbrparam *param = arg;
1066 sc->sc_brttimeout = param->ifbrp_ctime;
1068 return (0);
1071 static int
1072 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1074 struct ifbrparam *param = arg;
1076 param->ifbrp_ctime = sc->sc_brttimeout;
1078 return (0);
1081 static int
1082 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1084 struct ifbareq *req = arg;
1086 return (bridge_rtdaddr(sc, req->ifba_dst));
1089 static int
1090 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1092 struct ifbreq *req = arg;
1094 bridge_rtflush(sc, req->ifbr_ifsflags);
1096 return (0);
1099 static int
1100 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1102 struct ifbrparam *param = arg;
1104 param->ifbrp_prio = sc->sc_bridge_priority;
1106 return (0);
1109 static int
1110 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1112 struct ifbrparam *param = arg;
1114 sc->sc_bridge_priority = param->ifbrp_prio;
1116 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1117 bstp_initialization(sc);
1119 return (0);
1122 static int
1123 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1125 struct ifbrparam *param = arg;
1127 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1129 return (0);
1132 static int
1133 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1135 struct ifbrparam *param = arg;
1137 if (param->ifbrp_hellotime == 0)
1138 return (EINVAL);
1139 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1141 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1142 bstp_initialization(sc);
1144 return (0);
1147 static int
1148 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1150 struct ifbrparam *param = arg;
1152 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1154 return (0);
1157 static int
1158 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1160 struct ifbrparam *param = arg;
1162 if (param->ifbrp_fwddelay == 0)
1163 return (EINVAL);
1164 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1166 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1167 bstp_initialization(sc);
1169 return (0);
1172 static int
1173 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1175 struct ifbrparam *param = arg;
1177 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1179 return (0);
1182 static int
1183 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1185 struct ifbrparam *param = arg;
1187 if (param->ifbrp_maxage == 0)
1188 return (EINVAL);
1189 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1191 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1192 bstp_initialization(sc);
1194 return (0);
1197 static int
1198 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1200 struct ifbreq *req = arg;
1201 struct bridge_iflist *bif;
1203 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1204 if (bif == NULL)
1205 return (ENOENT);
1207 bif->bif_priority = req->ifbr_priority;
1209 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1210 bstp_initialization(sc);
1212 return (0);
1215 static int
1216 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1218 struct ifbreq *req = arg;
1219 struct bridge_iflist *bif;
1221 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1222 if (bif == NULL)
1223 return (ENOENT);
1225 bif->bif_path_cost = req->ifbr_path_cost;
1227 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1228 bstp_initialization(sc);
1230 return (0);
1233 static int
1234 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1236 struct ifbreq *req = arg;
1237 struct bridge_iflist *bif = NULL;
1238 struct ifnet *ifs;
1240 ifs = ifunit(req->ifbr_ifsname);
1241 if (ifs == NULL)
1242 return (ENOENT);
1244 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1245 if (ifs == bif->bif_ifp)
1246 return (EBUSY);
1248 if (ifs->if_bridge != NULL)
1249 return (EBUSY);
1251 switch (ifs->if_type) {
1252 case IFT_ETHER:
1253 case IFT_GIF:
1254 case IFT_L2VLAN:
1255 break;
1256 default:
1257 return (EINVAL);
1260 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
1262 bif->bif_ifp = ifs;
1263 bif->bif_flags = IFBIF_SPAN;
1265 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1267 return (0);
1270 static int
1271 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1273 struct ifbreq *req = arg;
1274 struct bridge_iflist *bif;
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 break;
1285 if (bif == NULL)
1286 return (ENOENT);
1288 bridge_delete_span(sc, bif);
1290 return (0);
1294 * bridge_ifdetach:
1296 * Detach an interface from a bridge. Called when a member
1297 * interface is detaching.
1299 static void
1300 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1302 struct bridge_softc *sc = ifp->if_bridge;
1303 struct bridge_iflist *bif;
1305 /* Check if the interface is a bridge member */
1306 if (sc != NULL) {
1307 lwkt_serialize_enter(ifp->if_serializer);
1309 bif = bridge_lookup_member_if(sc, ifp);
1310 if (bif != NULL)
1311 bridge_delete_member(sc, bif, 1);
1313 lwkt_serialize_exit(ifp->if_serializer);
1314 return;
1317 /* Check if the interface is a span port */
1318 LIST_FOREACH(sc, &bridge_list, sc_list) {
1319 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1320 if (ifp == bif->bif_ifp) {
1321 bridge_delete_span(sc, bif);
1322 break;
1328 * bridge_init:
1330 * Initialize a bridge interface.
1332 static void
1333 bridge_init(void *xsc)
1335 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1336 struct ifnet *ifp = sc->sc_ifp;
1338 ASSERT_SERIALIZED(ifp->if_serializer);
1340 if (ifp->if_flags & IFF_RUNNING)
1341 return;
1343 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1344 bridge_timer, sc);
1346 ifp->if_flags |= IFF_RUNNING;
1347 bstp_initialization(sc);
1348 return;
1352 * bridge_stop:
1354 * Stop the bridge interface.
1356 static void
1357 bridge_stop(struct ifnet *ifp)
1359 struct bridge_softc *sc = ifp->if_softc;
1361 ASSERT_SERIALIZED(ifp->if_serializer);
1363 if ((ifp->if_flags & IFF_RUNNING) == 0)
1364 return;
1366 callout_stop(&sc->sc_brcallout);
1367 bstp_stop(sc);
1369 bridge_rtflush(sc, IFBF_FLUSHDYN);
1371 ifp->if_flags &= ~IFF_RUNNING;
1374 static void
1375 bridge_enqueue_internal(struct ifnet *dst_ifp, struct mbuf *m,
1376 netisr_fn_t handler)
1378 struct netmsg_packet *nmp;
1379 lwkt_port_t port;
1380 int cpu = mycpu->gd_cpuid;
1382 while (m->m_type == MT_TAG) {
1383 /* XXX see ether_output_frame for full rules check */
1384 m = m->m_next;
1387 nmp = &m->m_hdr.mh_netmsg;
1388 netmsg_init(&nmp->nm_netmsg, &netisr_apanic_rport, 0, handler);
1389 nmp->nm_packet = m;
1390 nmp->nm_netmsg.nm_lmsg.u.ms_resultp = dst_ifp;
1392 port = cpu_portfn(cpu);
1393 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
1396 static void
1397 bridge_pfil_enqueue(struct ifnet *dst_ifp, struct mbuf *m,
1398 int runfilt)
1400 netisr_fn_t handler;
1402 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1403 #ifdef INET6
1404 || inet6_pfil_hook.ph_hashooks > 0
1405 #endif
1406 )) {
1407 handler = bridge_pfil_enqueue_handler;
1408 } else {
1409 handler = bridge_enqueue_handler;
1411 bridge_enqueue_internal(dst_ifp, m, handler);
1415 * bridge_enqueue:
1417 * Enqueue a packet on a bridge member interface.
1420 void
1421 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1423 bridge_enqueue_internal(dst_ifp, m, bridge_enqueue_handler);
1427 * bridge_output:
1429 * Send output from a bridge member interface. This
1430 * performs the bridging function for locally originated
1431 * packets.
1433 * The mbuf has the Ethernet header already attached. We must
1434 * enqueue or free the mbuf before returning.
1436 static int
1437 bridge_output(struct ifnet *ifp, struct mbuf *m)
1439 struct bridge_softc *sc = ifp->if_bridge;
1440 struct ether_header *eh;
1441 struct ifnet *dst_if;
1443 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
1446 * Make sure that we are still a member of a bridge interface.
1448 if (sc == NULL) {
1449 m_freem(m);
1450 return (0);
1453 if (m->m_len < ETHER_HDR_LEN) {
1454 m = m_pullup(m, ETHER_HDR_LEN);
1455 if (m == NULL)
1456 return (0);
1459 /* Serialize our bridge interface. */
1460 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1462 eh = mtod(m, struct ether_header *);
1465 * If bridge is down, but the original output interface is up,
1466 * go ahead and send out that interface. Otherwise, the packet
1467 * is dropped below.
1469 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
1470 dst_if = ifp;
1471 goto sendunicast;
1475 * If the packet is a multicast, or we don't know a better way to
1476 * get there, send to all interfaces.
1478 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1479 dst_if = NULL;
1480 else
1481 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1482 if (dst_if == NULL) {
1483 struct bridge_iflist *bif;
1484 struct mbuf *mc;
1485 int used = 0;
1487 bridge_span(sc, m);
1489 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1490 dst_if = bif->bif_ifp;
1491 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1492 continue;
1495 * If this is not the original output interface,
1496 * and the interface is participating in spanning
1497 * tree, make sure the port is in a state that
1498 * allows forwarding.
1500 if (dst_if != ifp &&
1501 (bif->bif_flags & IFBIF_STP) != 0) {
1502 switch (bif->bif_state) {
1503 case BSTP_IFSTATE_BLOCKING:
1504 case BSTP_IFSTATE_LISTENING:
1505 case BSTP_IFSTATE_DISABLED:
1506 continue;
1510 if (LIST_NEXT(bif, bif_next) == NULL) {
1511 used = 1;
1512 mc = m;
1513 } else {
1514 mc = m_copypacket(m, MB_DONTWAIT);
1515 if (mc == NULL) {
1516 sc->sc_ifp->if_oerrors++;
1517 continue;
1520 bridge_enqueue(dst_if, mc);
1522 if (used == 0)
1523 m_freem(m);
1524 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1525 return (0);
1528 sendunicast:
1530 * XXX Spanning tree consideration here?
1533 bridge_span(sc, m);
1534 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1535 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1536 m_freem(m);
1537 else
1538 bridge_enqueue(dst_if, m);
1539 return (0);
1543 * bridge_start:
1545 * Start output on a bridge.
1548 static void
1549 bridge_start(struct ifnet *ifp)
1551 struct bridge_softc *sc = ifp->if_softc;
1553 ASSERT_SERIALIZED(ifp->if_serializer);
1555 ifp->if_flags |= IFF_OACTIVE;
1556 for (;;) {
1557 struct ifnet *dst_if = NULL;
1558 struct ether_header *eh;
1559 struct mbuf *m;
1561 m = ifq_dequeue(&ifp->if_snd, NULL);
1562 if (m == NULL)
1563 break;
1565 if (m->m_len < sizeof(*eh)) {
1566 m = m_pullup(m, sizeof(*eh));
1567 if (m == NULL) {
1568 ifp->if_oerrors++;
1569 continue;
1572 eh = mtod(m, struct ether_header *);
1574 BPF_MTAP(ifp, m);
1575 ifp->if_opackets++;
1577 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1578 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1580 if (dst_if == NULL)
1581 bridge_broadcast(sc, ifp, m, 0);
1582 else
1583 bridge_enqueue(dst_if, m);
1585 ifp->if_flags &= ~IFF_OACTIVE;
1589 * bridge_forward:
1591 * The forwarding function of the bridge.
1593 static void
1594 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1596 struct bridge_iflist *bif;
1597 struct ifnet *src_if, *dst_if, *ifp;
1598 struct ether_header *eh;
1600 src_if = m->m_pkthdr.rcvif;
1601 ifp = sc->sc_ifp;
1603 ASSERT_SERIALIZED(ifp->if_serializer);
1605 ifp->if_ipackets++;
1606 ifp->if_ibytes += m->m_pkthdr.len;
1609 * Look up the bridge_iflist.
1611 bif = bridge_lookup_member_if(sc, src_if);
1612 if (bif == NULL) {
1613 /* Interface is not a bridge member (anymore?) */
1614 m_freem(m);
1615 return;
1618 if (bif->bif_flags & IFBIF_STP) {
1619 switch (bif->bif_state) {
1620 case BSTP_IFSTATE_BLOCKING:
1621 case BSTP_IFSTATE_LISTENING:
1622 case BSTP_IFSTATE_DISABLED:
1623 m_freem(m);
1624 return;
1628 eh = mtod(m, struct ether_header *);
1631 * If the interface is learning, and the source
1632 * address is valid and not multicast, record
1633 * the address.
1635 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1636 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1637 (eh->ether_shost[0] == 0 &&
1638 eh->ether_shost[1] == 0 &&
1639 eh->ether_shost[2] == 0 &&
1640 eh->ether_shost[3] == 0 &&
1641 eh->ether_shost[4] == 0 &&
1642 eh->ether_shost[5] == 0) == 0) {
1643 bridge_rtupdate(sc, eh->ether_shost, src_if, 0, IFBAF_DYNAMIC);
1646 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1647 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1648 m_freem(m);
1649 return;
1653 * At this point, the port either doesn't participate
1654 * in spanning tree or it is in the forwarding state.
1658 * If the packet is unicast, destined for someone on
1659 * "this" side of the bridge, drop it.
1661 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1662 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1663 if (src_if == dst_if) {
1664 m_freem(m);
1665 return;
1667 } else {
1668 /* ...forward it to all interfaces. */
1669 sc->sc_ifp->if_imcasts++;
1670 dst_if = NULL;
1673 if (dst_if == NULL) {
1674 bridge_broadcast(sc, src_if, m, 1);
1675 return;
1679 * At this point, we're dealing with a unicast frame
1680 * going to a different interface.
1682 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1683 m_freem(m);
1684 return;
1686 bif = bridge_lookup_member_if(sc, dst_if);
1687 if (bif == NULL) {
1688 /* Not a member of the bridge (anymore?) */
1689 m_freem(m);
1690 return;
1693 if (bif->bif_flags & IFBIF_STP) {
1694 switch (bif->bif_state) {
1695 case BSTP_IFSTATE_DISABLED:
1696 case BSTP_IFSTATE_BLOCKING:
1697 m_freem(m);
1698 return;
1702 lwkt_serialize_exit(ifp->if_serializer);
1704 /* run the packet filter */
1705 if (inet_pfil_hook.ph_hashooks > 0
1706 #ifdef INET6
1707 || inet6_pfil_hook.ph_hashooks > 0
1708 #endif
1710 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1711 goto done;
1712 if (m == NULL)
1713 goto done;
1715 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
1716 goto done;
1717 if (m == NULL)
1718 goto done;
1720 bridge_handoff(dst_if, m);
1723 * ifp's serializer was held on entry and is expected to be held
1724 * on return.
1726 done:
1727 lwkt_serialize_enter(ifp->if_serializer);
1731 * bridge_input:
1733 * Receive input from a member interface. Queue the packet for
1734 * bridging if it is not for us.
1736 static struct mbuf *
1737 bridge_input(struct ifnet *ifp, struct mbuf *m)
1739 struct bridge_softc *sc = ifp->if_bridge;
1740 struct bridge_iflist *bif;
1741 struct ifnet *bifp, *new_ifp;
1742 struct ether_header *eh;
1743 struct mbuf *mc, *mc2;
1746 * Make sure that we are still a member of a bridge interface.
1748 if (sc == NULL)
1749 return m;
1751 new_ifp = NULL;
1752 bifp = sc->sc_ifp;
1754 lwkt_serialize_enter(bifp->if_serializer);
1756 if ((bifp->if_flags & IFF_RUNNING) == 0)
1757 goto out;
1760 * Implement support for bridge monitoring. If this flag has been
1761 * set on this interface, discard the packet once we push it through
1762 * the bpf(4) machinery, but before we do, increment the byte and
1763 * packet counters associated with this interface.
1765 if ((bifp->if_flags & IFF_MONITOR) != 0) {
1766 m->m_pkthdr.rcvif = bifp;
1767 BPF_MTAP(bifp, m);
1768 bifp->if_ipackets++;
1769 bifp->if_ibytes += m->m_pkthdr.len;
1770 m_freem(m);
1771 m = NULL;
1772 goto out;
1775 eh = mtod(m, struct ether_header *);
1777 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
1779 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
1781 * If the packet is for us, set the packets source as the
1782 * bridge, and return the packet back to ifnet.if_input for
1783 * local processing.
1785 KASSERT(bifp->if_bridge == NULL,
1786 ("loop created in bridge_input"));
1787 new_ifp = bifp;
1788 goto out;
1792 * Tap all packets arriving on the bridge, no matter if
1793 * they are local destinations or not. In is in.
1795 BPF_MTAP(bifp, m);
1797 bif = bridge_lookup_member_if(sc, ifp);
1798 if (bif == NULL)
1799 goto out;
1801 bridge_span(sc, m);
1803 if (m->m_flags & (M_BCAST | M_MCAST)) {
1804 /* Tap off 802.1D packets; they do not get forwarded. */
1805 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1806 ETHER_ADDR_LEN) == 0) {
1807 m = bstp_input(sc, bif, m);
1808 KASSERT(m == NULL,
1809 ("attempt to deliver 802.1D packet\n"));
1810 goto out;
1813 if (bif->bif_flags & IFBIF_STP) {
1814 switch (bif->bif_state) {
1815 case BSTP_IFSTATE_BLOCKING:
1816 case BSTP_IFSTATE_LISTENING:
1817 case BSTP_IFSTATE_DISABLED:
1818 goto out;
1823 * Make a deep copy of the packet and enqueue the copy
1824 * for bridge processing; return the original packet for
1825 * local processing.
1827 mc = m_dup(m, MB_DONTWAIT);
1828 if (mc == NULL)
1829 goto out;
1831 bridge_forward(sc, mc);
1834 * Reinject the mbuf as arriving on the bridge so we have a
1835 * chance at claiming multicast packets. We can not loop back
1836 * here from ether_input as a bridge is never a member of a
1837 * bridge.
1839 KASSERT(bifp->if_bridge == NULL,
1840 ("loop created in bridge_input"));
1841 mc2 = m_dup(m, MB_DONTWAIT);
1842 #ifdef notyet
1843 if (mc2 != NULL) {
1844 /* Keep the layer3 header aligned */
1845 int i = min(mc2->m_pkthdr.len, max_protohdr);
1846 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
1848 #endif
1849 if (mc2 != NULL) {
1850 mc2->m_pkthdr.rcvif = bifp;
1851 bifp->if_ipackets++;
1852 bifp->if_input(bifp, mc2);
1855 /* Return the original packet for local processing. */
1856 goto out;
1859 if (bif->bif_flags & IFBIF_STP) {
1860 switch (bif->bif_state) {
1861 case BSTP_IFSTATE_BLOCKING:
1862 case BSTP_IFSTATE_LISTENING:
1863 case BSTP_IFSTATE_DISABLED:
1864 goto out;
1869 * Unicast. Make sure it's not for us.
1871 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1872 if (bif->bif_ifp->if_type != IFT_ETHER)
1873 continue;
1875 /* It is destined for us. */
1876 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1877 ETHER_ADDR_LEN) == 0) {
1878 if (bif->bif_flags & IFBIF_LEARNING) {
1879 bridge_rtupdate(sc,
1880 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1883 if (bif->bif_ifp != ifp) {
1884 /* XXX loop prevention */
1885 m->m_flags |= M_PROTO1;
1886 new_ifp = bif->bif_ifp;
1888 goto out;
1891 /* We just received a packet that we sent out. */
1892 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1893 ETHER_ADDR_LEN) == 0) {
1894 m_freem(m);
1895 m = NULL;
1896 goto out;
1900 /* Perform the bridge forwarding function. */
1901 bridge_forward(sc, m);
1902 m = NULL;
1903 out:
1904 lwkt_serialize_exit(bifp->if_serializer);
1906 if (new_ifp != NULL) {
1907 lwkt_serialize_enter(new_ifp->if_serializer);
1909 m->m_pkthdr.rcvif = new_ifp;
1910 new_ifp->if_ipackets++;
1911 new_ifp->if_input(new_ifp, m);
1912 m = NULL;
1914 lwkt_serialize_exit(new_ifp->if_serializer);
1916 return (m);
1920 * bridge_broadcast:
1922 * Send a frame to all interfaces that are members of
1923 * the bridge, except for the one on which the packet
1924 * arrived.
1926 static void
1927 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1928 struct mbuf *m, int runfilt)
1930 struct bridge_iflist *bif;
1931 struct mbuf *mc;
1932 struct ifnet *dst_if, *bifp;
1933 int used = 0;
1935 bifp = sc->sc_ifp;
1937 ASSERT_SERIALIZED(bifp->if_serializer);
1939 /* run the packet filter */
1940 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1941 #ifdef INET6
1942 || inet6_pfil_hook.ph_hashooks > 0
1943 #endif
1944 )) {
1945 lwkt_serialize_exit(bifp->if_serializer);
1947 /* Filter on the bridge interface before broadcasting */
1949 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
1950 goto filt;
1951 if (m == NULL)
1952 goto filt;
1954 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
1955 m = NULL;
1956 filt:
1957 lwkt_serialize_enter(bifp->if_serializer);
1958 if (m == NULL)
1959 return;
1962 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1963 dst_if = bif->bif_ifp;
1964 if (dst_if == src_if)
1965 continue;
1967 if (bif->bif_flags & IFBIF_STP) {
1968 switch (bif->bif_state) {
1969 case BSTP_IFSTATE_BLOCKING:
1970 case BSTP_IFSTATE_DISABLED:
1971 continue;
1975 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1976 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1977 continue;
1979 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1980 continue;
1982 if (LIST_NEXT(bif, bif_next) == NULL) {
1983 mc = m;
1984 used = 1;
1985 } else {
1986 mc = m_copypacket(m, MB_DONTWAIT);
1987 if (mc == NULL) {
1988 sc->sc_ifp->if_oerrors++;
1989 continue;
1992 bridge_pfil_enqueue(dst_if, mc, runfilt);
1994 if (used == 0)
1995 m_freem(m);
1999 * bridge_span:
2001 * Duplicate a packet out one or more interfaces that are in span mode,
2002 * the original mbuf is unmodified.
2004 static void
2005 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2007 struct bridge_iflist *bif;
2008 struct ifnet *dst_if;
2009 struct mbuf *mc;
2011 if (LIST_EMPTY(&sc->sc_spanlist))
2012 return;
2014 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2015 dst_if = bif->bif_ifp;
2017 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2018 continue;
2020 mc = m_copypacket(m, MB_DONTWAIT);
2021 if (mc == NULL) {
2022 sc->sc_ifp->if_oerrors++;
2023 continue;
2026 bridge_enqueue(dst_if, mc);
2031 * bridge_rtupdate:
2033 * Add a bridge routing entry.
2034 * Can be called from interrupt context.
2036 static int
2037 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2038 struct ifnet *dst_if, int setflags, uint8_t flags)
2040 struct bridge_rtnode *brt;
2041 int error;
2044 * A route for this destination might already exist. If so,
2045 * update it, otherwise create a new one.
2047 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2048 if (sc->sc_brtcnt >= sc->sc_brtmax)
2049 return (ENOSPC);
2052 * Allocate a new bridge forwarding node, and
2053 * initialize the expiration time and Ethernet
2054 * address.
2056 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2057 M_INTNOWAIT|M_ZERO);
2058 if (brt == NULL)
2059 return (ENOMEM);
2061 brt->brt_flags = IFBAF_DYNAMIC;
2062 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2064 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2065 kfree(brt, M_DEVBUF);
2066 return (error);
2070 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2071 brt->brt_ifp = dst_if;
2072 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2073 brt->brt_expire = time_second + sc->sc_brttimeout;
2074 if (setflags)
2075 brt->brt_flags = flags;
2077 return (0);
2081 * bridge_rtlookup:
2083 * Lookup the destination interface for an address.
2085 static struct ifnet *
2086 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2088 struct bridge_rtnode *brt;
2090 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2091 return (NULL);
2093 return (brt->brt_ifp);
2097 * bridge_rttrim:
2099 * Trim the routine table so that we have a number
2100 * of routing entries less than or equal to the
2101 * maximum number.
2103 static void
2104 bridge_rttrim(struct bridge_softc *sc)
2106 struct bridge_rtnode *brt, *nbrt;
2108 /* Make sure we actually need to do this. */
2109 if (sc->sc_brtcnt <= sc->sc_brtmax)
2110 return;
2112 /* Force an aging cycle; this might trim enough addresses. */
2113 bridge_rtage(sc);
2114 if (sc->sc_brtcnt <= sc->sc_brtmax)
2115 return;
2117 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2118 nbrt = LIST_NEXT(brt, brt_list);
2119 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2120 bridge_rtnode_destroy(sc, brt);
2121 if (sc->sc_brtcnt <= sc->sc_brtmax)
2122 return;
2128 * bridge_timer:
2130 * Aging timer for the bridge.
2132 static void
2133 bridge_timer(void *arg)
2135 struct bridge_softc *sc = arg;
2137 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
2139 bridge_rtage(sc);
2141 if (sc->sc_ifp->if_flags & IFF_RUNNING)
2142 callout_reset(&sc->sc_brcallout,
2143 bridge_rtable_prune_period * hz, bridge_timer, sc);
2145 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
2149 * bridge_rtage:
2151 * Perform an aging cycle.
2153 static void
2154 bridge_rtage(struct bridge_softc *sc)
2156 struct bridge_rtnode *brt, *nbrt;
2158 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2159 nbrt = LIST_NEXT(brt, brt_list);
2160 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2161 if (time_second >= brt->brt_expire)
2162 bridge_rtnode_destroy(sc, brt);
2168 * bridge_rtflush:
2170 * Remove all dynamic addresses from the bridge.
2172 static void
2173 bridge_rtflush(struct bridge_softc *sc, int full)
2175 struct bridge_rtnode *brt, *nbrt;
2177 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2178 nbrt = LIST_NEXT(brt, brt_list);
2179 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2180 bridge_rtnode_destroy(sc, brt);
2185 * bridge_rtdaddr:
2187 * Remove an address from the table.
2189 static int
2190 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2192 struct bridge_rtnode *brt;
2194 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2195 return (ENOENT);
2197 bridge_rtnode_destroy(sc, brt);
2198 return (0);
2202 * bridge_rtdelete:
2204 * Delete routes to a speicifc member interface.
2206 void
2207 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2209 struct bridge_rtnode *brt, *nbrt;
2211 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2212 nbrt = LIST_NEXT(brt, brt_list);
2213 if (brt->brt_ifp == ifp && (full ||
2214 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2215 bridge_rtnode_destroy(sc, brt);
2220 * bridge_rtable_init:
2222 * Initialize the route table for this bridge.
2224 static int
2225 bridge_rtable_init(struct bridge_softc *sc)
2227 int i;
2229 sc->sc_rthash = kmalloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2230 M_DEVBUF, M_WAITOK);
2232 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2233 LIST_INIT(&sc->sc_rthash[i]);
2235 sc->sc_rthash_key = karc4random();
2237 LIST_INIT(&sc->sc_rtlist);
2239 return (0);
2243 * bridge_rtable_fini:
2245 * Deconstruct the route table for this bridge.
2247 static void
2248 bridge_rtable_fini(struct bridge_softc *sc)
2251 kfree(sc->sc_rthash, M_DEVBUF);
2255 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2256 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2258 #define mix(a, b, c) \
2259 do { \
2260 a -= b; a -= c; a ^= (c >> 13); \
2261 b -= c; b -= a; b ^= (a << 8); \
2262 c -= a; c -= b; c ^= (b >> 13); \
2263 a -= b; a -= c; a ^= (c >> 12); \
2264 b -= c; b -= a; b ^= (a << 16); \
2265 c -= a; c -= b; c ^= (b >> 5); \
2266 a -= b; a -= c; a ^= (c >> 3); \
2267 b -= c; b -= a; b ^= (a << 10); \
2268 c -= a; c -= b; c ^= (b >> 15); \
2269 } while (/*CONSTCOND*/0)
2271 static __inline uint32_t
2272 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2274 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2276 b += addr[5] << 8;
2277 b += addr[4];
2278 a += addr[3] << 24;
2279 a += addr[2] << 16;
2280 a += addr[1] << 8;
2281 a += addr[0];
2283 mix(a, b, c);
2285 return (c & BRIDGE_RTHASH_MASK);
2288 #undef mix
2290 static int
2291 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2293 int i, d;
2295 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2296 d = ((int)a[i]) - ((int)b[i]);
2299 return (d);
2303 * bridge_rtnode_lookup:
2305 * Look up a bridge route node for the specified destination.
2307 static struct bridge_rtnode *
2308 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2310 struct bridge_rtnode *brt;
2311 uint32_t hash;
2312 int dir;
2314 hash = bridge_rthash(sc, addr);
2315 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2316 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2317 if (dir == 0)
2318 return (brt);
2319 if (dir > 0)
2320 return (NULL);
2323 return (NULL);
2327 * bridge_rtnode_insert:
2329 * Insert the specified bridge node into the route table. We
2330 * assume the entry is not already in the table.
2332 static int
2333 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2335 struct bridge_rtnode *lbrt;
2336 uint32_t hash;
2337 int dir;
2339 hash = bridge_rthash(sc, brt->brt_addr);
2341 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2342 if (lbrt == NULL) {
2343 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2344 goto out;
2347 do {
2348 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2349 if (dir == 0)
2350 return (EEXIST);
2351 if (dir > 0) {
2352 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2353 goto out;
2355 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2356 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2357 goto out;
2359 lbrt = LIST_NEXT(lbrt, brt_hash);
2360 } while (lbrt != NULL);
2362 #ifdef DIAGNOSTIC
2363 panic("bridge_rtnode_insert: impossible");
2364 #endif
2366 out:
2367 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2368 sc->sc_brtcnt++;
2370 return (0);
2374 * bridge_rtnode_destroy:
2376 * Destroy a bridge rtnode.
2378 static void
2379 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2382 LIST_REMOVE(brt, brt_hash);
2384 LIST_REMOVE(brt, brt_list);
2385 sc->sc_brtcnt--;
2386 kfree(brt, M_DEVBUF);
2390 * Send bridge packets through pfil if they are one of the types pfil can deal
2391 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2392 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2393 * that interface.
2395 static int
2396 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2398 int snap, error, i, hlen;
2399 struct ether_header *eh1, eh2;
2400 struct ip *ip;
2401 struct llc llc1;
2402 u_int16_t ether_type;
2404 snap = 0;
2405 error = -1; /* Default error if not error == 0 */
2407 if (pfil_bridge == 0 && pfil_member == 0)
2408 return (0); /* filtering is disabled */
2410 i = min((*mp)->m_pkthdr.len, max_protohdr);
2411 if ((*mp)->m_len < i) {
2412 *mp = m_pullup(*mp, i);
2413 if (*mp == NULL) {
2414 kprintf("%s: m_pullup failed\n", __func__);
2415 return (-1);
2419 eh1 = mtod(*mp, struct ether_header *);
2420 ether_type = ntohs(eh1->ether_type);
2423 * Check for SNAP/LLC.
2425 if (ether_type < ETHERMTU) {
2426 struct llc *llc2 = (struct llc *)(eh1 + 1);
2428 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2429 llc2->llc_dsap == LLC_SNAP_LSAP &&
2430 llc2->llc_ssap == LLC_SNAP_LSAP &&
2431 llc2->llc_control == LLC_UI) {
2432 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2433 snap = 1;
2438 * If we're trying to filter bridge traffic, don't look at anything
2439 * other than IP and ARP traffic. If the filter doesn't understand
2440 * IPv6, don't allow IPv6 through the bridge either. This is lame
2441 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2442 * but of course we don't have an AppleTalk filter to begin with.
2443 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2444 * ARP traffic.)
2446 switch (ether_type) {
2447 case ETHERTYPE_ARP:
2448 case ETHERTYPE_REVARP:
2449 return (0); /* Automatically pass */
2450 case ETHERTYPE_IP:
2451 #ifdef INET6
2452 case ETHERTYPE_IPV6:
2453 #endif /* INET6 */
2454 break;
2455 default:
2457 * Check to see if the user wants to pass non-ip
2458 * packets, these will not be checked by pfil(9) and
2459 * passed unconditionally so the default is to drop.
2461 if (pfil_onlyip)
2462 goto bad;
2465 /* Strip off the Ethernet header and keep a copy. */
2466 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2467 m_adj(*mp, ETHER_HDR_LEN);
2469 /* Strip off snap header, if present */
2470 if (snap) {
2471 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2472 m_adj(*mp, sizeof(struct llc));
2476 * Check the IP header for alignment and errors
2478 if (dir == PFIL_IN) {
2479 switch (ether_type) {
2480 case ETHERTYPE_IP:
2481 error = bridge_ip_checkbasic(mp);
2482 break;
2483 #ifdef INET6
2484 case ETHERTYPE_IPV6:
2485 error = bridge_ip6_checkbasic(mp);
2486 break;
2487 #endif /* INET6 */
2488 default:
2489 error = 0;
2491 if (error)
2492 goto bad;
2495 error = 0;
2498 * Run the packet through pfil
2500 switch (ether_type)
2502 case ETHERTYPE_IP :
2504 * before calling the firewall, swap fields the same as
2505 * IP does. here we assume the header is contiguous
2507 ip = mtod(*mp, struct ip *);
2509 ip->ip_len = ntohs(ip->ip_len);
2510 ip->ip_off = ntohs(ip->ip_off);
2513 * Run pfil on the member interface and the bridge, both can
2514 * be skipped by clearing pfil_member or pfil_bridge.
2516 * Keep the order:
2517 * in_if -> bridge_if -> out_if
2519 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2520 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2521 dir);
2523 if (*mp == NULL || error != 0) /* filter may consume */
2524 break;
2526 if (pfil_member && ifp != NULL)
2527 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2528 dir);
2530 if (*mp == NULL || error != 0) /* filter may consume */
2531 break;
2533 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2534 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2535 dir);
2537 if (*mp == NULL || error != 0) /* filter may consume */
2538 break;
2540 /* check if we need to fragment the packet */
2541 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
2542 i = (*mp)->m_pkthdr.len;
2543 if (i > ifp->if_mtu) {
2544 error = bridge_fragment(ifp, *mp, &eh2, snap,
2545 &llc1);
2546 return (error);
2550 /* Recalculate the ip checksum and restore byte ordering */
2551 ip = mtod(*mp, struct ip *);
2552 hlen = ip->ip_hl << 2;
2553 if (hlen < sizeof(struct ip))
2554 goto bad;
2555 if (hlen > (*mp)->m_len) {
2556 if ((*mp = m_pullup(*mp, hlen)) == 0)
2557 goto bad;
2558 ip = mtod(*mp, struct ip *);
2559 if (ip == NULL)
2560 goto bad;
2562 ip->ip_len = htons(ip->ip_len);
2563 ip->ip_off = htons(ip->ip_off);
2564 ip->ip_sum = 0;
2565 if (hlen == sizeof(struct ip))
2566 ip->ip_sum = in_cksum_hdr(ip);
2567 else
2568 ip->ip_sum = in_cksum(*mp, hlen);
2570 break;
2571 #ifdef INET6
2572 case ETHERTYPE_IPV6 :
2573 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2574 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2575 dir);
2577 if (*mp == NULL || error != 0) /* filter may consume */
2578 break;
2580 if (pfil_member && ifp != NULL)
2581 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2582 dir);
2584 if (*mp == NULL || error != 0) /* filter may consume */
2585 break;
2587 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2588 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2589 dir);
2590 break;
2591 #endif
2592 default :
2593 error = 0;
2594 break;
2597 if (*mp == NULL)
2598 return (error);
2599 if (error != 0)
2600 goto bad;
2602 error = -1;
2605 * Finally, put everything back the way it was and return
2607 if (snap) {
2608 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
2609 if (*mp == NULL)
2610 return (error);
2611 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2614 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
2615 if (*mp == NULL)
2616 return (error);
2617 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2619 return (0);
2621 bad:
2622 m_freem(*mp);
2623 *mp = NULL;
2624 return (error);
2628 * Perform basic checks on header size since
2629 * pfil assumes ip_input has already processed
2630 * it for it. Cut-and-pasted from ip_input.c.
2631 * Given how simple the IPv6 version is,
2632 * does the IPv4 version really need to be
2633 * this complicated?
2635 * XXX Should we update ipstat here, or not?
2636 * XXX Right now we update ipstat but not
2637 * XXX csum_counter.
2639 static int
2640 bridge_ip_checkbasic(struct mbuf **mp)
2642 struct mbuf *m = *mp;
2643 struct ip *ip;
2644 int len, hlen;
2645 u_short sum;
2647 if (*mp == NULL)
2648 return (-1);
2649 #if notyet
2650 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2651 if ((m = m_copyup(m, sizeof(struct ip),
2652 (max_linkhdr + 3) & ~3)) == NULL) {
2653 /* XXXJRT new stat, please */
2654 ipstat.ips_toosmall++;
2655 goto bad;
2657 } else
2658 #endif
2659 #ifndef __predict_false
2660 #define __predict_false(x) x
2661 #endif
2662 if (__predict_false(m->m_len < sizeof (struct ip))) {
2663 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2664 ipstat.ips_toosmall++;
2665 goto bad;
2668 ip = mtod(m, struct ip *);
2669 if (ip == NULL) goto bad;
2671 if (ip->ip_v != IPVERSION) {
2672 ipstat.ips_badvers++;
2673 goto bad;
2675 hlen = ip->ip_hl << 2;
2676 if (hlen < sizeof(struct ip)) { /* minimum header length */
2677 ipstat.ips_badhlen++;
2678 goto bad;
2680 if (hlen > m->m_len) {
2681 if ((m = m_pullup(m, hlen)) == 0) {
2682 ipstat.ips_badhlen++;
2683 goto bad;
2685 ip = mtod(m, struct ip *);
2686 if (ip == NULL) goto bad;
2689 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2690 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2691 } else {
2692 if (hlen == sizeof(struct ip)) {
2693 sum = in_cksum_hdr(ip);
2694 } else {
2695 sum = in_cksum(m, hlen);
2698 if (sum) {
2699 ipstat.ips_badsum++;
2700 goto bad;
2703 /* Retrieve the packet length. */
2704 len = ntohs(ip->ip_len);
2707 * Check for additional length bogosity
2709 if (len < hlen) {
2710 ipstat.ips_badlen++;
2711 goto bad;
2715 * Check that the amount of data in the buffers
2716 * is as at least much as the IP header would have us expect.
2717 * Drop packet if shorter than we expect.
2719 if (m->m_pkthdr.len < len) {
2720 ipstat.ips_tooshort++;
2721 goto bad;
2724 /* Checks out, proceed */
2725 *mp = m;
2726 return (0);
2728 bad:
2729 *mp = m;
2730 return (-1);
2733 #ifdef INET6
2735 * Same as above, but for IPv6.
2736 * Cut-and-pasted from ip6_input.c.
2737 * XXX Should we update ip6stat, or not?
2739 static int
2740 bridge_ip6_checkbasic(struct mbuf **mp)
2742 struct mbuf *m = *mp;
2743 struct ip6_hdr *ip6;
2746 * If the IPv6 header is not aligned, slurp it up into a new
2747 * mbuf with space for link headers, in the event we forward
2748 * it. Otherwise, if it is aligned, make sure the entire base
2749 * IPv6 header is in the first mbuf of the chain.
2751 #if notyet
2752 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2753 struct ifnet *inifp = m->m_pkthdr.rcvif;
2754 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2755 (max_linkhdr + 3) & ~3)) == NULL) {
2756 /* XXXJRT new stat, please */
2757 ip6stat.ip6s_toosmall++;
2758 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2759 goto bad;
2761 } else
2762 #endif
2763 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2764 struct ifnet *inifp = m->m_pkthdr.rcvif;
2765 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2766 ip6stat.ip6s_toosmall++;
2767 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2768 goto bad;
2772 ip6 = mtod(m, struct ip6_hdr *);
2774 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2775 ip6stat.ip6s_badvers++;
2776 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2777 goto bad;
2780 /* Checks out, proceed */
2781 *mp = m;
2782 return (0);
2784 bad:
2785 *mp = m;
2786 return (-1);
2788 #endif /* INET6 */
2791 * bridge_fragment:
2793 * Return a fragmented mbuf chain.
2795 static int
2796 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
2797 int snap, struct llc *llc)
2799 struct mbuf *m0;
2800 struct ip *ip;
2801 int error = -1;
2803 if (m->m_len < sizeof(struct ip) &&
2804 (m = m_pullup(m, sizeof(struct ip))) == NULL)
2805 goto out;
2806 ip = mtod(m, struct ip *);
2808 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
2809 CSUM_DELAY_IP);
2810 if (error)
2811 goto out;
2813 /* walk the chain and re-add the Ethernet header */
2814 for (m0 = m; m0; m0 = m0->m_nextpkt) {
2815 if (error == 0) {
2816 if (snap) {
2817 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
2818 if (m0 == NULL) {
2819 error = ENOBUFS;
2820 continue;
2822 bcopy(llc, mtod(m0, caddr_t),
2823 sizeof(struct llc));
2825 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
2826 if (m0 == NULL) {
2827 error = ENOBUFS;
2828 continue;
2830 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
2831 } else
2832 m_freem(m);
2835 if (error == 0)
2836 ipstat.ips_fragmented++;
2838 return (error);
2840 out:
2841 if (m != NULL)
2842 m_freem(m);
2843 return (error);
2846 static void
2847 bridge_enqueue_handler(struct netmsg *nmsg)
2849 struct netmsg_packet *nmp;
2850 struct ifnet *dst_ifp;
2851 struct mbuf *m;
2853 nmp = (struct netmsg_packet *)nmsg;
2854 m = nmp->nm_packet;
2855 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2857 bridge_handoff_notags(dst_ifp, m);
2860 static void
2861 bridge_pfil_enqueue_handler(struct netmsg *nmsg)
2863 struct netmsg_packet *nmp;
2864 struct ifnet *dst_ifp;
2865 struct mbuf *m;
2867 nmp = (struct netmsg_packet *)nmsg;
2868 m = nmp->nm_packet;
2869 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2872 * Filter on the output interface. Pass a NULL bridge interface
2873 * pointer so we do not redundantly filter on the bridge for
2874 * each interface we broadcast on.
2876 if (inet_pfil_hook.ph_hashooks > 0
2877 #ifdef INET6
2878 || inet6_pfil_hook.ph_hashooks > 0
2879 #endif
2881 if (bridge_pfil(&m, NULL, dst_ifp, PFIL_OUT) != 0)
2882 return;
2883 if (m == NULL)
2884 return;
2886 bridge_handoff_notags(dst_ifp, m);
2889 static void
2890 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m)
2892 while (m->m_type == MT_TAG) {
2893 /* XXX see ether_output_frame for full rules check */
2894 m = m->m_next;
2896 bridge_handoff_notags(dst_ifp, m);
2899 static void
2900 bridge_handoff_notags(struct ifnet *dst_ifp, struct mbuf *m)
2902 struct mbuf *m0;
2904 KKASSERT(m->m_type != MT_TAG);
2906 lwkt_serialize_enter(dst_ifp->if_serializer);
2908 /* We may be sending a fragment so traverse the mbuf */
2909 for (; m; m = m0) {
2910 struct altq_pktattr pktattr;
2912 m0 = m->m_nextpkt;
2913 m->m_nextpkt = NULL;
2915 if (ifq_is_enabled(&dst_ifp->if_snd))
2916 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
2918 ifq_handoff(dst_ifp, m, &pktattr);
2921 lwkt_serialize_exit(dst_ifp->if_serializer);
2924 struct netmsg_brgctl {
2925 struct netmsg bc_nmsg;
2926 bridge_ctl_t bc_func;
2927 struct bridge_softc *bc_sc;
2928 void *bc_arg;
2931 static void
2932 bridge_control_dispatch(struct netmsg *nmsg)
2934 struct netmsg_brgctl *bc_msg = (struct netmsg_brgctl *)nmsg;
2935 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
2936 int error;
2938 lwkt_serialize_enter(bifp->if_serializer);
2939 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
2940 lwkt_serialize_exit(bifp->if_serializer);
2942 lwkt_replymsg(&nmsg->nm_lmsg, error);
2945 static int
2946 bridge_control(struct bridge_softc *sc, bridge_ctl_t bc_func, void *bc_arg)
2948 struct ifnet *bifp = sc->sc_ifp;
2949 struct netmsg_brgctl bc_msg;
2950 struct netmsg *nmsg;
2951 int error;
2953 ASSERT_SERIALIZED(bifp->if_serializer);
2955 bzero(&bc_msg, sizeof(bc_msg));
2956 nmsg = &bc_msg.bc_nmsg;
2958 netmsg_init(nmsg, &curthread->td_msgport, 0, bridge_control_dispatch);
2959 bc_msg.bc_func = bc_func;
2960 bc_msg.bc_sc = sc;
2961 bc_msg.bc_arg = bc_arg;
2963 lwkt_serialize_exit(bifp->if_serializer);
2964 error = lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
2965 lwkt_serialize_enter(bifp->if_serializer);
2966 return error;