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