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