Serialize bridge configuration operations by netisr0, so that "test and set"
[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.33 2008/06/16 14:48:01 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 #include <sys/cdefs.h>
84
85 #include "opt_inet.h"
86 #include "opt_inet6.h"
87
88 #include <sys/param.h>
89 #include <sys/mbuf.h>
90 #include <sys/malloc.h>
91 #include <sys/protosw.h>
92 #include <sys/systm.h>
93 #include <sys/time.h>
94 #include <sys/socket.h> /* for net/if.h */
95 #include <sys/sockio.h>
96 #include <sys/ctype.h>  /* string functions */
97 #include <sys/kernel.h>
98 #include <sys/random.h>
99 #include <sys/sysctl.h>
100 #include <sys/module.h>
101 #include <sys/proc.h>
102 #include <sys/lock.h>
103 #include <sys/thread.h>
104 #include <sys/thread2.h>
105 #include <sys/mpipe.h>
106
107 #include <net/bpf.h>
108 #include <net/if.h>
109 #include <net/if_dl.h>
110 #include <net/if_types.h>
111 #include <net/if_var.h>
112 #include <net/pfil.h>
113 #include <net/ifq_var.h>
114 #include <net/if_clone.h>
115
116 #include <netinet/in.h> /* for struct arpcom */
117 #include <netinet/in_systm.h>
118 #include <netinet/in_var.h>
119 #include <netinet/ip.h>
120 #include <netinet/ip_var.h>
121 #ifdef INET6
122 #include <netinet/ip6.h>
123 #include <netinet6/ip6_var.h>
124 #endif
125 #include <netinet/if_ether.h> /* for struct arpcom */
126 #include <net/bridge/if_bridgevar.h>
127 #include <net/if_llc.h>
128 #include <net/netmsg2.h>
129
130 #include <net/route.h>
131 #include <sys/in_cksum.h>
132
133 /*
134  * Size of the route hash table.  Must be a power of two.
135  */
136 #ifndef BRIDGE_RTHASH_SIZE
137 #define BRIDGE_RTHASH_SIZE              1024
138 #endif
139
140 #define BRIDGE_RTHASH_MASK              (BRIDGE_RTHASH_SIZE - 1)
141
142 /*
143  * Maximum number of addresses to cache.
144  */
145 #ifndef BRIDGE_RTABLE_MAX
146 #define BRIDGE_RTABLE_MAX               100
147 #endif
148
149 /*
150  * Spanning tree defaults.
151  */
152 #define BSTP_DEFAULT_MAX_AGE            (20 * 256)
153 #define BSTP_DEFAULT_HELLO_TIME         (2 * 256)
154 #define BSTP_DEFAULT_FORWARD_DELAY      (15 * 256)
155 #define BSTP_DEFAULT_HOLD_TIME          (1 * 256)
156 #define BSTP_DEFAULT_BRIDGE_PRIORITY    0x8000
157 #define BSTP_DEFAULT_PORT_PRIORITY      0x80
158 #define BSTP_DEFAULT_PATH_COST          55
159
160 /*
161  * Timeout (in seconds) for entries learned dynamically.
162  */
163 #ifndef BRIDGE_RTABLE_TIMEOUT
164 #define BRIDGE_RTABLE_TIMEOUT           (20 * 60)       /* same as ARP */
165 #endif
166
167 /*
168  * Number of seconds between walks of the route list.
169  */
170 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
171 #define BRIDGE_RTABLE_PRUNE_PERIOD      (5 * 60)
172 #endif
173
174 /*
175  * List of capabilities to mask on the member interface.
176  */
177 #define BRIDGE_IFCAPS_MASK              IFCAP_TXCSUM
178
179 eventhandler_tag        bridge_detach_cookie = NULL;
180
181 extern  struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
182 extern  int (*bridge_output_p)(struct ifnet *, struct mbuf *);
183 extern  void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
184
185 typedef int     (*bridge_ctl_t)(struct bridge_softc *, void *);
186
187 static int      bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
188
189 static int      bridge_clone_create(struct if_clone *, int);
190 static void     bridge_clone_destroy(struct ifnet *);
191
192 static int      bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
193 static void     bridge_mutecaps(struct bridge_iflist *, int);
194 static void     bridge_ifdetach(void *arg __unused, struct ifnet *);
195 static void     bridge_init(void *);
196 static void     bridge_stop(struct ifnet *);
197 static void     bridge_start(struct ifnet *);
198 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
199 static int      bridge_output(struct ifnet *, struct mbuf *);
200
201 static void     bridge_forward(struct bridge_softc *, struct mbuf *m);
202
203 static void     bridge_timer(void *);
204
205 static void     bridge_broadcast(struct bridge_softc *, struct ifnet *,
206                     struct mbuf *, int);
207 static void     bridge_span(struct bridge_softc *, struct mbuf *);
208
209 static int      bridge_rtupdate(struct bridge_softc *, const uint8_t *,
210                     struct ifnet *, int, uint8_t);
211 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
212 static void     bridge_rttrim(struct bridge_softc *);
213 static void     bridge_rtage(struct bridge_softc *);
214 static void     bridge_rtflush(struct bridge_softc *, int);
215 static int      bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
216
217 static int      bridge_rtable_init(struct bridge_softc *);
218 static void     bridge_rtable_fini(struct bridge_softc *);
219
220 static int      bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
221 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
222                     const uint8_t *);
223 static int      bridge_rtnode_insert(struct bridge_softc *,
224                     struct bridge_rtnode *);
225 static void     bridge_rtnode_destroy(struct bridge_softc *,
226                     struct bridge_rtnode *);
227
228 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
229                     const char *name);
230 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
231                     struct ifnet *ifp);
232 static void     bridge_delete_member(struct bridge_softc *,
233                     struct bridge_iflist *, int);
234 static void     bridge_delete_span(struct bridge_softc *,
235                     struct bridge_iflist *);
236
237 static int      bridge_control(struct bridge_softc *, bridge_ctl_t, void *);
238 static int      bridge_ioctl_add(struct bridge_softc *, void *);
239 static int      bridge_ioctl_del(struct bridge_softc *, void *);
240 static int      bridge_ioctl_gifflags(struct bridge_softc *, void *);
241 static int      bridge_ioctl_sifflags(struct bridge_softc *, void *);
242 static int      bridge_ioctl_scache(struct bridge_softc *, void *);
243 static int      bridge_ioctl_gcache(struct bridge_softc *, void *);
244 static int      bridge_ioctl_gifs(struct bridge_softc *, void *);
245 static int      bridge_ioctl_rts(struct bridge_softc *, void *);
246 static int      bridge_ioctl_saddr(struct bridge_softc *, void *);
247 static int      bridge_ioctl_sto(struct bridge_softc *, void *);
248 static int      bridge_ioctl_gto(struct bridge_softc *, void *);
249 static int      bridge_ioctl_daddr(struct bridge_softc *, void *);
250 static int      bridge_ioctl_flush(struct bridge_softc *, void *);
251 static int      bridge_ioctl_gpri(struct bridge_softc *, void *);
252 static int      bridge_ioctl_spri(struct bridge_softc *, void *);
253 static int      bridge_ioctl_ght(struct bridge_softc *, void *);
254 static int      bridge_ioctl_sht(struct bridge_softc *, void *);
255 static int      bridge_ioctl_gfd(struct bridge_softc *, void *);
256 static int      bridge_ioctl_sfd(struct bridge_softc *, void *);
257 static int      bridge_ioctl_gma(struct bridge_softc *, void *);
258 static int      bridge_ioctl_sma(struct bridge_softc *, void *);
259 static int      bridge_ioctl_sifprio(struct bridge_softc *, void *);
260 static int      bridge_ioctl_sifcost(struct bridge_softc *, void *);
261 static int      bridge_ioctl_addspan(struct bridge_softc *, void *);
262 static int      bridge_ioctl_delspan(struct bridge_softc *, void *);
263 static int      bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
264                     int);
265 static int      bridge_ip_checkbasic(struct mbuf **mp);
266 #ifdef INET6
267 static int      bridge_ip6_checkbasic(struct mbuf **mp);
268 #endif /* INET6 */
269 static int      bridge_fragment(struct ifnet *, struct mbuf *,
270                     struct ether_header *, int, struct llc *);
271 static void     bridge_enqueue_internal(struct ifnet *, struct mbuf *m,
272                                         netisr_fn_t);
273 static void     bridge_enqueue_handler(struct netmsg *);
274 static void     bridge_pfil_enqueue_handler(struct netmsg *);
275 static void     bridge_pfil_enqueue(struct ifnet *, struct mbuf *, int);
276 static void     bridge_handoff_notags(struct ifnet *, struct mbuf *);
277 static void     bridge_handoff(struct ifnet *, struct mbuf *);
278
279 SYSCTL_DECL(_net_link);
280 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
281
282 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
283 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
284 static int pfil_member = 1; /* run pfil hooks on the member interface */
285 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
286     &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
287 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
288     &pfil_bridge, 0, "Packet filter on the bridge interface");
289 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
290     &pfil_member, 0, "Packet filter on the member interface");
291
292 struct bridge_control {
293         bridge_ctl_t    bc_func;
294         int             bc_argsize;
295         int             bc_flags;
296 };
297
298 #define BC_F_COPYIN             0x01    /* copy arguments in */
299 #define BC_F_COPYOUT            0x02    /* copy arguments out */
300 #define BC_F_SUSER              0x04    /* do super-user check */
301
302 const struct bridge_control bridge_control_table[] = {
303         { bridge_ioctl_add,             sizeof(struct ifbreq),
304           BC_F_COPYIN|BC_F_SUSER },
305         { bridge_ioctl_del,             sizeof(struct ifbreq),
306           BC_F_COPYIN|BC_F_SUSER },
307
308         { bridge_ioctl_gifflags,        sizeof(struct ifbreq),
309           BC_F_COPYIN|BC_F_COPYOUT },
310         { bridge_ioctl_sifflags,        sizeof(struct ifbreq),
311           BC_F_COPYIN|BC_F_SUSER },
312
313         { bridge_ioctl_scache,          sizeof(struct ifbrparam),
314           BC_F_COPYIN|BC_F_SUSER },
315         { bridge_ioctl_gcache,          sizeof(struct ifbrparam),
316           BC_F_COPYOUT },
317
318         { bridge_ioctl_gifs,            sizeof(struct ifbifconf),
319           BC_F_COPYIN|BC_F_COPYOUT },
320         { bridge_ioctl_rts,             sizeof(struct ifbaconf),
321           BC_F_COPYIN|BC_F_COPYOUT },
322
323         { bridge_ioctl_saddr,           sizeof(struct ifbareq),
324           BC_F_COPYIN|BC_F_SUSER },
325
326         { bridge_ioctl_sto,             sizeof(struct ifbrparam),
327           BC_F_COPYIN|BC_F_SUSER },
328         { bridge_ioctl_gto,             sizeof(struct ifbrparam),
329           BC_F_COPYOUT },
330
331         { bridge_ioctl_daddr,           sizeof(struct ifbareq),
332           BC_F_COPYIN|BC_F_SUSER },
333
334         { bridge_ioctl_flush,           sizeof(struct ifbreq),
335           BC_F_COPYIN|BC_F_SUSER },
336
337         { bridge_ioctl_gpri,            sizeof(struct ifbrparam),
338           BC_F_COPYOUT },
339         { bridge_ioctl_spri,            sizeof(struct ifbrparam),
340           BC_F_COPYIN|BC_F_SUSER },
341
342         { bridge_ioctl_ght,             sizeof(struct ifbrparam),
343           BC_F_COPYOUT },
344         { bridge_ioctl_sht,             sizeof(struct ifbrparam),
345           BC_F_COPYIN|BC_F_SUSER },
346
347         { bridge_ioctl_gfd,             sizeof(struct ifbrparam),
348           BC_F_COPYOUT },
349         { bridge_ioctl_sfd,             sizeof(struct ifbrparam),
350           BC_F_COPYIN|BC_F_SUSER },
351
352         { bridge_ioctl_gma,             sizeof(struct ifbrparam),
353           BC_F_COPYOUT },
354         { bridge_ioctl_sma,             sizeof(struct ifbrparam),
355           BC_F_COPYIN|BC_F_SUSER },
356
357         { bridge_ioctl_sifprio,         sizeof(struct ifbreq),
358           BC_F_COPYIN|BC_F_SUSER },
359
360         { bridge_ioctl_sifcost,         sizeof(struct ifbreq),
361           BC_F_COPYIN|BC_F_SUSER },
362
363         { bridge_ioctl_addspan,         sizeof(struct ifbreq),
364           BC_F_COPYIN|BC_F_SUSER },
365         { bridge_ioctl_delspan,         sizeof(struct ifbreq),
366           BC_F_COPYIN|BC_F_SUSER },
367 };
368 const int bridge_control_table_size =
369     sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
370
371 LIST_HEAD(, bridge_softc) bridge_list;
372
373 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
374                                 bridge_clone_create,
375                                 bridge_clone_destroy, 0, IF_MAXUNIT);
376
377 static int
378 bridge_modevent(module_t mod, int type, void *data)
379 {
380         switch (type) {
381         case MOD_LOAD:
382                 LIST_INIT(&bridge_list);
383                 if_clone_attach(&bridge_cloner);
384                 bridge_input_p = bridge_input;
385                 bridge_output_p = bridge_output;
386                 bridge_detach_cookie = EVENTHANDLER_REGISTER(
387                     ifnet_detach_event, bridge_ifdetach, NULL,
388                     EVENTHANDLER_PRI_ANY);
389 #if notyet
390                 bstp_linkstate_p = bstp_linkstate;
391 #endif
392                 break;
393         case MOD_UNLOAD:
394                 if (!LIST_EMPTY(&bridge_list))
395                         return (EBUSY);
396                 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
397                     bridge_detach_cookie);
398                 if_clone_detach(&bridge_cloner);
399                 bridge_input_p = NULL;
400                 bridge_output_p = NULL;
401 #if notyet
402                 bstp_linkstate_p = NULL;
403 #endif
404                 break;
405         default:
406                 return (EOPNOTSUPP);
407         }
408         return (0);
409 }
410
411 static moduledata_t bridge_mod = {
412         "if_bridge",
413         bridge_modevent,
414         0
415 };
416
417 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
418
419
420 /*
421  * bridge_clone_create:
422  *
423  *      Create a new bridge instance.
424  */
425 static int
426 bridge_clone_create(struct if_clone *ifc, int unit)
427 {
428         struct bridge_softc *sc;
429         struct ifnet *ifp;
430         u_char eaddr[6];
431
432         sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
433         ifp = sc->sc_ifp = &sc->sc_if;
434
435         sc->sc_brtmax = BRIDGE_RTABLE_MAX;
436         sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
437         sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
438         sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
439         sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
440         sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
441         sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
442
443         /* Initialize our routing table. */
444         bridge_rtable_init(sc);
445
446         callout_init(&sc->sc_brcallout);
447         callout_init(&sc->sc_bstpcallout);
448
449         LIST_INIT(&sc->sc_iflist);
450         LIST_INIT(&sc->sc_spanlist);
451
452         ifp->if_softc = sc;
453         if_initname(ifp, ifc->ifc_name, unit);
454         ifp->if_mtu = ETHERMTU;
455         ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
456         ifp->if_ioctl = bridge_ioctl;
457         ifp->if_start = bridge_start;
458         ifp->if_init = bridge_init;
459         ifp->if_type = IFT_BRIDGE;
460         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
461         ifp->if_snd.ifq_maxlen = ifqmaxlen;
462         ifq_set_ready(&ifp->if_snd);
463         ifp->if_hdrlen = ETHER_HDR_LEN;
464
465         /*
466          * Generate a random ethernet address and use the private AC:DE:48
467          * OUI code.
468          */
469         {
470                 int rnd = karc4random();
471                 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
472                 rnd = karc4random();
473                 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
474         }
475         eaddr[0] &= ~1;         /* clear multicast bit */
476         eaddr[0] |= 2;          /* set the LAA bit */
477
478         ether_ifattach(ifp, eaddr, NULL);
479         /* Now undo some of the damage... */
480         ifp->if_baudrate = 0;
481         ifp->if_type = IFT_BRIDGE;
482
483         crit_enter();
484         LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
485         crit_exit();
486
487         return (0);
488 }
489
490 /*
491  * bridge_clone_destroy:
492  *
493  *      Destroy a bridge instance.
494  */
495 static void
496 bridge_clone_destroy(struct ifnet *ifp)
497 {
498         struct bridge_softc *sc = ifp->if_softc;
499         struct bridge_iflist *bif;
500
501         lwkt_serialize_enter(ifp->if_serializer);
502
503         bridge_stop(ifp);
504         ifp->if_flags &= ~IFF_UP;
505
506         while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
507                 bridge_delete_member(sc, bif, 0);
508
509         while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
510                 bridge_delete_span(sc, bif);
511         }
512
513         callout_stop(&sc->sc_brcallout);
514         callout_stop(&sc->sc_bstpcallout);
515
516         lwkt_serialize_exit(ifp->if_serializer);
517
518         crit_enter();
519         LIST_REMOVE(sc, sc_list);
520         crit_exit();
521
522         ether_ifdetach(ifp);
523
524         /* Tear down the routing table. */
525         bridge_rtable_fini(sc);
526
527         kfree(sc, M_DEVBUF);
528 }
529
530 /*
531  * bridge_ioctl:
532  *
533  *      Handle a control request from the operator.
534  */
535 static int
536 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
537 {
538         struct bridge_softc *sc = ifp->if_softc;
539         union {
540                 struct ifbreq ifbreq;
541                 struct ifbifconf ifbifconf;
542                 struct ifbareq ifbareq;
543                 struct ifbaconf ifbaconf;
544                 struct ifbrparam ifbrparam;
545         } args;
546         struct ifdrv *ifd = (struct ifdrv *) data;
547         const struct bridge_control *bc;
548         int error = 0;
549
550         ASSERT_SERIALIZED(ifp->if_serializer);
551
552         switch (cmd) {
553         case SIOCADDMULTI:
554         case SIOCDELMULTI:
555                 break;
556
557         case SIOCGDRVSPEC:
558         case SIOCSDRVSPEC:
559                 if (ifd->ifd_cmd >= bridge_control_table_size) {
560                         error = EINVAL;
561                         break;
562                 }
563                 bc = &bridge_control_table[ifd->ifd_cmd];
564
565                 if (cmd == SIOCGDRVSPEC &&
566                     (bc->bc_flags & BC_F_COPYOUT) == 0) {
567                         error = EINVAL;
568                         break;
569                 } else if (cmd == SIOCSDRVSPEC &&
570                     (bc->bc_flags & BC_F_COPYOUT) != 0) {
571                         error = EINVAL;
572                         break;
573                 }
574
575                 if (bc->bc_flags & BC_F_SUSER) {
576                         error = suser_cred(cr, NULL_CRED_OKAY);
577                         if (error)
578                                 break;
579                 }
580
581                 if (ifd->ifd_len != bc->bc_argsize ||
582                     ifd->ifd_len > sizeof(args)) {
583                         error = EINVAL;
584                         break;
585                 }
586
587                 memset(&args, 0, sizeof(args));
588                 if (bc->bc_flags & BC_F_COPYIN) {
589                         error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
590                         if (error)
591                                 break;
592                 }
593
594                 error = bridge_control(sc, bc->bc_func, &args);
595                 if (error)
596                         break;
597
598                 if (bc->bc_flags & BC_F_COPYOUT)
599                         error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
600                 break;
601
602         case SIOCSIFFLAGS:
603                 if (!(ifp->if_flags & IFF_UP) &&
604                     (ifp->if_flags & IFF_RUNNING)) {
605                         /*
606                          * If interface is marked down and it is running,
607                          * then stop it.
608                          */
609                         bridge_stop(ifp);
610                 } else if ((ifp->if_flags & IFF_UP) &&
611                     !(ifp->if_flags & IFF_RUNNING)) {
612                         /*
613                          * If interface is marked up and it is stopped, then
614                          * start it.
615                          */
616                         ifp->if_init(sc);
617                 }
618                 break;
619
620         case SIOCSIFMTU:
621                 /* Do not allow the MTU to be changed on the bridge */
622                 error = EINVAL;
623                 break;
624
625         default:
626                 error = ether_ioctl(ifp, cmd, data);
627                 break;
628         }
629         return (error);
630 }
631
632 /*
633  * bridge_mutecaps:
634  *
635  *      Clear or restore unwanted capabilities on the member interface
636  */
637 static void
638 bridge_mutecaps(struct bridge_iflist *bif, int mute)
639 {
640         struct ifnet *ifp = bif->bif_ifp;
641         struct ifreq ifr;
642         int error;
643
644         if (ifp->if_ioctl == NULL)
645                 return;
646
647         bzero(&ifr, sizeof(ifr));
648         ifr.ifr_reqcap = ifp->if_capenable;
649
650         if (mute) {
651                 /* mask off and save capabilities */
652                 bif->bif_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
653                 if (bif->bif_mutecap != 0)
654                         ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
655         } else
656                 /* restore muted capabilities */
657                 ifr.ifr_reqcap |= bif->bif_mutecap;
658
659         if (bif->bif_mutecap != 0) {
660                 lwkt_serialize_enter(ifp->if_serializer);
661                 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
662                 lwkt_serialize_exit(ifp->if_serializer);
663         }
664 }
665
666 /*
667  * bridge_lookup_member:
668  *
669  *      Lookup a bridge member interface.
670  */
671 static struct bridge_iflist *
672 bridge_lookup_member(struct bridge_softc *sc, const char *name)
673 {
674         struct bridge_iflist *bif;
675         struct ifnet *ifp;
676
677         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
678                 ifp = bif->bif_ifp;
679                 if (strcmp(ifp->if_xname, name) == 0)
680                         return (bif);
681         }
682
683         return (NULL);
684 }
685
686 /*
687  * bridge_lookup_member_if:
688  *
689  *      Lookup a bridge member interface by ifnet*.
690  */
691 static struct bridge_iflist *
692 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
693 {
694         struct bridge_iflist *bif;
695
696         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
697                 if (bif->bif_ifp == member_ifp)
698                         return (bif);
699         }
700
701         return (NULL);
702 }
703
704 /*
705  * bridge_delete_member:
706  *
707  *      Delete the specified member interface.
708  */
709 static void
710 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
711     int gone)
712 {
713         struct ifnet *ifs = bif->bif_ifp;
714
715         if (!gone) {
716                 switch (ifs->if_type) {
717                 case IFT_ETHER:
718                 case IFT_L2VLAN:
719                         /*
720                          * Take the interface out of promiscuous mode.
721                          */
722                         (void) ifpromisc(ifs, 0);
723                         bridge_mutecaps(bif, 0);
724                         break;
725
726                 case IFT_GIF:
727                         break;
728
729                 default:
730 #ifdef DIAGNOSTIC
731                         panic("bridge_delete_member: impossible");
732 #endif
733                         break;
734                 }
735         }
736
737         ifs->if_bridge = NULL;
738         LIST_REMOVE(bif, bif_next);
739
740         bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
741
742         kfree(bif, M_DEVBUF);
743
744         if (sc->sc_ifp->if_flags & IFF_RUNNING)
745                 bstp_initialization(sc);
746 }
747
748 /*
749  * bridge_delete_span:
750  *
751  *      Delete the specified span interface.
752  */
753 static void
754 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
755 {
756         KASSERT(bif->bif_ifp->if_bridge == NULL,
757             ("%s: not a span interface", __func__));
758
759         LIST_REMOVE(bif, bif_next);
760         kfree(bif, M_DEVBUF);
761 }
762
763 static int
764 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
765 {
766         struct ifbreq *req = arg;
767         struct bridge_iflist *bif = NULL;
768         struct ifnet *ifs;
769         int error = 0;
770
771         ifs = ifunit(req->ifbr_ifsname);
772         if (ifs == NULL)
773                 return (ENOENT);
774
775         /* If it's in the span list, it can't be a member. */
776         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
777                 if (ifs == bif->bif_ifp)
778                         return (EBUSY);
779
780         /* Allow the first Ethernet member to define the MTU */
781         if (ifs->if_type != IFT_GIF) {
782                 if (LIST_EMPTY(&sc->sc_iflist))
783                         sc->sc_ifp->if_mtu = ifs->if_mtu;
784                 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
785                         if_printf(sc->sc_ifp, "invalid MTU for %s\n",
786                             ifs->if_xname);
787                         return (EINVAL);
788                 }
789         }
790
791         if (ifs->if_bridge == sc)
792                 return (EEXIST);
793
794         if (ifs->if_bridge != NULL)
795                 return (EBUSY);
796
797         bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
798         bif->bif_ifp = ifs;
799         bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
800         bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
801         bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
802
803         switch (ifs->if_type) {
804         case IFT_ETHER:
805         case IFT_L2VLAN:
806                 /*
807                  * Place the interface into promiscuous mode.
808                  */
809                 error = ifpromisc(ifs, 1);
810                 if (error)
811                         goto out;
812
813                 bridge_mutecaps(bif, 1);
814                 break;
815
816         case IFT_GIF: /* :^) */
817                 break;
818
819         default:
820                 error = EINVAL;
821                 goto out;
822         }
823
824         ifs->if_bridge = sc;
825
826         LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
827
828         if (sc->sc_ifp->if_flags & IFF_RUNNING)
829                 bstp_initialization(sc);
830         else
831                 bstp_stop(sc);
832
833 out:
834         if (error) {
835                 if (bif != NULL)
836                         kfree(bif, M_DEVBUF);
837         }
838         return (error);
839 }
840
841 static int
842 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
843 {
844         struct ifbreq *req = arg;
845         struct bridge_iflist *bif;
846
847         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
848         if (bif == NULL)
849                 return (ENOENT);
850
851         bridge_delete_member(sc, bif, 0);
852
853         return (0);
854 }
855
856 static int
857 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
858 {
859         struct ifbreq *req = arg;
860         struct bridge_iflist *bif;
861
862         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
863         if (bif == NULL)
864                 return (ENOENT);
865
866         req->ifbr_ifsflags = bif->bif_flags;
867         req->ifbr_state = bif->bif_state;
868         req->ifbr_priority = bif->bif_priority;
869         req->ifbr_path_cost = bif->bif_path_cost;
870         req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
871
872         return (0);
873 }
874
875 static int
876 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
877 {
878         struct ifbreq *req = arg;
879         struct bridge_iflist *bif;
880
881         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
882         if (bif == NULL)
883                 return (ENOENT);
884
885         if (req->ifbr_ifsflags & IFBIF_SPAN)
886                 /* SPAN is readonly */
887                 return (EINVAL);
888
889         if (req->ifbr_ifsflags & IFBIF_STP) {
890                 switch (bif->bif_ifp->if_type) {
891                 case IFT_ETHER:
892                         /* These can do spanning tree. */
893                         break;
894
895                 default:
896                         /* Nothing else can. */
897                         return (EINVAL);
898                 }
899         }
900
901         bif->bif_flags = req->ifbr_ifsflags;
902
903         if (sc->sc_ifp->if_flags & IFF_RUNNING)
904                 bstp_initialization(sc);
905
906         return (0);
907 }
908
909 static int
910 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
911 {
912         struct ifbrparam *param = arg;
913
914         sc->sc_brtmax = param->ifbrp_csize;
915         bridge_rttrim(sc);
916
917         return (0);
918 }
919
920 static int
921 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
922 {
923         struct ifbrparam *param = arg;
924
925         param->ifbrp_csize = sc->sc_brtmax;
926
927         return (0);
928 }
929
930 static int
931 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
932 {
933         struct ifbifconf *bifc = arg;
934         struct bridge_iflist *bif;
935         struct ifbreq breq;
936         int count, len, error = 0;
937
938         count = 0;
939         LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
940                 count++;
941         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
942                 count++;
943
944         if (bifc->ifbic_len == 0) {
945                 bifc->ifbic_len = sizeof(breq) * count;
946                 return (0);
947         }
948
949         count = 0;
950         len = bifc->ifbic_len;
951         memset(&breq, 0, sizeof breq);
952         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
953                 if (len < sizeof(breq))
954                         break;
955
956                 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
957                     sizeof(breq.ifbr_ifsname));
958                 breq.ifbr_ifsflags = bif->bif_flags;
959                 breq.ifbr_state = bif->bif_state;
960                 breq.ifbr_priority = bif->bif_priority;
961                 breq.ifbr_path_cost = bif->bif_path_cost;
962                 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
963                 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
964                 if (error)
965                         break;
966                 count++;
967                 len -= sizeof(breq);
968         }
969         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
970                 if (len < sizeof(breq))
971                         break;
972
973                 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
974                     sizeof(breq.ifbr_ifsname));
975                 breq.ifbr_ifsflags = bif->bif_flags;
976                 breq.ifbr_state = bif->bif_state;
977                 breq.ifbr_priority = bif->bif_priority;
978                 breq.ifbr_path_cost = bif->bif_path_cost;
979                 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
980                 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
981                 if (error)
982                         break;
983                 count++;
984                 len -= sizeof(breq);
985         }
986
987         bifc->ifbic_len = sizeof(breq) * count;
988         return (error);
989 }
990
991 static int
992 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
993 {
994         struct ifbaconf *bac = arg;
995         struct bridge_rtnode *brt;
996         struct ifbareq bareq;
997         int count = 0, error = 0, len;
998
999         if (bac->ifbac_len == 0)
1000                 return (0);
1001
1002         len = bac->ifbac_len;
1003         memset(&bareq, 0, sizeof(bareq));
1004         LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1005                 if (len < sizeof(bareq))
1006                         goto out;
1007                 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1008                     sizeof(bareq.ifba_ifsname));
1009                 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1010                 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1011                                 time_second < brt->brt_expire)
1012                         bareq.ifba_expire = brt->brt_expire - time_second;
1013                 else
1014                         bareq.ifba_expire = 0;
1015                 bareq.ifba_flags = brt->brt_flags;
1016
1017                 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1018                 if (error)
1019                         goto out;
1020                 count++;
1021                 len -= sizeof(bareq);
1022         }
1023 out:
1024         bac->ifbac_len = sizeof(bareq) * count;
1025         return (error);
1026 }
1027
1028 static int
1029 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1030 {
1031         struct ifbareq *req = arg;
1032         struct bridge_iflist *bif;
1033         int error;
1034
1035         bif = bridge_lookup_member(sc, req->ifba_ifsname);
1036         if (bif == NULL)
1037                 return (ENOENT);
1038
1039         error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1040             req->ifba_flags);
1041
1042         return (error);
1043 }
1044
1045 static int
1046 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1047 {
1048         struct ifbrparam *param = arg;
1049
1050         sc->sc_brttimeout = param->ifbrp_ctime;
1051
1052         return (0);
1053 }
1054
1055 static int
1056 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1057 {
1058         struct ifbrparam *param = arg;
1059
1060         param->ifbrp_ctime = sc->sc_brttimeout;
1061
1062         return (0);
1063 }
1064
1065 static int
1066 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1067 {
1068         struct ifbareq *req = arg;
1069
1070         return (bridge_rtdaddr(sc, req->ifba_dst));
1071 }
1072
1073 static int
1074 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1075 {
1076         struct ifbreq *req = arg;
1077
1078         bridge_rtflush(sc, req->ifbr_ifsflags);
1079
1080         return (0);
1081 }
1082
1083 static int
1084 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1085 {
1086         struct ifbrparam *param = arg;
1087
1088         param->ifbrp_prio = sc->sc_bridge_priority;
1089
1090         return (0);
1091 }
1092
1093 static int
1094 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1095 {
1096         struct ifbrparam *param = arg;
1097
1098         sc->sc_bridge_priority = param->ifbrp_prio;
1099
1100         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1101                 bstp_initialization(sc);
1102
1103         return (0);
1104 }
1105
1106 static int
1107 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1108 {
1109         struct ifbrparam *param = arg;
1110
1111         param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1112
1113         return (0);
1114 }
1115
1116 static int
1117 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1118 {
1119         struct ifbrparam *param = arg;
1120
1121         if (param->ifbrp_hellotime == 0)
1122                 return (EINVAL);
1123         sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1124
1125         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1126                 bstp_initialization(sc);
1127
1128         return (0);
1129 }
1130
1131 static int
1132 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1133 {
1134         struct ifbrparam *param = arg;
1135
1136         param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1137
1138         return (0);
1139 }
1140
1141 static int
1142 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1143 {
1144         struct ifbrparam *param = arg;
1145
1146         if (param->ifbrp_fwddelay == 0)
1147                 return (EINVAL);
1148         sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1149
1150         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1151                 bstp_initialization(sc);
1152
1153         return (0);
1154 }
1155
1156 static int
1157 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1158 {
1159         struct ifbrparam *param = arg;
1160
1161         param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1162
1163         return (0);
1164 }
1165
1166 static int
1167 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1168 {
1169         struct ifbrparam *param = arg;
1170
1171         if (param->ifbrp_maxage == 0)
1172                 return (EINVAL);
1173         sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1174
1175         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1176                 bstp_initialization(sc);
1177
1178         return (0);
1179 }
1180
1181 static int
1182 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1183 {
1184         struct ifbreq *req = arg;
1185         struct bridge_iflist *bif;
1186
1187         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1188         if (bif == NULL)
1189                 return (ENOENT);
1190
1191         bif->bif_priority = req->ifbr_priority;
1192
1193         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1194                 bstp_initialization(sc);
1195
1196         return (0);
1197 }
1198
1199 static int
1200 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1201 {
1202         struct ifbreq *req = arg;
1203         struct bridge_iflist *bif;
1204
1205         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1206         if (bif == NULL)
1207                 return (ENOENT);
1208
1209         bif->bif_path_cost = req->ifbr_path_cost;
1210
1211         if (sc->sc_ifp->if_flags & IFF_RUNNING)
1212                 bstp_initialization(sc);
1213
1214         return (0);
1215 }
1216
1217 static int
1218 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1219 {
1220         struct ifbreq *req = arg;
1221         struct bridge_iflist *bif = NULL;
1222         struct ifnet *ifs;
1223
1224         ifs = ifunit(req->ifbr_ifsname);
1225         if (ifs == NULL)
1226                 return (ENOENT);
1227
1228         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1229                 if (ifs == bif->bif_ifp)
1230                         return (EBUSY);
1231
1232         if (ifs->if_bridge != NULL)
1233                 return (EBUSY);
1234
1235         switch (ifs->if_type) {
1236                 case IFT_ETHER:
1237                 case IFT_GIF:
1238                 case IFT_L2VLAN:
1239                         break;
1240                 default:
1241                         return (EINVAL);
1242         }
1243
1244         bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK|M_ZERO);
1245
1246         bif->bif_ifp = ifs;
1247         bif->bif_flags = IFBIF_SPAN;
1248
1249         LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1250
1251         return (0);
1252 }
1253
1254 static int
1255 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1256 {
1257         struct ifbreq *req = arg;
1258         struct bridge_iflist *bif;
1259         struct ifnet *ifs;
1260
1261         ifs = ifunit(req->ifbr_ifsname);
1262         if (ifs == NULL)
1263                 return (ENOENT);
1264
1265         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1266                 if (ifs == bif->bif_ifp)
1267                         break;
1268
1269         if (bif == NULL)
1270                 return (ENOENT);
1271
1272         bridge_delete_span(sc, bif);
1273
1274         return (0);
1275 }
1276
1277 /*
1278  * bridge_ifdetach:
1279  *
1280  *      Detach an interface from a bridge.  Called when a member
1281  *      interface is detaching.
1282  */
1283 static void
1284 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1285 {
1286         struct bridge_softc *sc = ifp->if_bridge;
1287         struct bridge_iflist *bif;
1288
1289         /* Check if the interface is a bridge member */
1290         if (sc != NULL) {
1291                 lwkt_serialize_enter(ifp->if_serializer);
1292
1293                 bif = bridge_lookup_member_if(sc, ifp);
1294                 if (bif != NULL)
1295                         bridge_delete_member(sc, bif, 1);
1296
1297                 lwkt_serialize_exit(ifp->if_serializer);
1298                 return;
1299         }
1300
1301         /* Check if the interface is a span port */
1302         LIST_FOREACH(sc, &bridge_list, sc_list) {
1303                 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1304                         if (ifp == bif->bif_ifp) {
1305                                 bridge_delete_span(sc, bif);
1306                                 break;
1307                         }
1308         }
1309 }
1310
1311 /*
1312  * bridge_init:
1313  *
1314  *      Initialize a bridge interface.
1315  */
1316 static void
1317 bridge_init(void *xsc)
1318 {
1319         struct bridge_softc *sc = (struct bridge_softc *)xsc;
1320         struct ifnet *ifp = sc->sc_ifp;
1321
1322         ASSERT_SERIALIZED(ifp->if_serializer);
1323
1324         if (ifp->if_flags & IFF_RUNNING)
1325                 return;
1326
1327         callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1328             bridge_timer, sc);
1329
1330         ifp->if_flags |= IFF_RUNNING;
1331         bstp_initialization(sc);
1332         return;
1333 }
1334
1335 /*
1336  * bridge_stop:
1337  *
1338  *      Stop the bridge interface.
1339  */
1340 static void
1341 bridge_stop(struct ifnet *ifp)
1342 {
1343         struct bridge_softc *sc = ifp->if_softc;
1344
1345         ASSERT_SERIALIZED(ifp->if_serializer);
1346
1347         if ((ifp->if_flags & IFF_RUNNING) == 0)
1348                 return;
1349
1350         callout_stop(&sc->sc_brcallout);
1351         bstp_stop(sc);
1352
1353         bridge_rtflush(sc, IFBF_FLUSHDYN);
1354
1355         ifp->if_flags &= ~IFF_RUNNING;
1356 }
1357
1358 static void
1359 bridge_enqueue_internal(struct ifnet *dst_ifp, struct mbuf *m,
1360                         netisr_fn_t handler)
1361 {
1362         struct netmsg_packet *nmp;
1363         lwkt_port_t port;
1364         int cpu = mycpu->gd_cpuid;
1365
1366         while (m->m_type == MT_TAG) {
1367                 /* XXX see ether_output_frame for full rules check */
1368                 m = m->m_next;
1369         }
1370
1371         nmp = &m->m_hdr.mh_netmsg;
1372         netmsg_init(&nmp->nm_netmsg, &netisr_apanic_rport, 0, handler);
1373         nmp->nm_packet = m;
1374         nmp->nm_netmsg.nm_lmsg.u.ms_resultp = dst_ifp;
1375
1376         port = cpu_portfn(cpu);
1377         lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
1378 }
1379
1380 static void
1381 bridge_pfil_enqueue(struct ifnet *dst_ifp, struct mbuf *m,
1382                     int runfilt)
1383 {
1384         netisr_fn_t handler;
1385
1386         if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1387 #ifdef INET6
1388             || inet6_pfil_hook.ph_hashooks > 0
1389 #endif
1390             )) {
1391                 handler = bridge_pfil_enqueue_handler;
1392         } else {
1393                 handler = bridge_enqueue_handler;
1394         }
1395         bridge_enqueue_internal(dst_ifp, m, handler);
1396 }
1397
1398 /*
1399  * bridge_enqueue:
1400  *
1401  *      Enqueue a packet on a bridge member interface.
1402  *
1403  */
1404 void
1405 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1406 {
1407         bridge_enqueue_internal(dst_ifp, m, bridge_enqueue_handler);
1408 }
1409
1410 /*
1411  * bridge_output:
1412  *
1413  *      Send output from a bridge member interface.  This
1414  *      performs the bridging function for locally originated
1415  *      packets.
1416  *
1417  *      The mbuf has the Ethernet header already attached.  We must
1418  *      enqueue or free the mbuf before returning.
1419  */
1420 static int
1421 bridge_output(struct ifnet *ifp, struct mbuf *m)
1422 {
1423         struct bridge_softc *sc = ifp->if_bridge;
1424         struct ether_header *eh;
1425         struct ifnet *dst_if;
1426
1427         ASSERT_NOT_SERIALIZED(ifp->if_serializer);
1428
1429         /*
1430          * Make sure that we are still a member of a bridge interface.
1431          */
1432         if (sc == NULL) {
1433                 m_freem(m);
1434                 return (0);
1435         }
1436
1437         if (m->m_len < ETHER_HDR_LEN) {
1438                 m = m_pullup(m, ETHER_HDR_LEN);
1439                 if (m == NULL)
1440                         return (0);
1441         }
1442
1443         /* Serialize our bridge interface. */
1444         lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1445
1446         eh = mtod(m, struct ether_header *);
1447
1448         /*
1449          * If bridge is down, but the original output interface is up,
1450          * go ahead and send out that interface.  Otherwise, the packet
1451          * is dropped below.
1452          */
1453         if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
1454                 dst_if = ifp;
1455                 goto sendunicast;
1456         }
1457
1458         /*
1459          * If the packet is a multicast, or we don't know a better way to
1460          * get there, send to all interfaces.
1461          */
1462         if (ETHER_IS_MULTICAST(eh->ether_dhost))
1463                 dst_if = NULL;
1464         else
1465                 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1466         if (dst_if == NULL) {
1467                 struct bridge_iflist *bif;
1468                 struct mbuf *mc;
1469                 int used = 0;
1470
1471                 bridge_span(sc, m);
1472
1473                 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1474                         dst_if = bif->bif_ifp;
1475                         if ((dst_if->if_flags & IFF_RUNNING) == 0)
1476                                 continue;
1477
1478                         /*
1479                          * If this is not the original output interface,
1480                          * and the interface is participating in spanning
1481                          * tree, make sure the port is in a state that
1482                          * allows forwarding.
1483                          */
1484                         if (dst_if != ifp &&
1485                             (bif->bif_flags & IFBIF_STP) != 0) {
1486                                 switch (bif->bif_state) {
1487                                 case BSTP_IFSTATE_BLOCKING:
1488                                 case BSTP_IFSTATE_LISTENING:
1489                                 case BSTP_IFSTATE_DISABLED:
1490                                         continue;
1491                                 }
1492                         }
1493
1494                         if (LIST_NEXT(bif, bif_next) == NULL) {
1495                                 used = 1;
1496                                 mc = m;
1497                         } else {
1498                                 mc = m_copypacket(m, MB_DONTWAIT);
1499                                 if (mc == NULL) {
1500                                         sc->sc_ifp->if_oerrors++;
1501                                         continue;
1502                                 }
1503                         }
1504                         bridge_enqueue(dst_if, mc);
1505                 }
1506                 if (used == 0)
1507                         m_freem(m);
1508                 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1509                 return (0);
1510         }
1511
1512 sendunicast:
1513         /*
1514          * XXX Spanning tree consideration here?
1515          */
1516
1517         bridge_span(sc, m);
1518         lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1519         if ((dst_if->if_flags & IFF_RUNNING) == 0)
1520                 m_freem(m);
1521         else
1522                 bridge_enqueue(dst_if, m);
1523         return (0);
1524 }
1525
1526 /*
1527  * bridge_start:
1528  *
1529  *      Start output on a bridge.
1530  *
1531  */
1532 static void
1533 bridge_start(struct ifnet *ifp)
1534 {
1535         struct bridge_softc *sc = ifp->if_softc;
1536
1537         ASSERT_SERIALIZED(ifp->if_serializer);
1538
1539         ifp->if_flags |= IFF_OACTIVE;
1540         for (;;) {
1541                 struct ifnet *dst_if = NULL;
1542                 struct ether_header *eh;
1543                 struct mbuf *m;
1544
1545                 m = ifq_dequeue(&ifp->if_snd, NULL);
1546                 if (m == NULL)
1547                         break;
1548
1549                 if (m->m_len < sizeof(*eh)) {
1550                         m = m_pullup(m, sizeof(*eh));
1551                         if (m == NULL) {
1552                                 ifp->if_oerrors++;
1553                                 continue;
1554                         }
1555                 }
1556                 eh = mtod(m, struct ether_header *);
1557
1558                 BPF_MTAP(ifp, m);
1559                 ifp->if_opackets++;
1560
1561                 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1562                         dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1563
1564                 if (dst_if == NULL)
1565                         bridge_broadcast(sc, ifp, m, 0);
1566                 else
1567                         bridge_enqueue(dst_if, m);
1568         }
1569         ifp->if_flags &= ~IFF_OACTIVE;
1570 }
1571
1572 /*
1573  * bridge_forward:
1574  *
1575  *      The forwarding function of the bridge.
1576  */
1577 static void
1578 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1579 {
1580         struct bridge_iflist *bif;
1581         struct ifnet *src_if, *dst_if, *ifp;
1582         struct ether_header *eh;
1583
1584         src_if = m->m_pkthdr.rcvif;
1585         ifp = sc->sc_ifp;
1586
1587         ASSERT_SERIALIZED(ifp->if_serializer);
1588
1589         ifp->if_ipackets++;
1590         ifp->if_ibytes += m->m_pkthdr.len;
1591
1592         /*
1593          * Look up the bridge_iflist.
1594          */
1595         bif = bridge_lookup_member_if(sc, src_if);
1596         if (bif == NULL) {
1597                 /* Interface is not a bridge member (anymore?) */
1598                 m_freem(m);
1599                 return;
1600         }
1601
1602         if (bif->bif_flags & IFBIF_STP) {
1603                 switch (bif->bif_state) {
1604                 case BSTP_IFSTATE_BLOCKING:
1605                 case BSTP_IFSTATE_LISTENING:
1606                 case BSTP_IFSTATE_DISABLED:
1607                         m_freem(m);
1608                         return;
1609                 }
1610         }
1611
1612         eh = mtod(m, struct ether_header *);
1613
1614         /*
1615          * If the interface is learning, and the source
1616          * address is valid and not multicast, record
1617          * the address.
1618          */
1619         if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1620             ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1621             (eh->ether_shost[0] == 0 &&
1622              eh->ether_shost[1] == 0 &&
1623              eh->ether_shost[2] == 0 &&
1624              eh->ether_shost[3] == 0 &&
1625              eh->ether_shost[4] == 0 &&
1626              eh->ether_shost[5] == 0) == 0) {
1627                 bridge_rtupdate(sc, eh->ether_shost, src_if, 0, IFBAF_DYNAMIC);
1628         }
1629
1630         if ((bif->bif_flags & IFBIF_STP) != 0 &&
1631             bif->bif_state == BSTP_IFSTATE_LEARNING) {
1632                 m_freem(m);
1633                 return;
1634         }
1635
1636         /*
1637          * At this point, the port either doesn't participate
1638          * in spanning tree or it is in the forwarding state.
1639          */
1640
1641         /*
1642          * If the packet is unicast, destined for someone on
1643          * "this" side of the bridge, drop it.
1644          */
1645         if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1646                 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1647                 if (src_if == dst_if) {
1648                         m_freem(m);
1649                         return;
1650                 }
1651         } else {
1652                 /* ...forward it to all interfaces. */
1653                 sc->sc_ifp->if_imcasts++;
1654                 dst_if = NULL;
1655         }
1656
1657         if (dst_if == NULL) {
1658                 bridge_broadcast(sc, src_if, m, 1);
1659                 return;
1660         }
1661
1662         /*
1663          * At this point, we're dealing with a unicast frame
1664          * going to a different interface.
1665          */
1666         if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1667                 m_freem(m);
1668                 return;
1669         }
1670         bif = bridge_lookup_member_if(sc, dst_if);
1671         if (bif == NULL) {
1672                 /* Not a member of the bridge (anymore?) */
1673                 m_freem(m);
1674                 return;
1675         }
1676
1677         if (bif->bif_flags & IFBIF_STP) {
1678                 switch (bif->bif_state) {
1679                 case BSTP_IFSTATE_DISABLED:
1680                 case BSTP_IFSTATE_BLOCKING:
1681                         m_freem(m);
1682                         return;
1683                 }
1684         }
1685
1686         lwkt_serialize_exit(ifp->if_serializer);
1687
1688         /* run the packet filter */
1689         if (inet_pfil_hook.ph_hashooks > 0
1690 #ifdef INET6
1691             || inet6_pfil_hook.ph_hashooks > 0
1692 #endif
1693             ) {
1694                 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1695                         goto done;
1696                 if (m == NULL)
1697                         goto done;
1698
1699                 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
1700                         goto done;
1701                 if (m == NULL)
1702                         goto done;
1703         }
1704         bridge_handoff(dst_if, m);
1705
1706         /*
1707          * ifp's serializer was held on entry and is expected to be held
1708          * on return.
1709          */
1710 done:
1711         lwkt_serialize_enter(ifp->if_serializer);
1712 }
1713
1714 /*
1715  * bridge_input:
1716  *
1717  *      Receive input from a member interface.  Queue the packet for
1718  *      bridging if it is not for us.
1719  */
1720 static struct mbuf *
1721 bridge_input(struct ifnet *ifp, struct mbuf *m)
1722 {
1723         struct bridge_softc *sc = ifp->if_bridge;
1724         struct bridge_iflist *bif;
1725         struct ifnet *bifp, *new_ifp;
1726         struct ether_header *eh;
1727         struct mbuf *mc, *mc2;
1728
1729         /*
1730          * Make sure that we are still a member of a bridge interface.
1731          */
1732         if (sc == NULL)
1733                 return m;
1734
1735         new_ifp = NULL;
1736         bifp = sc->sc_ifp;
1737
1738         lwkt_serialize_enter(bifp->if_serializer);
1739
1740         if ((bifp->if_flags & IFF_RUNNING) == 0)
1741                 goto out;
1742
1743         /*
1744          * Implement support for bridge monitoring. If this flag has been
1745          * set on this interface, discard the packet once we push it through
1746          * the bpf(4) machinery, but before we do, increment the byte and
1747          * packet counters associated with this interface.
1748          */
1749          if ((bifp->if_flags & IFF_MONITOR) != 0) {
1750                 m->m_pkthdr.rcvif = bifp;
1751                 BPF_MTAP(bifp, m);
1752                 bifp->if_ipackets++;
1753                 bifp->if_ibytes += m->m_pkthdr.len;
1754                 m_freem(m);
1755                 m = NULL;
1756                 goto out;
1757         }
1758
1759         eh = mtod(m, struct ether_header *);
1760
1761         m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
1762
1763         if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
1764                 /*
1765                  * If the packet is for us, set the packets source as the
1766                  * bridge, and return the packet back to ifnet.if_input for
1767                  * local processing.
1768                  */
1769                 KASSERT(bifp->if_bridge == NULL,
1770                         ("loop created in bridge_input"));
1771                 new_ifp = bifp;
1772                 goto out;
1773         }
1774
1775         /*
1776          * Tap all packets arriving on the bridge, no matter if
1777          * they are local destinations or not.  In is in.
1778          */
1779         BPF_MTAP(bifp, m);
1780
1781         bif = bridge_lookup_member_if(sc, ifp);
1782         if (bif == NULL)
1783                 goto out;
1784
1785         bridge_span(sc, m);
1786
1787         if (m->m_flags & (M_BCAST | M_MCAST)) {
1788                 /* Tap off 802.1D packets; they do not get forwarded. */
1789                 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1790                     ETHER_ADDR_LEN) == 0) {
1791                         m = bstp_input(sc, bif, m);
1792                         KASSERT(m == NULL,
1793                                 ("attempt to deliver 802.1D packet\n"));
1794                         goto out;
1795                 }
1796
1797                 if (bif->bif_flags & IFBIF_STP) {
1798                         switch (bif->bif_state) {
1799                         case BSTP_IFSTATE_BLOCKING:
1800                         case BSTP_IFSTATE_LISTENING:
1801                         case BSTP_IFSTATE_DISABLED:
1802                                 goto out;
1803                         }
1804                 }
1805
1806                 /*
1807                  * Make a deep copy of the packet and enqueue the copy
1808                  * for bridge processing; return the original packet for
1809                  * local processing.
1810                  */
1811                 mc = m_dup(m, MB_DONTWAIT);
1812                 if (mc == NULL)
1813                         goto out;
1814
1815                 bridge_forward(sc, mc);
1816
1817                 /*
1818                  * Reinject the mbuf as arriving on the bridge so we have a
1819                  * chance at claiming multicast packets. We can not loop back
1820                  * here from ether_input as a bridge is never a member of a
1821                  * bridge.
1822                  */
1823                 KASSERT(bifp->if_bridge == NULL,
1824                         ("loop created in bridge_input"));
1825                 mc2 = m_dup(m, MB_DONTWAIT);
1826 #ifdef notyet
1827                 if (mc2 != NULL) {
1828                         /* Keep the layer3 header aligned */
1829                         int i = min(mc2->m_pkthdr.len, max_protohdr);
1830                         mc2 = m_copyup(mc2, i, ETHER_ALIGN);
1831                 }
1832 #endif
1833                 if (mc2 != NULL) {
1834                         mc2->m_pkthdr.rcvif = bifp;
1835                         bifp->if_ipackets++;
1836                         bifp->if_input(bifp, mc2);
1837                 }
1838
1839                 /* Return the original packet for local processing. */
1840                 goto out;
1841         }
1842
1843         if (bif->bif_flags & IFBIF_STP) {
1844                 switch (bif->bif_state) {
1845                 case BSTP_IFSTATE_BLOCKING:
1846                 case BSTP_IFSTATE_LISTENING:
1847                 case BSTP_IFSTATE_DISABLED:
1848                         goto out;
1849                 }
1850         }
1851
1852         /*
1853          * Unicast.  Make sure it's not for us.
1854          */
1855         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1856                 if (bif->bif_ifp->if_type != IFT_ETHER)
1857                         continue;
1858
1859                 /* It is destined for us. */
1860                 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1861                     ETHER_ADDR_LEN) == 0) {
1862                         if (bif->bif_flags & IFBIF_LEARNING) {
1863                                 bridge_rtupdate(sc,
1864                                     eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1865                         }
1866
1867                         if (bif->bif_ifp != ifp) {
1868                                 /* XXX loop prevention */
1869                                 m->m_flags |= M_PROTO1;
1870                                 new_ifp = bif->bif_ifp;
1871                         }
1872                         goto out;
1873                 }
1874
1875                 /* We just received a packet that we sent out. */
1876                 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1877                     ETHER_ADDR_LEN) == 0) {
1878                         m_freem(m);
1879                         m = NULL;
1880                         goto out;
1881                 }
1882         }
1883
1884         /* Perform the bridge forwarding function. */
1885         bridge_forward(sc, m);
1886         m = NULL;
1887 out:
1888         lwkt_serialize_exit(bifp->if_serializer);
1889
1890         if (new_ifp != NULL) {
1891                 lwkt_serialize_enter(new_ifp->if_serializer);
1892
1893                 m->m_pkthdr.rcvif = new_ifp;
1894                 new_ifp->if_ipackets++;
1895                 new_ifp->if_input(new_ifp, m);
1896                 m = NULL;
1897
1898                 lwkt_serialize_exit(new_ifp->if_serializer);
1899         }
1900         return (m);
1901 }
1902
1903 /*
1904  * bridge_broadcast:
1905  *
1906  *      Send a frame to all interfaces that are members of
1907  *      the bridge, except for the one on which the packet
1908  *      arrived.
1909  */
1910 static void
1911 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1912     struct mbuf *m, int runfilt)
1913 {
1914         struct bridge_iflist *bif;
1915         struct mbuf *mc;
1916         struct ifnet *dst_if, *bifp;
1917         int used = 0;
1918
1919         bifp = sc->sc_ifp;
1920
1921         ASSERT_SERIALIZED(bifp->if_serializer);
1922
1923         /* run the packet filter */
1924         if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1925 #ifdef INET6
1926             || inet6_pfil_hook.ph_hashooks > 0
1927 #endif
1928             )) {
1929                 lwkt_serialize_exit(bifp->if_serializer);
1930
1931                 /* Filter on the bridge interface before broadcasting */
1932
1933                 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
1934                         goto filt;
1935                 if (m == NULL)
1936                         goto filt;
1937
1938                 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
1939                         m = NULL;
1940 filt:
1941                 lwkt_serialize_enter(bifp->if_serializer);
1942                 if (m == NULL)
1943                         return;
1944         }
1945
1946         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1947                 dst_if = bif->bif_ifp;
1948                 if (dst_if == src_if)
1949                         continue;
1950
1951                 if (bif->bif_flags & IFBIF_STP) {
1952                         switch (bif->bif_state) {
1953                         case BSTP_IFSTATE_BLOCKING:
1954                         case BSTP_IFSTATE_DISABLED:
1955                                 continue;
1956                         }
1957                 }
1958
1959                 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1960                     (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1961                         continue;
1962
1963                 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1964                         continue;
1965
1966                 if (LIST_NEXT(bif, bif_next) == NULL) {
1967                         mc = m;
1968                         used = 1;
1969                 } else {
1970                         mc = m_copypacket(m, MB_DONTWAIT);
1971                         if (mc == NULL) {
1972                                 sc->sc_ifp->if_oerrors++;
1973                                 continue;
1974                         }
1975                 }
1976                 bridge_pfil_enqueue(dst_if, mc, runfilt);
1977         }
1978         if (used == 0)
1979                 m_freem(m);
1980 }
1981
1982 /*
1983  * bridge_span:
1984  *
1985  *      Duplicate a packet out one or more interfaces that are in span mode,
1986  *      the original mbuf is unmodified.
1987  */
1988 static void
1989 bridge_span(struct bridge_softc *sc, struct mbuf *m)
1990 {
1991         struct bridge_iflist *bif;
1992         struct ifnet *dst_if;
1993         struct mbuf *mc;
1994
1995         if (LIST_EMPTY(&sc->sc_spanlist))
1996                 return;
1997
1998         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1999                 dst_if = bif->bif_ifp;
2000
2001                 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2002                         continue;
2003
2004                 mc = m_copypacket(m, MB_DONTWAIT);
2005                 if (mc == NULL) {
2006                         sc->sc_ifp->if_oerrors++;
2007                         continue;
2008                 }
2009
2010                 bridge_enqueue(dst_if, mc);
2011         }
2012 }
2013
2014 /*
2015  * bridge_rtupdate:
2016  *
2017  *      Add a bridge routing entry.
2018  *      Can be called from interrupt context.
2019  */
2020 static int
2021 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2022     struct ifnet *dst_if, int setflags, uint8_t flags)
2023 {
2024         struct bridge_rtnode *brt;
2025         int error;
2026
2027         /*
2028          * A route for this destination might already exist.  If so,
2029          * update it, otherwise create a new one.
2030          */
2031         if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2032                 if (sc->sc_brtcnt >= sc->sc_brtmax)
2033                         return (ENOSPC);
2034
2035                 /*
2036                  * Allocate a new bridge forwarding node, and
2037                  * initialize the expiration time and Ethernet
2038                  * address.
2039                  */
2040                 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2041                               M_INTNOWAIT|M_ZERO);
2042                 if (brt == NULL)
2043                         return (ENOMEM);
2044
2045                 brt->brt_flags = IFBAF_DYNAMIC;
2046                 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2047
2048                 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2049                         kfree(brt, M_DEVBUF);
2050                         return (error);
2051                 }
2052         }
2053
2054         if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2055                 brt->brt_ifp = dst_if;
2056         if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2057                 brt->brt_expire = time_second + sc->sc_brttimeout;
2058         if (setflags)
2059                 brt->brt_flags = flags;
2060
2061         return (0);
2062 }
2063
2064 /*
2065  * bridge_rtlookup:
2066  *
2067  *      Lookup the destination interface for an address.
2068  */
2069 static struct ifnet *
2070 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2071 {
2072         struct bridge_rtnode *brt;
2073
2074         if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2075                 return (NULL);
2076
2077         return (brt->brt_ifp);
2078 }
2079
2080 /*
2081  * bridge_rttrim:
2082  *
2083  *      Trim the routine table so that we have a number
2084  *      of routing entries less than or equal to the
2085  *      maximum number.
2086  */
2087 static void
2088 bridge_rttrim(struct bridge_softc *sc)
2089 {
2090         struct bridge_rtnode *brt, *nbrt;
2091
2092         /* Make sure we actually need to do this. */
2093         if (sc->sc_brtcnt <= sc->sc_brtmax)
2094                 return;
2095
2096         /* Force an aging cycle; this might trim enough addresses. */
2097         bridge_rtage(sc);
2098         if (sc->sc_brtcnt <= sc->sc_brtmax)
2099                 return;
2100
2101         for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2102                 nbrt = LIST_NEXT(brt, brt_list);
2103                 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2104                         bridge_rtnode_destroy(sc, brt);
2105                         if (sc->sc_brtcnt <= sc->sc_brtmax)
2106                                 return;
2107                 }
2108         }
2109 }
2110
2111 /*
2112  * bridge_timer:
2113  *
2114  *      Aging timer for the bridge.
2115  */
2116 static void
2117 bridge_timer(void *arg)
2118 {
2119         struct bridge_softc *sc = arg;
2120
2121         lwkt_serialize_enter(sc->sc_ifp->if_serializer);
2122
2123         bridge_rtage(sc);
2124
2125         if (sc->sc_ifp->if_flags & IFF_RUNNING)
2126                 callout_reset(&sc->sc_brcallout,
2127                     bridge_rtable_prune_period * hz, bridge_timer, sc);
2128
2129         lwkt_serialize_exit(sc->sc_ifp->if_serializer);
2130 }
2131
2132 /*
2133  * bridge_rtage:
2134  *
2135  *      Perform an aging cycle.
2136  */
2137 static void
2138 bridge_rtage(struct bridge_softc *sc)
2139 {
2140         struct bridge_rtnode *brt, *nbrt;
2141
2142         for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2143                 nbrt = LIST_NEXT(brt, brt_list);
2144                 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2145                         if (time_second >= brt->brt_expire)
2146                                 bridge_rtnode_destroy(sc, brt);
2147                 }
2148         }
2149 }
2150
2151 /*
2152  * bridge_rtflush:
2153  *
2154  *      Remove all dynamic addresses from the bridge.
2155  */
2156 static void
2157 bridge_rtflush(struct bridge_softc *sc, int full)
2158 {
2159         struct bridge_rtnode *brt, *nbrt;
2160
2161         for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2162                 nbrt = LIST_NEXT(brt, brt_list);
2163                 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2164                         bridge_rtnode_destroy(sc, brt);
2165         }
2166 }
2167
2168 /*
2169  * bridge_rtdaddr:
2170  *
2171  *      Remove an address from the table.
2172  */
2173 static int
2174 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2175 {
2176         struct bridge_rtnode *brt;
2177
2178         if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2179                 return (ENOENT);
2180
2181         bridge_rtnode_destroy(sc, brt);
2182         return (0);
2183 }
2184
2185 /*
2186  * bridge_rtdelete:
2187  *
2188  *      Delete routes to a speicifc member interface.
2189  */
2190 void
2191 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2192 {
2193         struct bridge_rtnode *brt, *nbrt;
2194
2195         for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2196                 nbrt = LIST_NEXT(brt, brt_list);
2197                 if (brt->brt_ifp == ifp && (full ||
2198                             (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2199                         bridge_rtnode_destroy(sc, brt);
2200         }
2201 }
2202
2203 /*
2204  * bridge_rtable_init:
2205  *
2206  *      Initialize the route table for this bridge.
2207  */
2208 static int
2209 bridge_rtable_init(struct bridge_softc *sc)
2210 {
2211         int i;
2212
2213         sc->sc_rthash = kmalloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2214             M_DEVBUF, M_WAITOK);
2215
2216         for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2217                 LIST_INIT(&sc->sc_rthash[i]);
2218
2219         sc->sc_rthash_key = karc4random();
2220
2221         LIST_INIT(&sc->sc_rtlist);
2222
2223         return (0);
2224 }
2225
2226 /*
2227  * bridge_rtable_fini:
2228  *
2229  *      Deconstruct the route table for this bridge.
2230  */
2231 static void
2232 bridge_rtable_fini(struct bridge_softc *sc)
2233 {
2234
2235         kfree(sc->sc_rthash, M_DEVBUF);
2236 }
2237
2238 /*
2239  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2240  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2241  */
2242 #define mix(a, b, c)                                                    \
2243 do {                                                                    \
2244         a -= b; a -= c; a ^= (c >> 13);                                 \
2245         b -= c; b -= a; b ^= (a << 8);                                  \
2246         c -= a; c -= b; c ^= (b >> 13);                                 \
2247         a -= b; a -= c; a ^= (c >> 12);                                 \
2248         b -= c; b -= a; b ^= (a << 16);                                 \
2249         c -= a; c -= b; c ^= (b >> 5);                                  \
2250         a -= b; a -= c; a ^= (c >> 3);                                  \
2251         b -= c; b -= a; b ^= (a << 10);                                 \
2252         c -= a; c -= b; c ^= (b >> 15);                                 \
2253 } while (/*CONSTCOND*/0)
2254
2255 static __inline uint32_t
2256 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2257 {
2258         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2259
2260         b += addr[5] << 8;
2261         b += addr[4];
2262         a += addr[3] << 24;
2263         a += addr[2] << 16;
2264         a += addr[1] << 8;
2265         a += addr[0];
2266
2267         mix(a, b, c);
2268
2269         return (c & BRIDGE_RTHASH_MASK);
2270 }
2271
2272 #undef mix
2273
2274 static int
2275 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2276 {
2277         int i, d;
2278
2279         for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2280                 d = ((int)a[i]) - ((int)b[i]);
2281         }
2282
2283         return (d);
2284 }
2285
2286 /*
2287  * bridge_rtnode_lookup:
2288  *
2289  *      Look up a bridge route node for the specified destination.
2290  */
2291 static struct bridge_rtnode *
2292 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2293 {
2294         struct bridge_rtnode *brt;
2295         uint32_t hash;
2296         int dir;
2297
2298         hash = bridge_rthash(sc, addr);
2299         LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2300                 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2301                 if (dir == 0)
2302                         return (brt);
2303                 if (dir > 0)
2304                         return (NULL);
2305         }
2306
2307         return (NULL);
2308 }
2309
2310 /*
2311  * bridge_rtnode_insert:
2312  *
2313  *      Insert the specified bridge node into the route table.  We
2314  *      assume the entry is not already in the table.
2315  */
2316 static int
2317 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2318 {
2319         struct bridge_rtnode *lbrt;
2320         uint32_t hash;
2321         int dir;
2322
2323         hash = bridge_rthash(sc, brt->brt_addr);
2324
2325         lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2326         if (lbrt == NULL) {
2327                 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2328                 goto out;
2329         }
2330
2331         do {
2332                 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2333                 if (dir == 0)
2334                         return (EEXIST);
2335                 if (dir > 0) {
2336                         LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2337                         goto out;
2338                 }
2339                 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2340                         LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2341                         goto out;
2342                 }
2343                 lbrt = LIST_NEXT(lbrt, brt_hash);
2344         } while (lbrt != NULL);
2345
2346 #ifdef DIAGNOSTIC
2347         panic("bridge_rtnode_insert: impossible");
2348 #endif
2349
2350 out:
2351         LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2352         sc->sc_brtcnt++;
2353
2354         return (0);
2355 }
2356
2357 /*
2358  * bridge_rtnode_destroy:
2359  *
2360  *      Destroy a bridge rtnode.
2361  */
2362 static void
2363 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2364 {
2365
2366         LIST_REMOVE(brt, brt_hash);
2367
2368         LIST_REMOVE(brt, brt_list);
2369         sc->sc_brtcnt--;
2370         kfree(brt, M_DEVBUF);
2371 }
2372
2373 /*
2374  * Send bridge packets through pfil if they are one of the types pfil can deal
2375  * with, or if they are ARP or REVARP.  (pfil will pass ARP and REVARP without
2376  * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2377  * that interface.
2378  */
2379 static int
2380 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2381 {
2382         int snap, error, i, hlen;
2383         struct ether_header *eh1, eh2;
2384         struct ip *ip;
2385         struct llc llc1;
2386         u_int16_t ether_type;
2387
2388         snap = 0;
2389         error = -1;     /* Default error if not error == 0 */
2390
2391         if (pfil_bridge == 0 && pfil_member == 0)
2392                 return (0); /* filtering is disabled */
2393
2394         i = min((*mp)->m_pkthdr.len, max_protohdr);
2395         if ((*mp)->m_len < i) {
2396             *mp = m_pullup(*mp, i);
2397             if (*mp == NULL) {
2398                 kprintf("%s: m_pullup failed\n", __func__);
2399                 return (-1);
2400             }
2401         }
2402
2403         eh1 = mtod(*mp, struct ether_header *);
2404         ether_type = ntohs(eh1->ether_type);
2405
2406         /*
2407          * Check for SNAP/LLC.
2408          */
2409         if (ether_type < ETHERMTU) {
2410                 struct llc *llc2 = (struct llc *)(eh1 + 1);
2411
2412                 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2413                     llc2->llc_dsap == LLC_SNAP_LSAP &&
2414                     llc2->llc_ssap == LLC_SNAP_LSAP &&
2415                     llc2->llc_control == LLC_UI) {
2416                         ether_type = htons(llc2->llc_un.type_snap.ether_type);
2417                         snap = 1;
2418                 }
2419         }
2420
2421         /*
2422          * If we're trying to filter bridge traffic, don't look at anything
2423          * other than IP and ARP traffic.  If the filter doesn't understand
2424          * IPv6, don't allow IPv6 through the bridge either.  This is lame
2425          * since if we really wanted, say, an AppleTalk filter, we are hosed,
2426          * but of course we don't have an AppleTalk filter to begin with.
2427          * (Note that since pfil doesn't understand ARP it will pass *ALL*
2428          * ARP traffic.)
2429          */
2430         switch (ether_type) {
2431                 case ETHERTYPE_ARP:
2432                 case ETHERTYPE_REVARP:
2433                         return (0); /* Automatically pass */
2434                 case ETHERTYPE_IP:
2435 #ifdef INET6
2436                 case ETHERTYPE_IPV6:
2437 #endif /* INET6 */
2438                         break;
2439                 default:
2440                         /*
2441                          * Check to see if the user wants to pass non-ip
2442                          * packets, these will not be checked by pfil(9) and
2443                          * passed unconditionally so the default is to drop.
2444                          */
2445                         if (pfil_onlyip)
2446                                 goto bad;
2447         }
2448
2449         /* Strip off the Ethernet header and keep a copy. */
2450         m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2451         m_adj(*mp, ETHER_HDR_LEN);
2452
2453         /* Strip off snap header, if present */
2454         if (snap) {
2455                 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2456                 m_adj(*mp, sizeof(struct llc));
2457         }
2458
2459         /*
2460          * Check the IP header for alignment and errors
2461          */
2462         if (dir == PFIL_IN) {
2463                 switch (ether_type) {
2464                         case ETHERTYPE_IP:
2465                                 error = bridge_ip_checkbasic(mp);
2466                                 break;
2467 #ifdef INET6
2468                         case ETHERTYPE_IPV6:
2469                                 error = bridge_ip6_checkbasic(mp);
2470                                 break;
2471 #endif /* INET6 */
2472                         default:
2473                                 error = 0;
2474                 }
2475                 if (error)
2476                         goto bad;
2477         }
2478
2479         error = 0;
2480
2481         /*
2482          * Run the packet through pfil
2483          */
2484         switch (ether_type)
2485         {
2486         case ETHERTYPE_IP :
2487                 /*
2488                  * before calling the firewall, swap fields the same as
2489                  * IP does. here we assume the header is contiguous
2490                  */
2491                 ip = mtod(*mp, struct ip *);
2492
2493                 ip->ip_len = ntohs(ip->ip_len);
2494                 ip->ip_off = ntohs(ip->ip_off);
2495
2496                 /*
2497                  * Run pfil on the member interface and the bridge, both can
2498                  * be skipped by clearing pfil_member or pfil_bridge.
2499                  *
2500                  * Keep the order:
2501                  *   in_if -> bridge_if -> out_if
2502                  */
2503                 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2504                         error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2505                                         dir);
2506
2507                 if (*mp == NULL || error != 0) /* filter may consume */
2508                         break;
2509
2510                 if (pfil_member && ifp != NULL)
2511                         error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2512                                         dir);
2513
2514                 if (*mp == NULL || error != 0) /* filter may consume */
2515                         break;
2516
2517                 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2518                         error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2519                                         dir);
2520
2521                 if (*mp == NULL || error != 0) /* filter may consume */
2522                         break;
2523
2524                 /* check if we need to fragment the packet */
2525                 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
2526                         i = (*mp)->m_pkthdr.len;
2527                         if (i > ifp->if_mtu) {
2528                                 error = bridge_fragment(ifp, *mp, &eh2, snap,
2529                                             &llc1);
2530                                 return (error);
2531                         }
2532                 }
2533
2534                 /* Recalculate the ip checksum and restore byte ordering */
2535                 ip = mtod(*mp, struct ip *);
2536                 hlen = ip->ip_hl << 2;
2537                 if (hlen < sizeof(struct ip))
2538                         goto bad;
2539                 if (hlen > (*mp)->m_len) {
2540                         if ((*mp = m_pullup(*mp, hlen)) == 0)
2541                                 goto bad;
2542                         ip = mtod(*mp, struct ip *);
2543                         if (ip == NULL)
2544                                 goto bad;
2545                 }
2546                 ip->ip_len = htons(ip->ip_len);
2547                 ip->ip_off = htons(ip->ip_off);
2548                 ip->ip_sum = 0;
2549                 if (hlen == sizeof(struct ip))
2550                         ip->ip_sum = in_cksum_hdr(ip);
2551                 else
2552                         ip->ip_sum = in_cksum(*mp, hlen);
2553
2554                 break;
2555 #ifdef INET6
2556         case ETHERTYPE_IPV6 :
2557                 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2558                         error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2559                                         dir);
2560
2561                 if (*mp == NULL || error != 0) /* filter may consume */
2562                         break;
2563
2564                 if (pfil_member && ifp != NULL)
2565                         error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2566                                         dir);
2567
2568                 if (*mp == NULL || error != 0) /* filter may consume */
2569                         break;
2570
2571                 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2572                         error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2573                                         dir);
2574                 break;
2575 #endif
2576         default :
2577                 error = 0;
2578                 break;
2579         }
2580
2581         if (*mp == NULL)
2582                 return (error);
2583         if (error != 0)
2584                 goto bad;
2585
2586         error = -1;
2587
2588         /*
2589          * Finally, put everything back the way it was and return
2590          */
2591         if (snap) {
2592                 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
2593                 if (*mp == NULL)
2594                         return (error);
2595                 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2596         }
2597
2598         M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
2599         if (*mp == NULL)
2600                 return (error);
2601         bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2602
2603         return (0);
2604
2605 bad:
2606         m_freem(*mp);
2607         *mp = NULL;
2608         return (error);
2609 }
2610
2611 /*
2612  * Perform basic checks on header size since
2613  * pfil assumes ip_input has already processed
2614  * it for it.  Cut-and-pasted from ip_input.c.
2615  * Given how simple the IPv6 version is,
2616  * does the IPv4 version really need to be
2617  * this complicated?
2618  *
2619  * XXX Should we update ipstat here, or not?
2620  * XXX Right now we update ipstat but not
2621  * XXX csum_counter.
2622  */
2623 static int
2624 bridge_ip_checkbasic(struct mbuf **mp)
2625 {
2626         struct mbuf *m = *mp;
2627         struct ip *ip;
2628         int len, hlen;
2629         u_short sum;
2630
2631         if (*mp == NULL)
2632                 return (-1);
2633 #if notyet
2634         if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2635                 if ((m = m_copyup(m, sizeof(struct ip),
2636                         (max_linkhdr + 3) & ~3)) == NULL) {
2637                         /* XXXJRT new stat, please */
2638                         ipstat.ips_toosmall++;
2639                         goto bad;
2640                 }
2641         } else
2642 #endif
2643 #ifndef __predict_false
2644 #define __predict_false(x) x
2645 #endif
2646          if (__predict_false(m->m_len < sizeof (struct ip))) {
2647                 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2648                         ipstat.ips_toosmall++;
2649                         goto bad;
2650                 }
2651         }
2652         ip = mtod(m, struct ip *);
2653         if (ip == NULL) goto bad;
2654
2655         if (ip->ip_v != IPVERSION) {
2656                 ipstat.ips_badvers++;
2657                 goto bad;
2658         }
2659         hlen = ip->ip_hl << 2;
2660         if (hlen < sizeof(struct ip)) { /* minimum header length */
2661                 ipstat.ips_badhlen++;
2662                 goto bad;
2663         }
2664         if (hlen > m->m_len) {
2665                 if ((m = m_pullup(m, hlen)) == 0) {
2666                         ipstat.ips_badhlen++;
2667                         goto bad;
2668                 }
2669                 ip = mtod(m, struct ip *);
2670                 if (ip == NULL) goto bad;
2671         }
2672
2673         if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2674                 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2675         } else {
2676                 if (hlen == sizeof(struct ip)) {
2677                         sum = in_cksum_hdr(ip);
2678                 } else {
2679                         sum = in_cksum(m, hlen);
2680                 }
2681         }
2682         if (sum) {
2683                 ipstat.ips_badsum++;
2684                 goto bad;
2685         }
2686
2687         /* Retrieve the packet length. */
2688         len = ntohs(ip->ip_len);
2689
2690         /*
2691          * Check for additional length bogosity
2692          */
2693         if (len < hlen) {
2694                 ipstat.ips_badlen++;
2695                 goto bad;
2696         }
2697
2698         /*
2699          * Check that the amount of data in the buffers
2700          * is as at least much as the IP header would have us expect.
2701          * Drop packet if shorter than we expect.
2702          */
2703         if (m->m_pkthdr.len < len) {
2704                 ipstat.ips_tooshort++;
2705                 goto bad;
2706         }
2707
2708         /* Checks out, proceed */
2709         *mp = m;
2710         return (0);
2711
2712 bad:
2713         *mp = m;
2714         return (-1);
2715 }
2716
2717 #ifdef INET6
2718 /*
2719  * Same as above, but for IPv6.
2720  * Cut-and-pasted from ip6_input.c.
2721  * XXX Should we update ip6stat, or not?
2722  */
2723 static int
2724 bridge_ip6_checkbasic(struct mbuf **mp)
2725 {
2726         struct mbuf *m = *mp;
2727         struct ip6_hdr *ip6;
2728
2729         /*
2730          * If the IPv6 header is not aligned, slurp it up into a new
2731          * mbuf with space for link headers, in the event we forward
2732          * it.  Otherwise, if it is aligned, make sure the entire base
2733          * IPv6 header is in the first mbuf of the chain.
2734          */
2735 #if notyet
2736         if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2737                 struct ifnet *inifp = m->m_pkthdr.rcvif;
2738                 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2739                             (max_linkhdr + 3) & ~3)) == NULL) {
2740                         /* XXXJRT new stat, please */
2741                         ip6stat.ip6s_toosmall++;
2742                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2743                         goto bad;
2744                 }
2745         } else
2746 #endif
2747         if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2748                 struct ifnet *inifp = m->m_pkthdr.rcvif;
2749                 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2750                         ip6stat.ip6s_toosmall++;
2751                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2752                         goto bad;
2753                 }
2754         }
2755
2756         ip6 = mtod(m, struct ip6_hdr *);
2757
2758         if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2759                 ip6stat.ip6s_badvers++;
2760                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2761                 goto bad;
2762         }
2763
2764         /* Checks out, proceed */
2765         *mp = m;
2766         return (0);
2767
2768 bad:
2769         *mp = m;
2770         return (-1);
2771 }
2772 #endif /* INET6 */
2773
2774 /*
2775  * bridge_fragment:
2776  *
2777  *      Return a fragmented mbuf chain.
2778  */
2779 static int
2780 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
2781     int snap, struct llc *llc)
2782 {
2783         struct mbuf *m0;
2784         struct ip *ip;
2785         int error = -1;
2786
2787         if (m->m_len < sizeof(struct ip) &&
2788             (m = m_pullup(m, sizeof(struct ip))) == NULL)
2789                 goto out;
2790         ip = mtod(m, struct ip *);
2791
2792         error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
2793                     CSUM_DELAY_IP);
2794         if (error)
2795                 goto out;
2796
2797         /* walk the chain and re-add the Ethernet header */
2798         for (m0 = m; m0; m0 = m0->m_nextpkt) {
2799                 if (error == 0) {
2800                         if (snap) {
2801                                 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
2802                                 if (m0 == NULL) {
2803                                         error = ENOBUFS;
2804                                         continue;
2805                                 }
2806                                 bcopy(llc, mtod(m0, caddr_t),
2807                                     sizeof(struct llc));
2808                         }
2809                         M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
2810                         if (m0 == NULL) {
2811                                 error = ENOBUFS;
2812                                 continue;
2813                         }
2814                         bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
2815                 } else 
2816                         m_freem(m);
2817         }
2818
2819         if (error == 0)
2820                 ipstat.ips_fragmented++;
2821
2822         return (error);
2823
2824 out:
2825         if (m != NULL)
2826                 m_freem(m);
2827         return (error);
2828 }
2829
2830 static void
2831 bridge_enqueue_handler(struct netmsg *nmsg)
2832 {
2833         struct netmsg_packet *nmp;
2834         struct ifnet *dst_ifp;
2835         struct mbuf *m;
2836
2837         nmp = (struct netmsg_packet *)nmsg;
2838         m = nmp->nm_packet;
2839         dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2840
2841         bridge_handoff_notags(dst_ifp, m);
2842 }
2843
2844 static void
2845 bridge_pfil_enqueue_handler(struct netmsg *nmsg)
2846 {
2847         struct netmsg_packet *nmp;
2848         struct ifnet *dst_ifp;
2849         struct mbuf *m;
2850
2851         nmp = (struct netmsg_packet *)nmsg;
2852         m = nmp->nm_packet;
2853         dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
2854
2855         /*
2856          * Filter on the output interface. Pass a NULL bridge interface
2857          * pointer so we do not redundantly filter on the bridge for
2858          * each interface we broadcast on.
2859          */
2860         if (inet_pfil_hook.ph_hashooks > 0
2861 #ifdef INET6
2862             || inet6_pfil_hook.ph_hashooks > 0
2863 #endif
2864             ) {
2865                 if (bridge_pfil(&m, NULL, dst_ifp, PFIL_OUT) != 0)
2866                         return;
2867                 if (m == NULL)
2868                         return;
2869         }
2870         bridge_handoff_notags(dst_ifp, m);
2871 }
2872
2873 static void
2874 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m)
2875 {
2876         while (m->m_type == MT_TAG) {
2877                 /* XXX see ether_output_frame for full rules check */
2878                 m = m->m_next;
2879         }
2880         bridge_handoff_notags(dst_ifp, m);
2881 }
2882
2883 static void
2884 bridge_handoff_notags(struct ifnet *dst_ifp, struct mbuf *m)
2885 {
2886         struct mbuf *m0;
2887
2888         KKASSERT(m->m_type != MT_TAG);
2889
2890         lwkt_serialize_enter(dst_ifp->if_serializer);
2891
2892         /* We may be sending a fragment so traverse the mbuf */
2893         for (; m; m = m0) {
2894                 struct altq_pktattr pktattr;
2895
2896                 m0 = m->m_nextpkt;
2897                 m->m_nextpkt = NULL;
2898
2899                 if (ifq_is_enabled(&dst_ifp->if_snd))
2900                         altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
2901
2902                 ifq_handoff(dst_ifp, m, &pktattr);
2903         }
2904
2905         lwkt_serialize_exit(dst_ifp->if_serializer);
2906 }
2907
2908 struct netmsg_brgctl {
2909         struct netmsg           bc_nmsg;
2910         bridge_ctl_t            bc_func;
2911         struct bridge_softc     *bc_sc;
2912         void                    *bc_arg;
2913 };
2914
2915 static void
2916 bridge_control_dispatch(struct netmsg *nmsg)
2917 {
2918         struct netmsg_brgctl *bc_msg = (struct netmsg_brgctl *)nmsg;
2919         struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
2920         int error;
2921
2922         lwkt_serialize_enter(bifp->if_serializer);
2923         error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
2924         lwkt_serialize_exit(bifp->if_serializer);
2925
2926         lwkt_replymsg(&nmsg->nm_lmsg, error);
2927 }
2928
2929 static int
2930 bridge_control(struct bridge_softc *sc, bridge_ctl_t bc_func, void *bc_arg)
2931 {
2932         struct ifnet *bifp = sc->sc_ifp;
2933         struct netmsg_brgctl bc_msg;
2934         struct netmsg *nmsg;
2935         int error;
2936
2937         ASSERT_SERIALIZED(bifp->if_serializer);
2938
2939         bzero(&bc_msg, sizeof(bc_msg));
2940         nmsg = &bc_msg.bc_nmsg;
2941
2942         netmsg_init(nmsg, &curthread->td_msgport, 0, bridge_control_dispatch);
2943         bc_msg.bc_func = bc_func;
2944         bc_msg.bc_sc = sc;
2945         bc_msg.bc_arg = bc_arg;
2946
2947         lwkt_serialize_exit(bifp->if_serializer);
2948         error = lwkt_domsg(cpu_portfn(0), &nmsg->nm_lmsg, 0);
2949         lwkt_serialize_enter(bifp->if_serializer);
2950         return error;
2951 }