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