]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/bridge.c
- We don't need to cache_purge() in nfs_reclaim(), vclean() does it for us.
[FreeBSD/FreeBSD.git] / sys / net / bridge.c
1 /*
2  * Copyright (c) 1998-2002 Luigi Rizzo
3  *
4  * Work partly supported by: Cisco Systems, Inc. - NSITE lab, RTP, NC
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 /*
31  * This code implements bridging in FreeBSD. It only acts on ethernet
32  * interfaces, including VLANs (others are still usable for routing).
33  * A FreeBSD host can implement multiple logical bridges, called
34  * "clusters". Each cluster is made of a set of interfaces, and
35  * identified by a "cluster-id" which is a number in the range 1..2^16-1.
36  *
37  * Bridging is enabled by the sysctl variable
38  *      net.link.ether.bridge.enable
39  * the grouping of interfaces into clusters is done with
40  *      net.link.ether.bridge.config
41  * containing a list of interfaces each optionally followed by
42  * a colon and the cluster it belongs to (1 is the default).
43  * Separators can be * spaces, commas or tabs, e.g.
44  *      net.link.ether.bridge.config="fxp0:2 fxp1:2 dc0 dc1:1"
45  * Optionally bridged packets can be passed through the firewall,
46  * this is controlled by the variable
47  *      net.link.ether.bridge.ipfw
48  *
49  * For each cluster there is a descriptor (cluster_softc) storing
50  * the following data structures:
51  * - a hash table with the MAC address and destination interface for each
52  *   known node. The table is indexed using a hash of the source address.
53  * - an array with the MAC addresses of the interfaces used in the cluster.
54  *
55  * Input packets are tapped near the beginning of ether_input(), and
56  * analysed by bridge_in(). Depending on the result, the packet
57  * can be forwarded to one or more output interfaces using bdg_forward(),
58  * and/or sent to the upper layer (e.g. in case of multicast).
59  *
60  * Output packets are intercepted near the end of ether_output().
61  * The correct destination is selected by bridge_dst_lookup(),
62  * and then forwarding is done by bdg_forward().
63  *
64  * The arp code is also modified to let a machine answer to requests
65  * irrespective of the port the request came from.
66  *
67  * In case of loops in the bridging topology, the bridge detects this
68  * event and temporarily mutes output bridging on one of the ports.
69  * Periodically, interfaces are unmuted by bdg_timeout().
70  * Muting is only implemented as a safety measure, and also as
71  * a mechanism to support a user-space implementation of the spanning
72  * tree algorithm.
73  *
74  * To build a bridging kernel, use the following option
75  *    option BRIDGE
76  * and then at runtime set the sysctl variable to enable bridging.
77  *
78  * Only one interface per cluster is supposed to have addresses set (but
79  * there are no substantial problems if you set addresses for none or
80  * for more than one interface).
81  * Bridging will act before routing, but nothing prevents a machine
82  * from doing both (modulo bugs in the implementation...).
83  *
84  * THINGS TO REMEMBER
85  *  - bridging is incompatible with multicast routing on the same
86  *    machine. There is not an easy fix to this.
87  *  - be very careful when bridging VLANs
88  *  - loop detection is still not very robust.
89  */
90 #include "opt_pfil_hooks.h"
91
92 #include <sys/param.h>
93 #include <sys/mbuf.h>
94 #include <sys/malloc.h>
95 #include <sys/protosw.h>
96 #include <sys/systm.h>
97 #include <sys/socket.h> /* for net/if.h */
98 #include <sys/ctype.h>  /* string functions */
99 #include <sys/kernel.h>
100 #include <sys/sysctl.h>
101
102 #include <net/if.h>
103 #include <net/if_types.h>
104 #include <net/if_var.h>
105
106 #include <netinet/in.h> /* for struct arpcom */
107 #include <netinet/in_systm.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip.h>
110 #include <netinet/if_ether.h> /* for struct arpcom */
111
112 #ifdef PFIL_HOOKS
113 #include <net/pfil.h>
114 #include <netinet/ip_var.h>
115 #endif
116
117 #include <net/route.h>
118 #include <netinet/ip_fw.h>
119 #include <netinet/ip_dummynet.h>
120 #include <net/bridge.h>
121
122 /*--------------------*/
123
124 #define ETHER_ADDR_COPY(_dst,_src)      bcopy(_src, _dst, ETHER_ADDR_LEN)
125 #define ETHER_ADDR_EQ(_a1,_a2)          (bcmp(_a1, _a2, ETHER_ADDR_LEN) == 0)
126
127 /*
128  * For each cluster, source MAC addresses are stored into a hash
129  * table which locates the port they reside on.
130  */
131 #define HASH_SIZE 8192  /* Table size, must be a power of 2 */
132
133 typedef struct hash_table {             /* each entry.          */
134     struct ifnet *      name;
135     u_char              etheraddr[ETHER_ADDR_LEN];
136     u_int16_t           used;           /* also, padding        */
137 } bdg_hash_table ;
138
139 /*
140  * The hash function applied to MAC addresses. Out of the 6 bytes,
141  * the last ones tend to vary more. Since we are on a little endian machine,
142  * we have to do some gimmick...
143  */
144 #define HASH_FN(addr)   (       \
145     ntohs( ((u_int16_t *)addr)[1] ^ ((u_int16_t *)addr)[2] ) & (HASH_SIZE -1))
146
147 /*
148  * This is the data structure where local addresses are stored.
149  */
150 struct bdg_addr {
151     u_char      etheraddr[ETHER_ADDR_LEN];
152     u_int16_t   _padding;
153 };
154
155 /*
156  * The configuration of each cluster includes the cluster id, a pointer to
157  * the hash table, and an array of local MAC addresses (of size "ports").
158  */
159 struct cluster_softc {
160     u_int16_t   cluster_id;
161     u_int16_t   ports;
162     bdg_hash_table *ht;
163     struct bdg_addr     *my_macs;       /* local MAC addresses */
164 };
165
166
167 extern struct protosw inetsw[];                 /* from netinet/ip_input.c */
168
169 static int n_clusters;                          /* number of clusters */
170 static struct cluster_softc *clusters;
171
172 static struct mtx bdg_mtx;
173 #define BDG_LOCK_INIT()         mtx_init(&bdg_mtx, "bridge", NULL, MTX_DEF)
174 #define BDG_LOCK_DESTROY()      mtx_destroy(&bdg_mtx)
175 #define BDG_LOCK()              mtx_lock(&bdg_mtx)
176 #define BDG_UNLOCK()            mtx_unlock(&bdg_mtx)
177 #define BDG_LOCK_ASSERT()       mtx_assert(&bdg_mtx, MA_OWNED)
178
179 #define BDG_MUTED(ifp) (ifp2sc[ifp->if_index].flags & IFF_MUTE)
180 #define BDG_MUTE(ifp) ifp2sc[ifp->if_index].flags |= IFF_MUTE
181 #define BDG_CLUSTER(ifp) (ifp2sc[ifp->if_index].cluster)
182
183 #define BDG_SAMECLUSTER(ifp,src) \
184         (src == NULL || BDG_CLUSTER(ifp) == BDG_CLUSTER(src) )
185
186 #ifdef __i386__
187 #define BDG_MATCH(a,b) ( \
188     ((u_int16_t *)(a))[2] == ((u_int16_t *)(b))[2] && \
189     *((u_int32_t *)(a)) == *((u_int32_t *)(b)) )
190 #define IS_ETHER_BROADCAST(a) ( \
191         *((u_int32_t *)(a)) == 0xffffffff && \
192         ((u_int16_t *)(a))[2] == 0xffff )
193 #else
194 /* for machines that do not support unaligned access */
195 #define BDG_MATCH(a,b)          ETHER_ADDR_EQ(a,b)
196 #define IS_ETHER_BROADCAST(a)   ETHER_ADDR_EQ(a,"\377\377\377\377\377\377")
197 #endif
198
199 SYSCTL_DECL(_net_link_ether);
200 SYSCTL_NODE(_net_link_ether, OID_AUTO, bridge, CTLFLAG_RD, 0,
201         "Bridge parameters");
202 static char bridge_version[] = "$Revision$ $Date$";
203 SYSCTL_STRING(_net_link_ether_bridge, OID_AUTO, version, CTLFLAG_RD,
204         bridge_version, 0, "software version");
205
206 #define BRIDGE_DEBUG
207 #ifdef BRIDGE_DEBUG
208 int     bridge_debug = 0;
209 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, debug, CTLFLAG_RW, &bridge_debug,
210             0, "control debugging printfs");
211 #define DPRINTF(X)      if (bridge_debug) printf X
212 #else
213 #define DPRINTF(X)
214 #endif
215
216 #ifdef BRIDGE_TIMING
217 /*
218  * For timing-related debugging, you can use the following macros.
219  * remember, rdtsc() only works on Pentium-class machines
220
221     quad_t ticks;
222     DDB(ticks = rdtsc();)
223     ... interesting code ...
224     DDB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;)
225
226  *
227  */
228 #define DDB(x)  x
229
230 static int bdg_fw_avg;
231 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, fw_avg, CTLFLAG_RW,
232             &bdg_fw_avg, 0,"Cycle counter avg");
233 static int bdg_fw_ticks;
234 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, fw_ticks, CTLFLAG_RW,
235             &bdg_fw_ticks, 0,"Cycle counter item");
236 static int bdg_fw_count;
237 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, fw_count, CTLFLAG_RW,
238             &bdg_fw_count, 0,"Cycle counter count");
239 #else
240 #define DDB(x)
241 #endif
242
243 static int bdginit(void);
244 static void parse_bdg_cfg(void);
245
246 static int bdg_ipf;             /* IPFilter enabled in bridge */
247 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, ipf, CTLFLAG_RW,
248             &bdg_ipf, 0,"Pass bridged pkts through IPFilter");
249 static int bdg_ipfw;
250 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, ipfw, CTLFLAG_RW,
251             &bdg_ipfw,0,"Pass bridged pkts through firewall");
252
253 static int bdg_copy;
254 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, copy, CTLFLAG_RW,
255         &bdg_copy, 0, "Force packet copy in bdg_forward");
256
257 int bdg_ipfw_drops;
258 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, ipfw_drop,
259         CTLFLAG_RW, &bdg_ipfw_drops,0,"");
260 int bdg_ipfw_colls;
261 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, ipfw_collisions,
262         CTLFLAG_RW, &bdg_ipfw_colls,0,"");
263
264 static int bdg_thru;
265 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, packets, CTLFLAG_RW,
266         &bdg_thru, 0, "Packets through bridge");
267 static int bdg_dropped;
268 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, dropped, CTLFLAG_RW,
269         &bdg_dropped, 0, "Packets dropped in bdg_forward");
270 static int bdg_predict;
271 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, predict, CTLFLAG_RW,
272         &bdg_predict, 0, "Correctly predicted header location");
273
274 #ifdef BRIDGE_DEBUG
275 static char *bdg_dst_names[] = {
276         "BDG_NULL    ",
277         "BDG_BCAST   ",
278         "BDG_MCAST   ",
279         "BDG_LOCAL   ",
280         "BDG_DROP    ",
281         "BDG_UNKNOWN ",
282         "BDG_IN      ",
283         "BDG_OUT     ",
284         "BDG_FORWARD " };
285 #endif /* BRIDGE_DEBUG */
286
287 /*
288  * System initialization
289  */
290 static struct bdg_stats bdg_stats ;
291 SYSCTL_STRUCT(_net_link_ether_bridge, OID_AUTO, stats, CTLFLAG_RD,
292         &bdg_stats, bdg_stats, "bridge statistics");
293
294 static struct callout bdg_callout;
295
296 /*
297  * Add an interface to a cluster, possibly creating a new entry in
298  * the cluster table. This requires reallocation of the table and
299  * updating pointers in ifp2sc.
300  */
301 static struct cluster_softc *
302 add_cluster(u_int16_t cluster_id, struct arpcom *ac)
303 {
304     struct cluster_softc *c = NULL;
305     int i;
306
307     BDG_LOCK_ASSERT();
308
309     for (i = 0; i < n_clusters ; i++)
310         if (clusters[i].cluster_id == cluster_id)
311             goto found;
312
313     /* Not found, need to reallocate */
314     c = malloc((1+n_clusters) * sizeof (*c), M_IFADDR, M_NOWAIT | M_ZERO);
315     if (c == NULL) {/* malloc failure */
316         printf("-- bridge: cannot add new cluster\n");
317         goto bad;
318     }
319     c[n_clusters].ht = (struct hash_table *)
320             malloc(HASH_SIZE * sizeof(struct hash_table),
321                 M_IFADDR, M_NOWAIT | M_ZERO);
322     if (c[n_clusters].ht == NULL) {
323         printf("-- bridge: cannot allocate hash table for new cluster\n");
324         goto bad;
325     }
326     c[n_clusters].my_macs = (struct bdg_addr *)
327             malloc(BDG_MAX_PORTS * sizeof(struct bdg_addr),
328                 M_IFADDR, M_NOWAIT | M_ZERO);
329     if (c[n_clusters].my_macs == NULL) {
330         printf("-- bridge: cannot allocate mac addr table for new cluster\n");
331         free(c[n_clusters].ht, M_IFADDR);
332         goto bad;
333     }
334
335     c[n_clusters].cluster_id = cluster_id;
336     c[n_clusters].ports = 0;
337     /*
338      * now copy old descriptors here
339      */
340     if (n_clusters > 0) {
341         for (i=0; i < n_clusters; i++)
342             c[i] = clusters[i];
343         /*
344          * and finally update pointers in ifp2sc
345          */
346         for (i = 0 ; i < if_index && i < BDG_MAX_PORTS; i++)
347             if (ifp2sc[i].cluster != NULL)
348                 ifp2sc[i].cluster = c + (ifp2sc[i].cluster - clusters);
349         free(clusters, M_IFADDR);
350     }
351     clusters = c;
352     i = n_clusters;             /* index of cluster entry */
353     n_clusters++;
354 found:
355     c = clusters + i;           /* the right cluster ... */
356     ETHER_ADDR_COPY(c->my_macs[c->ports].etheraddr, ac->ac_enaddr);
357     c->ports++;
358     return c;
359 bad:
360     if (c)
361         free(c, M_IFADDR);
362     return NULL;
363 }
364
365
366 /*
367  * Turn off bridging, by clearing promisc mode on the interface,
368  * marking the interface as unused, and clearing the name in the
369  * stats entry.
370  * Also dispose the hash tables associated with the clusters.
371  */
372 static void
373 bridge_off(void)
374 {
375     struct ifnet *ifp ;
376     int i;
377
378     BDG_LOCK_ASSERT();
379
380     DPRINTF(("%s: n_clusters %d\n", __func__, n_clusters));
381
382     IFNET_RLOCK();
383     TAILQ_FOREACH(ifp, &ifnet, if_link) {
384         struct bdg_softc *b;
385
386         if (ifp->if_index >= BDG_MAX_PORTS)
387             continue;   /* make sure we do not go beyond the end */
388         b = &ifp2sc[ifp->if_index];
389
390         if ( b->flags & IFF_BDG_PROMISC ) {
391             ifpromisc(ifp, 0);
392             b->flags &= ~(IFF_BDG_PROMISC|IFF_MUTE) ;
393             DPRINTF(("%s: %s%d promisc OFF if_flags 0x%x "
394                 "bdg_flags 0x%x\n", __func__, ifp->if_name, ifp->if_unit,
395                 ifp->if_flags, b->flags));
396         }
397         b->flags &= ~(IFF_USED) ;
398         b->cluster = NULL;
399         bdg_stats.s[ifp->if_index].name[0] = '\0';
400     }
401     IFNET_RUNLOCK();
402     /* flush_tables */
403
404     for (i=0; i < n_clusters; i++) {
405         free(clusters[i].ht, M_IFADDR);
406         free(clusters[i].my_macs, M_IFADDR);
407     }
408     if (clusters != NULL)
409         free(clusters, M_IFADDR);
410     clusters = NULL;
411     n_clusters =0;
412 }
413
414 /*
415  * set promisc mode on the interfaces we use.
416  */
417 static void
418 bridge_on(void)
419 {
420     struct ifnet *ifp ;
421
422     BDG_LOCK_ASSERT();
423
424     IFNET_RLOCK();
425     TAILQ_FOREACH(ifp, &ifnet, if_link) {
426         struct bdg_softc *b = &ifp2sc[ifp->if_index];
427
428         if ( !(b->flags & IFF_USED) )
429             continue ;
430         if ( !( ifp->if_flags & IFF_UP) ) {
431             if_up(ifp);
432         }
433         if ( !(b->flags & IFF_BDG_PROMISC) ) {
434             (void) ifpromisc(ifp, 1);
435             b->flags |= IFF_BDG_PROMISC ;
436             DPRINTF(("%s: %s%d promisc ON if_flags 0x%x bdg_flags 0x%x\n",
437                 __func__, ifp->if_name, ifp->if_unit, ifp->if_flags, b->flags));
438         }
439         if (b->flags & IFF_MUTE) {
440             DPRINTF(("%s: unmuting %s%d\n", __func__,
441                 ifp->if_name, ifp->if_unit));
442             b->flags &= ~IFF_MUTE;
443         }
444     }
445     IFNET_RUNLOCK();
446 }
447
448 static char bridge_cfg[1024];           /* NB: in BSS so initialized to zero */
449
450 /**
451  * reconfigure bridge.
452  * This is also done every time we attach or detach an interface.
453  * Main use is to make sure that we do not bridge on some old
454  * (ejected) device. So, it would be really useful to have a
455  * pointer to the modified device as an argument. Without it, we
456  * have to scan all interfaces.
457  */
458 static void
459 reconfigure_bridge_locked(void)
460 {
461     BDG_LOCK_ASSERT();
462
463     bridge_off();
464     if (do_bridge) {
465         if (if_index >= BDG_MAX_PORTS) {
466             printf("-- sorry too many interfaces (%d, max is %d),"
467                 " disabling bridging\n", if_index, BDG_MAX_PORTS);
468             do_bridge = 0;
469             return;
470         }
471         parse_bdg_cfg();
472         bridge_on();
473     }
474 }
475
476 static void
477 reconfigure_bridge(void)
478 {
479     BDG_LOCK();
480     reconfigure_bridge_locked();
481     BDG_UNLOCK();
482 }
483
484 /*
485  * parse the config string, set IFF_USED, name and cluster_id
486  * for all interfaces found.
487  * The config string is a list of "if[:cluster]" with
488  * a number of possible separators (see "sep"). In particular the
489  * use of the space lets you set bridge_cfg with the output from
490  * "ifconfig -l"
491  */
492 static void
493 parse_bdg_cfg(void)
494 {
495     char *p, *beg;
496     int l, cluster;
497     static const char *sep = ", \t";
498
499     BDG_LOCK_ASSERT();
500
501     for (p = bridge_cfg; *p ; p++) {
502         struct ifnet *ifp;
503         int found = 0;
504         char c;
505
506         if (index(sep, *p))     /* skip separators */
507             continue ;
508         /* names are lowercase and digits */
509         for ( beg = p ; islower(*p) || isdigit(*p) ; p++ )
510             ;
511         l = p - beg ;           /* length of name string */
512         if (l == 0)             /* invalid name */
513             break ;
514         if ( *p != ':' )        /* no ':', assume default cluster 1 */
515             cluster = 1 ;
516         else                    /* fetch cluster */
517             cluster = strtoul( p+1, &p, 10);
518         c = *p;
519         *p = '\0';
520         /*
521          * now search in interface list for a matching name
522          */
523         IFNET_RLOCK();          /* could sleep XXX */
524         TAILQ_FOREACH(ifp, &ifnet, if_link) {
525             char buf[IFNAMSIZ];
526
527             snprintf(buf, sizeof(buf), "%s%d", ifp->if_name, ifp->if_unit);
528             if (!strncmp(beg, buf, max(l, strlen(buf)))) {
529                 struct bdg_softc *b = &ifp2sc[ifp->if_index];
530                 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN) {
531                     printf("%s is not an ethernet, continue\n", buf);
532                     continue;
533                 }
534                 if (b->flags & IFF_USED) {
535                     printf("%s already used, skipping\n", buf);
536                     break;
537                 }
538                 b->cluster = add_cluster(htons(cluster), (struct arpcom *)ifp);
539                 b->flags |= IFF_USED ;
540                 sprintf(bdg_stats.s[ifp->if_index].name,
541                         "%s%d:%d", ifp->if_name, ifp->if_unit, cluster);
542
543                 DPRINTF(("%s: found %s next c %d\n", __func__,
544                     bdg_stats.s[ifp->if_index].name, c));
545                 found = 1;
546                 break ;
547             }
548         }
549         IFNET_RUNLOCK();
550         if (!found)
551             printf("interface %s Not found in bridge\n", beg);
552         *p = c;
553         if (c == '\0')
554             break; /* no more */
555     }
556 }
557
558 /*
559  * handler for net.link.ether.bridge
560  */
561 static int
562 sysctl_bdg(SYSCTL_HANDLER_ARGS)
563 {
564     int enable = do_bridge;
565     int error;
566
567     error = sysctl_handle_int(oidp, &enable, 0, req);
568     BDG_LOCK();
569     if (enable != do_bridge) {
570         do_bridge = enable;
571         reconfigure_bridge_locked();
572     }
573     BDG_UNLOCK();
574     return error ;
575 }
576 SYSCTL_PROC(_net_link_ether_bridge, OID_AUTO, enable, CTLTYPE_INT|CTLFLAG_RW,
577             &do_bridge, 0, &sysctl_bdg, "I", "Bridging");
578
579 /*
580  * handler for net.link.ether.bridge_cfg
581  */
582 static int
583 sysctl_bdg_cfg(SYSCTL_HANDLER_ARGS)
584 {
585     int error;
586     char *new_cfg;
587
588     new_cfg = malloc(sizeof(bridge_cfg), M_TEMP, M_WAITOK);
589     bcopy(bridge_cfg, new_cfg, sizeof(bridge_cfg));
590
591     error = sysctl_handle_string(oidp, new_cfg, oidp->oid_arg2, req);
592     if (error == 0) {
593         BDG_LOCK();
594         if (strcmp(new_cfg, bridge_cfg)) {
595             bcopy(new_cfg, bridge_cfg, sizeof(bridge_cfg));
596             reconfigure_bridge_locked();
597         }
598         BDG_UNLOCK();
599     }
600
601     free(new_cfg, M_TEMP);
602
603     return error;
604 }
605 SYSCTL_PROC(_net_link_ether_bridge, OID_AUTO, config, CTLTYPE_STRING|CTLFLAG_RW,
606             &bridge_cfg, sizeof(bridge_cfg), &sysctl_bdg_cfg, "A",
607             "Bridge configuration");
608
609 static int
610 sysctl_refresh(SYSCTL_HANDLER_ARGS)
611 {
612     if (req->newptr)
613         reconfigure_bridge();
614
615     return 0;
616 }
617 SYSCTL_PROC(_net_link_ether_bridge, OID_AUTO, refresh, CTLTYPE_INT|CTLFLAG_WR,
618             NULL, 0, &sysctl_refresh, "I", "iface refresh");
619
620 #ifndef BURN_BRIDGES
621 #define SYSCTL_OID_COMPAT(parent, nbr, name, kind, a1, a2, handler, fmt, descr)\
622         static struct sysctl_oid sysctl__##parent##_##name##_compat = {  \
623                 &sysctl_##parent##_children, { 0 },                      \
624                 nbr, kind, a1, a2, #name, handler, fmt, 0, descr };      \
625         DATA_SET(sysctl_set, sysctl__##parent##_##name##_compat)
626 #define SYSCTL_INT_COMPAT(parent, nbr, name, access, ptr, val, descr)    \
627         SYSCTL_OID_COMPAT(parent, nbr, name, CTLTYPE_INT|(access),       \
628                 ptr, val, sysctl_handle_int, "I", descr)
629 #define SYSCTL_STRUCT_COMPAT(parent, nbr, name, access, ptr, type, descr)\
630         SYSCTL_OID_COMPAT(parent, nbr, name, CTLTYPE_OPAQUE|(access),    \
631                 ptr, sizeof(struct type), sysctl_handle_opaque,          \
632                 "S," #type, descr)
633 #define SYSCTL_PROC_COMPAT(parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
634         SYSCTL_OID_COMPAT(parent, nbr, name, (access),                   \
635                 ptr, arg, handler, fmt, descr)
636
637 SYSCTL_INT_COMPAT(_net_link_ether, OID_AUTO, bridge_ipf, CTLFLAG_RW,
638             &bdg_ipf, 0,"Pass bridged pkts through IPFilter");
639 SYSCTL_INT_COMPAT(_net_link_ether, OID_AUTO, bridge_ipfw, CTLFLAG_RW,
640             &bdg_ipfw,0,"Pass bridged pkts through firewall");
641 SYSCTL_STRUCT_COMPAT(_net_link_ether, PF_BDG, bdgstats, CTLFLAG_RD,
642         &bdg_stats, bdg_stats, "bridge statistics");
643 SYSCTL_PROC_COMPAT(_net_link_ether, OID_AUTO, bridge_cfg, 
644             CTLTYPE_STRING|CTLFLAG_RW,
645             &bridge_cfg, sizeof(bridge_cfg), &sysctl_bdg_cfg, "A",
646             "Bridge configuration");
647 SYSCTL_PROC_COMPAT(_net_link_ether, OID_AUTO, bridge_refresh,
648             CTLTYPE_INT|CTLFLAG_WR,
649             NULL, 0, &sysctl_refresh, "I", "iface refresh");
650 #endif
651
652 static int bdg_loops;
653 static int bdg_slowtimer = 0;
654 static int bdg_age_index = 0;   /* index of table position to age */
655
656 /*
657  * called periodically to flush entries etc.
658  */
659 static void
660 bdg_timeout(void *dummy)
661 {
662     if (do_bridge) {
663         int l, i;
664
665         BDG_LOCK();
666         /*
667          * age entries in the forwarding table.
668          */
669         l = bdg_age_index + HASH_SIZE/4 ;
670         if (l > HASH_SIZE)
671             l = HASH_SIZE;
672
673         for (i = 0; i < n_clusters; i++) {
674             bdg_hash_table *bdg_table = clusters[i].ht;
675             for (; bdg_age_index < l; bdg_age_index++)
676                 if (bdg_table[bdg_age_index].used)
677                     bdg_table[bdg_age_index].used = 0;
678                 else if (bdg_table[bdg_age_index].name) {
679                     DPRINTF(("%s: flushing stale entry %d\n",
680                         __func__, bdg_age_index));
681                     bdg_table[bdg_age_index].name = NULL;
682                 }
683         }
684         if (bdg_age_index >= HASH_SIZE)
685             bdg_age_index = 0;
686
687         if (--bdg_slowtimer <= 0 ) {
688             bdg_slowtimer = 5;
689
690             bridge_on();        /* we just need unmute, really */
691             bdg_loops = 0;
692         }
693         BDG_UNLOCK();
694     }
695     callout_reset(&bdg_callout, 2*hz, bdg_timeout, NULL);
696 }
697
698 /*
699  * Find the right pkt destination:
700  *      BDG_BCAST       is a broadcast
701  *      BDG_MCAST       is a multicast
702  *      BDG_LOCAL       is for a local address
703  *      BDG_DROP        must be dropped
704  *      other           ifp of the dest. interface (incl.self)
705  *
706  * We assume this is only called for interfaces for which bridging
707  * is enabled, i.e. BDG_USED(ifp) is true.
708  */
709 static __inline struct ifnet *
710 bridge_dst_lookup(struct ether_header *eh, struct cluster_softc *c)
711 {
712     bdg_hash_table *bt;         /* pointer to entry in hash table */
713
714     BDG_LOCK_ASSERT();
715
716     if (ETHER_IS_MULTICAST(eh->ether_dhost))
717         return IS_ETHER_BROADCAST(eh->ether_dhost) ? BDG_BCAST : BDG_MCAST;
718     /*
719      * Lookup local addresses in case one matches.  We optimize
720      * for the common case of two interfaces.
721      */
722     KASSERT(c->ports != 0, ("lookup with no ports!"));
723     switch (c->ports) {
724         int i;
725     default:
726         for (i = c->ports-1; i > 1; i--) {
727             if (ETHER_ADDR_EQ(c->my_macs[i].etheraddr, eh->ether_dhost))
728                 return BDG_LOCAL;
729         }
730         /* fall thru... */
731     case 2:
732         if (ETHER_ADDR_EQ(c->my_macs[1].etheraddr, eh->ether_dhost))
733             return BDG_LOCAL;
734     case 1:
735         if (ETHER_ADDR_EQ(c->my_macs[0].etheraddr, eh->ether_dhost))
736             return BDG_LOCAL;
737     }
738     /*
739      * Look for a possible destination in table
740      */
741     bt = &c->ht[HASH_FN(eh->ether_dhost)];
742     if (bt->name && ETHER_ADDR_EQ(bt->etheraddr, eh->ether_dhost))
743         return bt->name;
744     else
745         return BDG_UNKNOWN;
746 }
747
748 /**
749  * bridge_in() is invoked to perform bridging decision on input packets.
750  *
751  * On Input:
752  *   eh         Ethernet header of the incoming packet.
753  *   ifp        interface the packet is coming from.
754  *
755  * On Return: destination of packet, one of
756  *   BDG_BCAST  broadcast
757  *   BDG_MCAST  multicast
758  *   BDG_LOCAL  is only for a local address (do not forward)
759  *   BDG_DROP   drop the packet
760  *   ifp        ifp of the destination interface.
761  *
762  * Forwarding is not done directly to give a chance to some drivers
763  * to fetch more of the packet, or simply drop it completely.
764  */
765
766 static struct ifnet *
767 bridge_in(struct ifnet *ifp, struct ether_header *eh)
768 {
769     int index;
770     struct ifnet *dst, *old;
771     bdg_hash_table *bt;                 /* location in hash table */
772     int dropit = BDG_MUTED(ifp);
773
774     /*
775      * hash the source address
776      */
777     BDG_LOCK();
778     index = HASH_FN(eh->ether_shost);
779     bt = &BDG_CLUSTER(ifp)->ht[index];
780     bt->used = 1;
781     old = bt->name;
782     if (old) {                          /* the entry is valid */
783         if (!ETHER_ADDR_EQ(eh->ether_shost, bt->etheraddr)) {
784             bdg_ipfw_colls++;
785             bt->name = NULL;            /* NB: will overwrite below */
786         } else if (old != ifp) {
787             /*
788              * Found a loop. Either a machine has moved, or there
789              * is a misconfiguration/reconfiguration of the network.
790              * First, do not forward this packet!
791              * Record the relocation anyways; then, if loops persist,
792              * suspect a reconfiguration and disable forwarding
793              * from the old interface.
794              */
795             bt->name = ifp;             /* relocate address */
796             printf("-- loop (%d) %6D to %s%d from %s%d (%s)\n",
797                         bdg_loops, eh->ether_shost, ".",
798                         ifp->if_name, ifp->if_unit,
799                         old->if_name, old->if_unit,
800                         BDG_MUTED(old) ? "muted":"active");
801             dropit = 1;
802             if (!BDG_MUTED(old)) {
803                 if (bdg_loops++ > 10)
804                     BDG_MUTE(old);
805             }
806         }
807     }
808
809     /*
810      * now write the source address into the table
811      */
812     if (bt->name == NULL) {
813         DPRINTF(("%s: new addr %6D at %d for %s%d\n",
814             __func__, eh->ether_shost, ".", index, ifp->if_name, ifp->if_unit));
815         ETHER_ADDR_COPY(bt->etheraddr, eh->ether_shost);
816         bt->name = ifp;
817     }
818     dst = bridge_dst_lookup(eh, BDG_CLUSTER(ifp));
819     BDG_UNLOCK();
820
821     /*
822      * bridge_dst_lookup can return the following values:
823      *   BDG_BCAST, BDG_MCAST, BDG_LOCAL, BDG_UNKNOWN, BDG_DROP, ifp.
824      * For muted interfaces, or when we detect a loop, the first 3 are
825      * changed in BDG_LOCAL (we still listen to incoming traffic),
826      * and others to BDG_DROP (no use for the local host).
827      * Also, for incoming packets, ifp is changed to BDG_DROP if ifp == src.
828      * These changes are not necessary for outgoing packets from ether_output().
829      */
830     BDG_STAT(ifp, BDG_IN);
831     switch ((uintptr_t)dst) {
832     case (uintptr_t)BDG_BCAST:
833     case (uintptr_t)BDG_MCAST:
834     case (uintptr_t)BDG_LOCAL:
835     case (uintptr_t)BDG_UNKNOWN:
836     case (uintptr_t)BDG_DROP:
837         BDG_STAT(ifp, dst);
838         break;
839     default:
840         if (dst == ifp || dropit)
841             BDG_STAT(ifp, BDG_DROP);
842         else
843             BDG_STAT(ifp, BDG_FORWARD);
844         break;
845     }
846
847     if (dropit) {
848         if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_LOCAL)
849             dst = BDG_LOCAL;
850         else
851             dst = BDG_DROP;
852     } else {
853         if (dst == ifp)
854             dst = BDG_DROP;
855     }
856     DPRINTF(("%s: %6D ->%6D ty 0x%04x dst %s%d\n", __func__,
857         eh->ether_shost, ".",
858         eh->ether_dhost, ".",
859         ntohs(eh->ether_type),
860         (dst <= BDG_FORWARD) ? bdg_dst_names[(uintptr_t)dst] :
861                 dst->if_name,
862         (dst <= BDG_FORWARD) ? 0 : dst->if_unit));
863
864     return dst;
865 }
866
867 /*
868  * Return 1 if it's ok to send a packet out the specified interface.
869  * The interface must be:
870  *      used for bridging,
871  *      not muted,
872  *      not full,
873  *      up and running,
874  *      not the source interface, and
875  *      belong to the same cluster as the 'real_dst'.
876  */
877 static __inline int
878 bridge_ifok(struct ifnet *ifp, struct ifnet *src, struct ifnet *dst)
879 {
880     return (BDG_USED(ifp)
881         && !BDG_MUTED(ifp)
882         && !_IF_QFULL(&ifp->if_snd)
883         && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING)
884         && ifp != src
885         && BDG_SAMECLUSTER(ifp, dst));
886 }
887
888 /*
889  * Forward a packet to dst -- which can be a single interface or
890  * an entire cluster. The src port and muted interfaces are excluded.
891  *
892  * If src == NULL, the pkt comes from ether_output, and dst is the real
893  * interface the packet is originally sent to. In this case, we must forward
894  * it to the whole cluster.
895  * We never call bdg_forward from ether_output on interfaces which are
896  * not part of a cluster.
897  *
898  * If possible (i.e. we can determine that the caller does not need
899  * a copy), the packet is consumed here, and bdg_forward returns NULL.
900  * Otherwise, a pointer to a copy of the packet is returned.
901  */
902 static struct mbuf *
903 bdg_forward(struct mbuf *m0, struct ifnet *dst)
904 {
905 #define EH_RESTORE(_m) do {                                                \
906     M_PREPEND((_m), ETHER_HDR_LEN, M_DONTWAIT);                            \
907     if ((_m) == NULL) {                                                    \
908         bdg_dropped++;                                                     \
909         return NULL;                                                       \
910     }                                                                      \
911     if (eh != mtod((_m), struct ether_header *))                           \
912         bcopy(&save_eh, mtod((_m), struct ether_header *), ETHER_HDR_LEN); \
913     else                                                                   \
914         bdg_predict++;                                                     \
915 } while (0);
916     struct ether_header *eh;
917     struct ifnet *src;
918     struct ifnet *ifp, *last;
919     int shared = bdg_copy;              /* someone else is using the mbuf */
920     struct ifnet *real_dst = dst;       /* real dst from ether_output */
921     struct ip_fw_args args;
922     struct ether_header save_eh;
923     struct mbuf *m;
924
925     DDB(quad_t ticks; ticks = rdtsc();)
926
927     args.rule = NULL;           /* did we match a firewall rule ? */
928     /* Fetch state from dummynet tag, ignore others */
929     for (;m0->m_type == MT_TAG; m0 = m0->m_next)
930         if (m0->_m_tag_id == PACKET_TAG_DUMMYNET) {
931             args.rule = ((struct dn_pkt *)m0)->rule;
932             shared = 0;                 /* For sure this is our own mbuf. */
933         }
934     if (args.rule == NULL)
935         bdg_thru++;                     /* count 1st time through bdg_forward */
936
937     /*
938      * The packet arrives with the Ethernet header at the front.
939      */
940     eh = mtod(m0, struct ether_header *);
941
942     src = m0->m_pkthdr.rcvif;
943     if (src == NULL) {                  /* packet from ether_output */
944         BDG_LOCK();
945         dst = bridge_dst_lookup(eh, BDG_CLUSTER(real_dst));
946         BDG_UNLOCK();
947     }
948
949     if (dst == BDG_DROP) {              /* this should not happen */
950         printf("xx bdg_forward for BDG_DROP\n");
951         m_freem(m0);
952         bdg_dropped++;
953         return NULL;
954     }
955     if (dst == BDG_LOCAL) {             /* this should not happen as well */
956         printf("xx ouch, bdg_forward for local pkt\n");
957         return m0;
958     }
959     if (dst == BDG_BCAST || dst == BDG_MCAST) {
960          /* need a copy for the local stack */
961          shared = 1;
962     }
963
964     /*
965      * Do filtering in a very similar way to what is done in ip_output.
966      * Only if firewall is loaded, enabled, and the packet is not
967      * from ether_output() (src==NULL, or we would filter it twice).
968      * Additional restrictions may apply e.g. non-IP, short packets,
969      * and pkts already gone through a pipe.
970      */
971     if (src != NULL && (
972 #ifdef PFIL_HOOKS
973         (inet_pfil_hook.ph_busy_count >= 0 && bdg_ipf != 0) ||
974 #endif
975         (IPFW_LOADED && bdg_ipfw != 0))) {
976
977         int i;
978
979         if (args.rule != NULL && fw_one_pass)
980             goto forward; /* packet already partially processed */
981         /*
982          * i need some amt of data to be contiguous, and in case others need
983          * the packet (shared==1) also better be in the first mbuf.
984          */
985         i = min(m0->m_pkthdr.len, max_protohdr) ;
986         if (shared || m0->m_len < i) {
987             m0 = m_pullup(m0, i);
988             if (m0 == NULL) {
989                 printf("%s: m_pullup failed\n", __func__);      /* XXXDPRINTF*/
990                 bdg_dropped++;
991                 return NULL;
992             }
993             eh = mtod(m0, struct ether_header *);
994         }
995
996         /*
997          * Processing below expects the Ethernet header is stripped.
998          * Furthermore, the mbuf chain might be replaced at various
999          * places.  To deal with this we copy the header to a temporary
1000          * location, strip the header, and restore it as needed.
1001          */
1002         bcopy(eh, &save_eh, ETHER_HDR_LEN);     /* local copy for restore */
1003         m_adj(m0, ETHER_HDR_LEN);               /* temporarily strip header */
1004
1005 #ifdef PFIL_HOOKS
1006         /*
1007          * NetBSD-style generic packet filter, pfil(9), hooks.
1008          * Enables ipf(8) in bridging.
1009          */
1010         if (inet_pfil_hook.ph_busy_count >= 0 &&
1011             m0->m_pkthdr.len >= sizeof(struct ip) &&
1012             ntohs(save_eh.ether_type) == ETHERTYPE_IP) {
1013             /*
1014              * before calling the firewall, swap fields the same as IP does.
1015              * here we assume the pkt is an IP one and the header is contiguous
1016              */
1017             struct ip *ip = mtod(m0, struct ip *);
1018
1019             ip->ip_len = ntohs(ip->ip_len);
1020             ip->ip_off = ntohs(ip->ip_off);
1021
1022             if (pfil_run_hooks(&inet_pfil_hook, &m0, src, PFIL_IN) != 0) {
1023                 /* NB: hook should consume packet */
1024                 return NULL;
1025             }
1026             if (m0 == NULL)                     /* consumed by filter */
1027                 return m0;
1028             /*
1029              * If we get here, the firewall has passed the pkt, but the mbuf
1030              * pointer might have changed. Restore ip and the fields ntohs()'d.
1031              */
1032             ip = mtod(m0, struct ip *);
1033             ip->ip_len = htons(ip->ip_len);
1034             ip->ip_off = htons(ip->ip_off);
1035         }
1036 #endif /* PFIL_HOOKS */
1037
1038         /*
1039          * Prepare arguments and call the firewall.
1040          */
1041         if (!IPFW_LOADED || bdg_ipfw == 0) {
1042             EH_RESTORE(m0);     /* restore Ethernet header */
1043             goto forward;       /* not using ipfw, accept the packet */
1044         }
1045
1046         /*
1047          * XXX The following code is very similar to the one in
1048          * if_ethersubr.c:ether_ipfw_chk()
1049          */
1050
1051         args.m = m0;            /* the packet we are looking at         */
1052         args.oif = NULL;        /* this is an input packet              */
1053         args.divert_rule = 0;   /* we do not support divert yet         */
1054         args.next_hop = NULL;   /* we do not support forward yet        */
1055         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
1056         i = ip_fw_chk_ptr(&args);
1057         m0 = args.m;            /* in case the firewall used the mbuf   */
1058
1059         if (m0 != NULL)
1060                 EH_RESTORE(m0); /* restore Ethernet header */
1061
1062         if ( (i & IP_FW_PORT_DENY_FLAG) || m0 == NULL) /* drop */
1063             return m0;
1064
1065         if (i == 0) /* a PASS rule.  */
1066             goto forward;
1067         if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
1068             /*
1069              * Pass the pkt to dummynet, which consumes it.
1070              * If shared, make a copy and keep the original.
1071              */
1072             if (shared) {
1073                 m = m_copypacket(m0, M_DONTWAIT);
1074                 if (m == NULL) {        /* copy failed, give up */
1075                     bdg_dropped++;
1076                     return NULL;
1077                 }
1078             } else {
1079                 m = m0 ; /* pass the original to dummynet */
1080                 m0 = NULL ; /* and nothing back to the caller */
1081             }
1082
1083             args.oif = real_dst;
1084             ip_dn_io_ptr(m, (i & 0xffff),DN_TO_BDG_FWD, &args);
1085             return m0;
1086         }
1087         /*
1088          * XXX at some point, add support for divert/forward actions.
1089          * If none of the above matches, we have to drop the packet.
1090          */
1091         bdg_ipfw_drops++;
1092         return m0;
1093     }
1094 forward:
1095     /*
1096      * Again, bring up the headers in case of shared bufs to avoid
1097      * corruptions in the future.
1098      */
1099     if (shared) {
1100         int i = min(m0->m_pkthdr.len, max_protohdr);
1101
1102         m0 = m_pullup(m0, i);
1103         if (m0 == NULL) {
1104             bdg_dropped++;
1105             return NULL;
1106         }
1107         /* NB: eh is not used below; no need to recalculate it */
1108     }
1109
1110     /*
1111      * now real_dst is used to determine the cluster where to forward.
1112      * For packets coming from ether_input, this is the one of the 'src'
1113      * interface, whereas for locally generated packets (src==NULL) it
1114      * is the cluster of the original destination interface, which
1115      * was already saved into real_dst.
1116      */
1117     if (src != NULL)
1118         real_dst = src;
1119
1120     last = NULL;
1121     if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_UNKNOWN) {
1122         /*
1123          * Scan all ports and send copies to all but the last.
1124          */
1125         IFNET_RLOCK();          /* XXX replace with generation # */
1126         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1127             if (bridge_ifok(ifp, src, real_dst)) {
1128                 if (last) {
1129                     /*
1130                      * At this point we know two interfaces need a copy
1131                      * of the packet (last + ifp) so we must create a
1132                      * copy to handoff to last.
1133                      */
1134                     m = m_copypacket(m0, M_DONTWAIT);
1135                     if (m == NULL) {
1136                         IFNET_RUNLOCK();
1137                         printf("%s: , m_copypacket failed!\n", __func__);
1138                         bdg_dropped++;
1139                         return m0;      /* the original is still there... */
1140                     }
1141                     if (IF_HANDOFF(&last->if_snd, m, last))
1142                         BDG_STAT(last, BDG_OUT);
1143                     else
1144                         bdg_dropped++;
1145                 }
1146                 last = ifp;
1147             }
1148         }
1149         IFNET_RUNLOCK();
1150     } else {
1151         if (bridge_ifok(dst, src, real_dst))
1152             last = dst;
1153     }
1154     if (last) {
1155         if (shared) {                   /* need to copy */
1156             m = m_copypacket(m0, M_DONTWAIT);
1157             if (m == NULL) {
1158                 printf("%s: sorry, m_copypacket failed!\n", __func__);
1159                 bdg_dropped++ ;
1160                 return m0;              /* the original is still there... */
1161             }
1162         } else {                        /* consume original */
1163             m = m0, m0 = NULL;
1164         }
1165         if (IF_HANDOFF(&last->if_snd, m, last))
1166             BDG_STAT(last, BDG_OUT);
1167         else
1168             bdg_dropped++;
1169     }
1170
1171     DDB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;
1172         if (bdg_fw_count != 0) bdg_fw_avg = bdg_fw_ticks/bdg_fw_count; )
1173     return m0;
1174 #undef EH_RESTORE
1175 }
1176
1177 /*
1178  * initialization of bridge code.
1179  */
1180 static int
1181 bdginit(void)
1182 {
1183     if (bootverbose)
1184             printf("BRIDGE %s loaded\n", bridge_version);
1185
1186     ifp2sc = malloc(BDG_MAX_PORTS * sizeof(struct bdg_softc),
1187                 M_IFADDR, M_WAITOK | M_ZERO );
1188     if (ifp2sc == NULL)
1189         return ENOMEM;
1190
1191     BDG_LOCK_INIT();
1192
1193     n_clusters = 0;
1194     clusters = NULL;
1195     do_bridge = 0;
1196
1197     bzero(&bdg_stats, sizeof(bdg_stats));
1198
1199     bridge_in_ptr = bridge_in;
1200     bdg_forward_ptr = bdg_forward;
1201     bdgtakeifaces_ptr = reconfigure_bridge;
1202
1203     bdgtakeifaces_ptr();                /* XXX does this do anything? */
1204
1205     callout_init(&bdg_callout, CALLOUT_MPSAFE);
1206     bdg_timeout(0);
1207     return 0 ;
1208 }
1209
1210 static void
1211 bdgdestroy(void)
1212 {
1213     bridge_in_ptr = NULL;
1214     bdg_forward_ptr = NULL;
1215     bdgtakeifaces_ptr = NULL;
1216
1217     callout_stop(&bdg_callout);
1218     BDG_LOCK();
1219     bridge_off();
1220
1221     if (ifp2sc) {
1222         free(ifp2sc, M_IFADDR);
1223         ifp2sc = NULL;
1224     }
1225     BDG_LOCK_DESTROY();
1226 }
1227
1228 /*
1229  * initialization code, both for static and dynamic loading.
1230  */
1231 static int
1232 bridge_modevent(module_t mod, int type, void *unused)
1233 {
1234         int err;
1235
1236         switch (type) {
1237         case MOD_LOAD:
1238                 if (BDG_LOADED)
1239                         err = EEXIST;
1240                 else
1241                         err = bdginit();
1242                 break;
1243         case MOD_UNLOAD:
1244                 do_bridge = 0;
1245                 bdgdestroy();
1246                 err = 0;
1247                 break;
1248         default:
1249                 err = EINVAL;
1250                 break;
1251         }
1252         return err;
1253 }
1254
1255 static moduledata_t bridge_mod = {
1256         "bridge",
1257         bridge_modevent,
1258         0
1259 };
1260
1261 DECLARE_MODULE(bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1262 MODULE_VERSION(bridge, 1);