Remove unused parameters to bridge_output
[dragonfly.git] / sys / net / bridge / if_bridge.c
bloba29d093c49e23690ae3ebbe7614c3414bf5ddaed
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.30 2008/06/14 04:00:51 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 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
187 static int bridge_clone_create(struct if_clone *, int);
188 static void bridge_clone_destroy(struct ifnet *);
190 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
191 static void bridge_mutecaps(struct bridge_iflist *, int);
192 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
193 static void bridge_init(void *);
194 static void bridge_stop(struct ifnet *);
195 static void bridge_start(struct ifnet *);
196 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
197 static int bridge_output(struct ifnet *, struct mbuf *);
199 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
201 static void bridge_timer(void *);
203 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
204 struct mbuf *, int);
205 static void bridge_span(struct bridge_softc *, struct mbuf *);
207 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
208 struct ifnet *, int, uint8_t);
209 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
210 static void bridge_rttrim(struct bridge_softc *);
211 static void bridge_rtage(struct bridge_softc *);
212 static void bridge_rtflush(struct bridge_softc *, int);
213 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
215 static int bridge_rtable_init(struct bridge_softc *);
216 static void bridge_rtable_fini(struct bridge_softc *);
218 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
219 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
220 const uint8_t *);
221 static int bridge_rtnode_insert(struct bridge_softc *,
222 struct bridge_rtnode *);
223 static void bridge_rtnode_destroy(struct bridge_softc *,
224 struct bridge_rtnode *);
226 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
227 const char *name);
228 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
229 struct ifnet *ifp);
230 static void bridge_delete_member(struct bridge_softc *,
231 struct bridge_iflist *, int);
232 static void bridge_delete_span(struct bridge_softc *,
233 struct bridge_iflist *);
235 static int bridge_ioctl_add(struct bridge_softc *, void *);
236 static int bridge_ioctl_del(struct bridge_softc *, void *);
237 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
238 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
239 static int bridge_ioctl_scache(struct bridge_softc *, void *);
240 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
241 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
242 static int bridge_ioctl_rts(struct bridge_softc *, void *);
243 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
244 static int bridge_ioctl_sto(struct bridge_softc *, void *);
245 static int bridge_ioctl_gto(struct bridge_softc *, void *);
246 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
247 static int bridge_ioctl_flush(struct bridge_softc *, void *);
248 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
249 static int bridge_ioctl_spri(struct bridge_softc *, void *);
250 static int bridge_ioctl_ght(struct bridge_softc *, void *);
251 static int bridge_ioctl_sht(struct bridge_softc *, void *);
252 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
253 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
254 static int bridge_ioctl_gma(struct bridge_softc *, void *);
255 static int bridge_ioctl_sma(struct bridge_softc *, void *);
256 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
257 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
258 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
259 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
260 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
261 int);
262 static int bridge_ip_checkbasic(struct mbuf **mp);
263 #ifdef INET6
264 static int bridge_ip6_checkbasic(struct mbuf **mp);
265 #endif /* INET6 */
266 static int bridge_fragment(struct ifnet *, struct mbuf *,
267 struct ether_header *, int, struct llc *);
268 static void bridge_enqueue_internal(struct ifnet *, struct mbuf *m,
269 netisr_fn_t);
270 static void bridge_enqueue_handler(struct netmsg *);
271 static void bridge_pfil_enqueue_handler(struct netmsg *);
272 static void bridge_pfil_enqueue(struct ifnet *, struct mbuf *, int);
273 static void bridge_handoff_notags(struct ifnet *, struct mbuf *);
274 static void bridge_handoff(struct ifnet *, struct mbuf *);
276 SYSCTL_DECL(_net_link);
277 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
279 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
280 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
281 static int pfil_member = 1; /* run pfil hooks on the member interface */
282 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
283 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
284 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
285 &pfil_bridge, 0, "Packet filter on the bridge interface");
286 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
287 &pfil_member, 0, "Packet filter on the member interface");
289 struct bridge_control {
290 int (*bc_func)(struct bridge_softc *, void *);
291 int bc_argsize;
292 int bc_flags;
295 #define BC_F_COPYIN 0x01 /* copy arguments in */
296 #define BC_F_COPYOUT 0x02 /* copy arguments out */
297 #define BC_F_SUSER 0x04 /* do super-user check */
299 const struct bridge_control bridge_control_table[] = {
300 { bridge_ioctl_add, sizeof(struct ifbreq),
301 BC_F_COPYIN|BC_F_SUSER },
302 { bridge_ioctl_del, sizeof(struct ifbreq),
303 BC_F_COPYIN|BC_F_SUSER },
305 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
306 BC_F_COPYIN|BC_F_COPYOUT },
307 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
308 BC_F_COPYIN|BC_F_SUSER },
310 { bridge_ioctl_scache, sizeof(struct ifbrparam),
311 BC_F_COPYIN|BC_F_SUSER },
312 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
313 BC_F_COPYOUT },
315 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
316 BC_F_COPYIN|BC_F_COPYOUT },
317 { bridge_ioctl_rts, sizeof(struct ifbaconf),
318 BC_F_COPYIN|BC_F_COPYOUT },
320 { bridge_ioctl_saddr, sizeof(struct ifbareq),
321 BC_F_COPYIN|BC_F_SUSER },
323 { bridge_ioctl_sto, sizeof(struct ifbrparam),
324 BC_F_COPYIN|BC_F_SUSER },
325 { bridge_ioctl_gto, sizeof(struct ifbrparam),
326 BC_F_COPYOUT },
328 { bridge_ioctl_daddr, sizeof(struct ifbareq),
329 BC_F_COPYIN|BC_F_SUSER },
331 { bridge_ioctl_flush, sizeof(struct ifbreq),
332 BC_F_COPYIN|BC_F_SUSER },
334 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
335 BC_F_COPYOUT },
336 { bridge_ioctl_spri, sizeof(struct ifbrparam),
337 BC_F_COPYIN|BC_F_SUSER },
339 { bridge_ioctl_ght, sizeof(struct ifbrparam),
340 BC_F_COPYOUT },
341 { bridge_ioctl_sht, sizeof(struct ifbrparam),
342 BC_F_COPYIN|BC_F_SUSER },
344 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
345 BC_F_COPYOUT },
346 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
347 BC_F_COPYIN|BC_F_SUSER },
349 { bridge_ioctl_gma, sizeof(struct ifbrparam),
350 BC_F_COPYOUT },
351 { bridge_ioctl_sma, sizeof(struct ifbrparam),
352 BC_F_COPYIN|BC_F_SUSER },
354 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
355 BC_F_COPYIN|BC_F_SUSER },
357 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
358 BC_F_COPYIN|BC_F_SUSER },
360 { bridge_ioctl_addspan, sizeof(struct ifbreq),
361 BC_F_COPYIN|BC_F_SUSER },
362 { bridge_ioctl_delspan, sizeof(struct ifbreq),
363 BC_F_COPYIN|BC_F_SUSER },
365 const int bridge_control_table_size =
366 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
368 LIST_HEAD(, bridge_softc) bridge_list;
370 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
371 bridge_clone_create,
372 bridge_clone_destroy, 0, IF_MAXUNIT);
374 static int
375 bridge_modevent(module_t mod, int type, void *data)
377 switch (type) {
378 case MOD_LOAD:
379 LIST_INIT(&bridge_list);
380 if_clone_attach(&bridge_cloner);
381 bridge_input_p = bridge_input;
382 bridge_output_p = bridge_output;
383 bridge_detach_cookie = EVENTHANDLER_REGISTER(
384 ifnet_detach_event, bridge_ifdetach, NULL,
385 EVENTHANDLER_PRI_ANY);
386 #if notyet
387 bstp_linkstate_p = bstp_linkstate;
388 #endif
389 break;
390 case MOD_UNLOAD:
391 if (!LIST_EMPTY(&bridge_list))
392 return (EBUSY);
393 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
394 bridge_detach_cookie);
395 if_clone_detach(&bridge_cloner);
396 bridge_input_p = NULL;
397 bridge_output_p = NULL;
398 #if notyet
399 bstp_linkstate_p = NULL;
400 #endif
401 break;
402 default:
403 return (EOPNOTSUPP);
405 return (0);
408 static moduledata_t bridge_mod = {
409 "if_bridge",
410 bridge_modevent,
414 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
418 * bridge_clone_create:
420 * Create a new bridge instance.
422 static int
423 bridge_clone_create(struct if_clone *ifc, int unit)
425 struct bridge_softc *sc;
426 struct ifnet *ifp;
427 u_char eaddr[6];
429 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
430 ifp = sc->sc_ifp = &sc->sc_if;
432 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
433 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
434 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
435 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
436 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
437 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
438 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
440 /* Initialize our routing table. */
441 bridge_rtable_init(sc);
443 callout_init(&sc->sc_brcallout);
444 callout_init(&sc->sc_bstpcallout);
446 LIST_INIT(&sc->sc_iflist);
447 LIST_INIT(&sc->sc_spanlist);
449 ifp->if_softc = sc;
450 if_initname(ifp, ifc->ifc_name, unit);
451 ifp->if_mtu = ETHERMTU;
452 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
453 ifp->if_ioctl = bridge_ioctl;
454 ifp->if_start = bridge_start;
455 ifp->if_init = bridge_init;
456 ifp->if_type = IFT_BRIDGE;
457 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
458 ifp->if_snd.ifq_maxlen = ifqmaxlen;
459 ifq_set_ready(&ifp->if_snd);
460 ifp->if_hdrlen = ETHER_HDR_LEN;
463 * Generate a random ethernet address and use the private AC:DE:48
464 * OUI code.
467 int rnd = karc4random();
468 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
469 rnd = karc4random();
470 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
472 eaddr[0] &= ~1; /* clear multicast bit */
473 eaddr[0] |= 2; /* set the LAA bit */
475 ether_ifattach(ifp, eaddr, NULL);
476 /* Now undo some of the damage... */
477 ifp->if_baudrate = 0;
478 ifp->if_type = IFT_BRIDGE;
480 crit_enter();
481 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
482 crit_exit();
484 return (0);
488 * bridge_clone_destroy:
490 * Destroy a bridge instance.
492 static void
493 bridge_clone_destroy(struct ifnet *ifp)
495 struct bridge_softc *sc = ifp->if_softc;
496 struct bridge_iflist *bif;
498 lwkt_serialize_enter(ifp->if_serializer);
500 bridge_stop(ifp);
501 ifp->if_flags &= ~IFF_UP;
503 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
504 bridge_delete_member(sc, bif, 0);
506 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
507 bridge_delete_span(sc, bif);
510 callout_stop(&sc->sc_brcallout);
511 callout_stop(&sc->sc_bstpcallout);
513 lwkt_serialize_exit(ifp->if_serializer);
515 crit_enter();
516 LIST_REMOVE(sc, sc_list);
517 crit_exit();
519 ether_ifdetach(ifp);
521 /* Tear down the routing table. */
522 bridge_rtable_fini(sc);
524 kfree(sc, M_DEVBUF);
528 * bridge_ioctl:
530 * Handle a control request from the operator.
532 static int
533 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
535 struct bridge_softc *sc = ifp->if_softc;
536 union {
537 struct ifbreq ifbreq;
538 struct ifbifconf ifbifconf;
539 struct ifbareq ifbareq;
540 struct ifbaconf ifbaconf;
541 struct ifbrparam ifbrparam;
542 } args;
543 struct ifdrv *ifd = (struct ifdrv *) data;
544 const struct bridge_control *bc;
545 int error = 0;
547 ASSERT_SERIALIZED(ifp->if_serializer);
549 switch (cmd) {
550 case SIOCADDMULTI:
551 case SIOCDELMULTI:
552 break;
554 case SIOCGDRVSPEC:
555 case SIOCSDRVSPEC:
556 if (ifd->ifd_cmd >= bridge_control_table_size) {
557 error = EINVAL;
558 break;
560 bc = &bridge_control_table[ifd->ifd_cmd];
562 if (cmd == SIOCGDRVSPEC &&
563 (bc->bc_flags & BC_F_COPYOUT) == 0) {
564 error = EINVAL;
565 break;
566 } else if (cmd == SIOCSDRVSPEC &&
567 (bc->bc_flags & BC_F_COPYOUT) != 0) {
568 error = EINVAL;
569 break;
572 if (bc->bc_flags & BC_F_SUSER) {
573 error = suser_cred(cr, NULL_CRED_OKAY);
574 if (error)
575 break;
578 if (ifd->ifd_len != bc->bc_argsize ||
579 ifd->ifd_len > sizeof(args)) {
580 error = EINVAL;
581 break;
584 memset(&args, 0, sizeof(args));
585 if (bc->bc_flags & BC_F_COPYIN) {
586 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
587 if (error)
588 break;
591 error = bc->bc_func(sc, &args);
592 if (error)
593 break;
595 if (bc->bc_flags & BC_F_COPYOUT)
596 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
597 break;
599 case SIOCSIFFLAGS:
600 if (!(ifp->if_flags & IFF_UP) &&
601 (ifp->if_flags & IFF_RUNNING)) {
603 * If interface is marked down and it is running,
604 * then stop it.
606 bridge_stop(ifp);
607 } else if ((ifp->if_flags & IFF_UP) &&
608 !(ifp->if_flags & IFF_RUNNING)) {
610 * If interface is marked up and it is stopped, then
611 * start it.
613 ifp->if_init(sc);
615 break;
617 case SIOCSIFMTU:
618 /* Do not allow the MTU to be changed on the bridge */
619 error = EINVAL;
620 break;
622 default:
623 error = ether_ioctl(ifp, cmd, data);
624 break;
626 return (error);
630 * bridge_mutecaps:
632 * Clear or restore unwanted capabilities on the member interface
634 static void
635 bridge_mutecaps(struct bridge_iflist *bif, int mute)
637 struct ifnet *ifp = bif->bif_ifp;
638 struct ifreq ifr;
639 int error;
641 if (ifp->if_ioctl == NULL)
642 return;
644 bzero(&ifr, sizeof(ifr));
645 ifr.ifr_reqcap = ifp->if_capenable;
647 if (mute) {
648 /* mask off and save capabilities */
649 bif->bif_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
650 if (bif->bif_mutecap != 0)
651 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
652 } else
653 /* restore muted capabilities */
654 ifr.ifr_reqcap |= bif->bif_mutecap;
656 if (bif->bif_mutecap != 0) {
657 lwkt_serialize_enter(ifp->if_serializer);
658 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
659 lwkt_serialize_exit(ifp->if_serializer);
664 * bridge_lookup_member:
666 * Lookup a bridge member interface.
668 static struct bridge_iflist *
669 bridge_lookup_member(struct bridge_softc *sc, const char *name)
671 struct bridge_iflist *bif;
672 struct ifnet *ifp;
674 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
675 ifp = bif->bif_ifp;
676 if (strcmp(ifp->if_xname, name) == 0)
677 return (bif);
680 return (NULL);
684 * bridge_lookup_member_if:
686 * Lookup a bridge member interface by ifnet*.
688 static struct bridge_iflist *
689 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
691 struct bridge_iflist *bif;
693 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
694 if (bif->bif_ifp == member_ifp)
695 return (bif);
698 return (NULL);
702 * bridge_delete_member:
704 * Delete the specified member interface.
706 static void
707 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
708 int gone)
710 struct ifnet *ifs = bif->bif_ifp;
712 if (!gone) {
713 switch (ifs->if_type) {
714 case IFT_ETHER:
715 case IFT_L2VLAN:
717 * Take the interface out of promiscuous mode.
719 (void) ifpromisc(ifs, 0);
720 bridge_mutecaps(bif, 0);
721 break;
723 case IFT_GIF:
724 break;
726 default:
727 #ifdef DIAGNOSTIC
728 panic("bridge_delete_member: impossible");
729 #endif
730 break;
734 ifs->if_bridge = NULL;
735 LIST_REMOVE(bif, bif_next);
737 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
739 kfree(bif, M_DEVBUF);
741 if (sc->sc_ifp->if_flags & IFF_RUNNING)
742 bstp_initialization(sc);
746 * bridge_delete_span:
748 * Delete the specified span interface.
750 static void
751 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
753 KASSERT(bif->bif_ifp->if_bridge == NULL,
754 ("%s: not a span interface", __func__));
756 LIST_REMOVE(bif, bif_next);
757 kfree(bif, M_DEVBUF);
760 static int
761 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
763 struct ifbreq *req = arg;
764 struct bridge_iflist *bif = NULL;
765 struct ifnet *ifs;
766 int error = 0;
768 ifs = ifunit(req->ifbr_ifsname);
769 if (ifs == NULL)
770 return (ENOENT);
772 /* If it's in the span list, it can't be a member. */
773 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
774 if (ifs == bif->bif_ifp)
775 return (EBUSY);
777 /* Allow the first Ethernet member to define the MTU */
778 if (ifs->if_type != IFT_GIF) {
779 if (LIST_EMPTY(&sc->sc_iflist))
780 sc->sc_ifp->if_mtu = ifs->if_mtu;
781 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
782 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
783 ifs->if_xname);
784 return (EINVAL);
788 if (ifs->if_bridge == sc)
789 return (EEXIST);
791 if (ifs->if_bridge != NULL)
792 return (EBUSY);
794 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
795 bif->bif_ifp = ifs;
796 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
797 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
798 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
800 switch (ifs->if_type) {
801 case IFT_ETHER:
802 case IFT_L2VLAN:
804 * Place the interface into promiscuous mode.
806 error = ifpromisc(ifs, 1);
807 if (error)
808 goto out;
810 bridge_mutecaps(bif, 1);
811 break;
813 case IFT_GIF: /* :^) */
814 break;
816 default:
817 error = EINVAL;
818 goto out;
821 ifs->if_bridge = sc;
823 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
825 if (sc->sc_ifp->if_flags & IFF_RUNNING)
826 bstp_initialization(sc);
827 else
828 bstp_stop(sc);
830 out:
831 if (error) {
832 if (bif != NULL)
833 kfree(bif, M_DEVBUF);
835 return (error);
838 static int
839 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
841 struct ifbreq *req = arg;
842 struct bridge_iflist *bif;
844 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
845 if (bif == NULL)
846 return (ENOENT);
848 bridge_delete_member(sc, bif, 0);
850 return (0);
853 static int
854 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
856 struct ifbreq *req = arg;
857 struct bridge_iflist *bif;
859 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
860 if (bif == NULL)
861 return (ENOENT);
863 req->ifbr_ifsflags = bif->bif_flags;
864 req->ifbr_state = bif->bif_state;
865 req->ifbr_priority = bif->bif_priority;
866 req->ifbr_path_cost = bif->bif_path_cost;
867 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
869 return (0);
872 static int
873 bridge_ioctl_sifflags(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 if (req->ifbr_ifsflags & IFBIF_SPAN)
883 /* SPAN is readonly */
884 return (EINVAL);
886 if (req->ifbr_ifsflags & IFBIF_STP) {
887 switch (bif->bif_ifp->if_type) {
888 case IFT_ETHER:
889 /* These can do spanning tree. */
890 break;
892 default:
893 /* Nothing else can. */
894 return (EINVAL);
898 bif->bif_flags = req->ifbr_ifsflags;
900 if (sc->sc_ifp->if_flags & IFF_RUNNING)
901 bstp_initialization(sc);
903 return (0);
906 static int
907 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
909 struct ifbrparam *param = arg;
911 sc->sc_brtmax = param->ifbrp_csize;
912 bridge_rttrim(sc);
914 return (0);
917 static int
918 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
920 struct ifbrparam *param = arg;
922 param->ifbrp_csize = sc->sc_brtmax;
924 return (0);
927 static int
928 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
930 struct ifbifconf *bifc = arg;
931 struct bridge_iflist *bif;
932 struct ifbreq breq;
933 int count, len, error = 0;
935 count = 0;
936 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
937 count++;
938 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
939 count++;
941 if (bifc->ifbic_len == 0) {
942 bifc->ifbic_len = sizeof(breq) * count;
943 return (0);
946 count = 0;
947 len = bifc->ifbic_len;
948 memset(&breq, 0, sizeof breq);
949 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
950 if (len < sizeof(breq))
951 break;
953 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
954 sizeof(breq.ifbr_ifsname));
955 breq.ifbr_ifsflags = bif->bif_flags;
956 breq.ifbr_state = bif->bif_state;
957 breq.ifbr_priority = bif->bif_priority;
958 breq.ifbr_path_cost = bif->bif_path_cost;
959 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
960 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
961 if (error)
962 break;
963 count++;
964 len -= sizeof(breq);
966 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
967 if (len < sizeof(breq))
968 break;
970 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
971 sizeof(breq.ifbr_ifsname));
972 breq.ifbr_ifsflags = bif->bif_flags;
973 breq.ifbr_state = bif->bif_state;
974 breq.ifbr_priority = bif->bif_priority;
975 breq.ifbr_path_cost = bif->bif_path_cost;
976 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
977 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
978 if (error)
979 break;
980 count++;
981 len -= sizeof(breq);
984 bifc->ifbic_len = sizeof(breq) * count;
985 return (error);
988 static int
989 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
991 struct ifbaconf *bac = arg;
992 struct bridge_rtnode *brt;
993 struct ifbareq bareq;
994 int count = 0, error = 0, len;
996 if (bac->ifbac_len == 0)
997 return (0);
999 len = bac->ifbac_len;
1000 memset(&bareq, 0, sizeof(bareq));
1001 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1002 if (len < sizeof(bareq))
1003 goto out;
1004 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1005 sizeof(bareq.ifba_ifsname));
1006 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1007 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1008 time_second < brt->brt_expire)
1009 bareq.ifba_expire = brt->brt_expire - time_second;
1010 else
1011 bareq.ifba_expire = 0;
1012 bareq.ifba_flags = brt->brt_flags;
1014 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1015 if (error)
1016 goto out;
1017 count++;
1018 len -= sizeof(bareq);
1020 out:
1021 bac->ifbac_len = sizeof(bareq) * count;
1022 return (error);
1025 static int
1026 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1028 struct ifbareq *req = arg;
1029 struct bridge_iflist *bif;
1030 int error;
1032 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1033 if (bif == NULL)
1034 return (ENOENT);
1036 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1037 req->ifba_flags);
1039 return (error);
1042 static int
1043 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1045 struct ifbrparam *param = arg;
1047 sc->sc_brttimeout = param->ifbrp_ctime;
1049 return (0);
1052 static int
1053 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1055 struct ifbrparam *param = arg;
1057 param->ifbrp_ctime = sc->sc_brttimeout;
1059 return (0);
1062 static int
1063 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1065 struct ifbareq *req = arg;
1067 return (bridge_rtdaddr(sc, req->ifba_dst));
1070 static int
1071 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1073 struct ifbreq *req = arg;
1075 bridge_rtflush(sc, req->ifbr_ifsflags);
1077 return (0);
1080 static int
1081 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1083 struct ifbrparam *param = arg;
1085 param->ifbrp_prio = sc->sc_bridge_priority;
1087 return (0);
1090 static int
1091 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1093 struct ifbrparam *param = arg;
1095 sc->sc_bridge_priority = param->ifbrp_prio;
1097 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1098 bstp_initialization(sc);
1100 return (0);
1103 static int
1104 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1106 struct ifbrparam *param = arg;
1108 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1110 return (0);
1113 static int
1114 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1116 struct ifbrparam *param = arg;
1118 if (param->ifbrp_hellotime == 0)
1119 return (EINVAL);
1120 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1122 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1123 bstp_initialization(sc);
1125 return (0);
1128 static int
1129 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1131 struct ifbrparam *param = arg;
1133 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1135 return (0);
1138 static int
1139 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1141 struct ifbrparam *param = arg;
1143 if (param->ifbrp_fwddelay == 0)
1144 return (EINVAL);
1145 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1147 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1148 bstp_initialization(sc);
1150 return (0);
1153 static int
1154 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1156 struct ifbrparam *param = arg;
1158 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1160 return (0);
1163 static int
1164 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1166 struct ifbrparam *param = arg;
1168 if (param->ifbrp_maxage == 0)
1169 return (EINVAL);
1170 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1172 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1173 bstp_initialization(sc);
1175 return (0);
1178 static int
1179 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1181 struct ifbreq *req = arg;
1182 struct bridge_iflist *bif;
1184 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1185 if (bif == NULL)
1186 return (ENOENT);
1188 bif->bif_priority = req->ifbr_priority;
1190 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1191 bstp_initialization(sc);
1193 return (0);
1196 static int
1197 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1199 struct ifbreq *req = arg;
1200 struct bridge_iflist *bif;
1202 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1203 if (bif == NULL)
1204 return (ENOENT);
1206 bif->bif_path_cost = req->ifbr_path_cost;
1208 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1209 bstp_initialization(sc);
1211 return (0);
1214 static int
1215 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1217 struct ifbreq *req = arg;
1218 struct bridge_iflist *bif = NULL;
1219 struct ifnet *ifs;
1221 ifs = ifunit(req->ifbr_ifsname);
1222 if (ifs == NULL)
1223 return (ENOENT);
1225 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1226 if (ifs == bif->bif_ifp)
1227 return (EBUSY);
1229 if (ifs->if_bridge != NULL)
1230 return (EBUSY);
1232 switch (ifs->if_type) {
1233 case IFT_ETHER:
1234 case IFT_GIF:
1235 case IFT_L2VLAN:
1236 break;
1237 default:
1238 return (EINVAL);
1241 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
1243 bif->bif_ifp = ifs;
1244 bif->bif_flags = IFBIF_SPAN;
1246 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1248 return (0);
1251 static int
1252 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1254 struct ifbreq *req = arg;
1255 struct bridge_iflist *bif;
1256 struct ifnet *ifs;
1258 ifs = ifunit(req->ifbr_ifsname);
1259 if (ifs == NULL)
1260 return (ENOENT);
1262 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1263 if (ifs == bif->bif_ifp)
1264 break;
1266 if (bif == NULL)
1267 return (ENOENT);
1269 bridge_delete_span(sc, bif);
1271 return (0);
1275 * bridge_ifdetach:
1277 * Detach an interface from a bridge. Called when a member
1278 * interface is detaching.
1280 static void
1281 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1283 struct bridge_softc *sc = ifp->if_bridge;
1284 struct bridge_iflist *bif;
1286 /* Check if the interface is a bridge member */
1287 if (sc != NULL) {
1288 lwkt_serialize_enter(ifp->if_serializer);
1290 bif = bridge_lookup_member_if(sc, ifp);
1291 if (bif != NULL)
1292 bridge_delete_member(sc, bif, 1);
1294 lwkt_serialize_exit(ifp->if_serializer);
1295 return;
1298 /* Check if the interface is a span port */
1299 LIST_FOREACH(sc, &bridge_list, sc_list) {
1300 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1301 if (ifp == bif->bif_ifp) {
1302 bridge_delete_span(sc, bif);
1303 break;
1309 * bridge_init:
1311 * Initialize a bridge interface.
1313 static void
1314 bridge_init(void *xsc)
1316 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1317 struct ifnet *ifp = sc->sc_ifp;
1319 ASSERT_SERIALIZED(ifp->if_serializer);
1321 if (ifp->if_flags & IFF_RUNNING)
1322 return;
1324 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1325 bridge_timer, sc);
1327 ifp->if_flags |= IFF_RUNNING;
1328 bstp_initialization(sc);
1329 return;
1333 * bridge_stop:
1335 * Stop the bridge interface.
1337 static void
1338 bridge_stop(struct ifnet *ifp)
1340 struct bridge_softc *sc = ifp->if_softc;
1342 ASSERT_SERIALIZED(ifp->if_serializer);
1344 if ((ifp->if_flags & IFF_RUNNING) == 0)
1345 return;
1347 callout_stop(&sc->sc_brcallout);
1348 bstp_stop(sc);
1350 bridge_rtflush(sc, IFBF_FLUSHDYN);
1352 ifp->if_flags &= ~IFF_RUNNING;
1355 static void
1356 bridge_enqueue_internal(struct ifnet *dst_ifp, struct mbuf *m,
1357 netisr_fn_t handler)
1359 struct netmsg_packet *nmp;
1360 lwkt_port_t port;
1361 int cpu = mycpu->gd_cpuid;
1363 while (m->m_type == MT_TAG) {
1364 /* XXX see ether_output_frame for full rules check */
1365 m = m->m_next;
1368 nmp = &m->m_hdr.mh_netmsg;
1369 netmsg_init(&nmp->nm_netmsg, &netisr_apanic_rport, 0, handler);
1370 nmp->nm_packet = m;
1371 nmp->nm_netmsg.nm_lmsg.u.ms_resultp = dst_ifp;
1373 port = cpu_portfn(cpu);
1374 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
1377 static void
1378 bridge_pfil_enqueue(struct ifnet *dst_ifp, struct mbuf *m,
1379 int runfilt)
1381 netisr_fn_t handler;
1383 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1384 #ifdef INET6
1385 || inet6_pfil_hook.ph_hashooks > 0
1386 #endif
1387 )) {
1388 handler = bridge_pfil_enqueue_handler;
1389 } else {
1390 handler = bridge_enqueue_handler;
1392 bridge_enqueue_internal(dst_ifp, m, handler);
1396 * bridge_enqueue:
1398 * Enqueue a packet on a bridge member interface.
1401 void
1402 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1404 bridge_enqueue_internal(dst_ifp, m, bridge_enqueue_handler);
1408 * bridge_output:
1410 * Send output from a bridge member interface. This
1411 * performs the bridging function for locally originated
1412 * packets.
1414 * The mbuf has the Ethernet header already attached. We must
1415 * enqueue or free the mbuf before returning.
1417 static int
1418 bridge_output(struct ifnet *ifp, struct mbuf *m)
1420 struct ether_header *eh;
1421 struct ifnet *dst_if;
1422 struct bridge_softc *sc;
1424 sc = ifp->if_bridge;
1426 ASSERT_SERIALIZED(ifp->if_serializer);
1428 if (m->m_len < ETHER_HDR_LEN) {
1429 m = m_pullup(m, ETHER_HDR_LEN);
1430 if (m == NULL)
1431 return (0);
1435 * Serialize our bridge interface. We have to get rid of the
1436 * originating interface lock to avoid a deadlock.
1438 lwkt_serialize_exit(ifp->if_serializer);
1439 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1441 eh = mtod(m, struct ether_header *);
1444 * If bridge is down, but the original output interface is up,
1445 * go ahead and send out that interface. Otherwise, the packet
1446 * is dropped below.
1448 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
1449 dst_if = ifp;
1450 goto sendunicast;
1454 * If the packet is a multicast, or we don't know a better way to
1455 * get there, send to all interfaces.
1457 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1458 dst_if = NULL;
1459 else
1460 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1461 if (dst_if == NULL) {
1462 struct bridge_iflist *bif;
1463 struct mbuf *mc;
1464 int used = 0;
1466 bridge_span(sc, m);
1468 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1469 dst_if = bif->bif_ifp;
1470 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1471 continue;
1474 * If this is not the original output interface,
1475 * and the interface is participating in spanning
1476 * tree, make sure the port is in a state that
1477 * allows forwarding.
1479 if (dst_if != ifp &&
1480 (bif->bif_flags & IFBIF_STP) != 0) {
1481 switch (bif->bif_state) {
1482 case BSTP_IFSTATE_BLOCKING:
1483 case BSTP_IFSTATE_LISTENING:
1484 case BSTP_IFSTATE_DISABLED:
1485 continue;
1489 if (LIST_NEXT(bif, bif_next) == NULL) {
1490 used = 1;
1491 mc = m;
1492 } else {
1493 mc = m_copypacket(m, MB_DONTWAIT);
1494 if (mc == NULL) {
1495 sc->sc_ifp->if_oerrors++;
1496 continue;
1499 bridge_enqueue(dst_if, mc);
1501 if (used == 0)
1502 m_freem(m);
1503 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1504 goto done;
1507 sendunicast:
1509 * XXX Spanning tree consideration here?
1512 bridge_span(sc, m);
1513 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1514 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1515 m_freem(m);
1516 } else {
1517 bridge_enqueue(dst_if, m);
1519 done:
1520 lwkt_serialize_enter(ifp->if_serializer);
1521 return (0);
1525 * bridge_start:
1527 * Start output on a bridge.
1530 static void
1531 bridge_start(struct ifnet *ifp)
1533 struct bridge_softc *sc = ifp->if_softc;
1535 ASSERT_SERIALIZED(ifp->if_serializer);
1537 ifp->if_flags |= IFF_OACTIVE;
1538 for (;;) {
1539 struct ifnet *dst_if = NULL;
1540 struct ether_header *eh;
1541 struct mbuf *m;
1543 m = ifq_dequeue(&ifp->if_snd, NULL);
1544 if (m == NULL)
1545 break;
1547 if (m->m_len < sizeof(*eh)) {
1548 m = m_pullup(m, sizeof(*eh));
1549 if (m == NULL) {
1550 ifp->if_oerrors++;
1551 continue;
1554 eh = mtod(m, struct ether_header *);
1556 BPF_MTAP(ifp, m);
1557 ifp->if_opackets++;
1559 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1560 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1562 if (dst_if == NULL)
1563 bridge_broadcast(sc, ifp, m, 0);
1564 else
1565 bridge_enqueue(dst_if, m);
1567 ifp->if_flags &= ~IFF_OACTIVE;
1571 * bridge_forward:
1573 * The forwarding function of the bridge.
1575 static void
1576 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1578 struct bridge_iflist *bif;
1579 struct ifnet *src_if, *dst_if, *ifp;
1580 struct ether_header *eh;
1582 src_if = m->m_pkthdr.rcvif;
1583 ifp = sc->sc_ifp;
1585 ASSERT_SERIALIZED(ifp->if_serializer);
1587 ifp->if_ipackets++;
1588 ifp->if_ibytes += m->m_pkthdr.len;
1591 * Look up the bridge_iflist.
1593 bif = bridge_lookup_member_if(sc, src_if);
1594 if (bif == NULL) {
1595 /* Interface is not a bridge member (anymore?) */
1596 m_freem(m);
1597 return;
1600 if (bif->bif_flags & IFBIF_STP) {
1601 switch (bif->bif_state) {
1602 case BSTP_IFSTATE_BLOCKING:
1603 case BSTP_IFSTATE_LISTENING:
1604 case BSTP_IFSTATE_DISABLED:
1605 m_freem(m);
1606 return;
1610 eh = mtod(m, struct ether_header *);
1613 * If the interface is learning, and the source
1614 * address is valid and not multicast, record
1615 * the address.
1617 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1618 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1619 (eh->ether_shost[0] == 0 &&
1620 eh->ether_shost[1] == 0 &&
1621 eh->ether_shost[2] == 0 &&
1622 eh->ether_shost[3] == 0 &&
1623 eh->ether_shost[4] == 0 &&
1624 eh->ether_shost[5] == 0) == 0) {
1625 bridge_rtupdate(sc, eh->ether_shost, src_if, 0, IFBAF_DYNAMIC);
1628 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1629 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1630 m_freem(m);
1631 return;
1635 * At this point, the port either doesn't participate
1636 * in spanning tree or it is in the forwarding state.
1640 * If the packet is unicast, destined for someone on
1641 * "this" side of the bridge, drop it.
1643 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1644 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1645 if (src_if == dst_if) {
1646 m_freem(m);
1647 return;
1649 } else {
1650 /* ...forward it to all interfaces. */
1651 sc->sc_ifp->if_imcasts++;
1652 dst_if = NULL;
1655 if (dst_if == NULL) {
1656 bridge_broadcast(sc, src_if, m, 1);
1657 return;
1661 * At this point, we're dealing with a unicast frame
1662 * going to a different interface.
1664 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1665 m_freem(m);
1666 return;
1668 bif = bridge_lookup_member_if(sc, dst_if);
1669 if (bif == NULL) {
1670 /* Not a member of the bridge (anymore?) */
1671 m_freem(m);
1672 return;
1675 if (bif->bif_flags & IFBIF_STP) {
1676 switch (bif->bif_state) {
1677 case BSTP_IFSTATE_DISABLED:
1678 case BSTP_IFSTATE_BLOCKING:
1679 m_freem(m);
1680 return;
1684 lwkt_serialize_exit(ifp->if_serializer);
1686 /* run the packet filter */
1687 if (inet_pfil_hook.ph_hashooks > 0
1688 #ifdef INET6
1689 || inet6_pfil_hook.ph_hashooks > 0
1690 #endif
1692 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1693 goto done;
1694 if (m == NULL)
1695 goto done;
1697 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
1698 goto done;
1699 if (m == NULL)
1700 goto done;
1702 bridge_handoff(dst_if, m);
1705 * ifp's serializer was held on entry and is expected to be held
1706 * on return.
1708 done:
1709 lwkt_serialize_enter(ifp->if_serializer);
1713 * bridge_input:
1715 * Receive input from a member interface. Queue the packet for
1716 * bridging if it is not for us.
1718 static struct mbuf *
1719 bridge_input(struct ifnet *ifp, struct mbuf *m)
1721 struct bridge_softc *sc = ifp->if_bridge;
1722 struct bridge_iflist *bif;
1723 struct ifnet *bifp, *new_ifp;
1724 struct ether_header *eh;
1725 struct mbuf *mc, *mc2;
1727 new_ifp = NULL;
1728 bifp = sc->sc_ifp;
1730 lwkt_serialize_enter(bifp->if_serializer);
1732 if ((bifp->if_flags & IFF_RUNNING) == 0)
1733 goto out;
1736 * Implement support for bridge monitoring. If this flag has been
1737 * set on this interface, discard the packet once we push it through
1738 * the bpf(4) machinery, but before we do, increment the byte and
1739 * packet counters associated with this interface.
1741 if ((bifp->if_flags & IFF_MONITOR) != 0) {
1742 m->m_pkthdr.rcvif = bifp;
1743 BPF_MTAP(bifp, m);
1744 bifp->if_ipackets++;
1745 bifp->if_ibytes += m->m_pkthdr.len;
1746 m_freem(m);
1747 m = NULL;
1748 goto out;
1751 eh = mtod(m, struct ether_header *);
1753 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
1755 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
1757 * If the packet is for us, set the packets source as the
1758 * bridge, and return the packet back to ifnet.if_input for
1759 * local processing.
1761 KASSERT(bifp->if_bridge == NULL,
1762 ("loop created in bridge_input"));
1763 new_ifp = bifp;
1764 goto out;
1768 * Tap all packets arriving on the bridge, no matter if
1769 * they are local destinations or not. In is in.
1771 BPF_MTAP(bifp, m);
1773 bif = bridge_lookup_member_if(sc, ifp);
1774 if (bif == NULL)
1775 goto out;
1777 bridge_span(sc, m);
1779 if (m->m_flags & (M_BCAST | M_MCAST)) {
1780 /* Tap off 802.1D packets; they do not get forwarded. */
1781 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1782 ETHER_ADDR_LEN) == 0) {
1783 m = bstp_input(ifp, m);
1784 KASSERT(m == NULL,
1785 ("attempt to deliver 802.1D packet\n"));
1786 goto out;
1789 if (bif->bif_flags & IFBIF_STP) {
1790 switch (bif->bif_state) {
1791 case BSTP_IFSTATE_BLOCKING:
1792 case BSTP_IFSTATE_LISTENING:
1793 case BSTP_IFSTATE_DISABLED:
1794 goto out;
1799 * Make a deep copy of the packet and enqueue the copy
1800 * for bridge processing; return the original packet for
1801 * local processing.
1803 mc = m_dup(m, MB_DONTWAIT);
1804 if (mc == NULL)
1805 goto out;
1807 bridge_forward(sc, mc);
1810 * Reinject the mbuf as arriving on the bridge so we have a
1811 * chance at claiming multicast packets. We can not loop back
1812 * here from ether_input as a bridge is never a member of a
1813 * bridge.
1815 KASSERT(bifp->if_bridge == NULL,
1816 ("loop created in bridge_input"));
1817 mc2 = m_dup(m, MB_DONTWAIT);
1818 #ifdef notyet
1819 if (mc2 != NULL) {
1820 /* Keep the layer3 header aligned */
1821 int i = min(mc2->m_pkthdr.len, max_protohdr);
1822 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
1824 #endif
1825 if (mc2 != NULL) {
1826 mc2->m_pkthdr.rcvif = bifp;
1827 bifp->if_ipackets++;
1828 bifp->if_input(bifp, mc2);
1831 /* Return the original packet for local processing. */
1832 goto out;
1835 if (bif->bif_flags & IFBIF_STP) {
1836 switch (bif->bif_state) {
1837 case BSTP_IFSTATE_BLOCKING:
1838 case BSTP_IFSTATE_LISTENING:
1839 case BSTP_IFSTATE_DISABLED:
1840 goto out;
1845 * Unicast. Make sure it's not for us.
1847 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1848 if (bif->bif_ifp->if_type != IFT_ETHER)
1849 continue;
1851 /* It is destined for us. */
1852 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1853 ETHER_ADDR_LEN) == 0) {
1854 if (bif->bif_flags & IFBIF_LEARNING) {
1855 bridge_rtupdate(sc,
1856 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1859 if (bif->bif_ifp != ifp) {
1860 /* XXX loop prevention */
1861 m->m_flags |= M_PROTO1;
1862 new_ifp = bif->bif_ifp;
1864 goto out;
1867 /* We just received a packet that we sent out. */
1868 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1869 ETHER_ADDR_LEN) == 0) {
1870 m_freem(m);
1871 m = NULL;
1872 goto out;
1876 /* Perform the bridge forwarding function. */
1877 bridge_forward(sc, m);
1878 m = NULL;
1879 out:
1880 lwkt_serialize_exit(bifp->if_serializer);
1882 if (new_ifp != NULL) {
1883 lwkt_serialize_enter(new_ifp->if_serializer);
1885 m->m_pkthdr.rcvif = new_ifp;
1886 new_ifp->if_ipackets++;
1887 new_ifp->if_input(new_ifp, m);
1888 m = NULL;
1890 lwkt_serialize_exit(new_ifp->if_serializer);
1892 return (m);
1896 * bridge_broadcast:
1898 * Send a frame to all interfaces that are members of
1899 * the bridge, except for the one on which the packet
1900 * arrived.
1902 static void
1903 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1904 struct mbuf *m, int runfilt)
1906 struct bridge_iflist *bif;
1907 struct mbuf *mc;
1908 struct ifnet *dst_if, *bifp;
1909 int used = 0;
1911 bifp = sc->sc_ifp;
1913 ASSERT_SERIALIZED(bifp->if_serializer);
1915 /* run the packet filter */
1916 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1917 #ifdef INET6
1918 || inet6_pfil_hook.ph_hashooks > 0
1919 #endif
1920 )) {
1921 lwkt_serialize_exit(bifp->if_serializer);
1923 /* Filter on the bridge interface before broadcasting */
1925 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
1926 goto filt;
1927 if (m == NULL)
1928 goto filt;
1930 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
1931 m = NULL;
1932 filt:
1933 lwkt_serialize_enter(bifp->if_serializer);
1934 if (m == NULL)
1935 return;
1938 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1939 dst_if = bif->bif_ifp;
1940 if (dst_if == src_if)
1941 continue;
1943 if (bif->bif_flags & IFBIF_STP) {
1944 switch (bif->bif_state) {
1945 case BSTP_IFSTATE_BLOCKING:
1946 case BSTP_IFSTATE_DISABLED:
1947 continue;
1951 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1952 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1953 continue;
1955 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1956 continue;
1958 if (LIST_NEXT(bif, bif_next) == NULL) {
1959 mc = m;
1960 used = 1;
1961 } else {
1962 mc = m_copypacket(m, MB_DONTWAIT);
1963 if (mc == NULL) {
1964 sc->sc_ifp->if_oerrors++;
1965 continue;
1968 bridge_pfil_enqueue(dst_if, mc, runfilt);
1970 if (used == 0)
1971 m_freem(m);
1975 * bridge_span:
1977 * Duplicate a packet out one or more interfaces that are in span mode,
1978 * the original mbuf is unmodified.
1980 static void
1981 bridge_span(struct bridge_softc *sc, struct mbuf *m)
1983 struct bridge_iflist *bif;
1984 struct ifnet *dst_if;
1985 struct mbuf *mc;
1987 if (LIST_EMPTY(&sc->sc_spanlist))
1988 return;
1990 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1991 dst_if = bif->bif_ifp;
1993 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1994 continue;
1996 mc = m_copypacket(m, MB_DONTWAIT);
1997 if (mc == NULL) {
1998 sc->sc_ifp->if_oerrors++;
1999 continue;
2002 bridge_enqueue(dst_if, mc);
2007 * bridge_rtupdate:
2009 * Add a bridge routing entry.
2010 * Can be called from interrupt context.
2012 static int
2013 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2014 struct ifnet *dst_if, int setflags, uint8_t flags)
2016 struct bridge_rtnode *brt;
2017 int error;
2020 * A route for this destination might already exist. If so,
2021 * update it, otherwise create a new one.
2023 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2024 if (sc->sc_brtcnt >= sc->sc_brtmax)
2025 return (ENOSPC);
2028 * Allocate a new bridge forwarding node, and
2029 * initialize the expiration time and Ethernet
2030 * address.
2032 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2033 M_INTNOWAIT|M_ZERO);
2034 if (brt == NULL)
2035 return (ENOMEM);
2037 brt->brt_flags = IFBAF_DYNAMIC;
2038 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2040 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2041 kfree(brt, M_DEVBUF);
2042 return (error);
2046 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2047 brt->brt_ifp = dst_if;
2048 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2049 brt->brt_expire = time_second + sc->sc_brttimeout;
2050 if (setflags)
2051 brt->brt_flags = flags;
2053 return (0);
2057 * bridge_rtlookup:
2059 * Lookup the destination interface for an address.
2061 static struct ifnet *
2062 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2064 struct bridge_rtnode *brt;
2066 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2067 return (NULL);
2069 return (brt->brt_ifp);
2073 * bridge_rttrim:
2075 * Trim the routine table so that we have a number
2076 * of routing entries less than or equal to the
2077 * maximum number.
2079 static void
2080 bridge_rttrim(struct bridge_softc *sc)
2082 struct bridge_rtnode *brt, *nbrt;
2084 /* Make sure we actually need to do this. */
2085 if (sc->sc_brtcnt <= sc->sc_brtmax)
2086 return;
2088 /* Force an aging cycle; this might trim enough addresses. */
2089 bridge_rtage(sc);
2090 if (sc->sc_brtcnt <= sc->sc_brtmax)
2091 return;
2093 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2094 nbrt = LIST_NEXT(brt, brt_list);
2095 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2096 bridge_rtnode_destroy(sc, brt);
2097 if (sc->sc_brtcnt <= sc->sc_brtmax)
2098 return;
2104 * bridge_timer:
2106 * Aging timer for the bridge.
2108 static void
2109 bridge_timer(void *arg)
2111 struct bridge_softc *sc = arg;
2113 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
2115 bridge_rtage(sc);
2117 if (sc->sc_ifp->if_flags & IFF_RUNNING)
2118 callout_reset(&sc->sc_brcallout,
2119 bridge_rtable_prune_period * hz, bridge_timer, sc);
2121 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
2125 * bridge_rtage:
2127 * Perform an aging cycle.
2129 static void
2130 bridge_rtage(struct bridge_softc *sc)
2132 struct bridge_rtnode *brt, *nbrt;
2134 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2135 nbrt = LIST_NEXT(brt, brt_list);
2136 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2137 if (time_second >= brt->brt_expire)
2138 bridge_rtnode_destroy(sc, brt);
2144 * bridge_rtflush:
2146 * Remove all dynamic addresses from the bridge.
2148 static void
2149 bridge_rtflush(struct bridge_softc *sc, int full)
2151 struct bridge_rtnode *brt, *nbrt;
2153 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2154 nbrt = LIST_NEXT(brt, brt_list);
2155 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2156 bridge_rtnode_destroy(sc, brt);
2161 * bridge_rtdaddr:
2163 * Remove an address from the table.
2165 static int
2166 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2168 struct bridge_rtnode *brt;
2170 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2171 return (ENOENT);
2173 bridge_rtnode_destroy(sc, brt);
2174 return (0);
2178 * bridge_rtdelete:
2180 * Delete routes to a speicifc member interface.
2182 void
2183 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2185 struct bridge_rtnode *brt, *nbrt;
2187 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2188 nbrt = LIST_NEXT(brt, brt_list);
2189 if (brt->brt_ifp == ifp && (full ||
2190 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2191 bridge_rtnode_destroy(sc, brt);
2196 * bridge_rtable_init:
2198 * Initialize the route table for this bridge.
2200 static int
2201 bridge_rtable_init(struct bridge_softc *sc)
2203 int i;
2205 sc->sc_rthash = kmalloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2206 M_DEVBUF, M_WAITOK);
2208 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2209 LIST_INIT(&sc->sc_rthash[i]);
2211 sc->sc_rthash_key = karc4random();
2213 LIST_INIT(&sc->sc_rtlist);
2215 return (0);
2219 * bridge_rtable_fini:
2221 * Deconstruct the route table for this bridge.
2223 static void
2224 bridge_rtable_fini(struct bridge_softc *sc)
2227 kfree(sc->sc_rthash, M_DEVBUF);
2231 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2232 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2234 #define mix(a, b, c) \
2235 do { \
2236 a -= b; a -= c; a ^= (c >> 13); \
2237 b -= c; b -= a; b ^= (a << 8); \
2238 c -= a; c -= b; c ^= (b >> 13); \
2239 a -= b; a -= c; a ^= (c >> 12); \
2240 b -= c; b -= a; b ^= (a << 16); \
2241 c -= a; c -= b; c ^= (b >> 5); \
2242 a -= b; a -= c; a ^= (c >> 3); \
2243 b -= c; b -= a; b ^= (a << 10); \
2244 c -= a; c -= b; c ^= (b >> 15); \
2245 } while (/*CONSTCOND*/0)
2247 static __inline uint32_t
2248 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2250 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2252 b += addr[5] << 8;
2253 b += addr[4];
2254 a += addr[3] << 24;
2255 a += addr[2] << 16;
2256 a += addr[1] << 8;
2257 a += addr[0];
2259 mix(a, b, c);
2261 return (c & BRIDGE_RTHASH_MASK);
2264 #undef mix
2266 static int
2267 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2269 int i, d;
2271 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2272 d = ((int)a[i]) - ((int)b[i]);
2275 return (d);
2279 * bridge_rtnode_lookup:
2281 * Look up a bridge route node for the specified destination.
2283 static struct bridge_rtnode *
2284 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2286 struct bridge_rtnode *brt;
2287 uint32_t hash;
2288 int dir;
2290 hash = bridge_rthash(sc, addr);
2291 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2292 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2293 if (dir == 0)
2294 return (brt);
2295 if (dir > 0)
2296 return (NULL);
2299 return (NULL);
2303 * bridge_rtnode_insert:
2305 * Insert the specified bridge node into the route table. We
2306 * assume the entry is not already in the table.
2308 static int
2309 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2311 struct bridge_rtnode *lbrt;
2312 uint32_t hash;
2313 int dir;
2315 hash = bridge_rthash(sc, brt->brt_addr);
2317 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2318 if (lbrt == NULL) {
2319 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2320 goto out;
2323 do {
2324 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2325 if (dir == 0)
2326 return (EEXIST);
2327 if (dir > 0) {
2328 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2329 goto out;
2331 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2332 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2333 goto out;
2335 lbrt = LIST_NEXT(lbrt, brt_hash);
2336 } while (lbrt != NULL);
2338 #ifdef DIAGNOSTIC
2339 panic("bridge_rtnode_insert: impossible");
2340 #endif
2342 out:
2343 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2344 sc->sc_brtcnt++;
2346 return (0);
2350 * bridge_rtnode_destroy:
2352 * Destroy a bridge rtnode.
2354 static void
2355 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2358 LIST_REMOVE(brt, brt_hash);
2360 LIST_REMOVE(brt, brt_list);
2361 sc->sc_brtcnt--;
2362 kfree(brt, M_DEVBUF);
2366 * Send bridge packets through pfil if they are one of the types pfil can deal
2367 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2368 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2369 * that interface.
2371 static int
2372 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2374 int snap, error, i, hlen;
2375 struct ether_header *eh1, eh2;
2376 struct ip *ip;
2377 struct llc llc1;
2378 u_int16_t ether_type;
2380 snap = 0;
2381 error = -1; /* Default error if not error == 0 */
2383 if (pfil_bridge == 0 && pfil_member == 0)
2384 return (0); /* filtering is disabled */
2386 i = min((*mp)->m_pkthdr.len, max_protohdr);
2387 if ((*mp)->m_len < i) {
2388 *mp = m_pullup(*mp, i);
2389 if (*mp == NULL) {
2390 kprintf("%s: m_pullup failed\n", __func__);
2391 return (-1);
2395 eh1 = mtod(*mp, struct ether_header *);
2396 ether_type = ntohs(eh1->ether_type);
2399 * Check for SNAP/LLC.
2401 if (ether_type < ETHERMTU) {
2402 struct llc *llc2 = (struct llc *)(eh1 + 1);
2404 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2405 llc2->llc_dsap == LLC_SNAP_LSAP &&
2406 llc2->llc_ssap == LLC_SNAP_LSAP &&
2407 llc2->llc_control == LLC_UI) {
2408 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2409 snap = 1;
2414 * If we're trying to filter bridge traffic, don't look at anything
2415 * other than IP and ARP traffic. If the filter doesn't understand
2416 * IPv6, don't allow IPv6 through the bridge either. This is lame
2417 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2418 * but of course we don't have an AppleTalk filter to begin with.
2419 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2420 * ARP traffic.)
2422 switch (ether_type) {
2423 case ETHERTYPE_ARP:
2424 case ETHERTYPE_REVARP:
2425 return (0); /* Automatically pass */
2426 case ETHERTYPE_IP:
2427 #ifdef INET6
2428 case ETHERTYPE_IPV6:
2429 #endif /* INET6 */
2430 break;
2431 default:
2433 * Check to see if the user wants to pass non-ip
2434 * packets, these will not be checked by pfil(9) and
2435 * passed unconditionally so the default is to drop.
2437 if (pfil_onlyip)
2438 goto bad;
2441 /* Strip off the Ethernet header and keep a copy. */
2442 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2443 m_adj(*mp, ETHER_HDR_LEN);
2445 /* Strip off snap header, if present */
2446 if (snap) {
2447 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2448 m_adj(*mp, sizeof(struct llc));
2452 * Check the IP header for alignment and errors
2454 if (dir == PFIL_IN) {
2455 switch (ether_type) {
2456 case ETHERTYPE_IP:
2457 error = bridge_ip_checkbasic(mp);
2458 break;
2459 #ifdef INET6
2460 case ETHERTYPE_IPV6:
2461 error = bridge_ip6_checkbasic(mp);
2462 break;
2463 #endif /* INET6 */
2464 default:
2465 error = 0;
2467 if (error)
2468 goto bad;
2471 error = 0;
2474 * Run the packet through pfil
2476 switch (ether_type)
2478 case ETHERTYPE_IP :
2480 * before calling the firewall, swap fields the same as
2481 * IP does. here we assume the header is contiguous
2483 ip = mtod(*mp, struct ip *);
2485 ip->ip_len = ntohs(ip->ip_len);
2486 ip->ip_off = ntohs(ip->ip_off);
2489 * Run pfil on the member interface and the bridge, both can
2490 * be skipped by clearing pfil_member or pfil_bridge.
2492 * Keep the order:
2493 * in_if -> bridge_if -> out_if
2495 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2496 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2497 dir);
2499 if (*mp == NULL || error != 0) /* filter may consume */
2500 break;
2502 if (pfil_member && ifp != NULL)
2503 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2504 dir);
2506 if (*mp == NULL || error != 0) /* filter may consume */
2507 break;
2509 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2510 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2511 dir);
2513 if (*mp == NULL || error != 0) /* filter may consume */
2514 break;
2516 /* check if we need to fragment the packet */
2517 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
2518 i = (*mp)->m_pkthdr.len;
2519 if (i > ifp->if_mtu) {
2520 error = bridge_fragment(ifp, *mp, &eh2, snap,
2521 &llc1);
2522 return (error);
2526 /* Recalculate the ip checksum and restore byte ordering */
2527 ip = mtod(*mp, struct ip *);
2528 hlen = ip->ip_hl << 2;
2529 if (hlen < sizeof(struct ip))
2530 goto bad;
2531 if (hlen > (*mp)->m_len) {
2532 if ((*mp = m_pullup(*mp, hlen)) == 0)
2533 goto bad;
2534 ip = mtod(*mp, struct ip *);
2535 if (ip == NULL)
2536 goto bad;
2538 ip->ip_len = htons(ip->ip_len);
2539 ip->ip_off = htons(ip->ip_off);
2540 ip->ip_sum = 0;
2541 if (hlen == sizeof(struct ip))
2542 ip->ip_sum = in_cksum_hdr(ip);
2543 else
2544 ip->ip_sum = in_cksum(*mp, hlen);
2546 break;
2547 #ifdef INET6
2548 case ETHERTYPE_IPV6 :
2549 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2550 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2551 dir);
2553 if (*mp == NULL || error != 0) /* filter may consume */
2554 break;
2556 if (pfil_member && ifp != NULL)
2557 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2558 dir);
2560 if (*mp == NULL || error != 0) /* filter may consume */
2561 break;
2563 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2564 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2565 dir);
2566 break;
2567 #endif
2568 default :
2569 error = 0;
2570 break;
2573 if (*mp == NULL)
2574 return (error);
2575 if (error != 0)
2576 goto bad;
2578 error = -1;
2581 * Finally, put everything back the way it was and return
2583 if (snap) {
2584 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
2585 if (*mp == NULL)
2586 return (error);
2587 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2590 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
2591 if (*mp == NULL)
2592 return (error);
2593 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2595 return (0);
2597 bad:
2598 m_freem(*mp);
2599 *mp = NULL;
2600 return (error);
2604 * Perform basic checks on header size since
2605 * pfil assumes ip_input has already processed
2606 * it for it. Cut-and-pasted from ip_input.c.
2607 * Given how simple the IPv6 version is,
2608 * does the IPv4 version really need to be
2609 * this complicated?
2611 * XXX Should we update ipstat here, or not?
2612 * XXX Right now we update ipstat but not
2613 * XXX csum_counter.
2615 static int
2616 bridge_ip_checkbasic(struct mbuf **mp)
2618 struct mbuf *m = *mp;
2619 struct ip *ip;
2620 int len, hlen;
2621 u_short sum;
2623 if (*mp == NULL)
2624 return (-1);
2625 #if notyet
2626 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2627 if ((m = m_copyup(m, sizeof(struct ip),
2628 (max_linkhdr + 3) & ~3)) == NULL) {
2629 /* XXXJRT new stat, please */
2630 ipstat.ips_toosmall++;
2631 goto bad;
2633 } else
2634 #endif
2635 #ifndef __predict_false
2636 #define __predict_false(x) x
2637 #endif
2638 if (__predict_false(m->m_len < sizeof (struct ip))) {
2639 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2640 ipstat.ips_toosmall++;
2641 goto bad;
2644 ip = mtod(m, struct ip *);
2645 if (ip == NULL) goto bad;
2647 if (ip->ip_v != IPVERSION) {
2648 ipstat.ips_badvers++;
2649 goto bad;
2651 hlen = ip->ip_hl << 2;
2652 if (hlen < sizeof(struct ip)) { /* minimum header length */
2653 ipstat.ips_badhlen++;
2654 goto bad;
2656 if (hlen > m->m_len) {
2657 if ((m = m_pullup(m, hlen)) == 0) {
2658 ipstat.ips_badhlen++;
2659 goto bad;
2661 ip = mtod(m, struct ip *);
2662 if (ip == NULL) goto bad;
2665 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2666 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2667 } else {
2668 if (hlen == sizeof(struct ip)) {
2669 sum = in_cksum_hdr(ip);
2670 } else {
2671 sum = in_cksum(m, hlen);
2674 if (sum) {
2675 ipstat.ips_badsum++;
2676 goto bad;
2679 /* Retrieve the packet length. */
2680 len = ntohs(ip->ip_len);
2683 * Check for additional length bogosity
2685 if (len < hlen) {
2686 ipstat.ips_badlen++;
2687 goto bad;
2691 * Check that the amount of data in the buffers
2692 * is as at least much as the IP header would have us expect.
2693 * Drop packet if shorter than we expect.
2695 if (m->m_pkthdr.len < len) {
2696 ipstat.ips_tooshort++;
2697 goto bad;
2700 /* Checks out, proceed */
2701 *mp = m;
2702 return (0);
2704 bad:
2705 *mp = m;
2706 return (-1);
2709 #ifdef INET6
2711 * Same as above, but for IPv6.
2712 * Cut-and-pasted from ip6_input.c.
2713 * XXX Should we update ip6stat, or not?
2715 static int
2716 bridge_ip6_checkbasic(struct mbuf **mp)
2718 struct mbuf *m = *mp;
2719 struct ip6_hdr *ip6;
2722 * If the IPv6 header is not aligned, slurp it up into a new
2723 * mbuf with space for link headers, in the event we forward
2724 * it. Otherwise, if it is aligned, make sure the entire base
2725 * IPv6 header is in the first mbuf of the chain.
2727 #if notyet
2728 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2729 struct ifnet *inifp = m->m_pkthdr.rcvif;
2730 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2731 (max_linkhdr + 3) & ~3)) == NULL) {
2732 /* XXXJRT new stat, please */
2733 ip6stat.ip6s_toosmall++;
2734 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2735 goto bad;
2737 } else
2738 #endif
2739 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2740 struct ifnet *inifp = m->m_pkthdr.rcvif;
2741 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2742 ip6stat.ip6s_toosmall++;
2743 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2744 goto bad;
2748 ip6 = mtod(m, struct ip6_hdr *);
2750 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2751 ip6stat.ip6s_badvers++;
2752 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2753 goto bad;
2756 /* Checks out, proceed */
2757 *mp = m;
2758 return (0);
2760 bad:
2761 *mp = m;
2762 return (-1);
2764 #endif /* INET6 */
2767 * bridge_fragment:
2769 * Return a fragmented mbuf chain.
2771 static int
2772 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
2773 int snap, struct llc *llc)
2775 struct mbuf *m0;
2776 struct ip *ip;
2777 int error = -1;
2779 if (m->m_len < sizeof(struct ip) &&
2780 (m = m_pullup(m, sizeof(struct ip))) == NULL)
2781 goto out;
2782 ip = mtod(m, struct ip *);
2784 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
2785 CSUM_DELAY_IP);
2786 if (error)
2787 goto out;
2789 /* walk the chain and re-add the Ethernet header */
2790 for (m0 = m; m0; m0 = m0->m_nextpkt) {
2791 if (error == 0) {
2792 if (snap) {
2793 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
2794 if (m0 == NULL) {
2795 error = ENOBUFS;
2796 continue;
2798 bcopy(llc, mtod(m0, caddr_t),
2799 sizeof(struct llc));
2801 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
2802 if (m0 == NULL) {
2803 error = ENOBUFS;
2804 continue;
2806 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
2807 } else
2808 m_freem(m);
2811 if (error == 0)
2812 ipstat.ips_fragmented++;
2814 return (error);
2816 out:
2817 if (m != NULL)
2818 m_freem(m);
2819 return (error);
2822 static void
2823 bridge_enqueue_handler(struct netmsg *nmsg)
2825 struct netmsg_packet *nmp;
2826 struct ifnet *dst_ifp;
2827 struct mbuf *m;
2829 nmp = (struct netmsg_packet *)nmsg;
2830 m = nmp->nm_packet;
2831 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2833 bridge_handoff_notags(dst_ifp, m);
2836 static void
2837 bridge_pfil_enqueue_handler(struct netmsg *nmsg)
2839 struct netmsg_packet *nmp;
2840 struct ifnet *dst_ifp;
2841 struct mbuf *m;
2843 nmp = (struct netmsg_packet *)nmsg;
2844 m = nmp->nm_packet;
2845 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2848 * Filter on the output interface. Pass a NULL bridge interface
2849 * pointer so we do not redundantly filter on the bridge for
2850 * each interface we broadcast on.
2852 if (inet_pfil_hook.ph_hashooks > 0
2853 #ifdef INET6
2854 || inet6_pfil_hook.ph_hashooks > 0
2855 #endif
2857 if (bridge_pfil(&m, NULL, dst_ifp, PFIL_OUT) != 0)
2858 return;
2859 if (m == NULL)
2860 return;
2862 bridge_handoff_notags(dst_ifp, m);
2865 static void
2866 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m)
2868 while (m->m_type == MT_TAG) {
2869 /* XXX see ether_output_frame for full rules check */
2870 m = m->m_next;
2872 bridge_handoff_notags(dst_ifp, m);
2875 static void
2876 bridge_handoff_notags(struct ifnet *dst_ifp, struct mbuf *m)
2878 struct mbuf *m0;
2880 KKASSERT(m->m_type != MT_TAG);
2882 lwkt_serialize_enter(dst_ifp->if_serializer);
2884 /* We may be sending a fragment so traverse the mbuf */
2885 for (; m; m = m0) {
2886 struct altq_pktattr pktattr;
2888 m0 = m->m_nextpkt;
2889 m->m_nextpkt = NULL;
2891 if (ifq_is_enabled(&dst_ifp->if_snd))
2892 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
2894 ifq_handoff(dst_ifp, m, &pktattr);
2897 lwkt_serialize_exit(dst_ifp->if_serializer);