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