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