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