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