kernel - Greatly enhance if_bridge
[dragonfly.git] / sys / net / bridge / if_bridge.c
1 /*
2  * Copyright 2001 Wasabi Systems, Inc.
3  * All rights reserved.
4  *
5  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
6  *
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.
22  *
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.
34  */
35
36 /*
37  * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
38  * All rights reserved.
39  *
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.
53  *
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.
65  *
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  */
70
71 /*
72  * Network interface bridge support.
73  *
74  * TODO:
75  *
76  *      - Currently only supports Ethernet-like interfaces (Ethernet,
77  *        802.11, VLANs on Ethernet, etc.)  Figure out a nice way
78  *        to bridge other types of interfaces (FDDI-FDDI, and maybe
79  *        consider heterogenous bridges).
80  *
81  *
82  * Bridge's route information is duplicated to each CPUs:
83  *
84  *      CPU0          CPU1          CPU2          CPU3
85  * +-----------+ +-----------+ +-----------+ +-----------+
86  * |  rtnode   | |  rtnode   | |  rtnode   | |  rtnode   |
87  * |           | |           | |           | |           |
88  * | dst eaddr | | dst eaddr | | dst eaddr | | dst eaddr |
89  * +-----------+ +-----------+ +-----------+ +-----------+
90  *       |         |                     |         |
91  *       |         |                     |         |
92  *       |         |     +----------+    |         |
93  *       |         |     |  rtinfo  |    |         |
94  *       |         +---->|          |<---+         |
95  *       |               |  flags   |              |
96  *       +-------------->|  timeout |<-------------+
97  *                       |  dst_ifp |
98  *                       +----------+
99  *
100  * We choose to put timeout and dst_ifp into shared part, so updating
101  * them will be cheaper than using message forwarding.  Also there is
102  * not need to use spinlock to protect the updating: timeout and dst_ifp
103  * is not related and specific field's updating order has no importance.
104  * The cache pollution by the share part should not be heavy: in a stable
105  * setup, dst_ifp probably will be not changed in rtnode's life time,
106  * while timeout is refreshed once per second; most of the time, timeout
107  * and dst_ifp are read-only accessed.
108  *
109  *
110  * Bridge route information installation on bridge_input path:
111  *
112  *      CPU0           CPU1         CPU2          CPU3
113  *
114  *                               tcp_thread2
115  *                                    |
116  *                                alloc nmsg
117  *                    snd nmsg        |
118  *                    w/o rtinfo      |
119  *      ifnet0<-----------------------+
120  *        |                           :
121  *    lookup dst                      :
122  *   rtnode exists?(Y)free nmsg       :
123  *        |(N)                        :
124  *        |
125  *  alloc rtinfo
126  *  alloc rtnode
127  * install rtnode
128  *        |
129  *        +---------->ifnet1
130  *        : fwd nmsg    |
131  *        : w/ rtinfo   |
132  *        :             |
133  *        :             |
134  *                 alloc rtnode
135  *               (w/ nmsg's rtinfo)
136  *                install rtnode
137  *                      |
138  *                      +---------->ifnet2
139  *                      : fwd nmsg    |
140  *                      : w/ rtinfo   |
141  *                      :             |
142  *                      :         same as ifnet1
143  *                                    |
144  *                                    +---------->ifnet3
145  *                                    : fwd nmsg    |
146  *                                    : w/ rtinfo   |
147  *                                    :             |
148  *                                    :         same as ifnet1
149  *                                               free nmsg
150  *                                                  :
151  *                                                  :
152  *
153  * The netmsgs forwarded between protocol threads and ifnet threads are
154  * allocated with (M_WAITOK|M_NULLOK), so it will not fail under most
155  * cases (route information is too precious to be not installed :).
156  * Since multiple threads may try to install route information for the
157  * same dst eaddr, we look up route information in ifnet0.  However, this
158  * looking up only need to be performed on ifnet0, which is the start
159  * point of the route information installation process.
160  *
161  *
162  * Bridge route information deleting/flushing:
163  *
164  *  CPU0            CPU1             CPU2             CPU3
165  *
166  * netisr0
167  *   |
168  * find suitable rtnodes,
169  * mark their rtinfo dead
170  *   |
171  *   | domsg <------------------------------------------+
172  *   |                                                  | replymsg
173  *   |                                                  |
174  *   V     fwdmsg           fwdmsg           fwdmsg     |
175  * ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3
176  * delete rtnodes   delete rtnodes   delete rtnodes   delete rtnodes
177  * w/ dead rtinfo   w/ dead rtinfo   w/ dead rtinfo   w/ dead rtinfo
178  *                                                    free dead rtinfos
179  *
180  * All deleting/flushing operations are serialized by netisr0, so each
181  * operation only reaps the route information marked dead by itself.
182  *
183  *
184  * Bridge route information adding/deleting/flushing:
185  * Since all operation is serialized by the fixed message flow between
186  * ifnet threads, it is not possible to create corrupted per-cpu route
187  * information.
188  *
189  *
190  *
191  * Percpu member interface list iteration with blocking operation:
192  * Since one bridge could only delete one member interface at a time and
193  * the deleted member interface is not freed after netmsg_service_sync(),
194  * following way is used to make sure that even if the certain member
195  * interface is ripped from the percpu list during the blocking operation,
196  * the iteration still could keep going:
197  *
198  * LIST_FOREACH_MUTABLE(bif, sc->sc_iflists[mycpuid], bif_next, nbif) {
199  *     blocking operation;
200  *     blocking operation;
201  *     ...
202  *     ...
203  *     if (nbif != NULL && !nbif->bif_onlist) {
204  *         KKASSERT(bif->bif_onlist);
205  *         nbif = LIST_NEXT(bif, bif_next);
206  *     }
207  * }
208  *
209  * As mentioned above only one member interface could be unlinked from the
210  * percpu member interface list, so either bif or nbif may be not on the list,
211  * but _not_ both.  To keep the list iteration, we don't care about bif, but
212  * only nbif.  Since removed member interface will only be freed after we
213  * finish our work, it is safe to access any field in an unlinked bif (here
214  * bif_onlist).  If nbif is no longer on the list, then bif must be on the
215  * list, so we change nbif to the next element of bif and keep going.
216  */
217
218 #include "opt_inet.h"
219 #include "opt_inet6.h"
220
221 #include <sys/param.h>
222 #include <sys/mbuf.h>
223 #include <sys/malloc.h>
224 #include <sys/protosw.h>
225 #include <sys/systm.h>
226 #include <sys/time.h>
227 #include <sys/socket.h> /* for net/if.h */
228 #include <sys/sockio.h>
229 #include <sys/ctype.h>  /* string functions */
230 #include <sys/kernel.h>
231 #include <sys/random.h>
232 #include <sys/sysctl.h>
233 #include <sys/module.h>
234 #include <sys/proc.h>
235 #include <sys/priv.h>
236 #include <sys/lock.h>
237 #include <sys/thread.h>
238 #include <sys/thread2.h>
239 #include <sys/mpipe.h>
240
241 #include <net/bpf.h>
242 #include <net/if.h>
243 #include <net/if_dl.h>
244 #include <net/if_types.h>
245 #include <net/if_var.h>
246 #include <net/pfil.h>
247 #include <net/ifq_var.h>
248 #include <net/if_clone.h>
249
250 #include <netinet/in.h> /* for struct arpcom */
251 #include <netinet/in_systm.h>
252 #include <netinet/in_var.h>
253 #include <netinet/ip.h>
254 #include <netinet/ip_var.h>
255 #ifdef INET6
256 #include <netinet/ip6.h>
257 #include <netinet6/ip6_var.h>
258 #endif
259 #include <netinet/if_ether.h> /* for struct arpcom */
260 #include <net/bridge/if_bridgevar.h>
261 #include <net/if_llc.h>
262 #include <net/netmsg2.h>
263
264 #include <net/route.h>
265 #include <sys/in_cksum.h>
266
267 /*
268  * Size of the route hash table.  Must be a power of two.
269  */
270 #ifndef BRIDGE_RTHASH_SIZE
271 #define BRIDGE_RTHASH_SIZE              1024
272 #endif
273
274 #define BRIDGE_RTHASH_MASK              (BRIDGE_RTHASH_SIZE - 1)
275
276 /*
277  * Maximum number of addresses to cache.
278  */
279 #ifndef BRIDGE_RTABLE_MAX
280 #define BRIDGE_RTABLE_MAX               100
281 #endif
282
283 /*
284  * Spanning tree defaults.
285  */
286 #define BSTP_DEFAULT_MAX_AGE            (20 * 256)
287 #define BSTP_DEFAULT_HELLO_TIME         (2 * 256)
288 #define BSTP_DEFAULT_FORWARD_DELAY      (15 * 256)
289 #define BSTP_DEFAULT_HOLD_TIME          (1 * 256)
290 #define BSTP_DEFAULT_BRIDGE_PRIORITY    0x8000
291 #define BSTP_DEFAULT_PORT_PRIORITY      0x80
292 #define BSTP_DEFAULT_PATH_COST          55
293
294 /*
295  * Timeout (in seconds) for entries learned dynamically.
296  */
297 #ifndef BRIDGE_RTABLE_TIMEOUT
298 #define BRIDGE_RTABLE_TIMEOUT           (20 * 60)       /* same as ARP */
299 #endif
300
301 /*
302  * Number of seconds between walks of the route list.
303  */
304 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
305 #define BRIDGE_RTABLE_PRUNE_PERIOD      (5 * 60)
306 #endif
307
308 /*
309  * List of capabilities to mask on the member interface.
310  */
311 #define BRIDGE_IFCAPS_MASK              IFCAP_TXCSUM
312
313 typedef int     (*bridge_ctl_t)(struct bridge_softc *, void *);
314
315 struct netmsg_brctl {
316         struct netmsg_base      base;
317         bridge_ctl_t            bc_func;
318         struct bridge_softc     *bc_sc;
319         void                    *bc_arg;
320 };
321
322 struct netmsg_brsaddr {
323         struct netmsg_base      base;
324         struct bridge_softc     *br_softc;
325         struct ifnet            *br_dst_if;
326         struct bridge_rtinfo    *br_rtinfo;
327         int                     br_setflags;
328         uint8_t                 br_dst[ETHER_ADDR_LEN];
329         uint8_t                 br_flags;
330 };
331
332 struct netmsg_braddbif {
333         struct netmsg_base      base;
334         struct bridge_softc     *br_softc;
335         struct bridge_ifinfo    *br_bif_info;
336         struct ifnet            *br_bif_ifp;
337 };
338
339 struct netmsg_brdelbif {
340         struct netmsg_base      base;
341         struct bridge_softc     *br_softc;
342         struct bridge_ifinfo    *br_bif_info;
343         struct bridge_iflist_head *br_bif_list;
344 };
345
346 struct netmsg_brsflags {
347         struct netmsg_base      base;
348         struct bridge_softc     *br_softc;
349         struct bridge_ifinfo    *br_bif_info;
350         uint32_t                br_bif_flags;
351 };
352
353 eventhandler_tag        bridge_detach_cookie = NULL;
354
355 extern  struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
356 extern  int (*bridge_output_p)(struct ifnet *, struct mbuf *);
357 extern  void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
358
359 static int      bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
360
361 static int      bridge_clone_create(struct if_clone *, int, caddr_t);
362 static int      bridge_clone_destroy(struct ifnet *);
363
364 static int      bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
365 static void     bridge_mutecaps(struct bridge_ifinfo *, struct ifnet *, int);
366 static void     bridge_ifdetach(void *, struct ifnet *);
367 static void     bridge_init(void *);
368 static void     bridge_stop(struct ifnet *);
369 static void     bridge_start(struct ifnet *);
370 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
371 static int      bridge_output(struct ifnet *, struct mbuf *);
372
373 static void     bridge_forward(struct bridge_softc *, struct mbuf *m);
374
375 static void     bridge_timer_handler(netmsg_t);
376 static void     bridge_timer(void *);
377
378 static void     bridge_start_bcast(struct bridge_softc *, struct mbuf *);
379 static void     bridge_broadcast(struct bridge_softc *, struct ifnet *,
380                     struct mbuf *);
381 static void     bridge_span(struct bridge_softc *, struct mbuf *);
382
383 static int      bridge_rtupdate(struct bridge_softc *, const uint8_t *,
384                     struct ifnet *, uint8_t);
385 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
386 static void     bridge_rtreap(struct bridge_softc *);
387 static void     bridge_rtreap_async(struct bridge_softc *);
388 static void     bridge_rttrim(struct bridge_softc *);
389 static int      bridge_rtage_finddead(struct bridge_softc *);
390 static void     bridge_rtage(struct bridge_softc *);
391 static void     bridge_rtflush(struct bridge_softc *, int);
392 static int      bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
393 static int      bridge_rtsaddr(struct bridge_softc *, const uint8_t *,
394                     struct ifnet *, uint8_t);
395 static void     bridge_rtmsg_sync(struct bridge_softc *sc);
396 static void     bridge_rtreap_handler(netmsg_t);
397 static void     bridge_rtinstall_handler(netmsg_t);
398 static int      bridge_rtinstall_oncpu(struct bridge_softc *, const uint8_t *,
399                     struct ifnet *, int, uint8_t, struct bridge_rtinfo **);
400
401 static void     bridge_rtable_init(struct bridge_softc *);
402 static void     bridge_rtable_fini(struct bridge_softc *);
403
404 static int      bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
405 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
406                     const uint8_t *);
407 static void     bridge_rtnode_insert(struct bridge_softc *,
408                     struct bridge_rtnode *);
409 static void     bridge_rtnode_destroy(struct bridge_softc *,
410                     struct bridge_rtnode *);
411
412 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
413                     const char *name);
414 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
415                     struct ifnet *ifp);
416 static struct bridge_iflist *bridge_lookup_member_ifinfo(struct bridge_softc *,
417                     struct bridge_ifinfo *);
418 static void     bridge_delete_member(struct bridge_softc *,
419                     struct bridge_iflist *, int);
420 static void     bridge_delete_span(struct bridge_softc *,
421                     struct bridge_iflist *);
422
423 static int      bridge_control(struct bridge_softc *, u_long,
424                                bridge_ctl_t, void *);
425 static int      bridge_ioctl_init(struct bridge_softc *, void *);
426 static int      bridge_ioctl_stop(struct bridge_softc *, void *);
427 static int      bridge_ioctl_add(struct bridge_softc *, void *);
428 static int      bridge_ioctl_del(struct bridge_softc *, void *);
429 static int      bridge_ioctl_gifflags(struct bridge_softc *, void *);
430 static int      bridge_ioctl_sifflags(struct bridge_softc *, void *);
431 static int      bridge_ioctl_scache(struct bridge_softc *, void *);
432 static int      bridge_ioctl_gcache(struct bridge_softc *, void *);
433 static int      bridge_ioctl_gifs(struct bridge_softc *, void *);
434 static int      bridge_ioctl_rts(struct bridge_softc *, void *);
435 static int      bridge_ioctl_saddr(struct bridge_softc *, void *);
436 static int      bridge_ioctl_sto(struct bridge_softc *, void *);
437 static int      bridge_ioctl_gto(struct bridge_softc *, void *);
438 static int      bridge_ioctl_daddr(struct bridge_softc *, void *);
439 static int      bridge_ioctl_flush(struct bridge_softc *, void *);
440 static int      bridge_ioctl_gpri(struct bridge_softc *, void *);
441 static int      bridge_ioctl_spri(struct bridge_softc *, void *);
442 static int      bridge_ioctl_ght(struct bridge_softc *, void *);
443 static int      bridge_ioctl_sht(struct bridge_softc *, void *);
444 static int      bridge_ioctl_gfd(struct bridge_softc *, void *);
445 static int      bridge_ioctl_sfd(struct bridge_softc *, void *);
446 static int      bridge_ioctl_gma(struct bridge_softc *, void *);
447 static int      bridge_ioctl_sma(struct bridge_softc *, void *);
448 static int      bridge_ioctl_sifprio(struct bridge_softc *, void *);
449 static int      bridge_ioctl_sifcost(struct bridge_softc *, void *);
450 static int      bridge_ioctl_addspan(struct bridge_softc *, void *);
451 static int      bridge_ioctl_delspan(struct bridge_softc *, void *);
452 static int      bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
453                     int);
454 static int      bridge_ip_checkbasic(struct mbuf **mp);
455 #ifdef INET6
456 static int      bridge_ip6_checkbasic(struct mbuf **mp);
457 #endif /* INET6 */
458 static int      bridge_fragment(struct ifnet *, struct mbuf *,
459                     struct ether_header *, int, struct llc *);
460 static void     bridge_enqueue_handler(netmsg_t);
461 static void     bridge_handoff(struct ifnet *, struct mbuf *, int);
462
463 static void     bridge_del_bif_handler(netmsg_t);
464 static void     bridge_add_bif_handler(netmsg_t);
465 static void     bridge_set_bifflags_handler(netmsg_t);
466 static void     bridge_del_bif(struct bridge_softc *, struct bridge_ifinfo *,
467                     struct bridge_iflist_head *);
468 static void     bridge_add_bif(struct bridge_softc *, struct bridge_ifinfo *,
469                     struct ifnet *);
470 static void     bridge_set_bifflags(struct bridge_softc *,
471                     struct bridge_ifinfo *, uint32_t);
472
473 SYSCTL_DECL(_net_link);
474 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
475
476 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
477 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
478 static int pfil_member = 1; /* run pfil hooks on the member interface */
479 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
480     &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
481 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
482     &pfil_bridge, 0, "Packet filter on the bridge interface");
483 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
484     &pfil_member, 0, "Packet filter on the member interface");
485
486 struct bridge_control_arg {
487         union {
488                 struct ifbreq ifbreq;
489                 struct ifbifconf ifbifconf;
490                 struct ifbareq ifbareq;
491                 struct ifbaconf ifbaconf;
492                 struct ifbrparam ifbrparam;
493         } bca_u;
494         int     bca_len;
495         void    *bca_uptr;
496         void    *bca_kptr;
497 };
498
499 struct bridge_control {
500         bridge_ctl_t    bc_func;
501         int             bc_argsize;
502         int             bc_flags;
503 };
504
505 #define BC_F_COPYIN             0x01    /* copy arguments in */
506 #define BC_F_COPYOUT            0x02    /* copy arguments out */
507 #define BC_F_SUSER              0x04    /* do super-user check */
508
509 const struct bridge_control bridge_control_table[] = {
510         { bridge_ioctl_add,             sizeof(struct ifbreq),
511           BC_F_COPYIN|BC_F_SUSER },
512         { bridge_ioctl_del,             sizeof(struct ifbreq),
513           BC_F_COPYIN|BC_F_SUSER },
514
515         { bridge_ioctl_gifflags,        sizeof(struct ifbreq),
516           BC_F_COPYIN|BC_F_COPYOUT },
517         { bridge_ioctl_sifflags,        sizeof(struct ifbreq),
518           BC_F_COPYIN|BC_F_SUSER },
519
520         { bridge_ioctl_scache,          sizeof(struct ifbrparam),
521           BC_F_COPYIN|BC_F_SUSER },
522         { bridge_ioctl_gcache,          sizeof(struct ifbrparam),
523           BC_F_COPYOUT },
524
525         { bridge_ioctl_gifs,            sizeof(struct ifbifconf),
526           BC_F_COPYIN|BC_F_COPYOUT },
527         { bridge_ioctl_rts,             sizeof(struct ifbaconf),
528           BC_F_COPYIN|BC_F_COPYOUT },
529
530         { bridge_ioctl_saddr,           sizeof(struct ifbareq),
531           BC_F_COPYIN|BC_F_SUSER },
532
533         { bridge_ioctl_sto,             sizeof(struct ifbrparam),
534           BC_F_COPYIN|BC_F_SUSER },
535         { bridge_ioctl_gto,             sizeof(struct ifbrparam),
536           BC_F_COPYOUT },
537
538         { bridge_ioctl_daddr,           sizeof(struct ifbareq),
539           BC_F_COPYIN|BC_F_SUSER },
540
541         { bridge_ioctl_flush,           sizeof(struct ifbreq),
542           BC_F_COPYIN|BC_F_SUSER },
543
544         { bridge_ioctl_gpri,            sizeof(struct ifbrparam),
545           BC_F_COPYOUT },
546         { bridge_ioctl_spri,            sizeof(struct ifbrparam),
547           BC_F_COPYIN|BC_F_SUSER },
548
549         { bridge_ioctl_ght,             sizeof(struct ifbrparam),
550           BC_F_COPYOUT },
551         { bridge_ioctl_sht,             sizeof(struct ifbrparam),
552           BC_F_COPYIN|BC_F_SUSER },
553
554         { bridge_ioctl_gfd,             sizeof(struct ifbrparam),
555           BC_F_COPYOUT },
556         { bridge_ioctl_sfd,             sizeof(struct ifbrparam),
557           BC_F_COPYIN|BC_F_SUSER },
558
559         { bridge_ioctl_gma,             sizeof(struct ifbrparam),
560           BC_F_COPYOUT },
561         { bridge_ioctl_sma,             sizeof(struct ifbrparam),
562           BC_F_COPYIN|BC_F_SUSER },
563
564         { bridge_ioctl_sifprio,         sizeof(struct ifbreq),
565           BC_F_COPYIN|BC_F_SUSER },
566
567         { bridge_ioctl_sifcost,         sizeof(struct ifbreq),
568           BC_F_COPYIN|BC_F_SUSER },
569
570         { bridge_ioctl_addspan,         sizeof(struct ifbreq),
571           BC_F_COPYIN|BC_F_SUSER },
572         { bridge_ioctl_delspan,         sizeof(struct ifbreq),
573           BC_F_COPYIN|BC_F_SUSER },
574 };
575 static const int bridge_control_table_size = NELEM(bridge_control_table);
576
577 LIST_HEAD(, bridge_softc) bridge_list;
578
579 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
580                                 bridge_clone_create,
581                                 bridge_clone_destroy, 0, IF_MAXUNIT);
582
583 static int
584 bridge_modevent(module_t mod, int type, void *data)
585 {
586         switch (type) {
587         case MOD_LOAD:
588                 LIST_INIT(&bridge_list);
589                 if_clone_attach(&bridge_cloner);
590                 bridge_input_p = bridge_input;
591                 bridge_output_p = bridge_output;
592                 bridge_detach_cookie = EVENTHANDLER_REGISTER(
593                     ifnet_detach_event, bridge_ifdetach, NULL,
594                     EVENTHANDLER_PRI_ANY);
595 #if notyet
596                 bstp_linkstate_p = bstp_linkstate;
597 #endif
598                 break;
599         case MOD_UNLOAD:
600                 if (!LIST_EMPTY(&bridge_list))
601                         return (EBUSY);
602                 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
603                     bridge_detach_cookie);
604                 if_clone_detach(&bridge_cloner);
605                 bridge_input_p = NULL;
606                 bridge_output_p = NULL;
607 #if notyet
608                 bstp_linkstate_p = NULL;
609 #endif
610                 break;
611         default:
612                 return (EOPNOTSUPP);
613         }
614         return (0);
615 }
616
617 static moduledata_t bridge_mod = {
618         "if_bridge",
619         bridge_modevent,
620         0
621 };
622
623 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
624
625
626 /*
627  * bridge_clone_create:
628  *
629  *      Create a new bridge instance.
630  */
631 static int
632 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
633 {
634         struct bridge_softc *sc;
635         struct ifnet *ifp;
636         u_char eaddr[6];
637         int cpu, rnd;
638
639         sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
640         ifp = sc->sc_ifp = &sc->sc_if;
641
642         sc->sc_brtmax = BRIDGE_RTABLE_MAX;
643         sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
644         sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
645         sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
646         sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
647         sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
648         sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
649
650         /* Initialize our routing table. */
651         bridge_rtable_init(sc);
652
653         callout_init(&sc->sc_brcallout);
654         netmsg_init(&sc->sc_brtimemsg, NULL, &netisr_adone_rport,
655                     MSGF_DROPABLE, bridge_timer_handler);
656         sc->sc_brtimemsg.lmsg.u.ms_resultp = sc;
657
658         callout_init(&sc->sc_bstpcallout);
659         netmsg_init(&sc->sc_bstptimemsg, NULL, &netisr_adone_rport,
660                     MSGF_DROPABLE, bstp_tick_handler);
661         sc->sc_bstptimemsg.lmsg.u.ms_resultp = sc;
662
663         /* Initialize per-cpu member iface lists */
664         sc->sc_iflists = kmalloc(sizeof(*sc->sc_iflists) * ncpus,
665                                  M_DEVBUF, M_WAITOK);
666         for (cpu = 0; cpu < ncpus; ++cpu)
667                 LIST_INIT(&sc->sc_iflists[cpu]);
668
669         LIST_INIT(&sc->sc_spanlist);
670
671         ifp->if_softc = sc;
672         if_initname(ifp, ifc->ifc_name, unit);
673         ifp->if_mtu = ETHERMTU;
674         ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
675         ifp->if_ioctl = bridge_ioctl;
676         ifp->if_start = bridge_start;
677         ifp->if_init = bridge_init;
678         ifp->if_type = IFT_BRIDGE;
679         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
680         ifq_set_ready(&ifp->if_snd);
681         ifp->if_hdrlen = ETHER_HDR_LEN;
682
683         /*
684          * Generate a random ethernet address and use the private AC:DE:48
685          * OUI code.
686          */
687         rnd = karc4random();
688         bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
689         rnd = karc4random();
690         bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
691
692         eaddr[0] &= ~1; /* clear multicast bit */
693         eaddr[0] |= 2;  /* set the LAA bit */
694
695         ether_ifattach(ifp, eaddr, NULL);
696         /* Now undo some of the damage... */
697         ifp->if_baudrate = 0;
698         ifp->if_type = IFT_BRIDGE;
699
700         crit_enter();   /* XXX MP */
701         LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
702         crit_exit();
703
704         return (0);
705 }
706
707 static void
708 bridge_delete_dispatch(netmsg_t msg)
709 {
710         struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
711         struct ifnet *bifp = sc->sc_ifp;
712         struct bridge_iflist *bif;
713
714         ifnet_serialize_all(bifp);
715
716         while ((bif = LIST_FIRST(&sc->sc_iflists[mycpuid])) != NULL)
717                 bridge_delete_member(sc, bif, 0);
718
719         while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL)
720                 bridge_delete_span(sc, bif);
721
722         ifnet_deserialize_all(bifp);
723
724         lwkt_replymsg(&msg->lmsg, 0);
725 }
726
727 /*
728  * bridge_clone_destroy:
729  *
730  *      Destroy a bridge instance.
731  */
732 static int
733 bridge_clone_destroy(struct ifnet *ifp)
734 {
735         struct bridge_softc *sc = ifp->if_softc;
736         struct netmsg_base msg;
737
738         ifnet_serialize_all(ifp);
739
740         bridge_stop(ifp);
741         ifp->if_flags &= ~IFF_UP;
742
743         ifnet_deserialize_all(ifp);
744
745         netmsg_init(&msg, NULL, &curthread->td_msgport,
746                     0, bridge_delete_dispatch);
747         msg.lmsg.u.ms_resultp = sc;
748         lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0);
749
750         crit_enter();   /* XXX MP */
751         LIST_REMOVE(sc, sc_list);
752         crit_exit();
753
754         ether_ifdetach(ifp);
755
756         /* Tear down the routing table. */
757         bridge_rtable_fini(sc);
758
759         /* Free per-cpu member iface lists */
760         kfree(sc->sc_iflists, M_DEVBUF);
761
762         kfree(sc, M_DEVBUF);
763
764         return 0;
765 }
766
767 /*
768  * bridge_ioctl:
769  *
770  *      Handle a control request from the operator.
771  */
772 static int
773 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
774 {
775         struct bridge_softc *sc = ifp->if_softc;
776         struct bridge_control_arg args;
777         struct ifdrv *ifd = (struct ifdrv *) data;
778         const struct bridge_control *bc;
779         int error = 0;
780
781         ASSERT_IFNET_SERIALIZED_ALL(ifp);
782
783         switch (cmd) {
784         case SIOCADDMULTI:
785         case SIOCDELMULTI:
786                 break;
787
788         case SIOCGDRVSPEC:
789         case SIOCSDRVSPEC:
790                 if (ifd->ifd_cmd >= bridge_control_table_size) {
791                         error = EINVAL;
792                         break;
793                 }
794                 bc = &bridge_control_table[ifd->ifd_cmd];
795
796                 if (cmd == SIOCGDRVSPEC &&
797                     (bc->bc_flags & BC_F_COPYOUT) == 0) {
798                         error = EINVAL;
799                         break;
800                 } else if (cmd == SIOCSDRVSPEC &&
801                            (bc->bc_flags & BC_F_COPYOUT)) {
802                         error = EINVAL;
803                         break;
804                 }
805
806                 if (bc->bc_flags & BC_F_SUSER) {
807                         error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
808                         if (error)
809                                 break;
810                 }
811
812                 if (ifd->ifd_len != bc->bc_argsize ||
813                     ifd->ifd_len > sizeof(args.bca_u)) {
814                         error = EINVAL;
815                         break;
816                 }
817
818                 memset(&args, 0, sizeof(args));
819                 if (bc->bc_flags & BC_F_COPYIN) {
820                         error = copyin(ifd->ifd_data, &args.bca_u,
821                                        ifd->ifd_len);
822                         if (error)
823                                 break;
824                 }
825
826                 error = bridge_control(sc, cmd, bc->bc_func, &args);
827                 if (error) {
828                         KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
829                         break;
830                 }
831
832                 if (bc->bc_flags & BC_F_COPYOUT) {
833                         error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
834                         if (args.bca_len != 0) {
835                                 KKASSERT(args.bca_kptr != NULL);
836                                 if (!error) {
837                                         error = copyout(args.bca_kptr,
838                                                 args.bca_uptr, args.bca_len);
839                                 }
840                                 kfree(args.bca_kptr, M_TEMP);
841                         } else {
842                                 KKASSERT(args.bca_kptr == NULL);
843                         }
844                 } else {
845                         KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
846                 }
847                 break;
848
849         case SIOCSIFFLAGS:
850                 if (!(ifp->if_flags & IFF_UP) &&
851                     (ifp->if_flags & IFF_RUNNING)) {
852                         /*
853                          * If interface is marked down and it is running,
854                          * then stop it.
855                          */
856                         bridge_stop(ifp);
857                 } else if ((ifp->if_flags & IFF_UP) &&
858                     !(ifp->if_flags & IFF_RUNNING)) {
859                         /*
860                          * If interface is marked up and it is stopped, then
861                          * start it.
862                          */
863                         ifp->if_init(sc);
864                 }
865                 break;
866
867         case SIOCSIFMTU:
868                 /* Do not allow the MTU to be changed on the bridge */
869                 error = EINVAL;
870                 break;
871
872         default:
873                 error = ether_ioctl(ifp, cmd, data);
874                 break;
875         }
876         return (error);
877 }
878
879 /*
880  * bridge_mutecaps:
881  *
882  *      Clear or restore unwanted capabilities on the member interface
883  */
884 static void
885 bridge_mutecaps(struct bridge_ifinfo *bif_info, struct ifnet *ifp, int mute)
886 {
887         struct ifreq ifr;
888         int error;
889
890         if (ifp->if_ioctl == NULL)
891                 return;
892
893         bzero(&ifr, sizeof(ifr));
894         ifr.ifr_reqcap = ifp->if_capenable;
895
896         if (mute) {
897                 /* mask off and save capabilities */
898                 bif_info->bifi_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
899                 if (bif_info->bifi_mutecap != 0)
900                         ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
901         } else {
902                 /* restore muted capabilities */
903                 ifr.ifr_reqcap |= bif_info->bifi_mutecap;
904         }
905
906         if (bif_info->bifi_mutecap != 0) {
907                 ifnet_serialize_all(ifp);
908                 error = ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
909                 ifnet_deserialize_all(ifp);
910         }
911 }
912
913 /*
914  * bridge_lookup_member:
915  *
916  *      Lookup a bridge member interface.
917  */
918 static struct bridge_iflist *
919 bridge_lookup_member(struct bridge_softc *sc, const char *name)
920 {
921         struct bridge_iflist *bif;
922
923         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
924                 if (strcmp(bif->bif_ifp->if_xname, name) == 0)
925                         return (bif);
926         }
927         return (NULL);
928 }
929
930 /*
931  * bridge_lookup_member_if:
932  *
933  *      Lookup a bridge member interface by ifnet*.
934  */
935 static struct bridge_iflist *
936 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
937 {
938         struct bridge_iflist *bif;
939
940         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
941                 if (bif->bif_ifp == member_ifp)
942                         return (bif);
943         }
944         return (NULL);
945 }
946
947 /*
948  * bridge_lookup_member_ifinfo:
949  *
950  *      Lookup a bridge member interface by bridge_ifinfo.
951  */
952 static struct bridge_iflist *
953 bridge_lookup_member_ifinfo(struct bridge_softc *sc,
954                             struct bridge_ifinfo *bif_info)
955 {
956         struct bridge_iflist *bif;
957
958         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
959                 if (bif->bif_info == bif_info)
960                         return (bif);
961         }
962         return (NULL);
963 }
964
965 /*
966  * bridge_delete_member:
967  *
968  *      Delete the specified member interface.
969  */
970 static void
971 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
972     int gone)
973 {
974         struct ifnet *ifs = bif->bif_ifp;
975         struct ifnet *bifp = sc->sc_ifp;
976         struct bridge_ifinfo *bif_info = bif->bif_info;
977         struct bridge_iflist_head saved_bifs;
978
979         ASSERT_IFNET_SERIALIZED_ALL(bifp);
980         KKASSERT(bif_info != NULL);
981
982         ifs->if_bridge = NULL;
983
984         /*
985          * Release bridge interface's serializer:
986          * - To avoid possible dead lock.
987          * - Various sync operation will block the current thread.
988          */
989         ifnet_deserialize_all(bifp);
990
991         if (!gone) {
992                 switch (ifs->if_type) {
993                 case IFT_ETHER:
994                 case IFT_L2VLAN:
995                         /*
996                          * Take the interface out of promiscuous mode.
997                          */
998                         ifpromisc(ifs, 0);
999                         bridge_mutecaps(bif_info, ifs, 0);
1000                         break;
1001
1002                 case IFT_GIF:
1003                         break;
1004
1005                 default:
1006                         panic("bridge_delete_member: impossible");
1007                         break;
1008                 }
1009         }
1010
1011         /*
1012          * Remove bifs from percpu linked list.
1013          *
1014          * Removed bifs are not freed immediately, instead,
1015          * they are saved in saved_bifs.  They will be freed
1016          * after we make sure that no one is accessing them,
1017          * i.e. after following netmsg_service_sync()
1018          */
1019         LIST_INIT(&saved_bifs);
1020         bridge_del_bif(sc, bif_info, &saved_bifs);
1021
1022         /*
1023          * Make sure that all protocol threads:
1024          * o  see 'ifs' if_bridge is changed
1025          * o  know that bif is removed from the percpu linked list
1026          */
1027         netmsg_service_sync();
1028
1029         /*
1030          * Free the removed bifs
1031          */
1032         KKASSERT(!LIST_EMPTY(&saved_bifs));
1033         while ((bif = LIST_FIRST(&saved_bifs)) != NULL) {
1034                 LIST_REMOVE(bif, bif_next);
1035                 kfree(bif, M_DEVBUF);
1036         }
1037
1038         /* See the comment in bridge_ioctl_stop() */
1039         bridge_rtmsg_sync(sc);
1040         bridge_rtdelete(sc, ifs, IFBF_FLUSHALL | IFBF_FLUSHSYNC);
1041
1042         ifnet_serialize_all(bifp);
1043
1044         if (bifp->if_flags & IFF_RUNNING)
1045                 bstp_initialization(sc);
1046
1047         /*
1048          * Free the bif_info after bstp_initialization(), so that
1049          * bridge_softc.sc_root_port will not reference a dangling
1050          * pointer.
1051          */
1052         kfree(bif_info, M_DEVBUF);
1053 }
1054
1055 /*
1056  * bridge_delete_span:
1057  *
1058  *      Delete the specified span interface.
1059  */
1060 static void
1061 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1062 {
1063         KASSERT(bif->bif_ifp->if_bridge == NULL,
1064             ("%s: not a span interface", __func__));
1065
1066         LIST_REMOVE(bif, bif_next);
1067         kfree(bif, M_DEVBUF);
1068 }
1069
1070 static int
1071 bridge_ioctl_init(struct bridge_softc *sc, void *arg __unused)
1072 {
1073         struct ifnet *ifp = sc->sc_ifp;
1074
1075         if (ifp->if_flags & IFF_RUNNING)
1076                 return 0;
1077
1078         callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1079             bridge_timer, sc);
1080
1081         ifp->if_flags |= IFF_RUNNING;
1082         bstp_initialization(sc);
1083         return 0;
1084 }
1085
1086 static int
1087 bridge_ioctl_stop(struct bridge_softc *sc, void *arg __unused)
1088 {
1089         struct ifnet *ifp = sc->sc_ifp;
1090         struct lwkt_msg *lmsg;
1091
1092         if ((ifp->if_flags & IFF_RUNNING) == 0)
1093                 return 0;
1094
1095         callout_stop(&sc->sc_brcallout);
1096
1097         crit_enter();
1098         lmsg = &sc->sc_brtimemsg.lmsg;
1099         if ((lmsg->ms_flags & MSGF_DONE) == 0) {
1100                 /* Pending to be processed; drop it */
1101                 lwkt_dropmsg(lmsg);
1102         }
1103         crit_exit();
1104
1105         bstp_stop(sc);
1106
1107         ifp->if_flags &= ~IFF_RUNNING;
1108
1109         ifnet_deserialize_all(ifp);
1110
1111         /* Let everyone know that we are stopped */
1112         netmsg_service_sync();
1113
1114         /*
1115          * Sync ifnetX msgports in the order we forward rtnode
1116          * installation message.  This is used to make sure that
1117          * all rtnode installation messages sent by bridge_rtupdate()
1118          * during above netmsg_service_sync() are flushed.
1119          */
1120         bridge_rtmsg_sync(sc);
1121         bridge_rtflush(sc, IFBF_FLUSHDYN | IFBF_FLUSHSYNC);
1122
1123         ifnet_serialize_all(ifp);
1124         return 0;
1125 }
1126
1127 static int
1128 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1129 {
1130         struct ifbreq *req = arg;
1131         struct bridge_iflist *bif;
1132         struct bridge_ifinfo *bif_info;
1133         struct ifnet *ifs, *bifp;
1134         int error = 0;
1135
1136         bifp = sc->sc_ifp;
1137         ASSERT_IFNET_SERIALIZED_ALL(bifp);
1138
1139         ifs = ifunit(req->ifbr_ifsname);
1140         if (ifs == NULL)
1141                 return (ENOENT);
1142
1143         /* If it's in the span list, it can't be a member. */
1144         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1145                 if (ifs == bif->bif_ifp)
1146                         return (EBUSY);
1147
1148         /* Allow the first Ethernet member to define the MTU */
1149         if (ifs->if_type != IFT_GIF) {
1150                 if (LIST_EMPTY(&sc->sc_iflists[mycpuid])) {
1151                         bifp->if_mtu = ifs->if_mtu;
1152                 } else if (bifp->if_mtu != ifs->if_mtu) {
1153                         if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
1154                         return (EINVAL);
1155                 }
1156         }
1157
1158         if (ifs->if_bridge == sc)
1159                 return (EEXIST);
1160
1161         if (ifs->if_bridge != NULL)
1162                 return (EBUSY);
1163
1164         bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO);
1165         bif_info->bifi_priority = BSTP_DEFAULT_PORT_PRIORITY;
1166         bif_info->bifi_path_cost = BSTP_DEFAULT_PATH_COST;
1167         bif_info->bifi_ifp = ifs;
1168
1169         /*
1170          * Release bridge interface's serializer:
1171          * - To avoid possible dead lock.
1172          * - Various sync operation will block the current thread.
1173          */
1174         ifnet_deserialize_all(bifp);
1175
1176         switch (ifs->if_type) {
1177         case IFT_ETHER:
1178         case IFT_L2VLAN:
1179                 /*
1180                  * Place the interface into promiscuous mode.
1181                  */
1182                 error = ifpromisc(ifs, 1);
1183                 if (error) {
1184                         ifnet_serialize_all(bifp);
1185                         goto out;
1186                 }
1187                 bridge_mutecaps(bif_info, ifs, 1);
1188                 break;
1189
1190         case IFT_GIF: /* :^) */
1191                 break;
1192
1193         default:
1194                 error = EINVAL;
1195                 ifnet_serialize_all(bifp);
1196                 goto out;
1197         }
1198
1199         /*
1200          * Add bifs to percpu linked lists
1201          */
1202         bridge_add_bif(sc, bif_info, ifs);
1203
1204         ifnet_serialize_all(bifp);
1205
1206         if (bifp->if_flags & IFF_RUNNING)
1207                 bstp_initialization(sc);
1208         else
1209                 bstp_stop(sc);
1210
1211         /*
1212          * Everything has been setup, so let the member interface
1213          * deliver packets to this bridge on its input/output path.
1214          */
1215         ifs->if_bridge = sc;
1216 out:
1217         if (error) {
1218                 if (bif_info != NULL)
1219                         kfree(bif_info, M_DEVBUF);
1220         }
1221         return (error);
1222 }
1223
1224 static int
1225 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1226 {
1227         struct ifbreq *req = arg;
1228         struct bridge_iflist *bif;
1229
1230         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1231         if (bif == NULL)
1232                 return (ENOENT);
1233
1234         bridge_delete_member(sc, bif, 0);
1235
1236         return (0);
1237 }
1238
1239 static int
1240 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1241 {
1242         struct ifbreq *req = arg;
1243         struct bridge_iflist *bif;
1244
1245         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1246         if (bif == NULL)
1247                 return (ENOENT);
1248
1249         req->ifbr_ifsflags = bif->bif_flags;
1250         req->ifbr_state = bif->bif_state;
1251         req->ifbr_priority = bif->bif_priority;
1252         req->ifbr_path_cost = bif->bif_path_cost;
1253         req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1254         req->ifbr_designated_root = bif->bif_designated_root;
1255         req->ifbr_designated_bridge = bif->bif_designated_bridge;
1256         req->ifbr_designated_cost = bif->bif_designated_cost;
1257         req->ifbr_designated_port = bif->bif_designated_port;
1258
1259         return (0);
1260 }
1261
1262 static int
1263 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1264 {
1265         struct ifbreq *req = arg;
1266         struct bridge_iflist *bif;
1267         struct ifnet *bifp = sc->sc_ifp;
1268
1269         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1270         if (bif == NULL)
1271                 return (ENOENT);
1272
1273         if (req->ifbr_ifsflags & IFBIF_SPAN) {
1274                 /* SPAN is readonly */
1275                 return (EINVAL);
1276         }
1277
1278         if (req->ifbr_ifsflags & IFBIF_STP) {
1279                 switch (bif->bif_ifp->if_type) {
1280                 case IFT_ETHER:
1281                         /* These can do spanning tree. */
1282                         break;
1283
1284                 default:
1285                         /* Nothing else can. */
1286                         return (EINVAL);
1287                 }
1288         }
1289
1290         ifnet_deserialize_all(bifp);
1291         bridge_set_bifflags(sc, bif->bif_info, req->ifbr_ifsflags);
1292         ifnet_serialize_all(bifp);
1293
1294         if (bifp->if_flags & IFF_RUNNING)
1295                 bstp_initialization(sc);
1296
1297         return (0);
1298 }
1299
1300 static int
1301 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1302 {
1303         struct ifbrparam *param = arg;
1304         struct ifnet *ifp = sc->sc_ifp;
1305
1306         sc->sc_brtmax = param->ifbrp_csize;
1307
1308         ifnet_deserialize_all(ifp);
1309         bridge_rttrim(sc);
1310         ifnet_serialize_all(ifp);
1311
1312         return (0);
1313 }
1314
1315 static int
1316 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1317 {
1318         struct ifbrparam *param = arg;
1319
1320         param->ifbrp_csize = sc->sc_brtmax;
1321
1322         return (0);
1323 }
1324
1325 static int
1326 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1327 {
1328         struct bridge_control_arg *bc_arg = arg;
1329         struct ifbifconf *bifc = arg;
1330         struct bridge_iflist *bif;
1331         struct ifbreq *breq;
1332         int count, len;
1333
1334         count = 0;
1335         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next)
1336                 count++;
1337         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1338                 count++;
1339
1340         if (bifc->ifbic_len == 0) {
1341                 bifc->ifbic_len = sizeof(*breq) * count;
1342                 return 0;
1343         } else if (count == 0 || bifc->ifbic_len < sizeof(*breq)) {
1344                 bifc->ifbic_len = 0;
1345                 return 0;
1346         }
1347
1348         len = min(bifc->ifbic_len, sizeof(*breq) * count);
1349         KKASSERT(len >= sizeof(*breq));
1350
1351         breq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1352         if (breq == NULL) {
1353                 bifc->ifbic_len = 0;
1354                 return ENOMEM;
1355         }
1356         bc_arg->bca_kptr = breq;
1357
1358         count = 0;
1359         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1360                 if (len < sizeof(*breq))
1361                         break;
1362
1363                 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1364                         sizeof(breq->ifbr_ifsname));
1365                 breq->ifbr_ifsflags = bif->bif_flags;
1366                 breq->ifbr_state = bif->bif_state;
1367                 breq->ifbr_priority = bif->bif_priority;
1368                 breq->ifbr_path_cost = bif->bif_path_cost;
1369                 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1370                 breq->ifbr_designated_root = bif->bif_designated_root;
1371                 breq->ifbr_designated_bridge = bif->bif_designated_bridge;
1372                 breq->ifbr_designated_cost = bif->bif_designated_cost;
1373                 breq->ifbr_designated_port = bif->bif_designated_port;
1374                 breq++;
1375                 count++;
1376                 len -= sizeof(*breq);
1377         }
1378         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1379                 if (len < sizeof(*breq))
1380                         break;
1381
1382                 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1383                         sizeof(breq->ifbr_ifsname));
1384                 breq->ifbr_ifsflags = bif->bif_flags;
1385                 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1386                 breq++;
1387                 count++;
1388                 len -= sizeof(*breq);
1389         }
1390
1391         bifc->ifbic_len = sizeof(*breq) * count;
1392         KKASSERT(bifc->ifbic_len > 0);
1393
1394         bc_arg->bca_len = bifc->ifbic_len;
1395         bc_arg->bca_uptr = bifc->ifbic_req;
1396         return 0;
1397 }
1398
1399 static int
1400 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1401 {
1402         struct bridge_control_arg *bc_arg = arg;
1403         struct ifbaconf *bac = arg;
1404         struct bridge_rtnode *brt;
1405         struct ifbareq *bareq;
1406         int count, len;
1407
1408         count = 0;
1409         LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list)
1410                 count++;
1411
1412         if (bac->ifbac_len == 0) {
1413                 bac->ifbac_len = sizeof(*bareq) * count;
1414                 return 0;
1415         } else if (count == 0 || bac->ifbac_len < sizeof(*bareq)) {
1416                 bac->ifbac_len = 0;
1417                 return 0;
1418         }
1419
1420         len = min(bac->ifbac_len, sizeof(*bareq) * count);
1421         KKASSERT(len >= sizeof(*bareq));
1422
1423         bareq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1424         if (bareq == NULL) {
1425                 bac->ifbac_len = 0;
1426                 return ENOMEM;
1427         }
1428         bc_arg->bca_kptr = bareq;
1429
1430         count = 0;
1431         LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
1432                 struct bridge_rtinfo *bri = brt->brt_info;
1433                 unsigned long expire;
1434
1435                 if (len < sizeof(*bareq))
1436                         break;
1437
1438                 strlcpy(bareq->ifba_ifsname, bri->bri_ifp->if_xname,
1439                         sizeof(bareq->ifba_ifsname));
1440                 memcpy(bareq->ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1441                 expire = bri->bri_expire;
1442                 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1443                     time_second < expire)
1444                         bareq->ifba_expire = expire - time_second;
1445                 else
1446                         bareq->ifba_expire = 0;
1447                 bareq->ifba_flags = bri->bri_flags;
1448                 bareq++;
1449                 count++;
1450                 len -= sizeof(*bareq);
1451         }
1452
1453         bac->ifbac_len = sizeof(*bareq) * count;
1454         KKASSERT(bac->ifbac_len > 0);
1455
1456         bc_arg->bca_len = bac->ifbac_len;
1457         bc_arg->bca_uptr = bac->ifbac_req;
1458         return 0;
1459 }
1460
1461 static int
1462 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1463 {
1464         struct ifbareq *req = arg;
1465         struct bridge_iflist *bif;
1466         struct ifnet *ifp = sc->sc_ifp;
1467         int error;
1468
1469         ASSERT_IFNET_SERIALIZED_ALL(ifp);
1470
1471         bif = bridge_lookup_member(sc, req->ifba_ifsname);
1472         if (bif == NULL)
1473                 return (ENOENT);
1474
1475         ifnet_deserialize_all(ifp);
1476         error = bridge_rtsaddr(sc, req->ifba_dst, bif->bif_ifp,
1477                                req->ifba_flags);
1478         ifnet_serialize_all(ifp);
1479         return (error);
1480 }
1481
1482 static int
1483 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1484 {
1485         struct ifbrparam *param = arg;
1486
1487         sc->sc_brttimeout = param->ifbrp_ctime;
1488
1489         return (0);
1490 }
1491
1492 static int
1493 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1494 {
1495         struct ifbrparam *param = arg;
1496
1497         param->ifbrp_ctime = sc->sc_brttimeout;
1498
1499         return (0);
1500 }
1501
1502 static int
1503 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1504 {
1505         struct ifbareq *req = arg;
1506         struct ifnet *ifp = sc->sc_ifp;
1507         int error;
1508
1509         ifnet_deserialize_all(ifp);
1510         error = bridge_rtdaddr(sc, req->ifba_dst);
1511         ifnet_serialize_all(ifp);
1512         return error;
1513 }
1514
1515 static int
1516 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1517 {
1518         struct ifbreq *req = arg;
1519         struct ifnet *ifp = sc->sc_ifp;
1520
1521         ifnet_deserialize_all(ifp);
1522         bridge_rtflush(sc, req->ifbr_ifsflags | IFBF_FLUSHSYNC);
1523         ifnet_serialize_all(ifp);
1524
1525         return (0);
1526 }
1527
1528 static int
1529 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1530 {
1531         struct ifbrparam *param = arg;
1532
1533         param->ifbrp_prio = sc->sc_bridge_priority;
1534
1535         return (0);
1536 }
1537
1538 static int
1539 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1540 {
1541         struct ifbrparam *param = arg;
1542
1543         sc->sc_bridge_priority = param->ifbrp_prio;
1544
1545         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1546                 bstp_initialization(sc);
1547
1548         return (0);
1549 }
1550
1551 static int
1552 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1553 {
1554         struct ifbrparam *param = arg;
1555
1556         param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1557
1558         return (0);
1559 }
1560
1561 static int
1562 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1563 {
1564         struct ifbrparam *param = arg;
1565
1566         if (param->ifbrp_hellotime == 0)
1567                 return (EINVAL);
1568         sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1569
1570         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1571                 bstp_initialization(sc);
1572
1573         return (0);
1574 }
1575
1576 static int
1577 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1578 {
1579         struct ifbrparam *param = arg;
1580
1581         param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1582
1583         return (0);
1584 }
1585
1586 static int
1587 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1588 {
1589         struct ifbrparam *param = arg;
1590
1591         if (param->ifbrp_fwddelay == 0)
1592                 return (EINVAL);
1593         sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1594
1595         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1596                 bstp_initialization(sc);
1597
1598         return (0);
1599 }
1600
1601 static int
1602 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1603 {
1604         struct ifbrparam *param = arg;
1605
1606         param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1607
1608         return (0);
1609 }
1610
1611 static int
1612 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1613 {
1614         struct ifbrparam *param = arg;
1615
1616         if (param->ifbrp_maxage == 0)
1617                 return (EINVAL);
1618         sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1619
1620         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1621                 bstp_initialization(sc);
1622
1623         return (0);
1624 }
1625
1626 static int
1627 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1628 {
1629         struct ifbreq *req = arg;
1630         struct bridge_iflist *bif;
1631
1632         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1633         if (bif == NULL)
1634                 return (ENOENT);
1635
1636         bif->bif_priority = req->ifbr_priority;
1637
1638         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1639                 bstp_initialization(sc);
1640
1641         return (0);
1642 }
1643
1644 static int
1645 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1646 {
1647         struct ifbreq *req = arg;
1648         struct bridge_iflist *bif;
1649
1650         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1651         if (bif == NULL)
1652                 return (ENOENT);
1653
1654         bif->bif_path_cost = req->ifbr_path_cost;
1655
1656         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1657                 bstp_initialization(sc);
1658
1659         return (0);
1660 }
1661
1662 static int
1663 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1664 {
1665         struct ifbreq *req = arg;
1666         struct bridge_iflist *bif;
1667         struct ifnet *ifs;
1668
1669         ifs = ifunit(req->ifbr_ifsname);
1670         if (ifs == NULL)
1671                 return (ENOENT);
1672
1673         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1674                 if (ifs == bif->bif_ifp)
1675                         return (EBUSY);
1676
1677         if (ifs->if_bridge != NULL)
1678                 return (EBUSY);
1679
1680         switch (ifs->if_type) {
1681         case IFT_ETHER:
1682         case IFT_GIF:
1683         case IFT_L2VLAN:
1684                 break;
1685
1686         default:
1687                 return (EINVAL);
1688         }
1689
1690         bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
1691         bif->bif_ifp = ifs;
1692         bif->bif_flags = IFBIF_SPAN;
1693         /* NOTE: span bif does not need bridge_ifinfo */
1694
1695         LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1696
1697         sc->sc_span = 1;
1698
1699         return (0);
1700 }
1701
1702 static int
1703 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1704 {
1705         struct ifbreq *req = arg;
1706         struct bridge_iflist *bif;
1707         struct ifnet *ifs;
1708
1709         ifs = ifunit(req->ifbr_ifsname);
1710         if (ifs == NULL)
1711                 return (ENOENT);
1712
1713         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1714                 if (ifs == bif->bif_ifp)
1715                         break;
1716
1717         if (bif == NULL)
1718                 return (ENOENT);
1719
1720         bridge_delete_span(sc, bif);
1721
1722         if (LIST_EMPTY(&sc->sc_spanlist))
1723                 sc->sc_span = 0;
1724
1725         return (0);
1726 }
1727
1728 static void
1729 bridge_ifdetach_dispatch(netmsg_t msg)
1730 {
1731         struct ifnet *ifp, *bifp;
1732         struct bridge_softc *sc;
1733         struct bridge_iflist *bif;
1734
1735         ifp = msg->lmsg.u.ms_resultp;
1736         sc = ifp->if_bridge;
1737
1738         /* Check if the interface is a bridge member */
1739         if (sc != NULL) {
1740                 bifp = sc->sc_ifp;
1741
1742                 ifnet_serialize_all(bifp);
1743
1744                 bif = bridge_lookup_member_if(sc, ifp);
1745                 if (bif != NULL) {
1746                         bridge_delete_member(sc, bif, 1);
1747                 } else {
1748                         /* XXX Why bif will be NULL? */
1749                 }
1750
1751                 ifnet_deserialize_all(bifp);
1752                 goto reply;
1753         }
1754
1755         crit_enter();   /* XXX MP */
1756
1757         /* Check if the interface is a span port */
1758         LIST_FOREACH(sc, &bridge_list, sc_list) {
1759                 bifp = sc->sc_ifp;
1760
1761                 ifnet_serialize_all(bifp);
1762
1763                 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1764                         if (ifp == bif->bif_ifp) {
1765                                 bridge_delete_span(sc, bif);
1766                                 break;
1767                         }
1768
1769                 ifnet_deserialize_all(bifp);
1770         }
1771
1772         crit_exit();
1773
1774 reply:
1775         lwkt_replymsg(&msg->lmsg, 0);
1776 }
1777
1778 /*
1779  * bridge_ifdetach:
1780  *
1781  *      Detach an interface from a bridge.  Called when a member
1782  *      interface is detaching.
1783  */
1784 static void
1785 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1786 {
1787         struct netmsg_base msg;
1788
1789         netmsg_init(&msg, NULL, &curthread->td_msgport,
1790                     0, bridge_ifdetach_dispatch);
1791         msg.lmsg.u.ms_resultp = ifp;
1792
1793         lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0);
1794 }
1795
1796 /*
1797  * bridge_init:
1798  *
1799  *      Initialize a bridge interface.
1800  */
1801 static void
1802 bridge_init(void *xsc)
1803 {
1804         bridge_control(xsc, SIOCSIFFLAGS, bridge_ioctl_init, NULL);
1805 }
1806
1807 /*
1808  * bridge_stop:
1809  *
1810  *      Stop the bridge interface.
1811  */
1812 static void
1813 bridge_stop(struct ifnet *ifp)
1814 {
1815         bridge_control(ifp->if_softc, SIOCSIFFLAGS, bridge_ioctl_stop, NULL);
1816 }
1817
1818 /*
1819  * bridge_enqueue:
1820  *
1821  *      Enqueue a packet on a bridge member interface.
1822  *
1823  */
1824 void
1825 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1826 {
1827         struct netmsg_packet *nmp;
1828
1829         nmp = &m->m_hdr.mh_netmsg;
1830         netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
1831                     0, bridge_enqueue_handler);
1832         nmp->nm_packet = m;
1833         nmp->base.lmsg.u.ms_resultp = dst_ifp;
1834
1835         lwkt_sendmsg(ifnet_portfn(mycpu->gd_cpuid), &nmp->base.lmsg);
1836 }
1837
1838 /*
1839  * bridge_output:
1840  *
1841  *      Send output from a bridge member interface.  This
1842  *      performs the bridging function for locally originated
1843  *      packets.
1844  *
1845  *      The mbuf has the Ethernet header already attached.  We must
1846  *      enqueue or free the mbuf before returning.
1847  */
1848 static int
1849 bridge_output(struct ifnet *ifp, struct mbuf *m)
1850 {
1851         struct bridge_softc *sc = ifp->if_bridge;
1852         struct bridge_iflist *bif, *nbif;
1853         struct ether_header *eh;
1854         struct ifnet *dst_if, *bifp;
1855         int from_us;
1856
1857         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
1858
1859         /*
1860          * Make sure that we are still a member of a bridge interface.
1861          */
1862         if (sc == NULL) {
1863                 m_freem(m);
1864                 return (0);
1865         }
1866         bifp = sc->sc_ifp;
1867
1868         if (m->m_len < ETHER_HDR_LEN) {
1869                 m = m_pullup(m, ETHER_HDR_LEN);
1870                 if (m == NULL)
1871                         return (0);
1872         }
1873         eh = mtod(m, struct ether_header *);
1874
1875         if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0)
1876                 from_us = 1;
1877         else
1878                 from_us = 0;
1879
1880         /*
1881          * If bridge is down, but the original output interface is up,
1882          * go ahead and send out that interface.  Otherwise, the packet
1883          * is dropped below.
1884          */
1885         if ((bifp->if_flags & IFF_RUNNING) == 0) {
1886                 dst_if = ifp;
1887                 goto sendunicast;
1888         }
1889
1890         /*
1891          * If the packet is a multicast, or we don't know a better way to
1892          * get there, send to all interfaces.
1893          */
1894         if (ETHER_IS_MULTICAST(eh->ether_dhost))
1895                 dst_if = NULL;
1896         else
1897                 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1898         if (dst_if == NULL) {
1899                 struct mbuf *mc;
1900                 int used = 0;
1901
1902                 if (sc->sc_span)
1903                         bridge_span(sc, m);
1904
1905                 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
1906                                      bif_next, nbif) {
1907                         dst_if = bif->bif_ifp;
1908                         if ((dst_if->if_flags & IFF_RUNNING) == 0)
1909                                 continue;
1910
1911                         /*
1912                          * If this is not the original output interface,
1913                          * and the interface is participating in spanning
1914                          * tree, make sure the port is in a state that
1915                          * allows forwarding.
1916                          */
1917                         if (dst_if != ifp &&
1918                             (bif->bif_flags & IFBIF_STP) != 0) {
1919                                 switch (bif->bif_state) {
1920                                 case BSTP_IFSTATE_L1BLOCKING:
1921                                 case BSTP_IFSTATE_BLOCKING:
1922                                 case BSTP_IFSTATE_LISTENING:
1923                                 case BSTP_IFSTATE_DISABLED:
1924                                         continue;
1925                                 }
1926                         }
1927
1928                         if (LIST_NEXT(bif, bif_next) == NULL) {
1929                                 used = 1;
1930                                 mc = m;
1931                         } else {
1932                                 mc = m_copypacket(m, MB_DONTWAIT);
1933                                 if (mc == NULL) {
1934                                         bifp->if_oerrors++;
1935                                         continue;
1936                                 }
1937                         }
1938
1939                         /*
1940                          * If the packet is 'from' us override ether_shost.
1941                          */
1942                         bridge_handoff(dst_if, mc, from_us);
1943
1944                         if (nbif != NULL && !nbif->bif_onlist) {
1945                                 KKASSERT(bif->bif_onlist);
1946                                 nbif = LIST_NEXT(bif, bif_next);
1947                         }
1948                 }
1949                 if (used == 0)
1950                         m_freem(m);
1951                 return (0);
1952         }
1953
1954 sendunicast:
1955         /*
1956          * If STP is enabled on the target and it is not in a good state
1957          * scan all bridged interfaces for any with a matching MAC which is
1958          * in a good state and use that one.
1959          *
1960          * We need to do this because arp entries tag onto a particular
1961          * interface and if it happens to be dead then the packets will
1962          * go into a bit bucket.
1963          */
1964         bif = bridge_lookup_member_if(sc, dst_if);
1965         if (bif->bif_flags & IFBIF_STP) {
1966                 switch (bif->bif_state) {
1967                 case BSTP_IFSTATE_L1BLOCKING:
1968                 case BSTP_IFSTATE_BLOCKING:
1969                 case BSTP_IFSTATE_LISTENING:
1970                 case BSTP_IFSTATE_DISABLED:
1971                         LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
1972                                              bif_next, nbif) {
1973                                 if (memcmp(IF_LLADDR(bif->bif_ifp),
1974                                            IF_LLADDR(dst_if),
1975                                            ETHER_ADDR_LEN) != 0) {
1976                                         continue;
1977                                 }
1978                                 if (bif->bif_state == BSTP_IFSTATE_L1BLOCKING||
1979                                     bif->bif_state == BSTP_IFSTATE_BLOCKING ||
1980                                     bif->bif_state == BSTP_IFSTATE_LISTENING||
1981                                     bif->bif_state == BSTP_IFSTATE_DISABLED) {
1982                                         continue;
1983                                 }
1984                                 dst_if = bif->bif_ifp;
1985                                 break;
1986                         }
1987                         break;
1988                 default:
1989                         /* keep dst_if */
1990                         break;
1991                 }
1992         }
1993
1994         if (sc->sc_span)
1995                 bridge_span(sc, m);
1996         if ((dst_if->if_flags & IFF_RUNNING) == 0)
1997                 m_freem(m);
1998         else
1999                 bridge_handoff(dst_if, m, from_us);
2000         return (0);
2001 }
2002
2003 /*
2004  * bridge_start:
2005  *
2006  *      Start output on a bridge.
2007  */
2008 static void
2009 bridge_start(struct ifnet *ifp)
2010 {
2011         struct bridge_softc *sc = ifp->if_softc;
2012
2013         ASSERT_IFNET_SERIALIZED_TX(ifp);
2014
2015         ifp->if_flags |= IFF_OACTIVE;
2016         for (;;) {
2017                 struct ifnet *dst_if = NULL;
2018                 struct ether_header *eh;
2019                 struct mbuf *m;
2020
2021                 m = ifq_dequeue(&ifp->if_snd, NULL);
2022                 if (m == NULL)
2023                         break;
2024
2025                 if (m->m_len < sizeof(*eh)) {
2026                         m = m_pullup(m, sizeof(*eh));
2027                         if (m == NULL) {
2028                                 ifp->if_oerrors++;
2029                                 continue;
2030                         }
2031                 }
2032                 eh = mtod(m, struct ether_header *);
2033
2034                 BPF_MTAP(ifp, m);
2035                 ifp->if_opackets++;
2036
2037                 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
2038                         dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2039
2040                 if (dst_if == NULL)
2041                         bridge_start_bcast(sc, m);
2042                 else
2043                         bridge_enqueue(dst_if, m);
2044         }
2045         ifp->if_flags &= ~IFF_OACTIVE;
2046 }
2047
2048 /*
2049  * bridge_forward:
2050  *
2051  *      Forward packets received on a bridge interface via the input
2052  *      path.
2053  *
2054  *      The forwarding function of the bridge.
2055  */
2056 static void
2057 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
2058 {
2059         struct bridge_iflist *bif;
2060         struct ifnet *src_if, *dst_if, *ifp;
2061         struct ether_header *eh;
2062
2063         src_if = m->m_pkthdr.rcvif;
2064         ifp = sc->sc_ifp;
2065
2066         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2067
2068         ifp->if_ipackets++;
2069         ifp->if_ibytes += m->m_pkthdr.len;
2070
2071         /*
2072          * Look up the bridge_iflist.
2073          */
2074         bif = bridge_lookup_member_if(sc, src_if);
2075         if (bif == NULL) {
2076                 /* Interface is not a bridge member (anymore?) */
2077                 m_freem(m);
2078                 return;
2079         }
2080
2081         if (bif->bif_flags & IFBIF_STP) {
2082                 switch (bif->bif_state) {
2083                 case BSTP_IFSTATE_L1BLOCKING:
2084                 case BSTP_IFSTATE_BLOCKING:
2085                 case BSTP_IFSTATE_LISTENING:
2086                 case BSTP_IFSTATE_DISABLED:
2087                         m_freem(m);
2088                         return;
2089                 }
2090         }
2091
2092         eh = mtod(m, struct ether_header *);
2093
2094         /*
2095          * If the interface is learning, and the source
2096          * address is valid and not multicast, record
2097          * the address.
2098          */
2099         if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
2100             ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
2101             (eh->ether_shost[0] == 0 &&
2102              eh->ether_shost[1] == 0 &&
2103              eh->ether_shost[2] == 0 &&
2104              eh->ether_shost[3] == 0 &&
2105              eh->ether_shost[4] == 0 &&
2106              eh->ether_shost[5] == 0) == 0)
2107                 bridge_rtupdate(sc, eh->ether_shost, src_if, IFBAF_DYNAMIC);
2108
2109         if ((bif->bif_flags & IFBIF_STP) != 0 &&
2110             bif->bif_state == BSTP_IFSTATE_LEARNING) {
2111                 m_freem(m);
2112                 return;
2113         }
2114
2115         /*
2116          * At this point, the port either doesn't participate
2117          * in spanning tree or it is in the forwarding state.
2118          */
2119
2120         /*
2121          * If the packet is unicast, destined for someone on
2122          * "this" side of the bridge, drop it.
2123          */
2124         if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2125                 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2126                 if (src_if == dst_if) {
2127                         m_freem(m);
2128                         return;
2129                 }
2130         } else {
2131                 /* ...forward it to all interfaces. */
2132                 ifp->if_imcasts++;
2133                 dst_if = NULL;
2134         }
2135
2136         if (dst_if == NULL) {
2137                 bridge_broadcast(sc, src_if, m);
2138                 return;
2139         }
2140
2141         /*
2142          * At this point, we're dealing with a unicast frame
2143          * going to a different interface.
2144          */
2145         if ((dst_if->if_flags & IFF_RUNNING) == 0) {
2146                 m_freem(m);
2147                 return;
2148         }
2149         bif = bridge_lookup_member_if(sc, dst_if);
2150         if (bif == NULL) {
2151                 /* Not a member of the bridge (anymore?) */
2152                 m_freem(m);
2153                 return;
2154         }
2155
2156         if (bif->bif_flags & IFBIF_STP) {
2157                 switch (bif->bif_state) {
2158                 case BSTP_IFSTATE_DISABLED:
2159                 case BSTP_IFSTATE_BLOCKING:
2160                 case BSTP_IFSTATE_L1BLOCKING:
2161                         m_freem(m);
2162                         return;
2163                 }
2164         }
2165
2166         if (inet_pfil_hook.ph_hashooks > 0
2167 #ifdef INET6
2168             || inet6_pfil_hook.ph_hashooks > 0
2169 #endif
2170             ) {
2171                 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2172                         return;
2173                 if (m == NULL)
2174                         return;
2175
2176                 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2177                         return;
2178                 if (m == NULL)
2179                         return;
2180         }
2181         bridge_handoff(dst_if, m, 0);
2182 }
2183
2184 /*
2185  * bridge_input:
2186  *
2187  *      Receive input from a member interface.  Queue the packet for
2188  *      bridging if it is not for us.
2189  */
2190 static struct mbuf *
2191 bridge_input(struct ifnet *ifp, struct mbuf *m)
2192 {
2193         struct bridge_softc *sc = ifp->if_bridge;
2194         struct bridge_iflist *bif;
2195         struct ifnet *bifp, *new_ifp;
2196         struct ether_header *eh;
2197         struct mbuf *mc, *mc2;
2198
2199         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2200
2201         /*
2202          * Make sure that we are still a member of a bridge interface.
2203          */
2204         if (sc == NULL)
2205                 return m;
2206
2207         new_ifp = NULL;
2208         bifp = sc->sc_ifp;
2209
2210         if ((bifp->if_flags & IFF_RUNNING) == 0)
2211                 goto out;
2212
2213         /*
2214          * Implement support for bridge monitoring.  If this flag has been
2215          * set on this interface, discard the packet once we push it through
2216          * the bpf(4) machinery, but before we do, increment various counters
2217          * associated with this bridge.
2218          */
2219         if (bifp->if_flags & IFF_MONITOR) {
2220                 /* Change input interface to this bridge */
2221                 m->m_pkthdr.rcvif = bifp;
2222
2223                 BPF_MTAP(bifp, m);
2224
2225                 /* Update bridge's ifnet statistics */
2226                 bifp->if_ipackets++;
2227                 bifp->if_ibytes += m->m_pkthdr.len;
2228                 if (m->m_flags & (M_MCAST | M_BCAST))
2229                         bifp->if_imcasts++;
2230
2231                 m_freem(m);
2232                 m = NULL;
2233                 goto out;
2234         }
2235
2236         /*
2237          * Handle the ether_header
2238          *
2239          * In all cases if the packet is destined for us via our MAC
2240          * we must clear BRIDGE_MBUF_TAGGED to ensure that we don't
2241          * repeat the source MAC out the same interface.
2242          */
2243         eh = mtod(m, struct ether_header *);
2244         m->m_pkthdr.fw_flags |= BRIDGE_MBUF_TAGGED;
2245         bcopy(eh, &m->m_pkthdr.br.ether, sizeof(*eh));
2246
2247         if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
2248                 /*
2249                  * If the packet is for us, set the packets source as the
2250                  * bridge, and return the packet back to ifnet.if_input for
2251                  * local processing.
2252                  */
2253                 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2254                 KASSERT(bifp->if_bridge == NULL,
2255                         ("loop created in bridge_input"));
2256                 if (pfil_member != 0) {
2257                         if (inet_pfil_hook.ph_hashooks > 0
2258 #ifdef INET6
2259                             || inet6_pfil_hook.ph_hashooks > 0
2260 #endif
2261                         ) {
2262                                 if (bridge_pfil(&m, NULL, ifp, PFIL_IN) != 0)
2263                                         goto out;
2264                                 if (m == NULL)
2265                                         goto out;
2266                         }
2267                 }
2268                 new_ifp = bifp;
2269                 goto out;
2270         }
2271
2272         /*
2273          * Tap all packets arriving on the bridge, no matter if
2274          * they are local destinations or not.  In is in.
2275          */
2276         BPF_MTAP(bifp, m);
2277
2278         bif = bridge_lookup_member_if(sc, ifp);
2279         if (bif == NULL)
2280                 goto out;
2281
2282         if (sc->sc_span)
2283                 bridge_span(sc, m);
2284
2285         if (m->m_flags & (M_BCAST | M_MCAST)) {
2286                 /* Tap off 802.1D packets; they do not get forwarded. */
2287                 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2288                             ETHER_ADDR_LEN) == 0) {
2289                         ifnet_serialize_all(bifp);
2290                         bstp_input(sc, bif, m);
2291                         ifnet_deserialize_all(bifp);
2292
2293                         /* m is freed by bstp_input */
2294                         m = NULL;
2295                         goto out;
2296                 }
2297
2298                 if (bif->bif_flags & IFBIF_STP) {
2299                         switch (bif->bif_state) {
2300                         case BSTP_IFSTATE_L1BLOCKING:
2301                         case BSTP_IFSTATE_BLOCKING:
2302                         case BSTP_IFSTATE_LISTENING:
2303                         case BSTP_IFSTATE_DISABLED:
2304                                 goto out;
2305                         }
2306                 }
2307
2308                 /*
2309                  * Make a deep copy of the packet and enqueue the copy
2310                  * for bridge processing; return the original packet for
2311                  * local processing.
2312                  */
2313                 mc = m_dup(m, MB_DONTWAIT);
2314                 if (mc == NULL)
2315                         goto out;
2316
2317                 bridge_forward(sc, mc);
2318
2319                 /*
2320                  * Reinject the mbuf as arriving on the bridge so we have a
2321                  * chance at claiming multicast packets. We can not loop back
2322                  * here from ether_input as a bridge is never a member of a
2323                  * bridge.
2324                  */
2325                 KASSERT(bifp->if_bridge == NULL,
2326                         ("loop created in bridge_input"));
2327                 mc2 = m_dup(m, MB_DONTWAIT);
2328 #ifdef notyet
2329                 if (mc2 != NULL) {
2330                         /* Keep the layer3 header aligned */
2331                         int i = min(mc2->m_pkthdr.len, max_protohdr);
2332                         mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2333                 }
2334 #endif
2335                 if (mc2 != NULL) {
2336                         /*
2337                          * Don't tap to bpf(4) again; we have already done
2338                          * the tapping.
2339                          *
2340                          * Leave m_pkthdr.rcvif alone, so ARP replies are
2341                          * processed as coming in on the correct interface.
2342                          *
2343                          * Clear the bridge flag for local processing in
2344                          * case the packet gets routed.
2345                          */
2346                         mc2->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2347                         ether_reinput_oncpu(bifp, mc2, REINPUT_KEEPRCVIF);
2348                 }
2349
2350                 /* Return the original packet for local processing. */
2351                 goto out;
2352         }
2353
2354         if (bif->bif_flags & IFBIF_STP) {
2355                 switch (bif->bif_state) {
2356                 case BSTP_IFSTATE_L1BLOCKING:
2357                 case BSTP_IFSTATE_BLOCKING:
2358                 case BSTP_IFSTATE_LISTENING:
2359                 case BSTP_IFSTATE_DISABLED:
2360                         goto out;
2361                 }
2362         }
2363
2364         /*
2365          * Unicast.  Make sure it's not for us.
2366          *
2367          * This loop is MPSAFE; the only blocking operation (bridge_rtupdate)
2368          * is followed by breaking out of the loop.
2369          */
2370         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2371                 if (bif->bif_ifp->if_type != IFT_ETHER)
2372                         continue;
2373
2374                 /*
2375                  * It is destined for us.  Reinput on the same interface
2376                  * it came in on so things like ARP responses get assigned
2377                  * to the correct member (the incoming interface) and not
2378                  * to the member which happens to have the matching dhost.
2379                  */
2380                 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
2381                     ETHER_ADDR_LEN) == 0) {
2382                         if (bif->bif_ifp != ifp) {
2383                                 /* XXX loop prevention */
2384                                 m->m_flags |= M_ETHER_BRIDGED;
2385                                 new_ifp = bif->bif_ifp;
2386                         }
2387                         if (bif->bif_flags & IFBIF_LEARNING) {
2388                                 bridge_rtupdate(sc, eh->ether_shost,
2389                                                 ifp, IFBAF_DYNAMIC);
2390                         }
2391                         m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2392                         goto out;
2393                 }
2394
2395                 /* We just received a packet that we sent out. */
2396                 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
2397                     ETHER_ADDR_LEN) == 0) {
2398                         m_freem(m);
2399                         m = NULL;
2400                         goto out;
2401                 }
2402         }
2403
2404         /* Perform the bridge forwarding function. */
2405         bridge_forward(sc, m);
2406         m = NULL;
2407
2408         /*
2409          * Leave m_pkthdr.rcvif alone, so ARP replies are
2410          * processed as coming in on the correct interface.
2411          */
2412 out:
2413         if (new_ifp != NULL) {
2414                 /*
2415                  * Clear the bridge flag for local processing in
2416                  * case the packet gets routed.
2417                  */
2418                 ether_reinput_oncpu(new_ifp, m,
2419                                     REINPUT_KEEPRCVIF|REINPUT_RUNBPF);
2420                 m = NULL;
2421         }
2422         return (m);
2423 }
2424
2425 /*
2426  * bridge_start_bcast:
2427  *
2428  *      Broadcast the packet sent from bridge to all member
2429  *      interfaces.
2430  *      This is a simplified version of bridge_broadcast(), however,
2431  *      this function expects caller to hold bridge's serializer.
2432  */
2433 static void
2434 bridge_start_bcast(struct bridge_softc *sc, struct mbuf *m)
2435 {
2436         struct bridge_iflist *bif;
2437         struct mbuf *mc;
2438         struct ifnet *dst_if, *bifp;
2439         int used = 0;
2440
2441         bifp = sc->sc_ifp;
2442         ASSERT_IFNET_SERIALIZED_ALL(bifp);
2443
2444         /*
2445          * Following loop is MPSAFE; nothing is blocking
2446          * in the loop body.
2447          */
2448         LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2449                 dst_if = bif->bif_ifp;
2450
2451                 if (bif->bif_flags & IFBIF_STP) {
2452                         switch (bif->bif_state) {
2453                         case BSTP_IFSTATE_L1BLOCKING:
2454                         case BSTP_IFSTATE_BLOCKING:
2455                         case BSTP_IFSTATE_DISABLED:
2456                                 continue;
2457                         }
2458                 }
2459
2460                 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2461                     (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2462                         continue;
2463
2464                 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2465                         continue;
2466
2467                 if (LIST_NEXT(bif, bif_next) == NULL) {
2468                         mc = m;
2469                         used = 1;
2470                 } else {
2471                         mc = m_copypacket(m, MB_DONTWAIT);
2472                         if (mc == NULL) {
2473                                 bifp->if_oerrors++;
2474                                 continue;
2475                         }
2476                 }
2477                 bridge_enqueue(dst_if, mc);
2478         }
2479         if (used == 0)
2480                 m_freem(m);
2481 }
2482
2483 /*
2484  * bridge_broadcast:
2485  *
2486  *      Send a frame to all interfaces that are members of
2487  *      the bridge, except for the one on which the packet
2488  *      arrived.
2489  */
2490 static void
2491 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2492                  struct mbuf *m)
2493 {
2494         struct bridge_iflist *bif, *nbif;
2495         struct ether_header *eh;
2496         struct mbuf *mc;
2497         struct ifnet *dst_if, *bifp;
2498         int used = 0;
2499         int from_us;
2500
2501         bifp = sc->sc_ifp;
2502         ASSERT_IFNET_NOT_SERIALIZED_ALL(bifp);
2503
2504         eh = mtod(m, struct ether_header *);
2505         if (memcmp(eh->ether_dhost, IF_LLADDR(src_if), ETHER_ADDR_LEN) == 0)
2506                 from_us = 1;
2507         else
2508                 from_us = 0;
2509
2510         if (inet_pfil_hook.ph_hashooks > 0
2511 #ifdef INET6
2512             || inet6_pfil_hook.ph_hashooks > 0
2513 #endif
2514             ) {
2515                 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
2516                         return;
2517                 if (m == NULL)
2518                         return;
2519
2520                 /* Filter on the bridge interface before broadcasting */
2521                 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
2522                         return;
2523                 if (m == NULL)
2524                         return;
2525         }
2526
2527         LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
2528                 dst_if = bif->bif_ifp;
2529                 if (dst_if == src_if)
2530                         continue;
2531
2532                 if (bif->bif_flags & IFBIF_STP) {
2533                         switch (bif->bif_state) {
2534                         case BSTP_IFSTATE_L1BLOCKING:
2535                         case BSTP_IFSTATE_BLOCKING:
2536                         case BSTP_IFSTATE_DISABLED:
2537                                 continue;
2538                         }
2539                 }
2540
2541                 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2542                     (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2543                         continue;
2544
2545                 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2546                         continue;
2547
2548                 if (LIST_NEXT(bif, bif_next) == NULL) {
2549                         mc = m;
2550                         used = 1;
2551                 } else {
2552                         mc = m_copypacket(m, MB_DONTWAIT);
2553                         if (mc == NULL) {
2554                                 sc->sc_ifp->if_oerrors++;
2555                                 continue;
2556                         }
2557                 }
2558
2559                 /*
2560                  * Filter on the output interface.  Pass a NULL bridge
2561                  * interface pointer so we do not redundantly filter on
2562                  * the bridge for each interface we broadcast on.
2563                  */
2564                 if (inet_pfil_hook.ph_hashooks > 0
2565 #ifdef INET6
2566                     || inet6_pfil_hook.ph_hashooks > 0
2567 #endif
2568                     ) {
2569                         if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2570                                 continue;
2571                         if (mc == NULL)
2572                                 continue;
2573                 }
2574                 bridge_handoff(dst_if, mc, from_us);
2575
2576                 if (nbif != NULL && !nbif->bif_onlist) {
2577                         KKASSERT(bif->bif_onlist);
2578                         nbif = LIST_NEXT(bif, bif_next);
2579                 }
2580         }
2581         if (used == 0)
2582                 m_freem(m);
2583 }
2584
2585 /*
2586  * bridge_span:
2587  *
2588  *      Duplicate a packet out one or more interfaces that are in span mode,
2589  *      the original mbuf is unmodified.
2590  */
2591 static void
2592 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2593 {
2594         struct bridge_iflist *bif;
2595         struct ifnet *dst_if, *bifp;
2596         struct mbuf *mc;
2597
2598         bifp = sc->sc_ifp;
2599         ifnet_serialize_all(bifp);
2600
2601         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2602                 dst_if = bif->bif_ifp;
2603
2604                 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2605                         continue;
2606
2607                 mc = m_copypacket(m, MB_DONTWAIT);
2608                 if (mc == NULL) {
2609                         sc->sc_ifp->if_oerrors++;
2610                         continue;
2611                 }
2612                 bridge_enqueue(dst_if, mc);
2613         }
2614
2615         ifnet_deserialize_all(bifp);
2616 }
2617
2618 static void
2619 bridge_rtmsg_sync_handler(netmsg_t msg)
2620 {
2621         ifnet_forwardmsg(&msg->lmsg, mycpuid + 1);
2622 }
2623
2624 static void
2625 bridge_rtmsg_sync(struct bridge_softc *sc)
2626 {
2627         struct netmsg_base msg;
2628
2629         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2630
2631         netmsg_init(&msg, NULL, &curthread->td_msgport,
2632                     0, bridge_rtmsg_sync_handler);
2633         ifnet_domsg(&msg.lmsg, 0);
2634 }
2635
2636 static __inline void
2637 bridge_rtinfo_update(struct bridge_rtinfo *bri, struct ifnet *dst_if,
2638                      int setflags, uint8_t flags, uint32_t timeo)
2639 {
2640         if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2641             bri->bri_ifp != dst_if)
2642                 bri->bri_ifp = dst_if;
2643         if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2644             bri->bri_expire != time_second + timeo)
2645                 bri->bri_expire = time_second + timeo;
2646         if (setflags)
2647                 bri->bri_flags = flags;
2648 }
2649
2650 static int
2651 bridge_rtinstall_oncpu(struct bridge_softc *sc, const uint8_t *dst,
2652                        struct ifnet *dst_if, int setflags, uint8_t flags,
2653                        struct bridge_rtinfo **bri0)
2654 {
2655         struct bridge_rtnode *brt;
2656         struct bridge_rtinfo *bri;
2657
2658         if (mycpuid == 0) {
2659                 brt = bridge_rtnode_lookup(sc, dst);
2660                 if (brt != NULL) {
2661                         /*
2662                          * rtnode for 'dst' already exists.  We inform the
2663                          * caller about this by leaving bri0 as NULL.  The
2664                          * caller will terminate the intallation upon getting
2665                          * NULL bri0.  However, we still need to update the
2666                          * rtinfo.
2667                          */
2668                         KKASSERT(*bri0 == NULL);
2669
2670                         /* Update rtinfo */
2671                         bridge_rtinfo_update(brt->brt_info, dst_if, setflags,
2672                                              flags, sc->sc_brttimeout);
2673                         return 0;
2674                 }
2675
2676                 /*
2677                  * We only need to check brtcnt on CPU0, since if limit
2678                  * is to be exceeded, ENOSPC is returned.  Caller knows
2679                  * this and will terminate the installation.
2680                  */
2681                 if (sc->sc_brtcnt >= sc->sc_brtmax)
2682                         return ENOSPC;
2683
2684                 KKASSERT(*bri0 == NULL);
2685                 bri = kmalloc(sizeof(struct bridge_rtinfo), M_DEVBUF,
2686                                   M_WAITOK | M_ZERO);
2687                 *bri0 = bri;
2688
2689                 /* Setup rtinfo */
2690                 bri->bri_flags = IFBAF_DYNAMIC;
2691                 bridge_rtinfo_update(bri, dst_if, setflags, flags,
2692                                      sc->sc_brttimeout);
2693         } else {
2694                 bri = *bri0;
2695                 KKASSERT(bri != NULL);
2696         }
2697
2698         brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2699                       M_WAITOK | M_ZERO);
2700         memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2701         brt->brt_info = bri;
2702
2703         bridge_rtnode_insert(sc, brt);
2704         return 0;
2705 }
2706
2707 static void
2708 bridge_rtinstall_handler(netmsg_t msg)
2709 {
2710         struct netmsg_brsaddr *brmsg = (struct netmsg_brsaddr *)msg;
2711         int error;
2712
2713         error = bridge_rtinstall_oncpu(brmsg->br_softc,
2714                                        brmsg->br_dst, brmsg->br_dst_if,
2715                                        brmsg->br_setflags, brmsg->br_flags,
2716                                        &brmsg->br_rtinfo);
2717         if (error) {
2718                 KKASSERT(mycpuid == 0 && brmsg->br_rtinfo == NULL);
2719                 lwkt_replymsg(&brmsg->base.lmsg, error);
2720                 return;
2721         } else if (brmsg->br_rtinfo == NULL) {
2722                 /* rtnode already exists for 'dst' */
2723                 KKASSERT(mycpuid == 0);
2724                 lwkt_replymsg(&brmsg->base.lmsg, 0);
2725                 return;
2726         }
2727         ifnet_forwardmsg(&brmsg->base.lmsg, mycpuid + 1);
2728 }
2729
2730 /*
2731  * bridge_rtupdate:
2732  *
2733  *      Add/Update a bridge routing entry.
2734  */
2735 static int
2736 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2737                 struct ifnet *dst_if, uint8_t flags)
2738 {
2739         struct bridge_rtnode *brt;
2740
2741         /*
2742          * A route for this destination might already exist.  If so,
2743          * update it, otherwise create a new one.
2744          */
2745         if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2746                 struct netmsg_brsaddr *brmsg;
2747
2748                 if (sc->sc_brtcnt >= sc->sc_brtmax)
2749                         return ENOSPC;
2750
2751                 brmsg = kmalloc(sizeof(*brmsg), M_LWKTMSG, M_WAITOK | M_NULLOK);
2752                 if (brmsg == NULL)
2753                         return ENOMEM;
2754
2755                 netmsg_init(&brmsg->base, NULL, &netisr_afree_rport,
2756                             0, bridge_rtinstall_handler);
2757                 memcpy(brmsg->br_dst, dst, ETHER_ADDR_LEN);
2758                 brmsg->br_dst_if = dst_if;
2759                 brmsg->br_flags = flags;
2760                 brmsg->br_setflags = 0;
2761                 brmsg->br_softc = sc;
2762                 brmsg->br_rtinfo = NULL;
2763
2764                 ifnet_sendmsg(&brmsg->base.lmsg, 0);
2765                 return 0;
2766         }
2767         bridge_rtinfo_update(brt->brt_info, dst_if, 0, flags,
2768                              sc->sc_brttimeout);
2769         return 0;
2770 }
2771
2772 static int
2773 bridge_rtsaddr(struct bridge_softc *sc, const uint8_t *dst,
2774                struct ifnet *dst_if, uint8_t flags)
2775 {
2776         struct netmsg_brsaddr brmsg;
2777
2778         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2779
2780         netmsg_init(&brmsg.base, NULL, &curthread->td_msgport,
2781                     0, bridge_rtinstall_handler);
2782         memcpy(brmsg.br_dst, dst, ETHER_ADDR_LEN);
2783         brmsg.br_dst_if = dst_if;
2784         brmsg.br_flags = flags;
2785         brmsg.br_setflags = 1;
2786         brmsg.br_softc = sc;
2787         brmsg.br_rtinfo = NULL;
2788
2789         return ifnet_domsg(&brmsg.base.lmsg, 0);
2790 }
2791
2792 /*
2793  * bridge_rtlookup:
2794  *
2795  *      Lookup the destination interface for an address.
2796  */
2797 static struct ifnet *
2798 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2799 {
2800         struct bridge_rtnode *brt;
2801
2802         if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2803                 return NULL;
2804         return brt->brt_info->bri_ifp;
2805 }
2806
2807 static void
2808 bridge_rtreap_handler(netmsg_t msg)
2809 {
2810         struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
2811         struct bridge_rtnode *brt, *nbrt;
2812
2813         LIST_FOREACH_MUTABLE(brt, &sc->sc_rtlists[mycpuid], brt_list, nbrt) {
2814                 if (brt->brt_info->bri_dead)
2815                         bridge_rtnode_destroy(sc, brt);
2816         }
2817         ifnet_forwardmsg(&msg->lmsg, mycpuid + 1);
2818 }
2819
2820 static void
2821 bridge_rtreap(struct bridge_softc *sc)
2822 {
2823         struct netmsg_base msg;
2824
2825         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2826
2827         netmsg_init(&msg, NULL, &curthread->td_msgport,
2828                     0, bridge_rtreap_handler);
2829         msg.lmsg.u.ms_resultp = sc;
2830
2831         ifnet_domsg(&msg.lmsg, 0);
2832 }
2833
2834 static void
2835 bridge_rtreap_async(struct bridge_softc *sc)
2836 {
2837         struct netmsg_base *msg;
2838
2839         msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK);
2840
2841         netmsg_init(msg, NULL, &netisr_afree_rport,
2842                     0, bridge_rtreap_handler);
2843         msg->lmsg.u.ms_resultp = sc;
2844
2845         ifnet_sendmsg(&msg->lmsg, 0);
2846 }
2847
2848 /*
2849  * bridge_rttrim:
2850  *
2851  *      Trim the routine table so that we have a number
2852  *      of routing entries less than or equal to the
2853  *      maximum number.
2854  */
2855 static void
2856 bridge_rttrim(struct bridge_softc *sc)
2857 {
2858         struct bridge_rtnode *brt;
2859         int dead;
2860
2861         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2862
2863         /* Make sure we actually need to do this. */
2864         if (sc->sc_brtcnt <= sc->sc_brtmax)
2865                 return;
2866
2867         /*
2868          * Find out how many rtnodes are dead
2869          */
2870         dead = bridge_rtage_finddead(sc);
2871         KKASSERT(dead <= sc->sc_brtcnt);
2872
2873         if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
2874                 /* Enough dead rtnodes are found */
2875                 bridge_rtreap(sc);
2876                 return;
2877         }
2878
2879         /*
2880          * Kill some dynamic rtnodes to meet the brtmax
2881          */
2882         LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2883                 struct bridge_rtinfo *bri = brt->brt_info;
2884
2885                 if (bri->bri_dead) {
2886                         /*
2887                          * We have counted this rtnode in
2888                          * bridge_rtage_finddead()
2889                          */
2890                         continue;
2891                 }
2892
2893                 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2894                         bri->bri_dead = 1;
2895                         ++dead;
2896                         KKASSERT(dead <= sc->sc_brtcnt);
2897
2898                         if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
2899                                 /* Enough rtnodes are collected */
2900                                 break;
2901                         }
2902                 }
2903         }
2904         if (dead)
2905                 bridge_rtreap(sc);
2906 }
2907
2908 /*
2909  * bridge_timer:
2910  *
2911  *      Aging timer for the bridge.
2912  */
2913 static void
2914 bridge_timer(void *arg)
2915 {
2916         struct bridge_softc *sc = arg;
2917         struct netmsg_base *msg;
2918
2919         KKASSERT(mycpuid == BRIDGE_CFGCPU);
2920
2921         crit_enter();
2922
2923         if (callout_pending(&sc->sc_brcallout) ||
2924             !callout_active(&sc->sc_brcallout)) {
2925                 crit_exit();
2926                 return;
2927         }
2928         callout_deactivate(&sc->sc_brcallout);
2929
2930         msg = &sc->sc_brtimemsg;
2931         KKASSERT(msg->lmsg.ms_flags & MSGF_DONE);
2932         lwkt_sendmsg(BRIDGE_CFGPORT, &msg->lmsg);
2933
2934         crit_exit();
2935 }
2936
2937 static void
2938 bridge_timer_handler(netmsg_t msg)
2939 {
2940         struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
2941
2942         KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
2943
2944         crit_enter();
2945         /* Reply ASAP */
2946         lwkt_replymsg(&msg->lmsg, 0);
2947         crit_exit();
2948
2949         bridge_rtage(sc);
2950         if (sc->sc_ifp->if_flags & IFF_RUNNING) {
2951                 callout_reset(&sc->sc_brcallout,
2952                     bridge_rtable_prune_period * hz, bridge_timer, sc);
2953         }
2954 }
2955
2956 static int
2957 bridge_rtage_finddead(struct bridge_softc *sc)
2958 {
2959         struct bridge_rtnode *brt;
2960         int dead = 0;
2961
2962         LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2963                 struct bridge_rtinfo *bri = brt->brt_info;
2964
2965                 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2966                     time_second >= bri->bri_expire) {
2967                         bri->bri_dead = 1;
2968                         ++dead;
2969                         KKASSERT(dead <= sc->sc_brtcnt);
2970                 }
2971         }
2972         return dead;
2973 }
2974
2975 /*
2976  * bridge_rtage:
2977  *
2978  *      Perform an aging cycle.
2979  */
2980 static void
2981 bridge_rtage(struct bridge_softc *sc)
2982 {
2983         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2984
2985         if (bridge_rtage_finddead(sc))
2986                 bridge_rtreap(sc);
2987 }
2988
2989 /*
2990  * bridge_rtflush:
2991  *
2992  *      Remove all dynamic addresses from the bridge.
2993  */
2994 static void
2995 bridge_rtflush(struct bridge_softc *sc, int bf)
2996 {
2997         struct bridge_rtnode *brt;
2998         int reap;
2999
3000         reap = 0;
3001         LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3002                 struct bridge_rtinfo *bri = brt->brt_info;
3003
3004                 if ((bf & IFBF_FLUSHALL) ||
3005                     (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
3006                         bri->bri_dead = 1;
3007                         reap = 1;
3008                 }
3009         }
3010         if (reap) {
3011                 if (bf & IFBF_FLUSHSYNC)
3012                         bridge_rtreap(sc);
3013                 else
3014                         bridge_rtreap_async(sc);
3015         }
3016 }
3017
3018 /*
3019  * bridge_rtdaddr:
3020  *
3021  *      Remove an address from the table.
3022  */
3023 static int
3024 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
3025 {
3026         struct bridge_rtnode *brt;
3027
3028         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3029
3030         if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
3031                 return (ENOENT);
3032
3033         /* TODO: add a cheaper delete operation */
3034         brt->brt_info->bri_dead = 1;
3035         bridge_rtreap(sc);
3036         return (0);
3037 }
3038
3039 /*
3040  * bridge_rtdelete:
3041  *
3042  *      Delete routes to a speicifc member interface.
3043  */
3044 void
3045 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int bf)
3046 {
3047         struct bridge_rtnode *brt;
3048         int reap;
3049
3050         reap = 0;
3051         LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3052                 struct bridge_rtinfo *bri = brt->brt_info;
3053
3054                 if (bri->bri_ifp == ifp &&
3055                     ((bf & IFBF_FLUSHALL) ||
3056                      (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) {
3057                         bri->bri_dead = 1;
3058                         reap = 1;
3059                 }
3060         }
3061         if (reap) {
3062                 if (bf & IFBF_FLUSHSYNC)
3063                         bridge_rtreap(sc);
3064                 else
3065                         bridge_rtreap_async(sc);
3066         }
3067 }
3068
3069 /*
3070  * bridge_rtable_init:
3071  *
3072  *      Initialize the route table for this bridge.
3073  */
3074 static void
3075 bridge_rtable_init(struct bridge_softc *sc)
3076 {
3077         int cpu;
3078
3079         /*
3080          * Initialize per-cpu hash tables
3081          */
3082         sc->sc_rthashs = kmalloc(sizeof(*sc->sc_rthashs) * ncpus,
3083                                  M_DEVBUF, M_WAITOK);
3084         for (cpu = 0; cpu < ncpus; ++cpu) {
3085                 int i;
3086
3087                 sc->sc_rthashs[cpu] =
3088                 kmalloc(sizeof(struct bridge_rtnode_head) * BRIDGE_RTHASH_SIZE,
3089                         M_DEVBUF, M_WAITOK);
3090
3091                 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
3092                         LIST_INIT(&sc->sc_rthashs[cpu][i]);
3093         }
3094         sc->sc_rthash_key = karc4random();
3095
3096         /*
3097          * Initialize per-cpu lists
3098          */
3099         sc->sc_rtlists = kmalloc(sizeof(struct bridge_rtnode_head) * ncpus,
3100                                  M_DEVBUF, M_WAITOK);
3101         for (cpu = 0; cpu < ncpus; ++cpu)
3102                 LIST_INIT(&sc->sc_rtlists[cpu]);
3103 }
3104
3105 /*
3106  * bridge_rtable_fini:
3107  *
3108  *      Deconstruct the route table for this bridge.
3109  */
3110 static void
3111 bridge_rtable_fini(struct bridge_softc *sc)
3112 {
3113         int cpu;
3114
3115         /*
3116          * Free per-cpu hash tables
3117          */
3118         for (cpu = 0; cpu < ncpus; ++cpu)
3119                 kfree(sc->sc_rthashs[cpu], M_DEVBUF);
3120         kfree(sc->sc_rthashs, M_DEVBUF);
3121
3122         /*
3123          * Free per-cpu lists
3124          */
3125         kfree(sc->sc_rtlists, M_DEVBUF);
3126 }
3127
3128 /*
3129  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
3130  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
3131  */
3132 #define mix(a, b, c)                                                    \
3133 do {                                                                    \
3134         a -= b; a -= c; a ^= (c >> 13);                                 \
3135         b -= c; b -= a; b ^= (a << 8);                                  \
3136         c -= a; c -= b; c ^= (b >> 13);                                 \
3137         a -= b; a -= c; a ^= (c >> 12);                                 \
3138         b -= c; b -= a; b ^= (a << 16);                                 \
3139         c -= a; c -= b; c ^= (b >> 5);                                  \
3140         a -= b; a -= c; a ^= (c >> 3);                                  \
3141         b -= c; b -= a; b ^= (a << 10);                                 \
3142         c -= a; c -= b; c ^= (b >> 15);                                 \
3143 } while (/*CONSTCOND*/0)
3144
3145 static __inline uint32_t
3146 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
3147 {
3148         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
3149
3150         b += addr[5] << 8;
3151         b += addr[4];
3152         a += addr[3] << 24;
3153         a += addr[2] << 16;
3154         a += addr[1] << 8;
3155         a += addr[0];
3156
3157         mix(a, b, c);
3158
3159         return (c & BRIDGE_RTHASH_MASK);
3160 }
3161
3162 #undef mix
3163
3164 static int
3165 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
3166 {
3167         int i, d;
3168
3169         for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
3170                 d = ((int)a[i]) - ((int)b[i]);
3171         }
3172
3173         return (d);
3174 }
3175
3176 /*
3177  * bridge_rtnode_lookup:
3178  *
3179  *      Look up a bridge route node for the specified destination.
3180  */
3181 static struct bridge_rtnode *
3182 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
3183 {
3184         struct bridge_rtnode *brt;
3185         uint32_t hash;
3186         int dir;
3187
3188         hash = bridge_rthash(sc, addr);
3189         LIST_FOREACH(brt, &sc->sc_rthashs[mycpuid][hash], brt_hash) {
3190                 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
3191                 if (dir == 0)
3192                         return (brt);
3193                 if (dir > 0)
3194                         return (NULL);
3195         }
3196
3197         return (NULL);
3198 }
3199
3200 /*
3201  * bridge_rtnode_insert:
3202  *
3203  *      Insert the specified bridge node into the route table.
3204  *      Caller has to make sure that rtnode does not exist.
3205  */
3206 static void
3207 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3208 {
3209         struct bridge_rtnode *lbrt;
3210         uint32_t hash;
3211         int dir;
3212
3213         hash = bridge_rthash(sc, brt->brt_addr);
3214
3215         lbrt = LIST_FIRST(&sc->sc_rthashs[mycpuid][hash]);
3216         if (lbrt == NULL) {
3217                 LIST_INSERT_HEAD(&sc->sc_rthashs[mycpuid][hash], brt, brt_hash);
3218                 goto out;
3219         }
3220
3221         do {
3222                 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
3223                 KASSERT(dir != 0, ("rtnode already exist\n"));
3224
3225                 if (dir > 0) {
3226                         LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3227                         goto out;
3228                 }
3229                 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
3230                         LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3231                         goto out;
3232                 }
3233                 lbrt = LIST_NEXT(lbrt, brt_hash);
3234         } while (lbrt != NULL);
3235
3236         panic("no suitable position found for rtnode\n");
3237 out:
3238         LIST_INSERT_HEAD(&sc->sc_rtlists[mycpuid], brt, brt_list);
3239         if (mycpuid == 0) {
3240                 /*
3241                  * Update the brtcnt.
3242                  * We only need to do it once and we do it on CPU0.
3243                  */
3244                 sc->sc_brtcnt++;
3245         }
3246 }
3247
3248 /*
3249  * bridge_rtnode_destroy:
3250  *
3251  *      Destroy a bridge rtnode.
3252  */
3253 static void
3254 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3255 {
3256         LIST_REMOVE(brt, brt_hash);
3257         LIST_REMOVE(brt, brt_list);
3258
3259         if (mycpuid + 1 == ncpus) {
3260                 /* Free rtinfo associated with rtnode on the last cpu */
3261                 kfree(brt->brt_info, M_DEVBUF);
3262         }
3263         kfree(brt, M_DEVBUF);
3264
3265         if (mycpuid == 0) {
3266                 /* Update brtcnt only on CPU0 */
3267                 sc->sc_brtcnt--;
3268         }
3269 }
3270
3271 static __inline int
3272 bridge_post_pfil(struct mbuf *m)
3273 {
3274         if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED)
3275                 return EOPNOTSUPP;
3276
3277         /* Not yet */
3278         if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED)
3279                 return EOPNOTSUPP;
3280
3281         return 0;
3282 }
3283
3284 /*
3285  * Send bridge packets through pfil if they are one of the types pfil can deal
3286  * with, or if they are ARP or REVARP.  (pfil will pass ARP and REVARP without
3287  * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3288  * that interface.
3289  */
3290 static int
3291 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3292 {
3293         int snap, error, i, hlen;
3294         struct ether_header *eh1, eh2;
3295         struct ip *ip;
3296         struct llc llc1;
3297         u_int16_t ether_type;
3298
3299         snap = 0;
3300         error = -1;     /* Default error if not error == 0 */
3301
3302         if (pfil_bridge == 0 && pfil_member == 0)
3303                 return (0); /* filtering is disabled */
3304
3305         i = min((*mp)->m_pkthdr.len, max_protohdr);
3306         if ((*mp)->m_len < i) {
3307                 *mp = m_pullup(*mp, i);
3308                 if (*mp == NULL) {
3309                         kprintf("%s: m_pullup failed\n", __func__);
3310                         return (-1);
3311                 }
3312         }
3313
3314         eh1 = mtod(*mp, struct ether_header *);
3315         ether_type = ntohs(eh1->ether_type);
3316
3317         /*
3318          * Check for SNAP/LLC.
3319          */
3320         if (ether_type < ETHERMTU) {
3321                 struct llc *llc2 = (struct llc *)(eh1 + 1);
3322
3323                 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3324                     llc2->llc_dsap == LLC_SNAP_LSAP &&
3325                     llc2->llc_ssap == LLC_SNAP_LSAP &&
3326                     llc2->llc_control == LLC_UI) {
3327                         ether_type = htons(llc2->llc_un.type_snap.ether_type);
3328                         snap = 1;
3329                 }
3330         }
3331
3332         /*
3333          * If we're trying to filter bridge traffic, don't look at anything
3334          * other than IP and ARP traffic.  If the filter doesn't understand
3335          * IPv6, don't allow IPv6 through the bridge either.  This is lame
3336          * since if we really wanted, say, an AppleTalk filter, we are hosed,
3337          * but of course we don't have an AppleTalk filter to begin with.
3338          * (Note that since pfil doesn't understand ARP it will pass *ALL*
3339          * ARP traffic.)
3340          */
3341         switch (ether_type) {
3342         case ETHERTYPE_ARP:
3343         case ETHERTYPE_REVARP:
3344                 return (0); /* Automatically pass */
3345
3346         case ETHERTYPE_IP:
3347 #ifdef INET6
3348         case ETHERTYPE_IPV6:
3349 #endif /* INET6 */
3350                 break;
3351
3352         default:
3353                 /*
3354                  * Check to see if the user wants to pass non-ip
3355                  * packets, these will not be checked by pfil(9)
3356                  * and passed unconditionally so the default is to drop.
3357                  */
3358                 if (pfil_onlyip)
3359                         goto bad;
3360         }
3361
3362         /* Strip off the Ethernet header and keep a copy. */
3363         m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3364         m_adj(*mp, ETHER_HDR_LEN);
3365
3366         /* Strip off snap header, if present */
3367         if (snap) {
3368                 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3369                 m_adj(*mp, sizeof(struct llc));
3370         }
3371
3372         /*
3373          * Check the IP header for alignment and errors
3374          */
3375         if (dir == PFIL_IN) {
3376                 switch (ether_type) {
3377                 case ETHERTYPE_IP:
3378                         error = bridge_ip_checkbasic(mp);
3379                         break;
3380 #ifdef INET6
3381                 case ETHERTYPE_IPV6:
3382                         error = bridge_ip6_checkbasic(mp);
3383                         break;
3384 #endif /* INET6 */
3385                 default:
3386                         error = 0;
3387                 }
3388                 if (error)
3389                         goto bad;
3390         }
3391
3392         error = 0;
3393
3394         /*
3395          * Run the packet through pfil
3396          */
3397         switch (ether_type) {
3398         case ETHERTYPE_IP:
3399                 /*
3400                  * before calling the firewall, swap fields the same as
3401                  * IP does. here we assume the header is contiguous
3402                  */
3403                 ip = mtod(*mp, struct ip *);
3404
3405                 ip->ip_len = ntohs(ip->ip_len);
3406                 ip->ip_off = ntohs(ip->ip_off);
3407
3408                 /*
3409                  * Run pfil on the member interface and the bridge, both can
3410                  * be skipped by clearing pfil_member or pfil_bridge.
3411                  *
3412                  * Keep the order:
3413                  *   in_if -> bridge_if -> out_if
3414                  */
3415                 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) {
3416                         error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3417                         if (*mp == NULL || error != 0) /* filter may consume */
3418                                 break;
3419                         error = bridge_post_pfil(*mp);
3420                         if (error)
3421                                 break;
3422                 }
3423
3424                 if (pfil_member && ifp != NULL) {
3425                         error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir);
3426                         if (*mp == NULL || error != 0) /* filter may consume */
3427                                 break;
3428                         error = bridge_post_pfil(*mp);
3429                         if (error)
3430                                 break;
3431                 }
3432
3433                 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) {
3434                         error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3435                         if (*mp == NULL || error != 0) /* filter may consume */
3436                                 break;
3437                         error = bridge_post_pfil(*mp);
3438                         if (error)
3439                                 break;
3440                 }
3441
3442                 /* check if we need to fragment the packet */
3443                 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3444                         i = (*mp)->m_pkthdr.len;
3445                         if (i > ifp->if_mtu) {
3446                                 error = bridge_fragment(ifp, *mp, &eh2, snap,
3447                                             &llc1);
3448                                 return (error);
3449                         }
3450                 }
3451
3452                 /* Recalculate the ip checksum and restore byte ordering */
3453                 ip = mtod(*mp, struct ip *);
3454                 hlen = ip->ip_hl << 2;
3455                 if (hlen < sizeof(struct ip))
3456                         goto bad;
3457                 if (hlen > (*mp)->m_len) {
3458                         if ((*mp = m_pullup(*mp, hlen)) == 0)
3459                                 goto bad;
3460                         ip = mtod(*mp, struct ip *);
3461                         if (ip == NULL)
3462                                 goto bad;
3463                 }
3464                 ip->ip_len = htons(ip->ip_len);
3465                 ip->ip_off = htons(ip->ip_off);
3466                 ip->ip_sum = 0;
3467                 if (hlen == sizeof(struct ip))
3468                         ip->ip_sum = in_cksum_hdr(ip);
3469                 else
3470                         ip->ip_sum = in_cksum(*mp, hlen);
3471
3472                 break;
3473 #ifdef INET6
3474         case ETHERTYPE_IPV6:
3475                 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3476                         error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3477                                         dir);
3478
3479                 if (*mp == NULL || error != 0) /* filter may consume */
3480                         break;
3481
3482                 if (pfil_member && ifp != NULL)
3483                         error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
3484                                         dir);
3485
3486                 if (*mp == NULL || error != 0) /* filter may consume */
3487                         break;
3488
3489                 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3490                         error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3491                                         dir);
3492                 break;
3493 #endif
3494         default:
3495                 error = 0;
3496                 break;
3497         }
3498
3499         if (*mp == NULL)
3500                 return (error);
3501         if (error != 0)
3502                 goto bad;
3503
3504         error = -1;
3505
3506         /*
3507          * Finally, put everything back the way it was and return
3508          */
3509         if (snap) {
3510                 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
3511                 if (*mp == NULL)
3512                         return (error);
3513                 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3514         }
3515
3516         M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
3517         if (*mp == NULL)
3518                 return (error);
3519         bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3520
3521         return (0);
3522
3523 bad:
3524         m_freem(*mp);
3525         *mp = NULL;
3526         return (error);
3527 }
3528
3529 /*
3530  * Perform basic checks on header size since
3531  * pfil assumes ip_input has already processed
3532  * it for it.  Cut-and-pasted from ip_input.c.
3533  * Given how simple the IPv6 version is,
3534  * does the IPv4 version really need to be
3535  * this complicated?
3536  *
3537  * XXX Should we update ipstat here, or not?
3538  * XXX Right now we update ipstat but not
3539  * XXX csum_counter.
3540  */
3541 static int
3542 bridge_ip_checkbasic(struct mbuf **mp)
3543 {
3544         struct mbuf *m = *mp;
3545         struct ip *ip;
3546         int len, hlen;
3547         u_short sum;
3548
3549         if (*mp == NULL)
3550                 return (-1);
3551 #if notyet
3552         if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3553                 if ((m = m_copyup(m, sizeof(struct ip),
3554                         (max_linkhdr + 3) & ~3)) == NULL) {
3555                         /* XXXJRT new stat, please */
3556                         ipstat.ips_toosmall++;
3557                         goto bad;
3558                 }
3559         } else
3560 #endif
3561 #ifndef __predict_false
3562 #define __predict_false(x) x
3563 #endif
3564          if (__predict_false(m->m_len < sizeof (struct ip))) {
3565                 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3566                         ipstat.ips_toosmall++;
3567                         goto bad;
3568                 }
3569         }
3570         ip = mtod(m, struct ip *);
3571         if (ip == NULL) goto bad;
3572
3573         if (ip->ip_v != IPVERSION) {
3574                 ipstat.ips_badvers++;
3575                 goto bad;
3576         }
3577         hlen = ip->ip_hl << 2;
3578         if (hlen < sizeof(struct ip)) { /* minimum header length */
3579                 ipstat.ips_badhlen++;
3580                 goto bad;
3581         }
3582         if (hlen > m->m_len) {
3583                 if ((m = m_pullup(m, hlen)) == 0) {
3584                         ipstat.ips_badhlen++;
3585                         goto bad;
3586                 }
3587                 ip = mtod(m, struct ip *);
3588                 if (ip == NULL) goto bad;
3589         }
3590
3591         if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3592                 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3593         } else {
3594                 if (hlen == sizeof(struct ip)) {
3595                         sum = in_cksum_hdr(ip);
3596                 } else {
3597                         sum = in_cksum(m, hlen);
3598                 }
3599         }
3600         if (sum) {
3601                 ipstat.ips_badsum++;
3602                 goto bad;
3603         }
3604
3605         /* Retrieve the packet length. */
3606         len = ntohs(ip->ip_len);
3607
3608         /*
3609          * Check for additional length bogosity
3610          */
3611         if (len < hlen) {
3612                 ipstat.ips_badlen++;
3613                 goto bad;
3614         }
3615
3616         /*
3617          * Check that the amount of data in the buffers
3618          * is as at least much as the IP header would have us expect.
3619          * Drop packet if shorter than we expect.
3620          */
3621         if (m->m_pkthdr.len < len) {
3622                 ipstat.ips_tooshort++;
3623                 goto bad;
3624         }
3625
3626         /* Checks out, proceed */
3627         *mp = m;
3628         return (0);
3629
3630 bad:
3631         *mp = m;
3632         return (-1);
3633 }
3634
3635 #ifdef INET6
3636 /*
3637  * Same as above, but for IPv6.
3638  * Cut-and-pasted from ip6_input.c.
3639  * XXX Should we update ip6stat, or not?
3640  */
3641 static int
3642 bridge_ip6_checkbasic(struct mbuf **mp)
3643 {
3644         struct mbuf *m = *mp;
3645         struct ip6_hdr *ip6;
3646
3647         /*
3648          * If the IPv6 header is not aligned, slurp it up into a new
3649          * mbuf with space for link headers, in the event we forward
3650          * it.  Otherwise, if it is aligned, make sure the entire base
3651          * IPv6 header is in the first mbuf of the chain.
3652          */
3653 #if notyet
3654         if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3655                 struct ifnet *inifp = m->m_pkthdr.rcvif;
3656                 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3657                             (max_linkhdr + 3) & ~3)) == NULL) {
3658                         /* XXXJRT new stat, please */
3659                         ip6stat.ip6s_toosmall++;
3660                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3661                         goto bad;
3662                 }
3663         } else
3664 #endif
3665         if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3666                 struct ifnet *inifp = m->m_pkthdr.rcvif;
3667                 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3668                         ip6stat.ip6s_toosmall++;
3669                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3670                         goto bad;
3671                 }
3672         }
3673
3674         ip6 = mtod(m, struct ip6_hdr *);
3675
3676         if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3677                 ip6stat.ip6s_badvers++;
3678                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3679                 goto bad;
3680         }
3681
3682         /* Checks out, proceed */
3683         *mp = m;
3684         return (0);
3685
3686 bad:
3687         *mp = m;
3688         return (-1);
3689 }
3690 #endif /* INET6 */
3691
3692 /*
3693  * bridge_fragment:
3694  *
3695  *      Return a fragmented mbuf chain.
3696  */
3697 static int
3698 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
3699     int snap, struct llc *llc)
3700 {
3701         struct mbuf *m0;
3702         struct ip *ip;
3703         int error = -1;
3704
3705         if (m->m_len < sizeof(struct ip) &&
3706             (m = m_pullup(m, sizeof(struct ip))) == NULL)
3707                 goto out;
3708         ip = mtod(m, struct ip *);
3709
3710         error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
3711                     CSUM_DELAY_IP);
3712         if (error)
3713                 goto out;
3714
3715         /* walk the chain and re-add the Ethernet header */
3716         for (m0 = m; m0; m0 = m0->m_nextpkt) {
3717                 if (error == 0) {
3718                         if (snap) {
3719                                 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
3720                                 if (m0 == NULL) {
3721                                         error = ENOBUFS;
3722                                         continue;
3723                                 }
3724                                 bcopy(llc, mtod(m0, caddr_t),
3725                                     sizeof(struct llc));
3726                         }
3727                         M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
3728                         if (m0 == NULL) {
3729                                 error = ENOBUFS;
3730                                 continue;
3731                         }
3732                         bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
3733                 } else 
3734                         m_freem(m);
3735         }
3736
3737         if (error == 0)
3738                 ipstat.ips_fragmented++;
3739
3740         return (error);
3741
3742 out:
3743         if (m != NULL)
3744                 m_freem(m);
3745         return (error);
3746 }
3747
3748 static void
3749 bridge_enqueue_handler(netmsg_t msg)
3750 {
3751         struct netmsg_packet *nmp;
3752         struct ifnet *dst_ifp;
3753         struct mbuf *m;
3754
3755         nmp = &msg->packet;
3756         m = nmp->nm_packet;
3757         dst_ifp = nmp->base.lmsg.u.ms_resultp;
3758
3759         bridge_handoff(dst_ifp, m, 1);
3760 }
3761
3762 static void
3763 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m, int from_us)
3764 {
3765         struct mbuf *m0;
3766         struct ifnet *bifp;
3767
3768         bifp = ((struct bridge_softc *)dst_ifp->if_bridge)->sc_ifp;
3769
3770         /* We may be sending a fragment so traverse the mbuf */
3771         for (; m; m = m0) {
3772                 struct altq_pktattr pktattr;
3773
3774                 m0 = m->m_nextpkt;
3775                 m->m_nextpkt = NULL;
3776
3777                 /*
3778                  * If being sent from our host override ether_shost
3779                  * so any replies go the correct interface.  This is
3780                  * mandatory or ARP replies will wind up on the wrong
3781                  * interface.
3782                  *
3783                  * Also override ether_shost when relaying a packet out
3784                  * the same interface it came in on, due to multi-homed
3785                  * addresses & default routes, otherwise switches will
3786                  * get very confused.
3787                  *
3788                  * Otherwise if we are in transparent mode.
3789                  */
3790                 if (from_us || m->m_pkthdr.rcvif == dst_ifp) {
3791                         m_copyback(m,
3792                                    offsetof(struct ether_header, ether_shost),
3793                                    ETHER_ADDR_LEN, IF_LLADDR(dst_ifp));
3794                 } else if ((bifp->if_flags & IFF_LINK0) &&
3795                            (m->m_pkthdr.fw_flags & BRIDGE_MBUF_TAGGED)) {
3796                         m_copyback(m,
3797                                    offsetof(struct ether_header, ether_shost),
3798                                    ETHER_ADDR_LEN,
3799                                    m->m_pkthdr.br.ether.ether_shost);
3800                 }
3801
3802                 if (ifq_is_enabled(&dst_ifp->if_snd))
3803                         altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
3804
3805                 ifq_dispatch(dst_ifp, m, &pktattr);
3806         }
3807 }
3808
3809 static void
3810 bridge_control_dispatch(netmsg_t msg)
3811 {
3812         struct netmsg_brctl *bc_msg = (struct netmsg_brctl *)msg;
3813         struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
3814         int error;
3815
3816         ifnet_serialize_all(bifp);
3817         error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
3818         ifnet_deserialize_all(bifp);
3819
3820         lwkt_replymsg(&bc_msg->base.lmsg, error);
3821 }
3822
3823 static int
3824 bridge_control(struct bridge_softc *sc, u_long cmd,
3825                bridge_ctl_t bc_func, void *bc_arg)
3826 {
3827         struct ifnet *bifp = sc->sc_ifp;
3828         struct netmsg_brctl bc_msg;
3829         int error;
3830
3831         ASSERT_IFNET_SERIALIZED_ALL(bifp);
3832
3833         bzero(&bc_msg, sizeof(bc_msg));
3834
3835         netmsg_init(&bc_msg.base, NULL, &curthread->td_msgport,
3836                     0, bridge_control_dispatch);
3837         bc_msg.bc_func = bc_func;
3838         bc_msg.bc_sc = sc;
3839         bc_msg.bc_arg = bc_arg;
3840
3841         ifnet_deserialize_all(bifp);
3842         error = lwkt_domsg(BRIDGE_CFGPORT, &bc_msg.base.lmsg, 0);
3843         ifnet_serialize_all(bifp);
3844         return error;
3845 }
3846
3847 static void
3848 bridge_add_bif_handler(netmsg_t msg)
3849 {
3850         struct netmsg_braddbif *amsg = (struct netmsg_braddbif *)msg;
3851         struct bridge_softc *sc;
3852         struct bridge_iflist *bif;
3853
3854         sc = amsg->br_softc;
3855
3856         bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
3857         bif->bif_ifp = amsg->br_bif_ifp;
3858         bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
3859         bif->bif_onlist = 1;
3860         bif->bif_info = amsg->br_bif_info;
3861
3862         LIST_INSERT_HEAD(&sc->sc_iflists[mycpuid], bif, bif_next);
3863
3864         ifnet_forwardmsg(&amsg->base.lmsg, mycpuid + 1);
3865 }
3866
3867 static void
3868 bridge_add_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3869                struct ifnet *ifp)
3870 {
3871         struct netmsg_braddbif amsg;
3872
3873         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3874
3875         netmsg_init(&amsg.base, NULL, &curthread->td_msgport,
3876                     0, bridge_add_bif_handler);
3877         amsg.br_softc = sc;
3878         amsg.br_bif_info = bif_info;
3879         amsg.br_bif_ifp = ifp;
3880
3881         ifnet_domsg(&amsg.base.lmsg, 0);
3882 }
3883
3884 static void
3885 bridge_del_bif_handler(netmsg_t msg)
3886 {
3887         struct netmsg_brdelbif *dmsg = (struct netmsg_brdelbif *)msg;
3888         struct bridge_softc *sc;
3889         struct bridge_iflist *bif;
3890
3891         sc = dmsg->br_softc;
3892
3893         /*
3894          * Locate the bif associated with the br_bif_info
3895          * on the current CPU
3896          */
3897         bif = bridge_lookup_member_ifinfo(sc, dmsg->br_bif_info);
3898         KKASSERT(bif != NULL && bif->bif_onlist);
3899
3900         /* Remove the bif from the current CPU's iflist */
3901         bif->bif_onlist = 0;
3902         LIST_REMOVE(bif, bif_next);
3903
3904         /* Save the removed bif for later freeing */
3905         LIST_INSERT_HEAD(dmsg->br_bif_list, bif, bif_next);
3906
3907         ifnet_forwardmsg(&dmsg->base.lmsg, mycpuid + 1);
3908 }
3909
3910 static void
3911 bridge_del_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3912                struct bridge_iflist_head *saved_bifs)
3913 {
3914         struct netmsg_brdelbif dmsg;
3915
3916         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3917
3918         netmsg_init(&dmsg.base, NULL, &curthread->td_msgport,
3919                     0, bridge_del_bif_handler);
3920         dmsg.br_softc = sc;
3921         dmsg.br_bif_info = bif_info;
3922         dmsg.br_bif_list = saved_bifs;
3923
3924         ifnet_domsg(&dmsg.base.lmsg, 0);
3925 }
3926
3927 static void
3928 bridge_set_bifflags_handler(netmsg_t msg)
3929 {
3930         struct netmsg_brsflags *smsg = (struct netmsg_brsflags *)msg;
3931         struct bridge_softc *sc;
3932         struct bridge_iflist *bif;
3933
3934         sc = smsg->br_softc;
3935
3936         /*
3937          * Locate the bif associated with the br_bif_info
3938          * on the current CPU
3939          */
3940         bif = bridge_lookup_member_ifinfo(sc, smsg->br_bif_info);
3941         KKASSERT(bif != NULL && bif->bif_onlist);
3942
3943         bif->bif_flags = smsg->br_bif_flags;
3944
3945         ifnet_forwardmsg(&smsg->base.lmsg, mycpuid + 1);
3946 }
3947
3948 static void
3949 bridge_set_bifflags(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3950                     uint32_t bif_flags)
3951 {
3952         struct netmsg_brsflags smsg;
3953
3954         ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3955
3956         netmsg_init(&smsg.base, NULL, &curthread->td_msgport,
3957                     0, bridge_set_bifflags_handler);
3958         smsg.br_softc = sc;
3959         smsg.br_bif_info = bif_info;
3960         smsg.br_bif_flags = bif_flags;
3961
3962         ifnet_domsg(&smsg.base.lmsg, 0);
3963 }