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