1c398230bSWarner Losh /*-
2ed2dbd31SArchie Cobbs * Copyright (c) 2000 Whistle Communications, Inc.
3ed2dbd31SArchie Cobbs * All rights reserved.
4ed2dbd31SArchie Cobbs *
5ed2dbd31SArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and
6ed2dbd31SArchie Cobbs * redistribution of this software, in source or object code forms, with or
7ed2dbd31SArchie Cobbs * without modifications are expressly permitted by Whistle Communications;
8ed2dbd31SArchie Cobbs * provided, however, that:
9ed2dbd31SArchie Cobbs * 1. Any and all reproductions of the source or object code must include the
10ed2dbd31SArchie Cobbs * copyright notice above and the following disclaimer of warranties; and
11ed2dbd31SArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle
12ed2dbd31SArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE
13ed2dbd31SArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
14ed2dbd31SArchie Cobbs * such appears in the above copyright notice or in the software.
15ed2dbd31SArchie Cobbs *
16ed2dbd31SArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
17ed2dbd31SArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
18ed2dbd31SArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
19ed2dbd31SArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
20ed2dbd31SArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21ed2dbd31SArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
22ed2dbd31SArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
23ed2dbd31SArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
24ed2dbd31SArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
25ed2dbd31SArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
26ed2dbd31SArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27ed2dbd31SArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
28ed2dbd31SArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
29ed2dbd31SArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30ed2dbd31SArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31ed2dbd31SArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
32ed2dbd31SArchie Cobbs * OF SUCH DAMAGE.
33ed2dbd31SArchie Cobbs *
34ed2dbd31SArchie Cobbs * Author: Archie Cobbs <archie@freebsd.org>
35ed2dbd31SArchie Cobbs */
36ed2dbd31SArchie Cobbs
37ed2dbd31SArchie Cobbs /*
38ed2dbd31SArchie Cobbs * ng_bridge(4) netgraph node type
39ed2dbd31SArchie Cobbs *
40ed2dbd31SArchie Cobbs * The node performs standard intelligent Ethernet bridging over
41ed2dbd31SArchie Cobbs * each of its connected hooks, or links. A simple loop detection
42ed2dbd31SArchie Cobbs * algorithm is included which disables a link for priv->conf.loopTimeout
43ed2dbd31SArchie Cobbs * seconds when a host is seen to have jumped from one link to
44ed2dbd31SArchie Cobbs * another within priv->conf.minStableAge seconds.
45ed2dbd31SArchie Cobbs *
46ed2dbd31SArchie Cobbs * We keep a hashtable that maps Ethernet addresses to host info,
47ed2dbd31SArchie Cobbs * which is contained in struct ng_bridge_host's. These structures
48ed2dbd31SArchie Cobbs * tell us on which link the host may be found. A host's entry will
49ed2dbd31SArchie Cobbs * expire after priv->conf.maxStaleness seconds.
50ed2dbd31SArchie Cobbs *
51ed2dbd31SArchie Cobbs * This node is optimzed for stable networks, where machines jump
52ed2dbd31SArchie Cobbs * from one port to the other only rarely.
53ed2dbd31SArchie Cobbs */
54ed2dbd31SArchie Cobbs
55ed2dbd31SArchie Cobbs #include <sys/param.h>
56ed2dbd31SArchie Cobbs #include <sys/systm.h>
57ed2dbd31SArchie Cobbs #include <sys/kernel.h>
58385195c0SMarko Zec #include <sys/lock.h>
59ed2dbd31SArchie Cobbs #include <sys/malloc.h>
60ed2dbd31SArchie Cobbs #include <sys/mbuf.h>
61ed2dbd31SArchie Cobbs #include <sys/errno.h>
62385195c0SMarko Zec #include <sys/rwlock.h>
63ed2dbd31SArchie Cobbs #include <sys/syslog.h>
64ed2dbd31SArchie Cobbs #include <sys/socket.h>
65ed2dbd31SArchie Cobbs #include <sys/ctype.h>
6666c72859SLutz Donnerhacke #include <sys/types.h>
6766c72859SLutz Donnerhacke #include <sys/counter.h>
68ed2dbd31SArchie Cobbs
69ed2dbd31SArchie Cobbs #include <net/if.h>
7076039bc8SGleb Smirnoff #include <net/if_var.h>
71ed2dbd31SArchie Cobbs #include <net/ethernet.h>
72530c0060SRobert Watson #include <net/vnet.h>
73ed2dbd31SArchie Cobbs
74ed2dbd31SArchie Cobbs #include <netinet/in.h>
75ed2dbd31SArchie Cobbs #include <netgraph/ng_message.h>
76ed2dbd31SArchie Cobbs #include <netgraph/netgraph.h>
77ed2dbd31SArchie Cobbs #include <netgraph/ng_parse.h>
78ed2dbd31SArchie Cobbs #include <netgraph/ng_bridge.h>
79ed2dbd31SArchie Cobbs
809c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
81d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge",
82d745c852SEd Schouten "netgraph bridge node");
839c8c302fSJulian Elischer #else
849c8c302fSJulian Elischer #define M_NETGRAPH_BRIDGE M_NETGRAPH
859c8c302fSJulian Elischer #endif
869c8c302fSJulian Elischer
8766c72859SLutz Donnerhacke /* Counter based stats */
8866c72859SLutz Donnerhacke struct ng_bridge_link_kernel_stats {
8966c72859SLutz Donnerhacke counter_u64_t recvOctets; /* total octets rec'd on link */
9066c72859SLutz Donnerhacke counter_u64_t recvPackets; /* total pkts rec'd on link */
9166c72859SLutz Donnerhacke counter_u64_t recvMulticasts; /* multicast pkts rec'd on link */
9266c72859SLutz Donnerhacke counter_u64_t recvBroadcasts; /* broadcast pkts rec'd on link */
9366c72859SLutz Donnerhacke counter_u64_t recvUnknown; /* pkts rec'd with unknown dest addr */
9466c72859SLutz Donnerhacke counter_u64_t recvRunts; /* pkts rec'd less than 14 bytes */
9566c72859SLutz Donnerhacke counter_u64_t recvInvalid; /* pkts rec'd with bogus source addr */
9666c72859SLutz Donnerhacke counter_u64_t xmitOctets; /* total octets xmit'd on link */
9766c72859SLutz Donnerhacke counter_u64_t xmitPackets; /* total pkts xmit'd on link */
9866c72859SLutz Donnerhacke counter_u64_t xmitMulticasts; /* multicast pkts xmit'd on link */
9966c72859SLutz Donnerhacke counter_u64_t xmitBroadcasts; /* broadcast pkts xmit'd on link */
10066c72859SLutz Donnerhacke counter_u64_t loopDrops; /* pkts dropped due to loopback */
101f6e0c471SLutz Donnerhacke u_int64_t loopDetects; /* number of loop detections */
10266c72859SLutz Donnerhacke counter_u64_t memoryFailures; /* times couldn't get mem or mbuf */
10366c72859SLutz Donnerhacke };
10466c72859SLutz Donnerhacke
105ed2dbd31SArchie Cobbs /* Per-link private data */
106ed2dbd31SArchie Cobbs struct ng_bridge_link {
107ed2dbd31SArchie Cobbs hook_p hook; /* netgraph hook */
108ed2dbd31SArchie Cobbs u_int16_t loopCount; /* loop ignore timer */
109f961caf2SLutz Donnerhacke unsigned int learnMac : 1, /* autolearn macs */
110f961caf2SLutz Donnerhacke sendUnknown : 1;/* send unknown macs out */
11166c72859SLutz Donnerhacke struct ng_bridge_link_kernel_stats stats; /* link stats */
112ed2dbd31SArchie Cobbs };
1136117aa58SLutz Donnerhacke typedef struct ng_bridge_link const *link_cp; /* read only access */
114ed2dbd31SArchie Cobbs
115ed2dbd31SArchie Cobbs /* Per-node private data */
116ed2dbd31SArchie Cobbs struct ng_bridge_private {
117ed2dbd31SArchie Cobbs struct ng_bridge_bucket *tab; /* hash table bucket array */
118ed2dbd31SArchie Cobbs struct ng_bridge_config conf; /* node configuration */
119ed2dbd31SArchie Cobbs node_p node; /* netgraph node */
120ed2dbd31SArchie Cobbs u_int numHosts; /* num entries in table */
121ed2dbd31SArchie Cobbs u_int numBuckets; /* num buckets in table */
122ed2dbd31SArchie Cobbs u_int hashMask; /* numBuckets - 1 */
123ed2dbd31SArchie Cobbs int numLinks; /* num connected links */
124c869d905SLutz Donnerhacke unsigned int persistent : 1, /* can exist w/o hooks */
125c869d905SLutz Donnerhacke sendUnknown : 1;/* links receive unknowns by default */
126ed2dbd31SArchie Cobbs struct callout timer; /* one second periodic timer */
127*86a6393aSDavid Marker struct unrhdr *linkUnit; /* link unit number allocator */
128*86a6393aSDavid Marker struct unrhdr *uplinkUnit; /* uplink unit number allocator */
129ed2dbd31SArchie Cobbs };
130ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p;
1316117aa58SLutz Donnerhacke typedef struct ng_bridge_private const *priv_cp; /* read only access */
132ed2dbd31SArchie Cobbs
133ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */
134ccf4cd2eSLutz Donnerhacke struct ng_bridge_host {
135ccf4cd2eSLutz Donnerhacke u_char addr[6]; /* ethernet address */
136ccf4cd2eSLutz Donnerhacke link_p link; /* link where addr can be found */
137ccf4cd2eSLutz Donnerhacke u_int16_t age; /* seconds ago entry was created */
138ccf4cd2eSLutz Donnerhacke u_int16_t staleness; /* seconds ago host last heard from */
139ccf4cd2eSLutz Donnerhacke SLIST_ENTRY(ng_bridge_host) next; /* next entry in bucket */
140ed2dbd31SArchie Cobbs };
141ed2dbd31SArchie Cobbs
142ed2dbd31SArchie Cobbs /* Hash table bucket declaration */
143ccf4cd2eSLutz Donnerhacke SLIST_HEAD(ng_bridge_bucket, ng_bridge_host);
144ed2dbd31SArchie Cobbs
145*86a6393aSDavid Marker /* [up]link prefix matching */
146*86a6393aSDavid Marker struct ng_link_prefix {
147*86a6393aSDavid Marker const char * const prefix;
148*86a6393aSDavid Marker size_t len;
149*86a6393aSDavid Marker };
150*86a6393aSDavid Marker
151*86a6393aSDavid Marker static const struct ng_link_prefix link_pfx = {
152*86a6393aSDavid Marker .prefix = NG_BRIDGE_HOOK_LINK_PREFIX,
153*86a6393aSDavid Marker .len = sizeof(NG_BRIDGE_HOOK_LINK_PREFIX) - 1,
154*86a6393aSDavid Marker };
155*86a6393aSDavid Marker static const struct ng_link_prefix uplink_pfx = {
156*86a6393aSDavid Marker .prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX,
157*86a6393aSDavid Marker .len = sizeof(NG_BRIDGE_HOOK_UPLINK_PREFIX) - 1,
158*86a6393aSDavid Marker };
159*86a6393aSDavid Marker
160ed2dbd31SArchie Cobbs /* Netgraph node methods */
161ed2dbd31SArchie Cobbs static ng_constructor_t ng_bridge_constructor;
162ed2dbd31SArchie Cobbs static ng_rcvmsg_t ng_bridge_rcvmsg;
163069154d5SJulian Elischer static ng_shutdown_t ng_bridge_shutdown;
164ed2dbd31SArchie Cobbs static ng_newhook_t ng_bridge_newhook;
165ed2dbd31SArchie Cobbs static ng_rcvdata_t ng_bridge_rcvdata;
166ed2dbd31SArchie Cobbs static ng_disconnect_t ng_bridge_disconnect;
167ed2dbd31SArchie Cobbs
168ed2dbd31SArchie Cobbs /* Other internal functions */
169*86a6393aSDavid Marker static const struct ng_link_prefix *ng_get_link_prefix(const char *name);
170e0e3ded7SMark Johnston static void ng_bridge_free_link(link_p link);
1716117aa58SLutz Donnerhacke static struct ng_bridge_host *ng_bridge_get(priv_cp priv, const u_char *addr);
172631cabbaSGleb Smirnoff static int ng_bridge_put(priv_p priv, const u_char *addr, link_p link);
173ed2dbd31SArchie Cobbs static void ng_bridge_rehash(priv_p priv);
174631cabbaSGleb Smirnoff static void ng_bridge_remove_hosts(priv_p priv, link_p link);
175e0d32af7SGleb Smirnoff static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
1766117aa58SLutz Donnerhacke static const char *ng_bridge_nodename(node_cp node);
177ed2dbd31SArchie Cobbs
178ed2dbd31SArchie Cobbs /* Ethernet broadcast */
179ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
180ed2dbd31SArchie Cobbs { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
181ed2dbd31SArchie Cobbs
182ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
183ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \
184ed2dbd31SArchie Cobbs == ((const u_int32_t *)(b))[0] \
185ed2dbd31SArchie Cobbs && ((const u_int16_t *)(a))[2] \
186ed2dbd31SArchie Cobbs == ((const u_int16_t *)(b))[2])
187ed2dbd31SArchie Cobbs
188ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */
189ed2dbd31SArchie Cobbs #define MIN_BUCKETS (1 << 5) /* 32 */
190ed2dbd31SArchie Cobbs #define MAX_BUCKETS (1 << 14) /* 16384 */
191ed2dbd31SArchie Cobbs
192ed2dbd31SArchie Cobbs /* Configuration default values */
193ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT 60
194ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */
195ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE 1
196ed2dbd31SArchie Cobbs
197ed2dbd31SArchie Cobbs /******************************************************************
198ed2dbd31SArchie Cobbs NETGRAPH PARSE TYPES
199ed2dbd31SArchie Cobbs ******************************************************************/
200ed2dbd31SArchie Cobbs
201ed2dbd31SArchie Cobbs /*
202ed2dbd31SArchie Cobbs * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
203ed2dbd31SArchie Cobbs */
204ed2dbd31SArchie Cobbs static int
ng_bridge_getTableLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)205ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type,
206ed2dbd31SArchie Cobbs const u_char *start, const u_char *buf)
207ed2dbd31SArchie Cobbs {
208ed2dbd31SArchie Cobbs const struct ng_bridge_host_ary *const hary
209ed2dbd31SArchie Cobbs = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
210ed2dbd31SArchie Cobbs
211ed2dbd31SArchie Cobbs return hary->numHosts;
212ed2dbd31SArchie Cobbs }
213ed2dbd31SArchie Cobbs
214ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */
215f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
2168c7e4101SRuslan Ermilov = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
217ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = {
218ed2dbd31SArchie Cobbs &ng_parse_struct_type,
219f0184ff8SArchie Cobbs &ng_bridge_host_type_fields
220ed2dbd31SArchie Cobbs };
221ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = {
222ed2dbd31SArchie Cobbs &ng_bridge_host_type,
223ed2dbd31SArchie Cobbs ng_bridge_getTableLength
224ed2dbd31SArchie Cobbs };
225ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = {
226ed2dbd31SArchie Cobbs &ng_parse_array_type,
227ed2dbd31SArchie Cobbs &ng_bridge_hary_type_info
228ed2dbd31SArchie Cobbs };
229f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
230ed2dbd31SArchie Cobbs = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
231ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = {
232ed2dbd31SArchie Cobbs &ng_parse_struct_type,
233f0184ff8SArchie Cobbs &ng_bridge_host_ary_type_fields
234ed2dbd31SArchie Cobbs };
235ed2dbd31SArchie Cobbs
236ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */
237f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
238631cabbaSGleb Smirnoff = NG_BRIDGE_CONFIG_TYPE_INFO;
239ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = {
240ed2dbd31SArchie Cobbs &ng_parse_struct_type,
241f0184ff8SArchie Cobbs &ng_bridge_config_type_fields
242ed2dbd31SArchie Cobbs };
243ed2dbd31SArchie Cobbs
244ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */
245f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
246f0184ff8SArchie Cobbs = NG_BRIDGE_STATS_TYPE_INFO;
247ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = {
248ed2dbd31SArchie Cobbs &ng_parse_struct_type,
249f0184ff8SArchie Cobbs &ng_bridge_stats_type_fields
250ed2dbd31SArchie Cobbs };
251b1bd4473SLutz Donnerhacke /* Parse type for struct ng_bridge_move_host */
252b1bd4473SLutz Donnerhacke static const struct ng_parse_struct_field ng_bridge_move_host_type_fields[]
253b1bd4473SLutz Donnerhacke = NG_BRIDGE_MOVE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
254b1bd4473SLutz Donnerhacke static const struct ng_parse_type ng_bridge_move_host_type = {
255b1bd4473SLutz Donnerhacke &ng_parse_struct_type,
256b1bd4473SLutz Donnerhacke &ng_bridge_move_host_type_fields
257b1bd4473SLutz Donnerhacke };
258ed2dbd31SArchie Cobbs
259ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
260ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = {
261ed2dbd31SArchie Cobbs {
262ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE,
263ed2dbd31SArchie Cobbs NGM_BRIDGE_SET_CONFIG,
264ed2dbd31SArchie Cobbs "setconfig",
265ed2dbd31SArchie Cobbs &ng_bridge_config_type,
266ed2dbd31SArchie Cobbs NULL
267ed2dbd31SArchie Cobbs },
268ed2dbd31SArchie Cobbs {
269ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE,
270ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_CONFIG,
271ed2dbd31SArchie Cobbs "getconfig",
272ed2dbd31SArchie Cobbs NULL,
273ed2dbd31SArchie Cobbs &ng_bridge_config_type
274ed2dbd31SArchie Cobbs },
275ed2dbd31SArchie Cobbs {
276ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE,
277ed2dbd31SArchie Cobbs NGM_BRIDGE_RESET,
278ed2dbd31SArchie Cobbs "reset",
279ed2dbd31SArchie Cobbs NULL,
280ed2dbd31SArchie Cobbs NULL
281ed2dbd31SArchie Cobbs },
282ed2dbd31SArchie Cobbs {
283ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE,
284ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_STATS,
285ed2dbd31SArchie Cobbs "getstats",
286ed2dbd31SArchie Cobbs &ng_parse_uint32_type,
287ed2dbd31SArchie Cobbs &ng_bridge_stats_type
288ed2dbd31SArchie Cobbs },
289ed2dbd31SArchie Cobbs {
290ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE,
291ed2dbd31SArchie Cobbs NGM_BRIDGE_CLR_STATS,
292ed2dbd31SArchie Cobbs "clrstats",
293ed2dbd31SArchie Cobbs &ng_parse_uint32_type,
294ed2dbd31SArchie Cobbs NULL
295ed2dbd31SArchie Cobbs },
296ed2dbd31SArchie Cobbs {
297ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE,
298ed2dbd31SArchie Cobbs NGM_BRIDGE_GETCLR_STATS,
299ed2dbd31SArchie Cobbs "getclrstats",
300ed2dbd31SArchie Cobbs &ng_parse_uint32_type,
301ed2dbd31SArchie Cobbs &ng_bridge_stats_type
302ed2dbd31SArchie Cobbs },
303ed2dbd31SArchie Cobbs {
304ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE,
305ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_TABLE,
306ed2dbd31SArchie Cobbs "gettable",
307ed2dbd31SArchie Cobbs NULL,
308ed2dbd31SArchie Cobbs &ng_bridge_host_ary_type
309ed2dbd31SArchie Cobbs },
310f8aab721SMarko Zec {
311f8aab721SMarko Zec NGM_BRIDGE_COOKIE,
312f8aab721SMarko Zec NGM_BRIDGE_SET_PERSISTENT,
313f8aab721SMarko Zec "setpersistent",
314f8aab721SMarko Zec NULL,
315f8aab721SMarko Zec NULL
316f8aab721SMarko Zec },
317b1bd4473SLutz Donnerhacke {
318b1bd4473SLutz Donnerhacke NGM_BRIDGE_COOKIE,
319b1bd4473SLutz Donnerhacke NGM_BRIDGE_MOVE_HOST,
320b1bd4473SLutz Donnerhacke "movehost",
321b1bd4473SLutz Donnerhacke &ng_bridge_move_host_type,
322b1bd4473SLutz Donnerhacke NULL
323b1bd4473SLutz Donnerhacke },
324ed2dbd31SArchie Cobbs { 0 }
325ed2dbd31SArchie Cobbs };
326ed2dbd31SArchie Cobbs
327ed2dbd31SArchie Cobbs /* Node type descriptor */
328ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = {
329f8aae777SJulian Elischer .version = NG_ABI_VERSION,
330f8aae777SJulian Elischer .name = NG_BRIDGE_NODE_TYPE,
331f8aae777SJulian Elischer .constructor = ng_bridge_constructor,
332f8aae777SJulian Elischer .rcvmsg = ng_bridge_rcvmsg,
333f8aae777SJulian Elischer .shutdown = ng_bridge_shutdown,
334f8aae777SJulian Elischer .newhook = ng_bridge_newhook,
335f8aae777SJulian Elischer .rcvdata = ng_bridge_rcvdata,
336f8aae777SJulian Elischer .disconnect = ng_bridge_disconnect,
337f8aae777SJulian Elischer .cmdlist = ng_bridge_cmdlist,
338ed2dbd31SArchie Cobbs };
339a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
340ed2dbd31SArchie Cobbs
341ed2dbd31SArchie Cobbs /******************************************************************
342ed2dbd31SArchie Cobbs NETGRAPH NODE METHODS
343ed2dbd31SArchie Cobbs ******************************************************************/
344ed2dbd31SArchie Cobbs
345ed2dbd31SArchie Cobbs /*
346ed2dbd31SArchie Cobbs * Node constructor
347ed2dbd31SArchie Cobbs */
348ed2dbd31SArchie Cobbs static int
ng_bridge_constructor(node_p node)349069154d5SJulian Elischer ng_bridge_constructor(node_p node)
350ed2dbd31SArchie Cobbs {
351ed2dbd31SArchie Cobbs priv_p priv;
352ed2dbd31SArchie Cobbs
353ed2dbd31SArchie Cobbs /* Allocate and initialize private info */
354674d86bfSGleb Smirnoff priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
355e0d32af7SGleb Smirnoff ng_callout_init(&priv->timer);
356ed2dbd31SArchie Cobbs
357ed2dbd31SArchie Cobbs /* Allocate and initialize hash table, etc. */
358e11e3f18SDag-Erling Smørgrav priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
359674d86bfSGleb Smirnoff M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
360ed2dbd31SArchie Cobbs priv->numBuckets = MIN_BUCKETS;
361ed2dbd31SArchie Cobbs priv->hashMask = MIN_BUCKETS - 1;
362ed2dbd31SArchie Cobbs priv->conf.debugLevel = 1;
363ed2dbd31SArchie Cobbs priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
364ed2dbd31SArchie Cobbs priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
365ed2dbd31SArchie Cobbs priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
366c869d905SLutz Donnerhacke priv->sendUnknown = 1; /* classic bridge */
367ed2dbd31SArchie Cobbs
36830400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv);
369069154d5SJulian Elischer priv->node = node;
370ed2dbd31SArchie Cobbs
371*86a6393aSDavid Marker /* Allocators for links. Historically "uplink0" is not allowed. */
372*86a6393aSDavid Marker priv->linkUnit = new_unrhdr(0, INT_MAX, NULL);
373*86a6393aSDavid Marker priv->uplinkUnit = new_unrhdr(1, INT_MAX, NULL);
374*86a6393aSDavid Marker
3756c12c2b1SArchie Cobbs /* Start timer; timer is always running while node is alive */
376e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
3776c12c2b1SArchie Cobbs
3786c12c2b1SArchie Cobbs /* Done */
379ed2dbd31SArchie Cobbs return (0);
380ed2dbd31SArchie Cobbs }
381ed2dbd31SArchie Cobbs
382ed2dbd31SArchie Cobbs /*
383ed2dbd31SArchie Cobbs * Method for attaching a new hook
384ed2dbd31SArchie Cobbs */
385ed2dbd31SArchie Cobbs static int
ng_bridge_newhook(node_p node,hook_p hook,const char * name)386ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name)
387ed2dbd31SArchie Cobbs {
38830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
389631cabbaSGleb Smirnoff link_p link;
390f961caf2SLutz Donnerhacke bool isUplink;
391*86a6393aSDavid Marker uint32_t linkNum;
392*86a6393aSDavid Marker struct unrhdr *unit;
393f961caf2SLutz Donnerhacke
394*86a6393aSDavid Marker const struct ng_link_prefix *pfx = ng_get_link_prefix(name);
395*86a6393aSDavid Marker if (pfx == NULL)
396*86a6393aSDavid Marker return (EINVAL); /* not a valid prefix */
397f961caf2SLutz Donnerhacke
398*86a6393aSDavid Marker isUplink = (pfx == &uplink_pfx);
399*86a6393aSDavid Marker unit = isUplink ? priv->uplinkUnit : priv->linkUnit;
400ed2dbd31SArchie Cobbs
401*86a6393aSDavid Marker if (strlen(name) > pfx->len) { /* given number */
402*86a6393aSDavid Marker char linkName[NG_HOOKSIZ];
403*86a6393aSDavid Marker int rvnum __diagused;
404*86a6393aSDavid Marker
405*86a6393aSDavid Marker linkNum = strtoul(name + pfx->len, NULL, 10);
406*86a6393aSDavid Marker /* Validate by comparing against the reconstucted name. */
407*86a6393aSDavid Marker snprintf(linkName, sizeof(linkName), "%s%u", pfx->prefix,
408*86a6393aSDavid Marker linkNum);
409631cabbaSGleb Smirnoff if (strcmp(linkName, name) != 0)
410ed2dbd31SArchie Cobbs return (EINVAL);
411f961caf2SLutz Donnerhacke if (linkNum == 0 && isUplink)
412f961caf2SLutz Donnerhacke return (EINVAL);
413*86a6393aSDavid Marker rvnum = alloc_unr_specific(unit, linkNum);
414*86a6393aSDavid Marker MPASS(rvnum == linkNum);
415*86a6393aSDavid Marker } else {
416*86a6393aSDavid Marker /* auto-assign and update hook name */
417*86a6393aSDavid Marker linkNum = alloc_unr(unit);
418*86a6393aSDavid Marker MPASS(linkNum != -1);
419*86a6393aSDavid Marker snprintf(NG_HOOK_NAME(hook), NG_HOOKSIZ, "%s%u", pfx->prefix,
420*86a6393aSDavid Marker linkNum);
421*86a6393aSDavid Marker }
422f961caf2SLutz Donnerhacke
423*86a6393aSDavid Marker if (NG_PEER_NODE(hook) == node) {
424*86a6393aSDavid Marker free_unr(unit, linkNum);
425631cabbaSGleb Smirnoff return (ELOOP);
426*86a6393aSDavid Marker }
427631cabbaSGleb Smirnoff
428e0e3ded7SMark Johnston link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
429*86a6393aSDavid Marker if (link == NULL) {
430*86a6393aSDavid Marker free_unr(unit, linkNum);
431e0e3ded7SMark Johnston return (ENOMEM);
432*86a6393aSDavid Marker }
433f961caf2SLutz Donnerhacke
434e0e3ded7SMark Johnston #define NG_BRIDGE_COUNTER_ALLOC(f) do { \
435e0e3ded7SMark Johnston link->stats.f = counter_u64_alloc(M_NOWAIT); \
436e0e3ded7SMark Johnston if (link->stats.f == NULL) \
437e0e3ded7SMark Johnston goto nomem; \
438e0e3ded7SMark Johnston } while (0)
439e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvOctets);
440e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvPackets);
441e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvMulticasts);
442e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvBroadcasts);
443e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvUnknown);
444e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvRunts);
445e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvInvalid);
446e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitOctets);
447e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitPackets);
448e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitMulticasts);
449e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitBroadcasts);
450e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(loopDrops);
451e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(memoryFailures);
452e0e3ded7SMark Johnston #undef NG_BRIDGE_COUNTER_ALLOC
45366c72859SLutz Donnerhacke
454631cabbaSGleb Smirnoff link->hook = hook;
455f961caf2SLutz Donnerhacke if (isUplink) {
456f961caf2SLutz Donnerhacke link->learnMac = 0;
457f961caf2SLutz Donnerhacke link->sendUnknown = 1;
458c869d905SLutz Donnerhacke if (priv->numLinks == 0) /* if the first link is an uplink */
459c869d905SLutz Donnerhacke priv->sendUnknown = 0; /* switch to restrictive mode */
460f961caf2SLutz Donnerhacke } else {
461f961caf2SLutz Donnerhacke link->learnMac = 1;
462c869d905SLutz Donnerhacke link->sendUnknown = priv->sendUnknown;
463f961caf2SLutz Donnerhacke }
464f961caf2SLutz Donnerhacke
465631cabbaSGleb Smirnoff NG_HOOK_SET_PRIVATE(hook, link);
466ed2dbd31SArchie Cobbs priv->numLinks++;
467ed2dbd31SArchie Cobbs return (0);
468e0e3ded7SMark Johnston
469e0e3ded7SMark Johnston nomem:
470*86a6393aSDavid Marker free_unr(unit, linkNum);
471e0e3ded7SMark Johnston ng_bridge_free_link(link);
472e0e3ded7SMark Johnston return (ENOMEM);
473ed2dbd31SArchie Cobbs }
474ed2dbd31SArchie Cobbs
475ed2dbd31SArchie Cobbs /*
476ed2dbd31SArchie Cobbs * Receive a control message
477ed2dbd31SArchie Cobbs */
478e0e3ded7SMark Johnston static void
ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats * p)479e0e3ded7SMark Johnston ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats *p)
48066c72859SLutz Donnerhacke {
48166c72859SLutz Donnerhacke counter_u64_zero(p->recvOctets);
48266c72859SLutz Donnerhacke counter_u64_zero(p->recvPackets);
48366c72859SLutz Donnerhacke counter_u64_zero(p->recvMulticasts);
48466c72859SLutz Donnerhacke counter_u64_zero(p->recvBroadcasts);
48566c72859SLutz Donnerhacke counter_u64_zero(p->recvUnknown);
48666c72859SLutz Donnerhacke counter_u64_zero(p->recvRunts);
48766c72859SLutz Donnerhacke counter_u64_zero(p->recvInvalid);
48866c72859SLutz Donnerhacke counter_u64_zero(p->xmitOctets);
48966c72859SLutz Donnerhacke counter_u64_zero(p->xmitPackets);
49066c72859SLutz Donnerhacke counter_u64_zero(p->xmitMulticasts);
49166c72859SLutz Donnerhacke counter_u64_zero(p->xmitBroadcasts);
49266c72859SLutz Donnerhacke counter_u64_zero(p->loopDrops);
493f6e0c471SLutz Donnerhacke p->loopDetects = 0;
49466c72859SLutz Donnerhacke counter_u64_zero(p->memoryFailures);
495e0e3ded7SMark Johnston }
496e0e3ded7SMark Johnston
497e0e3ded7SMark Johnston static void
ng_bridge_free_link(link_p link)498e0e3ded7SMark Johnston ng_bridge_free_link(link_p link)
499e0e3ded7SMark Johnston {
500e0e3ded7SMark Johnston counter_u64_free(link->stats.recvOctets);
501e0e3ded7SMark Johnston counter_u64_free(link->stats.recvPackets);
502e0e3ded7SMark Johnston counter_u64_free(link->stats.recvMulticasts);
503e0e3ded7SMark Johnston counter_u64_free(link->stats.recvBroadcasts);
504e0e3ded7SMark Johnston counter_u64_free(link->stats.recvUnknown);
505e0e3ded7SMark Johnston counter_u64_free(link->stats.recvRunts);
506e0e3ded7SMark Johnston counter_u64_free(link->stats.recvInvalid);
507e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitOctets);
508e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitPackets);
509e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitMulticasts);
510e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitBroadcasts);
511e0e3ded7SMark Johnston counter_u64_free(link->stats.loopDrops);
512e0e3ded7SMark Johnston counter_u64_free(link->stats.memoryFailures);
513e0e3ded7SMark Johnston free(link, M_NETGRAPH_BRIDGE);
514e0e3ded7SMark Johnston }
51566c72859SLutz Donnerhacke
516ed2dbd31SArchie Cobbs static int
ng_bridge_reset_link(hook_p hook,void * arg __unused)5170b951c55SGleb Smirnoff ng_bridge_reset_link(hook_p hook, void *arg __unused)
518631cabbaSGleb Smirnoff {
519631cabbaSGleb Smirnoff link_p priv = NG_HOOK_PRIVATE(hook);
520631cabbaSGleb Smirnoff
521631cabbaSGleb Smirnoff priv->loopCount = 0;
52266c72859SLutz Donnerhacke ng_bridge_clear_link_stats(&priv->stats);
5230b951c55SGleb Smirnoff return (1);
524631cabbaSGleb Smirnoff }
525631cabbaSGleb Smirnoff
526631cabbaSGleb Smirnoff static int
ng_bridge_rcvmsg(node_p node,item_p item,hook_p lasthook)527069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
528ed2dbd31SArchie Cobbs {
52930400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
530ed2dbd31SArchie Cobbs struct ng_mesg *resp = NULL;
531ed2dbd31SArchie Cobbs int error = 0;
532069154d5SJulian Elischer struct ng_mesg *msg;
533ed2dbd31SArchie Cobbs
534069154d5SJulian Elischer NGI_GET_MSG(item, msg);
535ed2dbd31SArchie Cobbs switch (msg->header.typecookie) {
536ed2dbd31SArchie Cobbs case NGM_BRIDGE_COOKIE:
537ed2dbd31SArchie Cobbs switch (msg->header.cmd) {
538ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_CONFIG:
539ed2dbd31SArchie Cobbs {
540ed2dbd31SArchie Cobbs struct ng_bridge_config *conf;
541ed2dbd31SArchie Cobbs
542ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg,
543ed2dbd31SArchie Cobbs sizeof(struct ng_bridge_config), M_NOWAIT);
544ed2dbd31SArchie Cobbs if (resp == NULL) {
545ed2dbd31SArchie Cobbs error = ENOMEM;
546ed2dbd31SArchie Cobbs break;
547ed2dbd31SArchie Cobbs }
548ed2dbd31SArchie Cobbs conf = (struct ng_bridge_config *)resp->data;
549ed2dbd31SArchie Cobbs *conf = priv->conf; /* no sanity checking needed */
550ed2dbd31SArchie Cobbs break;
551ed2dbd31SArchie Cobbs }
552ed2dbd31SArchie Cobbs case NGM_BRIDGE_SET_CONFIG:
553ed2dbd31SArchie Cobbs {
554ed2dbd31SArchie Cobbs struct ng_bridge_config *conf;
555ed2dbd31SArchie Cobbs
556ed2dbd31SArchie Cobbs if (msg->header.arglen
557ed2dbd31SArchie Cobbs != sizeof(struct ng_bridge_config)) {
558ed2dbd31SArchie Cobbs error = EINVAL;
559ed2dbd31SArchie Cobbs break;
560ed2dbd31SArchie Cobbs }
561ed2dbd31SArchie Cobbs conf = (struct ng_bridge_config *)msg->data;
562ed2dbd31SArchie Cobbs priv->conf = *conf;
563ed2dbd31SArchie Cobbs break;
564ed2dbd31SArchie Cobbs }
565ed2dbd31SArchie Cobbs case NGM_BRIDGE_RESET:
566ed2dbd31SArchie Cobbs {
567ed2dbd31SArchie Cobbs /* Flush all entries in the hash table */
568631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv, NULL);
569ed2dbd31SArchie Cobbs
570ed2dbd31SArchie Cobbs /* Reset all loop detection counters and stats */
5716d5f002eSJohn Baldwin NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL);
572ed2dbd31SArchie Cobbs break;
573ed2dbd31SArchie Cobbs }
574ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_STATS:
575ed2dbd31SArchie Cobbs case NGM_BRIDGE_CLR_STATS:
576ed2dbd31SArchie Cobbs case NGM_BRIDGE_GETCLR_STATS:
577ed2dbd31SArchie Cobbs {
578631cabbaSGleb Smirnoff hook_p hook;
579631cabbaSGleb Smirnoff link_p link;
580631cabbaSGleb Smirnoff char linkName[NG_HOOKSIZ];
581f961caf2SLutz Donnerhacke int linkNum;
582ed2dbd31SArchie Cobbs
583ed2dbd31SArchie Cobbs /* Get link number */
584ed2dbd31SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) {
585ed2dbd31SArchie Cobbs error = EINVAL;
586ed2dbd31SArchie Cobbs break;
587ed2dbd31SArchie Cobbs }
588f961caf2SLutz Donnerhacke linkNum = *((int32_t *)msg->data);
589f961caf2SLutz Donnerhacke if (linkNum < 0)
590631cabbaSGleb Smirnoff snprintf(linkName, sizeof(linkName),
591f961caf2SLutz Donnerhacke "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum);
592f961caf2SLutz Donnerhacke else
593f961caf2SLutz Donnerhacke snprintf(linkName, sizeof(linkName),
594f961caf2SLutz Donnerhacke "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum);
595631cabbaSGleb Smirnoff
596631cabbaSGleb Smirnoff if ((hook = ng_findhook(node, linkName)) == NULL) {
597ed2dbd31SArchie Cobbs error = ENOTCONN;
598ed2dbd31SArchie Cobbs break;
599ed2dbd31SArchie Cobbs }
600631cabbaSGleb Smirnoff link = NG_HOOK_PRIVATE(hook);
601ed2dbd31SArchie Cobbs
602ed2dbd31SArchie Cobbs /* Get/clear stats */
603ed2dbd31SArchie Cobbs if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
60466c72859SLutz Donnerhacke struct ng_bridge_link_stats *rs;
60566c72859SLutz Donnerhacke
606ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg,
607ed2dbd31SArchie Cobbs sizeof(link->stats), M_NOWAIT);
608ed2dbd31SArchie Cobbs if (resp == NULL) {
609ed2dbd31SArchie Cobbs error = ENOMEM;
610ed2dbd31SArchie Cobbs break;
611ed2dbd31SArchie Cobbs }
61266c72859SLutz Donnerhacke rs = (struct ng_bridge_link_stats *)resp->data;
61366c72859SLutz Donnerhacke #define FETCH(x) rs->x = counter_u64_fetch(link->stats.x)
61466c72859SLutz Donnerhacke FETCH(recvOctets);
61566c72859SLutz Donnerhacke FETCH(recvPackets);
61666c72859SLutz Donnerhacke FETCH(recvMulticasts);
61766c72859SLutz Donnerhacke FETCH(recvBroadcasts);
61866c72859SLutz Donnerhacke FETCH(recvUnknown);
61966c72859SLutz Donnerhacke FETCH(recvRunts);
62066c72859SLutz Donnerhacke FETCH(recvInvalid);
62166c72859SLutz Donnerhacke FETCH(xmitOctets);
62266c72859SLutz Donnerhacke FETCH(xmitPackets);
62366c72859SLutz Donnerhacke FETCH(xmitMulticasts);
62466c72859SLutz Donnerhacke FETCH(xmitBroadcasts);
62566c72859SLutz Donnerhacke FETCH(loopDrops);
626f6e0c471SLutz Donnerhacke rs->loopDetects = link->stats.loopDetects;
62766c72859SLutz Donnerhacke FETCH(memoryFailures);
62866c72859SLutz Donnerhacke #undef FETCH
629ed2dbd31SArchie Cobbs }
630ed2dbd31SArchie Cobbs if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
63166c72859SLutz Donnerhacke ng_bridge_clear_link_stats(&link->stats);
632ed2dbd31SArchie Cobbs break;
633ed2dbd31SArchie Cobbs }
634ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_TABLE:
635ed2dbd31SArchie Cobbs {
636ed2dbd31SArchie Cobbs struct ng_bridge_host_ary *ary;
637ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *host;
638ed2dbd31SArchie Cobbs int i = 0, bucket;
639ed2dbd31SArchie Cobbs
640ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*ary)
641ed2dbd31SArchie Cobbs + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
642ed2dbd31SArchie Cobbs if (resp == NULL) {
643ed2dbd31SArchie Cobbs error = ENOMEM;
644ed2dbd31SArchie Cobbs break;
645ed2dbd31SArchie Cobbs }
646ed2dbd31SArchie Cobbs ary = (struct ng_bridge_host_ary *)resp->data;
647ed2dbd31SArchie Cobbs ary->numHosts = priv->numHosts;
648ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) {
649ccf4cd2eSLutz Donnerhacke SLIST_FOREACH(host, &priv->tab[bucket], next) {
650631cabbaSGleb Smirnoff memcpy(ary->hosts[i].addr,
651ccf4cd2eSLutz Donnerhacke host->addr,
652631cabbaSGleb Smirnoff sizeof(ary->hosts[i].addr));
653ccf4cd2eSLutz Donnerhacke ary->hosts[i].age = host->age;
654ccf4cd2eSLutz Donnerhacke ary->hosts[i].staleness = host->staleness;
655631cabbaSGleb Smirnoff strncpy(ary->hosts[i].hook,
656ccf4cd2eSLutz Donnerhacke NG_HOOK_NAME(host->link->hook),
657631cabbaSGleb Smirnoff sizeof(ary->hosts[i].hook));
658631cabbaSGleb Smirnoff i++;
659631cabbaSGleb Smirnoff }
660ed2dbd31SArchie Cobbs }
661ed2dbd31SArchie Cobbs break;
662ed2dbd31SArchie Cobbs }
663f8aab721SMarko Zec case NGM_BRIDGE_SET_PERSISTENT:
664f8aab721SMarko Zec {
665f8aab721SMarko Zec priv->persistent = 1;
666f8aab721SMarko Zec break;
667f8aab721SMarko Zec }
668b1bd4473SLutz Donnerhacke case NGM_BRIDGE_MOVE_HOST:
669b1bd4473SLutz Donnerhacke {
670b1bd4473SLutz Donnerhacke struct ng_bridge_move_host *mh;
671b1bd4473SLutz Donnerhacke hook_p hook;
672b1bd4473SLutz Donnerhacke
673b1bd4473SLutz Donnerhacke if (msg->header.arglen < sizeof(*mh)) {
674b1bd4473SLutz Donnerhacke error = EINVAL;
675b1bd4473SLutz Donnerhacke break;
676b1bd4473SLutz Donnerhacke }
677b1bd4473SLutz Donnerhacke mh = (struct ng_bridge_move_host *)msg->data;
678b1bd4473SLutz Donnerhacke hook = (mh->hook[0] == 0)
679b1bd4473SLutz Donnerhacke ? lasthook
680b1bd4473SLutz Donnerhacke : ng_findhook(node, mh->hook);
681b1bd4473SLutz Donnerhacke if (hook == NULL) {
682b1bd4473SLutz Donnerhacke error = ENOENT;
683b1bd4473SLutz Donnerhacke break;
684b1bd4473SLutz Donnerhacke }
685b1bd4473SLutz Donnerhacke error = ng_bridge_put(priv, mh->addr, NG_HOOK_PRIVATE(hook));
686b1bd4473SLutz Donnerhacke break;
687b1bd4473SLutz Donnerhacke }
688ed2dbd31SArchie Cobbs default:
689ed2dbd31SArchie Cobbs error = EINVAL;
690ed2dbd31SArchie Cobbs break;
691ed2dbd31SArchie Cobbs }
692ed2dbd31SArchie Cobbs break;
693ed2dbd31SArchie Cobbs default:
694ed2dbd31SArchie Cobbs error = EINVAL;
695ed2dbd31SArchie Cobbs break;
696ed2dbd31SArchie Cobbs }
697ed2dbd31SArchie Cobbs
698ed2dbd31SArchie Cobbs /* Done */
699069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp);
700069154d5SJulian Elischer NG_FREE_MSG(msg);
701ed2dbd31SArchie Cobbs return (error);
702ed2dbd31SArchie Cobbs }
703ed2dbd31SArchie Cobbs
704ed2dbd31SArchie Cobbs /*
705ed2dbd31SArchie Cobbs * Receive data on a hook
706ed2dbd31SArchie Cobbs */
707631cabbaSGleb Smirnoff struct ng_bridge_send_ctx {
708631cabbaSGleb Smirnoff link_p foundFirst, incoming;
709631cabbaSGleb Smirnoff struct mbuf * m;
710631cabbaSGleb Smirnoff int manycast, error;
711631cabbaSGleb Smirnoff };
712631cabbaSGleb Smirnoff
7133c958f5fSLutz Donnerhacke /*
7143c958f5fSLutz Donnerhacke * Update stats and send out
7153c958f5fSLutz Donnerhacke */
7163c958f5fSLutz Donnerhacke static inline int
ng_bridge_send_data(link_cp dst,int manycast,struct mbuf * m,item_p item)7173c958f5fSLutz Donnerhacke ng_bridge_send_data(link_cp dst, int manycast, struct mbuf *m, item_p item) {
7183c958f5fSLutz Donnerhacke int error = 0;
7193c958f5fSLutz Donnerhacke size_t len = m->m_pkthdr.len;
7203c958f5fSLutz Donnerhacke
7213c958f5fSLutz Donnerhacke if(item != NULL)
7223c958f5fSLutz Donnerhacke NG_FWD_NEW_DATA(error, item, dst->hook, m);
7233c958f5fSLutz Donnerhacke else
7243c958f5fSLutz Donnerhacke NG_SEND_DATA_ONLY(error, dst->hook, m);
7253c958f5fSLutz Donnerhacke
726a56e5ad6SLutz Donnerhacke if (error) {
727319e9fc6SGleb Smirnoff if (error == ENOMEM)
728319e9fc6SGleb Smirnoff counter_u64_add(dst->stats.memoryFailures, 1);
729a56e5ad6SLutz Donnerhacke /* The packet is still ours */
730a56e5ad6SLutz Donnerhacke if (item != NULL)
731a56e5ad6SLutz Donnerhacke NG_FREE_ITEM(item);
732a56e5ad6SLutz Donnerhacke if (m != NULL)
733a56e5ad6SLutz Donnerhacke NG_FREE_M(m);
734a56e5ad6SLutz Donnerhacke return (error);
735a56e5ad6SLutz Donnerhacke }
736a56e5ad6SLutz Donnerhacke
7373c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitPackets, 1);
7383c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitOctets, len);
7393c958f5fSLutz Donnerhacke switch (manycast) {
7403c958f5fSLutz Donnerhacke default: /* unknown unicast */
7413c958f5fSLutz Donnerhacke break;
7423c958f5fSLutz Donnerhacke case 1: /* multicast */
7433c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitMulticasts, 1);
7443c958f5fSLutz Donnerhacke break;
7453c958f5fSLutz Donnerhacke case 2: /* broadcast */
7463c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitBroadcasts, 1);
7473c958f5fSLutz Donnerhacke break;
7483c958f5fSLutz Donnerhacke }
749a56e5ad6SLutz Donnerhacke return (0);
7503c958f5fSLutz Donnerhacke }
7513c958f5fSLutz Donnerhacke
7523c958f5fSLutz Donnerhacke /*
7533c958f5fSLutz Donnerhacke * Loop body for sending to multiple destinations
7543c958f5fSLutz Donnerhacke * return 0 to stop looping
7553c958f5fSLutz Donnerhacke */
756631cabbaSGleb Smirnoff static int
ng_bridge_send_ctx(hook_p dst,void * arg)7570b951c55SGleb Smirnoff ng_bridge_send_ctx(hook_p dst, void *arg)
758631cabbaSGleb Smirnoff {
7590b951c55SGleb Smirnoff struct ng_bridge_send_ctx *ctx = arg;
760631cabbaSGleb Smirnoff link_p destLink = NG_HOOK_PRIVATE(dst);
761631cabbaSGleb Smirnoff struct mbuf *m2 = NULL;
762631cabbaSGleb Smirnoff int error = 0;
763631cabbaSGleb Smirnoff
764631cabbaSGleb Smirnoff /* Skip incoming link */
765631cabbaSGleb Smirnoff if (destLink == ctx->incoming) {
766631cabbaSGleb Smirnoff return (1);
767631cabbaSGleb Smirnoff }
768631cabbaSGleb Smirnoff
769f961caf2SLutz Donnerhacke /* Skip sending unknowns to undesired links */
770f961caf2SLutz Donnerhacke if (!ctx->manycast && !destLink->sendUnknown)
771f961caf2SLutz Donnerhacke return (1);
772f961caf2SLutz Donnerhacke
773631cabbaSGleb Smirnoff if (ctx->foundFirst == NULL) {
774631cabbaSGleb Smirnoff /*
775631cabbaSGleb Smirnoff * This is the first usable link we have found.
776631cabbaSGleb Smirnoff * Reserve it for the originals.
777631cabbaSGleb Smirnoff * If we never find another we save a copy.
778631cabbaSGleb Smirnoff */
779631cabbaSGleb Smirnoff ctx->foundFirst = destLink;
780631cabbaSGleb Smirnoff return (1);
781631cabbaSGleb Smirnoff }
782631cabbaSGleb Smirnoff
783631cabbaSGleb Smirnoff /*
784631cabbaSGleb Smirnoff * It's usable link but not the reserved (first) one.
785631cabbaSGleb Smirnoff * Copy mbuf info for sending.
786631cabbaSGleb Smirnoff */
787a56e5ad6SLutz Donnerhacke m2 = m_dup(ctx->m, M_NOWAIT);
788631cabbaSGleb Smirnoff if (m2 == NULL) {
78966c72859SLutz Donnerhacke counter_u64_add(ctx->incoming->stats.memoryFailures, 1);
790631cabbaSGleb Smirnoff ctx->error = ENOBUFS;
791a56e5ad6SLutz Donnerhacke return (0); /* abort loop, do not try again and again */
792631cabbaSGleb Smirnoff }
793631cabbaSGleb Smirnoff
794631cabbaSGleb Smirnoff /* Send packet */
7953c958f5fSLutz Donnerhacke error = ng_bridge_send_data(destLink, ctx->manycast, m2, NULL);
796631cabbaSGleb Smirnoff if (error)
797631cabbaSGleb Smirnoff ctx->error = error;
798631cabbaSGleb Smirnoff return (1);
799631cabbaSGleb Smirnoff }
800631cabbaSGleb Smirnoff
801ed2dbd31SArchie Cobbs static int
ng_bridge_rcvdata(hook_p hook,item_p item)802069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item)
803ed2dbd31SArchie Cobbs {
80430400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook);
80530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
806ed2dbd31SArchie Cobbs struct ng_bridge_host *host;
807ed2dbd31SArchie Cobbs struct ether_header *eh;
808631cabbaSGleb Smirnoff struct ng_bridge_send_ctx ctx = { 0 };
809ed2dbd31SArchie Cobbs
810631cabbaSGleb Smirnoff NGI_GET_M(item, ctx.m);
811ed2dbd31SArchie Cobbs
812631cabbaSGleb Smirnoff ctx.incoming = NG_HOOK_PRIVATE(hook);
813ed2dbd31SArchie Cobbs /* Sanity check packet and pull up header */
814631cabbaSGleb Smirnoff if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) {
81566c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvRunts, 1);
816069154d5SJulian Elischer NG_FREE_ITEM(item);
817631cabbaSGleb Smirnoff NG_FREE_M(ctx.m);
818ed2dbd31SArchie Cobbs return (EINVAL);
819ed2dbd31SArchie Cobbs }
820631cabbaSGleb Smirnoff if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) {
82166c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
822069154d5SJulian Elischer NG_FREE_ITEM(item);
823ed2dbd31SArchie Cobbs return (ENOBUFS);
824ed2dbd31SArchie Cobbs }
825631cabbaSGleb Smirnoff eh = mtod(ctx.m, struct ether_header *);
826ed2dbd31SArchie Cobbs if ((eh->ether_shost[0] & 1) != 0) {
82766c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvInvalid, 1);
828069154d5SJulian Elischer NG_FREE_ITEM(item);
829631cabbaSGleb Smirnoff NG_FREE_M(ctx.m);
830ed2dbd31SArchie Cobbs return (EINVAL);
831ed2dbd31SArchie Cobbs }
832ed2dbd31SArchie Cobbs
833ed2dbd31SArchie Cobbs /* Is link disabled due to a loopback condition? */
834631cabbaSGleb Smirnoff if (ctx.incoming->loopCount != 0) {
83566c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.loopDrops, 1);
836069154d5SJulian Elischer NG_FREE_ITEM(item);
837631cabbaSGleb Smirnoff NG_FREE_M(ctx.m);
838f6e0c471SLutz Donnerhacke return (ELOOP);
839ed2dbd31SArchie Cobbs }
840ed2dbd31SArchie Cobbs
841ed2dbd31SArchie Cobbs /* Update stats */
84266c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvPackets, 1);
84366c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len);
844631cabbaSGleb Smirnoff if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) {
845ed2dbd31SArchie Cobbs if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
84666c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1);
847631cabbaSGleb Smirnoff ctx.manycast = 2;
848ed2dbd31SArchie Cobbs } else
84966c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvMulticasts, 1);
850ed2dbd31SArchie Cobbs }
851ed2dbd31SArchie Cobbs
852ed2dbd31SArchie Cobbs /* Look up packet's source Ethernet address in hashtable */
853f6e0c471SLutz Donnerhacke if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL)
854011b7317SLutz Donnerhacke /* Update time since last heard from this host.
855011b7317SLutz Donnerhacke * This is safe without locking, because it's
856011b7317SLutz Donnerhacke * the only operation during shared access.
857011b7317SLutz Donnerhacke */
8584dfe70fdSLutz Donnerhacke if (__predict_false(host->staleness > 0))
859ed2dbd31SArchie Cobbs host->staleness = 0;
860ed2dbd31SArchie Cobbs
861f6e0c471SLutz Donnerhacke if ((host == NULL && ctx.incoming->learnMac) ||
862f6e0c471SLutz Donnerhacke (host != NULL && host->link != ctx.incoming)) {
863b1bd4473SLutz Donnerhacke struct ng_mesg *msg;
864b1bd4473SLutz Donnerhacke struct ng_bridge_move_host *mh;
865b1bd4473SLutz Donnerhacke int error = 0;
866b1bd4473SLutz Donnerhacke
867b1bd4473SLutz Donnerhacke NG_MKMESSAGE(msg, NGM_BRIDGE_COOKIE, NGM_BRIDGE_MOVE_HOST,
868b1bd4473SLutz Donnerhacke sizeof(*mh), M_NOWAIT);
869b1bd4473SLutz Donnerhacke if (msg == NULL) {
87066c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
871069154d5SJulian Elischer NG_FREE_ITEM(item);
872631cabbaSGleb Smirnoff NG_FREE_M(ctx.m);
873ed2dbd31SArchie Cobbs return (ENOMEM);
874ed2dbd31SArchie Cobbs }
875b1bd4473SLutz Donnerhacke mh = (struct ng_bridge_move_host *)msg->data;
876b1bd4473SLutz Donnerhacke strncpy(mh->hook, NG_HOOK_NAME(ctx.incoming->hook),
877b1bd4473SLutz Donnerhacke sizeof(mh->hook));
878b1bd4473SLutz Donnerhacke memcpy(mh->addr, eh->ether_shost, sizeof(mh->addr));
879b1bd4473SLutz Donnerhacke NG_SEND_MSG_ID(error, node, msg, NG_NODE_ID(node),
880b1bd4473SLutz Donnerhacke NG_NODE_ID(node));
881b1bd4473SLutz Donnerhacke if (error)
882b1bd4473SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
883ed2dbd31SArchie Cobbs }
884ed2dbd31SArchie Cobbs
885f6e0c471SLutz Donnerhacke if (host != NULL && host->link != ctx.incoming) {
886f6e0c471SLutz Donnerhacke if (host->age < priv->conf.minStableAge) {
887f6e0c471SLutz Donnerhacke /* Drop packet on instable links */
888f6e0c471SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.loopDrops, 1);
889f6e0c471SLutz Donnerhacke NG_FREE_ITEM(item);
890f6e0c471SLutz Donnerhacke NG_FREE_M(ctx.m);
891f6e0c471SLutz Donnerhacke return (ELOOP);
892f6e0c471SLutz Donnerhacke }
893f6e0c471SLutz Donnerhacke }
894f6e0c471SLutz Donnerhacke
895ed2dbd31SArchie Cobbs /*
896ed2dbd31SArchie Cobbs * If unicast and destination host known, deliver to host's link,
897ed2dbd31SArchie Cobbs * unless it is the same link as the packet came in on.
898ed2dbd31SArchie Cobbs */
899631cabbaSGleb Smirnoff if (!ctx.manycast) {
900ed2dbd31SArchie Cobbs /* Determine packet destination link */
901ed2dbd31SArchie Cobbs if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
902631cabbaSGleb Smirnoff link_p destLink = host->link;
903ed2dbd31SArchie Cobbs
904ed2dbd31SArchie Cobbs /* If destination same as incoming link, do nothing */
905631cabbaSGleb Smirnoff if (destLink == ctx.incoming) {
906069154d5SJulian Elischer NG_FREE_ITEM(item);
907631cabbaSGleb Smirnoff NG_FREE_M(ctx.m);
908ed2dbd31SArchie Cobbs return (0);
909ed2dbd31SArchie Cobbs }
910ed2dbd31SArchie Cobbs
911ed2dbd31SArchie Cobbs /* Deliver packet out the destination link */
9123c958f5fSLutz Donnerhacke return (ng_bridge_send_data(destLink, ctx.manycast, ctx.m, item));
913ed2dbd31SArchie Cobbs }
914ed2dbd31SArchie Cobbs
915ed2dbd31SArchie Cobbs /* Destination host is not known */
91666c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvUnknown, 1);
917ed2dbd31SArchie Cobbs }
918ed2dbd31SArchie Cobbs
919ed2dbd31SArchie Cobbs /* Distribute unknown, multicast, broadcast pkts to all other links */
9206d5f002eSJohn Baldwin NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx);
921ed2dbd31SArchie Cobbs
922a56e5ad6SLutz Donnerhacke /* Finally send out on the first link found */
923a56e5ad6SLutz Donnerhacke if (ctx.foundFirst != NULL) {
924a56e5ad6SLutz Donnerhacke int error = ng_bridge_send_data(ctx.foundFirst, ctx.manycast, ctx.m, item);
925a56e5ad6SLutz Donnerhacke if (error)
926a56e5ad6SLutz Donnerhacke ctx.error = error;
927a56e5ad6SLutz Donnerhacke } else { /* nothing to send at all */
928069154d5SJulian Elischer NG_FREE_ITEM(item);
929631cabbaSGleb Smirnoff NG_FREE_M(ctx.m);
930069154d5SJulian Elischer }
931069154d5SJulian Elischer
932a56e5ad6SLutz Donnerhacke return (ctx.error);
933ed2dbd31SArchie Cobbs }
934ed2dbd31SArchie Cobbs
935ed2dbd31SArchie Cobbs /*
936ed2dbd31SArchie Cobbs * Shutdown node
937ed2dbd31SArchie Cobbs */
938ed2dbd31SArchie Cobbs static int
ng_bridge_shutdown(node_p node)939069154d5SJulian Elischer ng_bridge_shutdown(node_p node)
940ed2dbd31SArchie Cobbs {
94130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
942ed2dbd31SArchie Cobbs
9436c12c2b1SArchie Cobbs /*
944195cf617SRuslan Ermilov * Shut down everything including the timer. Even if the
945195cf617SRuslan Ermilov * callout has already been dequeued and is about to be
946195cf617SRuslan Ermilov * run, ng_bridge_timeout() won't be fired as the node
947195cf617SRuslan Ermilov * is already marked NGF_INVALID, so we're safe to free
948195cf617SRuslan Ermilov * the node now.
9496c12c2b1SArchie Cobbs */
950ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
951ed2dbd31SArchie Cobbs ("%s: numLinks=%d numHosts=%d",
9526e551fb6SDavid E. O'Brien __func__, priv->numLinks, priv->numHosts));
953195cf617SRuslan Ermilov ng_uncallout(&priv->timer, node);
954*86a6393aSDavid Marker delete_unrhdr(priv->linkUnit);
955*86a6393aSDavid Marker delete_unrhdr(priv->uplinkUnit);
956195cf617SRuslan Ermilov NG_NODE_SET_PRIVATE(node, NULL);
957195cf617SRuslan Ermilov NG_NODE_UNREF(node);
9581ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE);
9591ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH_BRIDGE);
960ed2dbd31SArchie Cobbs return (0);
961ed2dbd31SArchie Cobbs }
962ed2dbd31SArchie Cobbs
963ed2dbd31SArchie Cobbs /*
964ed2dbd31SArchie Cobbs * Hook disconnection.
965ed2dbd31SArchie Cobbs */
966ed2dbd31SArchie Cobbs static int
ng_bridge_disconnect(hook_p hook)967ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook)
968ed2dbd31SArchie Cobbs {
969*86a6393aSDavid Marker char *name = NG_HOOK_NAME(hook);
97030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
971631cabbaSGleb Smirnoff link_p link = NG_HOOK_PRIVATE(hook);
972*86a6393aSDavid Marker const struct ng_link_prefix *pfx = ng_get_link_prefix(name);
973*86a6393aSDavid Marker uint32_t linkNum;
974ed2dbd31SArchie Cobbs
975ed2dbd31SArchie Cobbs /* Remove all hosts associated with this link */
976631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv, link);
977ed2dbd31SArchie Cobbs
978ed2dbd31SArchie Cobbs /* Free associated link information */
979e0e3ded7SMark Johnston ng_bridge_free_link(link);
980ed2dbd31SArchie Cobbs priv->numLinks--;
981ed2dbd31SArchie Cobbs
982*86a6393aSDavid Marker linkNum = strtoul(name + pfx->len, NULL, 10);
983*86a6393aSDavid Marker free_unr(pfx == &link_pfx ? priv->linkUnit: priv->uplinkUnit, linkNum);
984*86a6393aSDavid Marker
985ed2dbd31SArchie Cobbs /* If no more hooks, go away */
98630400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
987f8aab721SMarko Zec && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
988f8aab721SMarko Zec && !priv->persistent) {
98930400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook));
99030400f03SJulian Elischer }
991ed2dbd31SArchie Cobbs return (0);
992ed2dbd31SArchie Cobbs }
993ed2dbd31SArchie Cobbs
994ed2dbd31SArchie Cobbs /******************************************************************
995ed2dbd31SArchie Cobbs HASH TABLE FUNCTIONS
996ed2dbd31SArchie Cobbs ******************************************************************/
997ed2dbd31SArchie Cobbs
998ed2dbd31SArchie Cobbs /*
999ed2dbd31SArchie Cobbs * Hash algorithm
1000ed2dbd31SArchie Cobbs */
1001ed2dbd31SArchie Cobbs #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \
1002ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[1] \
1003ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[2]) & (mask) )
1004ed2dbd31SArchie Cobbs
1005ed2dbd31SArchie Cobbs /*
1006ed2dbd31SArchie Cobbs * Find a host entry in the table.
1007ed2dbd31SArchie Cobbs */
1008ed2dbd31SArchie Cobbs static struct ng_bridge_host *
ng_bridge_get(priv_cp priv,const u_char * addr)10096117aa58SLutz Donnerhacke ng_bridge_get(priv_cp priv, const u_char *addr)
1010ed2dbd31SArchie Cobbs {
1011ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask);
1012ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *host;
1013ed2dbd31SArchie Cobbs
1014ccf4cd2eSLutz Donnerhacke SLIST_FOREACH(host, &priv->tab[bucket], next) {
1015ccf4cd2eSLutz Donnerhacke if (ETHER_EQUAL(host->addr, addr))
1016ccf4cd2eSLutz Donnerhacke return (host);
1017ed2dbd31SArchie Cobbs }
1018ed2dbd31SArchie Cobbs return (NULL);
1019ed2dbd31SArchie Cobbs }
1020ed2dbd31SArchie Cobbs
1021ed2dbd31SArchie Cobbs /*
1022f6e0c471SLutz Donnerhacke * Add a host entry to the table. If it already exists, move it
1023f6e0c471SLutz Donnerhacke * to the new link. Returns 0 on success.
1024ed2dbd31SArchie Cobbs */
1025ed2dbd31SArchie Cobbs static int
ng_bridge_put(priv_p priv,const u_char * addr,link_p link)1026631cabbaSGleb Smirnoff ng_bridge_put(priv_p priv, const u_char *addr, link_p link)
1027ed2dbd31SArchie Cobbs {
1028ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask);
1029ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *host;
1030ed2dbd31SArchie Cobbs
1031f6e0c471SLutz Donnerhacke if ((host = ng_bridge_get(priv, addr)) != NULL) {
1032f6e0c471SLutz Donnerhacke /* Host already on the correct link? */
1033f6e0c471SLutz Donnerhacke if (host->link == link)
1034f6e0c471SLutz Donnerhacke return 0;
1035f6e0c471SLutz Donnerhacke
1036f6e0c471SLutz Donnerhacke /* Move old host over to new link */
1037f6e0c471SLutz Donnerhacke if (host->age >= priv->conf.minStableAge) {
1038f6e0c471SLutz Donnerhacke host->link = link;
1039f6e0c471SLutz Donnerhacke host->age = 0;
1040f6e0c471SLutz Donnerhacke return (0);
1041f6e0c471SLutz Donnerhacke }
1042f6e0c471SLutz Donnerhacke /*
1043f6e0c471SLutz Donnerhacke * If the host was recently moved to the old link and
1044f6e0c471SLutz Donnerhacke * it's now jumping to a new link, declare a loopback
1045f6e0c471SLutz Donnerhacke * condition.
1046f6e0c471SLutz Donnerhacke */
1047f6e0c471SLutz Donnerhacke if (priv->conf.debugLevel >= 2)
1048f6e0c471SLutz Donnerhacke log(LOG_WARNING, "ng_bridge: %s:"
1049f6e0c471SLutz Donnerhacke " loopback detected on %s\n",
1050f6e0c471SLutz Donnerhacke ng_bridge_nodename(priv->node),
1051f6e0c471SLutz Donnerhacke NG_HOOK_NAME(link->hook));
1052f6e0c471SLutz Donnerhacke
1053f6e0c471SLutz Donnerhacke /* Mark link as linka non grata */
1054f6e0c471SLutz Donnerhacke link->loopCount = priv->conf.loopTimeout;
1055f6e0c471SLutz Donnerhacke link->stats.loopDetects++;
1056f6e0c471SLutz Donnerhacke
1057f6e0c471SLutz Donnerhacke /* Forget all hosts on this link */
1058f6e0c471SLutz Donnerhacke ng_bridge_remove_hosts(priv, link);
1059f6e0c471SLutz Donnerhacke return (ELOOP);
1060f6e0c471SLutz Donnerhacke }
1061ed2dbd31SArchie Cobbs
1062ed2dbd31SArchie Cobbs /* Allocate and initialize new hashtable entry */
1063ccf4cd2eSLutz Donnerhacke host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT);
1064ccf4cd2eSLutz Donnerhacke if (host == NULL)
1065b1bd4473SLutz Donnerhacke return (ENOMEM);
1066ccf4cd2eSLutz Donnerhacke bcopy(addr, host->addr, ETHER_ADDR_LEN);
1067ccf4cd2eSLutz Donnerhacke host->link = link;
1068ccf4cd2eSLutz Donnerhacke host->staleness = 0;
1069ccf4cd2eSLutz Donnerhacke host->age = 0;
1070ed2dbd31SArchie Cobbs
1071ed2dbd31SArchie Cobbs /* Add new element to hash bucket */
1072ccf4cd2eSLutz Donnerhacke SLIST_INSERT_HEAD(&priv->tab[bucket], host, next);
1073ed2dbd31SArchie Cobbs priv->numHosts++;
1074ed2dbd31SArchie Cobbs
1075ed2dbd31SArchie Cobbs /* Resize table if necessary */
1076ed2dbd31SArchie Cobbs ng_bridge_rehash(priv);
1077b1bd4473SLutz Donnerhacke return (0);
1078ed2dbd31SArchie Cobbs }
1079ed2dbd31SArchie Cobbs
1080ed2dbd31SArchie Cobbs /*
1081ed2dbd31SArchie Cobbs * Resize the hash table. We try to maintain the number of buckets
1082ed2dbd31SArchie Cobbs * such that the load factor is in the range 0.25 to 1.0.
1083ed2dbd31SArchie Cobbs *
1084ed2dbd31SArchie Cobbs * If we can't get the new memory then we silently fail. This is OK
1085ed2dbd31SArchie Cobbs * because things will still work and we'll try again soon anyway.
1086ed2dbd31SArchie Cobbs */
1087ed2dbd31SArchie Cobbs static void
ng_bridge_rehash(priv_p priv)1088ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv)
1089ed2dbd31SArchie Cobbs {
1090ed2dbd31SArchie Cobbs struct ng_bridge_bucket *newTab;
1091ed2dbd31SArchie Cobbs int oldBucket, newBucket;
1092ed2dbd31SArchie Cobbs int newNumBuckets;
1093ed2dbd31SArchie Cobbs u_int newMask;
1094ed2dbd31SArchie Cobbs
1095ed2dbd31SArchie Cobbs /* Is table too full or too empty? */
1096ed2dbd31SArchie Cobbs if (priv->numHosts > priv->numBuckets
1097ed2dbd31SArchie Cobbs && (priv->numBuckets << 1) <= MAX_BUCKETS)
1098ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets << 1;
1099ed2dbd31SArchie Cobbs else if (priv->numHosts < (priv->numBuckets >> 2)
1100ed2dbd31SArchie Cobbs && (priv->numBuckets >> 2) >= MIN_BUCKETS)
1101ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets >> 2;
1102ed2dbd31SArchie Cobbs else
1103ed2dbd31SArchie Cobbs return;
1104ed2dbd31SArchie Cobbs newMask = newNumBuckets - 1;
1105ed2dbd31SArchie Cobbs
1106ed2dbd31SArchie Cobbs /* Allocate and initialize new table */
1107ac2fffa4SPedro F. Giffuni newTab = malloc(newNumBuckets * sizeof(*newTab),
1108e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
1109ed2dbd31SArchie Cobbs if (newTab == NULL)
1110ed2dbd31SArchie Cobbs return;
1111ed2dbd31SArchie Cobbs
1112ed2dbd31SArchie Cobbs /* Move all entries from old table to new table */
1113ed2dbd31SArchie Cobbs for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
1114ed2dbd31SArchie Cobbs struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
1115ed2dbd31SArchie Cobbs
1116ed2dbd31SArchie Cobbs while (!SLIST_EMPTY(oldList)) {
1117ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host
1118ed2dbd31SArchie Cobbs = SLIST_FIRST(oldList);
1119ed2dbd31SArchie Cobbs
1120ed2dbd31SArchie Cobbs SLIST_REMOVE_HEAD(oldList, next);
1121ccf4cd2eSLutz Donnerhacke newBucket = HASH(host->addr, newMask);
1122ccf4cd2eSLutz Donnerhacke SLIST_INSERT_HEAD(&newTab[newBucket], host, next);
1123ed2dbd31SArchie Cobbs }
1124ed2dbd31SArchie Cobbs }
1125ed2dbd31SArchie Cobbs
1126ed2dbd31SArchie Cobbs /* Replace old table with new one */
1127ed2dbd31SArchie Cobbs if (priv->conf.debugLevel >= 3) {
1128ed2dbd31SArchie Cobbs log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
1129ed2dbd31SArchie Cobbs ng_bridge_nodename(priv->node),
1130ed2dbd31SArchie Cobbs priv->numBuckets, newNumBuckets);
1131ed2dbd31SArchie Cobbs }
11321ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE);
1133ed2dbd31SArchie Cobbs priv->numBuckets = newNumBuckets;
1134ed2dbd31SArchie Cobbs priv->hashMask = newMask;
1135ed2dbd31SArchie Cobbs priv->tab = newTab;
1136ed2dbd31SArchie Cobbs return;
1137ed2dbd31SArchie Cobbs }
1138ed2dbd31SArchie Cobbs
1139ed2dbd31SArchie Cobbs /******************************************************************
1140ed2dbd31SArchie Cobbs MISC FUNCTIONS
1141ed2dbd31SArchie Cobbs ******************************************************************/
1142ed2dbd31SArchie Cobbs
1143*86a6393aSDavid Marker static const struct ng_link_prefix *
ng_get_link_prefix(const char * name)1144*86a6393aSDavid Marker ng_get_link_prefix(const char *name)
1145*86a6393aSDavid Marker {
1146*86a6393aSDavid Marker static const struct ng_link_prefix *pfxs[] =
1147*86a6393aSDavid Marker { &link_pfx, &uplink_pfx, };
1148*86a6393aSDavid Marker
1149*86a6393aSDavid Marker for (u_int i = 0; i < nitems(pfxs); i++)
1150*86a6393aSDavid Marker if (strncmp(pfxs[i]->prefix, name, pfxs[i]->len) == 0)
1151*86a6393aSDavid Marker return (pfxs[i]);
1152*86a6393aSDavid Marker
1153*86a6393aSDavid Marker return (NULL);
1154*86a6393aSDavid Marker }
1155*86a6393aSDavid Marker
1156ed2dbd31SArchie Cobbs /*
1157ed2dbd31SArchie Cobbs * Remove all hosts associated with a specific link from the hashtable.
1158ed2dbd31SArchie Cobbs * If linkNum == -1, then remove all hosts in the table.
1159ed2dbd31SArchie Cobbs */
1160ed2dbd31SArchie Cobbs static void
ng_bridge_remove_hosts(priv_p priv,link_p link)1161631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv_p priv, link_p link)
1162ed2dbd31SArchie Cobbs {
1163ed2dbd31SArchie Cobbs int bucket;
1164ed2dbd31SArchie Cobbs
1165ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1166ccf4cd2eSLutz Donnerhacke struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1167ed2dbd31SArchie Cobbs
1168ed2dbd31SArchie Cobbs while (*hptr != NULL) {
1169ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host = *hptr;
1170ed2dbd31SArchie Cobbs
1171ccf4cd2eSLutz Donnerhacke if (link == NULL || host->link == link) {
1172ccf4cd2eSLutz Donnerhacke *hptr = SLIST_NEXT(host, next);
1173ccf4cd2eSLutz Donnerhacke free(host, M_NETGRAPH_BRIDGE);
1174ed2dbd31SArchie Cobbs priv->numHosts--;
1175ed2dbd31SArchie Cobbs } else
1176ccf4cd2eSLutz Donnerhacke hptr = &SLIST_NEXT(host, next);
1177ed2dbd31SArchie Cobbs }
1178ed2dbd31SArchie Cobbs }
1179ed2dbd31SArchie Cobbs }
1180ed2dbd31SArchie Cobbs
1181ed2dbd31SArchie Cobbs /*
1182ed2dbd31SArchie Cobbs * Handle our once-per-second timeout event. We do two things:
1183ed2dbd31SArchie Cobbs * we decrement link->loopCount for those links being muted due to
1184ed2dbd31SArchie Cobbs * a detected loopback condition, and we remove any hosts from
1185ed2dbd31SArchie Cobbs * the hashtable whom we haven't heard from in a long while.
1186ed2dbd31SArchie Cobbs */
1187631cabbaSGleb Smirnoff static int
ng_bridge_unmute(hook_p hook,void * arg)11880b951c55SGleb Smirnoff ng_bridge_unmute(hook_p hook, void *arg)
1189631cabbaSGleb Smirnoff {
1190631cabbaSGleb Smirnoff link_p link = NG_HOOK_PRIVATE(hook);
1191631cabbaSGleb Smirnoff node_p node = NG_HOOK_NODE(hook);
1192631cabbaSGleb Smirnoff priv_p priv = NG_NODE_PRIVATE(node);
11930b951c55SGleb Smirnoff int *counter = arg;
1194631cabbaSGleb Smirnoff
1195631cabbaSGleb Smirnoff if (link->loopCount != 0) {
1196631cabbaSGleb Smirnoff link->loopCount--;
1197631cabbaSGleb Smirnoff if (link->loopCount == 0 && priv->conf.debugLevel >= 2) {
1198631cabbaSGleb Smirnoff log(LOG_INFO, "ng_bridge: %s:"
1199631cabbaSGleb Smirnoff " restoring looped back %s\n",
1200631cabbaSGleb Smirnoff ng_bridge_nodename(node), NG_HOOK_NAME(hook));
1201631cabbaSGleb Smirnoff }
1202631cabbaSGleb Smirnoff }
1203abc4b11cSGleb Smirnoff (*counter)++;
1204631cabbaSGleb Smirnoff return (1);
1205631cabbaSGleb Smirnoff }
1206631cabbaSGleb Smirnoff
1207ed2dbd31SArchie Cobbs static void
ng_bridge_timeout(node_p node,hook_p hook,void * arg1,int arg2)1208e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1209ed2dbd31SArchie Cobbs {
121030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node);
1211e0d32af7SGleb Smirnoff int bucket;
1212ed2dbd31SArchie Cobbs int counter = 0;
1213ed2dbd31SArchie Cobbs
1214ed2dbd31SArchie Cobbs /* Update host time counters and remove stale entries */
1215ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1216ccf4cd2eSLutz Donnerhacke struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1217ed2dbd31SArchie Cobbs
1218ed2dbd31SArchie Cobbs while (*hptr != NULL) {
1219ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host = *hptr;
1220ed2dbd31SArchie Cobbs
1221ed2dbd31SArchie Cobbs /* Remove hosts we haven't heard from in a while */
1222ccf4cd2eSLutz Donnerhacke if (++host->staleness >= priv->conf.maxStaleness) {
1223ccf4cd2eSLutz Donnerhacke *hptr = SLIST_NEXT(host, next);
1224ccf4cd2eSLutz Donnerhacke free(host, M_NETGRAPH_BRIDGE);
1225ed2dbd31SArchie Cobbs priv->numHosts--;
1226ed2dbd31SArchie Cobbs } else {
1227ccf4cd2eSLutz Donnerhacke if (host->age < 0xffff)
1228ccf4cd2eSLutz Donnerhacke host->age++;
1229ccf4cd2eSLutz Donnerhacke hptr = &SLIST_NEXT(host, next);
1230ed2dbd31SArchie Cobbs counter++;
1231ed2dbd31SArchie Cobbs }
1232ed2dbd31SArchie Cobbs }
1233ed2dbd31SArchie Cobbs }
1234ed2dbd31SArchie Cobbs KASSERT(priv->numHosts == counter,
12356e551fb6SDavid E. O'Brien ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1236ed2dbd31SArchie Cobbs
1237ed2dbd31SArchie Cobbs /* Decrease table size if necessary */
1238ed2dbd31SArchie Cobbs ng_bridge_rehash(priv);
1239ed2dbd31SArchie Cobbs
1240ed2dbd31SArchie Cobbs /* Decrease loop counter on muted looped back links */
1241631cabbaSGleb Smirnoff counter = 0;
12426d5f002eSJohn Baldwin NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter);
1243ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == counter,
12446e551fb6SDavid E. O'Brien ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1245ed2dbd31SArchie Cobbs
1246e0d32af7SGleb Smirnoff /* Register a new timeout, keeping the existing node reference */
1247e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1248ed2dbd31SArchie Cobbs }
1249ed2dbd31SArchie Cobbs
1250ed2dbd31SArchie Cobbs /*
1251ed2dbd31SArchie Cobbs * Return node's "name", even if it doesn't have one.
1252ed2dbd31SArchie Cobbs */
1253ed2dbd31SArchie Cobbs static const char *
ng_bridge_nodename(node_cp node)12546117aa58SLutz Donnerhacke ng_bridge_nodename(node_cp node)
1255ed2dbd31SArchie Cobbs {
125687e2c66aSHartmut Brandt static char name[NG_NODESIZ];
1257ed2dbd31SArchie Cobbs
1258dd10c1e2SGleb Smirnoff if (NG_NODE_HAS_NAME(node))
125930400f03SJulian Elischer snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1260ed2dbd31SArchie Cobbs else
1261ed2dbd31SArchie Cobbs snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1262ed2dbd31SArchie Cobbs return name;
1263ed2dbd31SArchie Cobbs }
1264