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