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 * $FreeBSD$ 37ed2dbd31SArchie Cobbs */ 38ed2dbd31SArchie Cobbs 39ed2dbd31SArchie Cobbs /* 40ed2dbd31SArchie Cobbs * ng_bridge(4) netgraph node type 41ed2dbd31SArchie Cobbs * 42ed2dbd31SArchie Cobbs * The node performs standard intelligent Ethernet bridging over 43ed2dbd31SArchie Cobbs * each of its connected hooks, or links. A simple loop detection 44ed2dbd31SArchie Cobbs * algorithm is included which disables a link for priv->conf.loopTimeout 45ed2dbd31SArchie Cobbs * seconds when a host is seen to have jumped from one link to 46ed2dbd31SArchie Cobbs * another within priv->conf.minStableAge seconds. 47ed2dbd31SArchie Cobbs * 48ed2dbd31SArchie Cobbs * We keep a hashtable that maps Ethernet addresses to host info, 49ed2dbd31SArchie Cobbs * which is contained in struct ng_bridge_host's. These structures 50ed2dbd31SArchie Cobbs * tell us on which link the host may be found. A host's entry will 51ed2dbd31SArchie Cobbs * expire after priv->conf.maxStaleness seconds. 52ed2dbd31SArchie Cobbs * 53ed2dbd31SArchie Cobbs * This node is optimzed for stable networks, where machines jump 54ed2dbd31SArchie Cobbs * from one port to the other only rarely. 55ed2dbd31SArchie Cobbs */ 56ed2dbd31SArchie Cobbs 57ed2dbd31SArchie Cobbs #include <sys/param.h> 58ed2dbd31SArchie Cobbs #include <sys/systm.h> 59ed2dbd31SArchie Cobbs #include <sys/kernel.h> 60385195c0SMarko Zec #include <sys/lock.h> 61ed2dbd31SArchie Cobbs #include <sys/malloc.h> 62ed2dbd31SArchie Cobbs #include <sys/mbuf.h> 63ed2dbd31SArchie Cobbs #include <sys/errno.h> 64385195c0SMarko Zec #include <sys/rwlock.h> 65ed2dbd31SArchie Cobbs #include <sys/syslog.h> 66ed2dbd31SArchie Cobbs #include <sys/socket.h> 67ed2dbd31SArchie Cobbs #include <sys/ctype.h> 6866c72859SLutz Donnerhacke #include <sys/types.h> 6966c72859SLutz Donnerhacke #include <sys/counter.h> 70ed2dbd31SArchie Cobbs 71ed2dbd31SArchie Cobbs #include <net/if.h> 7276039bc8SGleb Smirnoff #include <net/if_var.h> 73ed2dbd31SArchie Cobbs #include <net/ethernet.h> 74530c0060SRobert Watson #include <net/vnet.h> 75ed2dbd31SArchie Cobbs 76ed2dbd31SArchie Cobbs #include <netinet/in.h> 775f2e1642SLuigi Rizzo #if 0 /* not used yet */ 78ed2dbd31SArchie Cobbs #include <netinet/ip_fw.h> 795f2e1642SLuigi Rizzo #endif 80ed2dbd31SArchie Cobbs #include <netgraph/ng_message.h> 81ed2dbd31SArchie Cobbs #include <netgraph/netgraph.h> 82ed2dbd31SArchie Cobbs #include <netgraph/ng_parse.h> 83ed2dbd31SArchie Cobbs #include <netgraph/ng_bridge.h> 84ed2dbd31SArchie Cobbs 859c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC 86d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", 87d745c852SEd Schouten "netgraph bridge node"); 889c8c302fSJulian Elischer #else 899c8c302fSJulian Elischer #define M_NETGRAPH_BRIDGE M_NETGRAPH 909c8c302fSJulian Elischer #endif 919c8c302fSJulian Elischer 9266c72859SLutz Donnerhacke /* Counter based stats */ 9366c72859SLutz Donnerhacke struct ng_bridge_link_kernel_stats { 9466c72859SLutz Donnerhacke counter_u64_t recvOctets; /* total octets rec'd on link */ 9566c72859SLutz Donnerhacke counter_u64_t recvPackets; /* total pkts rec'd on link */ 9666c72859SLutz Donnerhacke counter_u64_t recvMulticasts; /* multicast pkts rec'd on link */ 9766c72859SLutz Donnerhacke counter_u64_t recvBroadcasts; /* broadcast pkts rec'd on link */ 9866c72859SLutz Donnerhacke counter_u64_t recvUnknown; /* pkts rec'd with unknown dest addr */ 9966c72859SLutz Donnerhacke counter_u64_t recvRunts; /* pkts rec'd less than 14 bytes */ 10066c72859SLutz Donnerhacke counter_u64_t recvInvalid; /* pkts rec'd with bogus source addr */ 10166c72859SLutz Donnerhacke counter_u64_t xmitOctets; /* total octets xmit'd on link */ 10266c72859SLutz Donnerhacke counter_u64_t xmitPackets; /* total pkts xmit'd on link */ 10366c72859SLutz Donnerhacke counter_u64_t xmitMulticasts; /* multicast pkts xmit'd on link */ 10466c72859SLutz Donnerhacke counter_u64_t xmitBroadcasts; /* broadcast pkts xmit'd on link */ 10566c72859SLutz Donnerhacke counter_u64_t loopDrops; /* pkts dropped due to loopback */ 106f6e0c471SLutz Donnerhacke u_int64_t loopDetects; /* number of loop detections */ 10766c72859SLutz Donnerhacke counter_u64_t memoryFailures; /* times couldn't get mem or mbuf */ 10866c72859SLutz Donnerhacke }; 10966c72859SLutz Donnerhacke 110ed2dbd31SArchie Cobbs /* Per-link private data */ 111ed2dbd31SArchie Cobbs struct ng_bridge_link { 112ed2dbd31SArchie Cobbs hook_p hook; /* netgraph hook */ 113ed2dbd31SArchie Cobbs u_int16_t loopCount; /* loop ignore timer */ 114f961caf2SLutz Donnerhacke unsigned int learnMac : 1, /* autolearn macs */ 115f961caf2SLutz Donnerhacke sendUnknown : 1;/* send unknown macs out */ 11666c72859SLutz Donnerhacke struct ng_bridge_link_kernel_stats stats; /* link stats */ 117ed2dbd31SArchie Cobbs }; 1186117aa58SLutz Donnerhacke typedef struct ng_bridge_link const *link_cp; /* read only access */ 119ed2dbd31SArchie Cobbs 120ed2dbd31SArchie Cobbs /* Per-node private data */ 121ed2dbd31SArchie Cobbs struct ng_bridge_private { 122ed2dbd31SArchie Cobbs struct ng_bridge_bucket *tab; /* hash table bucket array */ 123ed2dbd31SArchie Cobbs struct ng_bridge_config conf; /* node configuration */ 124ed2dbd31SArchie Cobbs node_p node; /* netgraph node */ 125ed2dbd31SArchie Cobbs u_int numHosts; /* num entries in table */ 126ed2dbd31SArchie Cobbs u_int numBuckets; /* num buckets in table */ 127ed2dbd31SArchie Cobbs u_int hashMask; /* numBuckets - 1 */ 128ed2dbd31SArchie Cobbs int numLinks; /* num connected links */ 129c869d905SLutz Donnerhacke unsigned int persistent : 1, /* can exist w/o hooks */ 130c869d905SLutz Donnerhacke sendUnknown : 1;/* links receive unknowns by default */ 131ed2dbd31SArchie Cobbs struct callout timer; /* one second periodic timer */ 132ed2dbd31SArchie Cobbs }; 133ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p; 1346117aa58SLutz Donnerhacke typedef struct ng_bridge_private const *priv_cp; /* read only access */ 135ed2dbd31SArchie Cobbs 136ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */ 137ccf4cd2eSLutz Donnerhacke struct ng_bridge_host { 138ccf4cd2eSLutz Donnerhacke u_char addr[6]; /* ethernet address */ 139ccf4cd2eSLutz Donnerhacke link_p link; /* link where addr can be found */ 140ccf4cd2eSLutz Donnerhacke u_int16_t age; /* seconds ago entry was created */ 141ccf4cd2eSLutz Donnerhacke u_int16_t staleness; /* seconds ago host last heard from */ 142ccf4cd2eSLutz Donnerhacke SLIST_ENTRY(ng_bridge_host) next; /* next entry in bucket */ 143ed2dbd31SArchie Cobbs }; 144ed2dbd31SArchie Cobbs 145ed2dbd31SArchie Cobbs /* Hash table bucket declaration */ 146ccf4cd2eSLutz Donnerhacke SLIST_HEAD(ng_bridge_bucket, ng_bridge_host); 147ed2dbd31SArchie Cobbs 148ed2dbd31SArchie Cobbs /* Netgraph node methods */ 149ed2dbd31SArchie Cobbs static ng_constructor_t ng_bridge_constructor; 150ed2dbd31SArchie Cobbs static ng_rcvmsg_t ng_bridge_rcvmsg; 151069154d5SJulian Elischer static ng_shutdown_t ng_bridge_shutdown; 152ed2dbd31SArchie Cobbs static ng_newhook_t ng_bridge_newhook; 153ed2dbd31SArchie Cobbs static ng_rcvdata_t ng_bridge_rcvdata; 154ed2dbd31SArchie Cobbs static ng_disconnect_t ng_bridge_disconnect; 155ed2dbd31SArchie Cobbs 156ed2dbd31SArchie Cobbs /* Other internal functions */ 157e0e3ded7SMark Johnston static void ng_bridge_free_link(link_p link); 1586117aa58SLutz Donnerhacke static struct ng_bridge_host *ng_bridge_get(priv_cp priv, const u_char *addr); 159631cabbaSGleb Smirnoff static int ng_bridge_put(priv_p priv, const u_char *addr, link_p link); 160ed2dbd31SArchie Cobbs static void ng_bridge_rehash(priv_p priv); 161631cabbaSGleb Smirnoff static void ng_bridge_remove_hosts(priv_p priv, link_p link); 162e0d32af7SGleb Smirnoff static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2); 1636117aa58SLutz Donnerhacke static const char *ng_bridge_nodename(node_cp node); 164ed2dbd31SArchie Cobbs 165ed2dbd31SArchie Cobbs /* Ethernet broadcast */ 166ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 167ed2dbd31SArchie Cobbs { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 168ed2dbd31SArchie Cobbs 169ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 170ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 171ed2dbd31SArchie Cobbs == ((const u_int32_t *)(b))[0] \ 172ed2dbd31SArchie Cobbs && ((const u_int16_t *)(a))[2] \ 173ed2dbd31SArchie Cobbs == ((const u_int16_t *)(b))[2]) 174ed2dbd31SArchie Cobbs 175ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */ 176ed2dbd31SArchie Cobbs #define MIN_BUCKETS (1 << 5) /* 32 */ 177ed2dbd31SArchie Cobbs #define MAX_BUCKETS (1 << 14) /* 16384 */ 178ed2dbd31SArchie Cobbs 179ed2dbd31SArchie Cobbs /* Configuration default values */ 180ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT 60 181ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 182ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE 1 183ed2dbd31SArchie Cobbs 184ed2dbd31SArchie Cobbs /****************************************************************** 185ed2dbd31SArchie Cobbs NETGRAPH PARSE TYPES 186ed2dbd31SArchie Cobbs ******************************************************************/ 187ed2dbd31SArchie Cobbs 188ed2dbd31SArchie Cobbs /* 189ed2dbd31SArchie Cobbs * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 190ed2dbd31SArchie Cobbs */ 191ed2dbd31SArchie Cobbs static int 192ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type, 193ed2dbd31SArchie Cobbs const u_char *start, const u_char *buf) 194ed2dbd31SArchie Cobbs { 195ed2dbd31SArchie Cobbs const struct ng_bridge_host_ary *const hary 196ed2dbd31SArchie Cobbs = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 197ed2dbd31SArchie Cobbs 198ed2dbd31SArchie Cobbs return hary->numHosts; 199ed2dbd31SArchie Cobbs } 200ed2dbd31SArchie Cobbs 201ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */ 202f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[] 2038c7e4101SRuslan Ermilov = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type); 204ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = { 205ed2dbd31SArchie Cobbs &ng_parse_struct_type, 206f0184ff8SArchie Cobbs &ng_bridge_host_type_fields 207ed2dbd31SArchie Cobbs }; 208ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = { 209ed2dbd31SArchie Cobbs &ng_bridge_host_type, 210ed2dbd31SArchie Cobbs ng_bridge_getTableLength 211ed2dbd31SArchie Cobbs }; 212ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = { 213ed2dbd31SArchie Cobbs &ng_parse_array_type, 214ed2dbd31SArchie Cobbs &ng_bridge_hary_type_info 215ed2dbd31SArchie Cobbs }; 216f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[] 217ed2dbd31SArchie Cobbs = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 218ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = { 219ed2dbd31SArchie Cobbs &ng_parse_struct_type, 220f0184ff8SArchie Cobbs &ng_bridge_host_ary_type_fields 221ed2dbd31SArchie Cobbs }; 222ed2dbd31SArchie Cobbs 223ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */ 224f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[] 225631cabbaSGleb Smirnoff = NG_BRIDGE_CONFIG_TYPE_INFO; 226ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = { 227ed2dbd31SArchie Cobbs &ng_parse_struct_type, 228f0184ff8SArchie Cobbs &ng_bridge_config_type_fields 229ed2dbd31SArchie Cobbs }; 230ed2dbd31SArchie Cobbs 231ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */ 232f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[] 233f0184ff8SArchie Cobbs = NG_BRIDGE_STATS_TYPE_INFO; 234ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = { 235ed2dbd31SArchie Cobbs &ng_parse_struct_type, 236f0184ff8SArchie Cobbs &ng_bridge_stats_type_fields 237ed2dbd31SArchie Cobbs }; 238b1bd4473SLutz Donnerhacke /* Parse type for struct ng_bridge_move_host */ 239b1bd4473SLutz Donnerhacke static const struct ng_parse_struct_field ng_bridge_move_host_type_fields[] 240b1bd4473SLutz Donnerhacke = NG_BRIDGE_MOVE_HOST_TYPE_INFO(&ng_parse_enaddr_type); 241b1bd4473SLutz Donnerhacke static const struct ng_parse_type ng_bridge_move_host_type = { 242b1bd4473SLutz Donnerhacke &ng_parse_struct_type, 243b1bd4473SLutz Donnerhacke &ng_bridge_move_host_type_fields 244b1bd4473SLutz Donnerhacke }; 245ed2dbd31SArchie Cobbs 246ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 247ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = { 248ed2dbd31SArchie Cobbs { 249ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 250ed2dbd31SArchie Cobbs NGM_BRIDGE_SET_CONFIG, 251ed2dbd31SArchie Cobbs "setconfig", 252ed2dbd31SArchie Cobbs &ng_bridge_config_type, 253ed2dbd31SArchie Cobbs NULL 254ed2dbd31SArchie Cobbs }, 255ed2dbd31SArchie Cobbs { 256ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 257ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_CONFIG, 258ed2dbd31SArchie Cobbs "getconfig", 259ed2dbd31SArchie Cobbs NULL, 260ed2dbd31SArchie Cobbs &ng_bridge_config_type 261ed2dbd31SArchie Cobbs }, 262ed2dbd31SArchie Cobbs { 263ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 264ed2dbd31SArchie Cobbs NGM_BRIDGE_RESET, 265ed2dbd31SArchie Cobbs "reset", 266ed2dbd31SArchie Cobbs NULL, 267ed2dbd31SArchie Cobbs NULL 268ed2dbd31SArchie Cobbs }, 269ed2dbd31SArchie Cobbs { 270ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 271ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_STATS, 272ed2dbd31SArchie Cobbs "getstats", 273ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 274ed2dbd31SArchie Cobbs &ng_bridge_stats_type 275ed2dbd31SArchie Cobbs }, 276ed2dbd31SArchie Cobbs { 277ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 278ed2dbd31SArchie Cobbs NGM_BRIDGE_CLR_STATS, 279ed2dbd31SArchie Cobbs "clrstats", 280ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 281ed2dbd31SArchie Cobbs NULL 282ed2dbd31SArchie Cobbs }, 283ed2dbd31SArchie Cobbs { 284ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 285ed2dbd31SArchie Cobbs NGM_BRIDGE_GETCLR_STATS, 286ed2dbd31SArchie Cobbs "getclrstats", 287ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 288ed2dbd31SArchie Cobbs &ng_bridge_stats_type 289ed2dbd31SArchie Cobbs }, 290ed2dbd31SArchie Cobbs { 291ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 292ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_TABLE, 293ed2dbd31SArchie Cobbs "gettable", 294ed2dbd31SArchie Cobbs NULL, 295ed2dbd31SArchie Cobbs &ng_bridge_host_ary_type 296ed2dbd31SArchie Cobbs }, 297f8aab721SMarko Zec { 298f8aab721SMarko Zec NGM_BRIDGE_COOKIE, 299f8aab721SMarko Zec NGM_BRIDGE_SET_PERSISTENT, 300f8aab721SMarko Zec "setpersistent", 301f8aab721SMarko Zec NULL, 302f8aab721SMarko Zec NULL 303f8aab721SMarko Zec }, 304b1bd4473SLutz Donnerhacke { 305b1bd4473SLutz Donnerhacke NGM_BRIDGE_COOKIE, 306b1bd4473SLutz Donnerhacke NGM_BRIDGE_MOVE_HOST, 307b1bd4473SLutz Donnerhacke "movehost", 308b1bd4473SLutz Donnerhacke &ng_bridge_move_host_type, 309b1bd4473SLutz Donnerhacke NULL 310b1bd4473SLutz Donnerhacke }, 311ed2dbd31SArchie Cobbs { 0 } 312ed2dbd31SArchie Cobbs }; 313ed2dbd31SArchie Cobbs 314ed2dbd31SArchie Cobbs /* Node type descriptor */ 315ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = { 316f8aae777SJulian Elischer .version = NG_ABI_VERSION, 317f8aae777SJulian Elischer .name = NG_BRIDGE_NODE_TYPE, 318f8aae777SJulian Elischer .constructor = ng_bridge_constructor, 319f8aae777SJulian Elischer .rcvmsg = ng_bridge_rcvmsg, 320f8aae777SJulian Elischer .shutdown = ng_bridge_shutdown, 321f8aae777SJulian Elischer .newhook = ng_bridge_newhook, 322f8aae777SJulian Elischer .rcvdata = ng_bridge_rcvdata, 323f8aae777SJulian Elischer .disconnect = ng_bridge_disconnect, 324f8aae777SJulian Elischer .cmdlist = ng_bridge_cmdlist, 325ed2dbd31SArchie Cobbs }; 326a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct); 327ed2dbd31SArchie Cobbs 328ed2dbd31SArchie Cobbs /****************************************************************** 329ed2dbd31SArchie Cobbs NETGRAPH NODE METHODS 330ed2dbd31SArchie Cobbs ******************************************************************/ 331ed2dbd31SArchie Cobbs 332ed2dbd31SArchie Cobbs /* 333ed2dbd31SArchie Cobbs * Node constructor 334ed2dbd31SArchie Cobbs */ 335ed2dbd31SArchie Cobbs static int 336069154d5SJulian Elischer ng_bridge_constructor(node_p node) 337ed2dbd31SArchie Cobbs { 338ed2dbd31SArchie Cobbs priv_p priv; 339ed2dbd31SArchie Cobbs 340ed2dbd31SArchie Cobbs /* Allocate and initialize private info */ 341674d86bfSGleb Smirnoff priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO); 342e0d32af7SGleb Smirnoff ng_callout_init(&priv->timer); 343ed2dbd31SArchie Cobbs 344ed2dbd31SArchie Cobbs /* Allocate and initialize hash table, etc. */ 345e11e3f18SDag-Erling Smørgrav priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab), 346674d86bfSGleb Smirnoff M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO); 347ed2dbd31SArchie Cobbs priv->numBuckets = MIN_BUCKETS; 348ed2dbd31SArchie Cobbs priv->hashMask = MIN_BUCKETS - 1; 349ed2dbd31SArchie Cobbs priv->conf.debugLevel = 1; 350ed2dbd31SArchie Cobbs priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 351ed2dbd31SArchie Cobbs priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 352ed2dbd31SArchie Cobbs priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 353c869d905SLutz Donnerhacke priv->sendUnknown = 1; /* classic bridge */ 354ed2dbd31SArchie Cobbs 35530400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 356069154d5SJulian Elischer priv->node = node; 357ed2dbd31SArchie Cobbs 3586c12c2b1SArchie Cobbs /* Start timer; timer is always running while node is alive */ 359e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 3606c12c2b1SArchie Cobbs 3616c12c2b1SArchie Cobbs /* Done */ 362ed2dbd31SArchie Cobbs return (0); 363ed2dbd31SArchie Cobbs } 364ed2dbd31SArchie Cobbs 365ed2dbd31SArchie Cobbs /* 366ed2dbd31SArchie Cobbs * Method for attaching a new hook 367ed2dbd31SArchie Cobbs */ 368ed2dbd31SArchie Cobbs static int 369ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name) 370ed2dbd31SArchie Cobbs { 37130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 372631cabbaSGleb Smirnoff char linkName[NG_HOOKSIZ]; 373631cabbaSGleb Smirnoff u_int32_t linkNum; 374631cabbaSGleb Smirnoff link_p link; 375f961caf2SLutz Donnerhacke const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX; 376f961caf2SLutz Donnerhacke bool isUplink; 377f961caf2SLutz Donnerhacke 378f961caf2SLutz Donnerhacke /* Check for a link hook */ 379f961caf2SLutz Donnerhacke if (strlen(name) <= strlen(prefix)) 380f961caf2SLutz Donnerhacke return (EINVAL); /* Unknown hook name */ 381f961caf2SLutz Donnerhacke 382f961caf2SLutz Donnerhacke isUplink = (name[0] == 'u'); 383f961caf2SLutz Donnerhacke if (isUplink) 384f961caf2SLutz Donnerhacke prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX; 385ed2dbd31SArchie Cobbs 386631cabbaSGleb Smirnoff /* primitive parsing */ 387f961caf2SLutz Donnerhacke linkNum = strtoul(name + strlen(prefix), NULL, 10); 388631cabbaSGleb Smirnoff /* validation by comparing against the reconstucted name */ 389f961caf2SLutz Donnerhacke snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum); 390631cabbaSGleb Smirnoff if (strcmp(linkName, name) != 0) 391ed2dbd31SArchie Cobbs return (EINVAL); 392631cabbaSGleb Smirnoff 393f961caf2SLutz Donnerhacke if (linkNum == 0 && isUplink) 394f961caf2SLutz Donnerhacke return (EINVAL); 395f961caf2SLutz Donnerhacke 396631cabbaSGleb Smirnoff if(NG_PEER_NODE(hook) == node) 397631cabbaSGleb Smirnoff return (ELOOP); 398631cabbaSGleb Smirnoff 399e0e3ded7SMark Johnston link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 400e0e3ded7SMark Johnston if (link == NULL) 401e0e3ded7SMark Johnston return (ENOMEM); 402f961caf2SLutz Donnerhacke 403e0e3ded7SMark Johnston #define NG_BRIDGE_COUNTER_ALLOC(f) do { \ 404e0e3ded7SMark Johnston link->stats.f = counter_u64_alloc(M_NOWAIT); \ 405e0e3ded7SMark Johnston if (link->stats.f == NULL) \ 406e0e3ded7SMark Johnston goto nomem; \ 407e0e3ded7SMark Johnston } while (0) 408e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvOctets); 409e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvPackets); 410e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvMulticasts); 411e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvBroadcasts); 412e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvUnknown); 413e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvRunts); 414e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvInvalid); 415e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitOctets); 416e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitPackets); 417e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitMulticasts); 418e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitBroadcasts); 419e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(loopDrops); 420e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(memoryFailures); 421e0e3ded7SMark Johnston #undef NG_BRIDGE_COUNTER_ALLOC 42266c72859SLutz Donnerhacke 423631cabbaSGleb Smirnoff link->hook = hook; 424f961caf2SLutz Donnerhacke if (isUplink) { 425f961caf2SLutz Donnerhacke link->learnMac = 0; 426f961caf2SLutz Donnerhacke link->sendUnknown = 1; 427c869d905SLutz Donnerhacke if (priv->numLinks == 0) /* if the first link is an uplink */ 428c869d905SLutz Donnerhacke priv->sendUnknown = 0; /* switch to restrictive mode */ 429f961caf2SLutz Donnerhacke } else { 430f961caf2SLutz Donnerhacke link->learnMac = 1; 431c869d905SLutz Donnerhacke link->sendUnknown = priv->sendUnknown; 432f961caf2SLutz Donnerhacke } 433f961caf2SLutz Donnerhacke 434631cabbaSGleb Smirnoff NG_HOOK_SET_PRIVATE(hook, link); 435ed2dbd31SArchie Cobbs priv->numLinks++; 436ed2dbd31SArchie Cobbs return (0); 437e0e3ded7SMark Johnston 438e0e3ded7SMark Johnston nomem: 439e0e3ded7SMark Johnston ng_bridge_free_link(link); 440e0e3ded7SMark Johnston return (ENOMEM); 441ed2dbd31SArchie Cobbs } 442ed2dbd31SArchie Cobbs 443ed2dbd31SArchie Cobbs /* 444ed2dbd31SArchie Cobbs * Receive a control message 445ed2dbd31SArchie Cobbs */ 446e0e3ded7SMark Johnston static void 447e0e3ded7SMark Johnston ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats *p) 44866c72859SLutz Donnerhacke { 44966c72859SLutz Donnerhacke counter_u64_zero(p->recvOctets); 45066c72859SLutz Donnerhacke counter_u64_zero(p->recvPackets); 45166c72859SLutz Donnerhacke counter_u64_zero(p->recvMulticasts); 45266c72859SLutz Donnerhacke counter_u64_zero(p->recvBroadcasts); 45366c72859SLutz Donnerhacke counter_u64_zero(p->recvUnknown); 45466c72859SLutz Donnerhacke counter_u64_zero(p->recvRunts); 45566c72859SLutz Donnerhacke counter_u64_zero(p->recvInvalid); 45666c72859SLutz Donnerhacke counter_u64_zero(p->xmitOctets); 45766c72859SLutz Donnerhacke counter_u64_zero(p->xmitPackets); 45866c72859SLutz Donnerhacke counter_u64_zero(p->xmitMulticasts); 45966c72859SLutz Donnerhacke counter_u64_zero(p->xmitBroadcasts); 46066c72859SLutz Donnerhacke counter_u64_zero(p->loopDrops); 461f6e0c471SLutz Donnerhacke p->loopDetects = 0; 46266c72859SLutz Donnerhacke counter_u64_zero(p->memoryFailures); 463e0e3ded7SMark Johnston } 464e0e3ded7SMark Johnston 465e0e3ded7SMark Johnston static void 466e0e3ded7SMark Johnston ng_bridge_free_link(link_p link) 467e0e3ded7SMark Johnston { 468e0e3ded7SMark Johnston counter_u64_free(link->stats.recvOctets); 469e0e3ded7SMark Johnston counter_u64_free(link->stats.recvPackets); 470e0e3ded7SMark Johnston counter_u64_free(link->stats.recvMulticasts); 471e0e3ded7SMark Johnston counter_u64_free(link->stats.recvBroadcasts); 472e0e3ded7SMark Johnston counter_u64_free(link->stats.recvUnknown); 473e0e3ded7SMark Johnston counter_u64_free(link->stats.recvRunts); 474e0e3ded7SMark Johnston counter_u64_free(link->stats.recvInvalid); 475e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitOctets); 476e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitPackets); 477e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitMulticasts); 478e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitBroadcasts); 479e0e3ded7SMark Johnston counter_u64_free(link->stats.loopDrops); 480e0e3ded7SMark Johnston counter_u64_free(link->stats.memoryFailures); 481e0e3ded7SMark Johnston free(link, M_NETGRAPH_BRIDGE); 482e0e3ded7SMark Johnston } 48366c72859SLutz Donnerhacke 484ed2dbd31SArchie Cobbs static int 4850b951c55SGleb Smirnoff ng_bridge_reset_link(hook_p hook, void *arg __unused) 486631cabbaSGleb Smirnoff { 487631cabbaSGleb Smirnoff link_p priv = NG_HOOK_PRIVATE(hook); 488631cabbaSGleb Smirnoff 489631cabbaSGleb Smirnoff priv->loopCount = 0; 49066c72859SLutz Donnerhacke ng_bridge_clear_link_stats(&priv->stats); 4910b951c55SGleb Smirnoff return (1); 492631cabbaSGleb Smirnoff } 493631cabbaSGleb Smirnoff 494631cabbaSGleb Smirnoff static int 495069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) 496ed2dbd31SArchie Cobbs { 49730400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 498ed2dbd31SArchie Cobbs struct ng_mesg *resp = NULL; 499ed2dbd31SArchie Cobbs int error = 0; 500069154d5SJulian Elischer struct ng_mesg *msg; 501ed2dbd31SArchie Cobbs 502069154d5SJulian Elischer NGI_GET_MSG(item, msg); 503ed2dbd31SArchie Cobbs switch (msg->header.typecookie) { 504ed2dbd31SArchie Cobbs case NGM_BRIDGE_COOKIE: 505ed2dbd31SArchie Cobbs switch (msg->header.cmd) { 506ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_CONFIG: 507ed2dbd31SArchie Cobbs { 508ed2dbd31SArchie Cobbs struct ng_bridge_config *conf; 509ed2dbd31SArchie Cobbs 510ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, 511ed2dbd31SArchie Cobbs sizeof(struct ng_bridge_config), M_NOWAIT); 512ed2dbd31SArchie Cobbs if (resp == NULL) { 513ed2dbd31SArchie Cobbs error = ENOMEM; 514ed2dbd31SArchie Cobbs break; 515ed2dbd31SArchie Cobbs } 516ed2dbd31SArchie Cobbs conf = (struct ng_bridge_config *)resp->data; 517ed2dbd31SArchie Cobbs *conf = priv->conf; /* no sanity checking needed */ 518ed2dbd31SArchie Cobbs break; 519ed2dbd31SArchie Cobbs } 520ed2dbd31SArchie Cobbs case NGM_BRIDGE_SET_CONFIG: 521ed2dbd31SArchie Cobbs { 522ed2dbd31SArchie Cobbs struct ng_bridge_config *conf; 523ed2dbd31SArchie Cobbs 524ed2dbd31SArchie Cobbs if (msg->header.arglen 525ed2dbd31SArchie Cobbs != sizeof(struct ng_bridge_config)) { 526ed2dbd31SArchie Cobbs error = EINVAL; 527ed2dbd31SArchie Cobbs break; 528ed2dbd31SArchie Cobbs } 529ed2dbd31SArchie Cobbs conf = (struct ng_bridge_config *)msg->data; 530ed2dbd31SArchie Cobbs priv->conf = *conf; 531ed2dbd31SArchie Cobbs break; 532ed2dbd31SArchie Cobbs } 533ed2dbd31SArchie Cobbs case NGM_BRIDGE_RESET: 534ed2dbd31SArchie Cobbs { 535631cabbaSGleb Smirnoff hook_p rethook; 536ed2dbd31SArchie Cobbs 537ed2dbd31SArchie Cobbs /* Flush all entries in the hash table */ 538631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv, NULL); 539ed2dbd31SArchie Cobbs 540ed2dbd31SArchie Cobbs /* Reset all loop detection counters and stats */ 5410b951c55SGleb Smirnoff NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL, 5420b951c55SGleb Smirnoff rethook); 543ed2dbd31SArchie Cobbs break; 544ed2dbd31SArchie Cobbs } 545ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_STATS: 546ed2dbd31SArchie Cobbs case NGM_BRIDGE_CLR_STATS: 547ed2dbd31SArchie Cobbs case NGM_BRIDGE_GETCLR_STATS: 548ed2dbd31SArchie Cobbs { 549631cabbaSGleb Smirnoff hook_p hook; 550631cabbaSGleb Smirnoff link_p link; 551631cabbaSGleb Smirnoff char linkName[NG_HOOKSIZ]; 552f961caf2SLutz Donnerhacke int linkNum; 553ed2dbd31SArchie Cobbs 554ed2dbd31SArchie Cobbs /* Get link number */ 555ed2dbd31SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 556ed2dbd31SArchie Cobbs error = EINVAL; 557ed2dbd31SArchie Cobbs break; 558ed2dbd31SArchie Cobbs } 559f961caf2SLutz Donnerhacke linkNum = *((int32_t *)msg->data); 560f961caf2SLutz Donnerhacke if (linkNum < 0) 561631cabbaSGleb Smirnoff snprintf(linkName, sizeof(linkName), 562f961caf2SLutz Donnerhacke "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum); 563f961caf2SLutz Donnerhacke else 564f961caf2SLutz Donnerhacke snprintf(linkName, sizeof(linkName), 565f961caf2SLutz Donnerhacke "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum); 566631cabbaSGleb Smirnoff 567631cabbaSGleb Smirnoff if ((hook = ng_findhook(node, linkName)) == NULL) { 568ed2dbd31SArchie Cobbs error = ENOTCONN; 569ed2dbd31SArchie Cobbs break; 570ed2dbd31SArchie Cobbs } 571631cabbaSGleb Smirnoff link = NG_HOOK_PRIVATE(hook); 572ed2dbd31SArchie Cobbs 573ed2dbd31SArchie Cobbs /* Get/clear stats */ 574ed2dbd31SArchie Cobbs if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 57566c72859SLutz Donnerhacke struct ng_bridge_link_stats *rs; 57666c72859SLutz Donnerhacke 577ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, 578ed2dbd31SArchie Cobbs sizeof(link->stats), M_NOWAIT); 579ed2dbd31SArchie Cobbs if (resp == NULL) { 580ed2dbd31SArchie Cobbs error = ENOMEM; 581ed2dbd31SArchie Cobbs break; 582ed2dbd31SArchie Cobbs } 58366c72859SLutz Donnerhacke rs = (struct ng_bridge_link_stats *)resp->data; 58466c72859SLutz Donnerhacke #define FETCH(x) rs->x = counter_u64_fetch(link->stats.x) 58566c72859SLutz Donnerhacke FETCH(recvOctets); 58666c72859SLutz Donnerhacke FETCH(recvPackets); 58766c72859SLutz Donnerhacke FETCH(recvMulticasts); 58866c72859SLutz Donnerhacke FETCH(recvBroadcasts); 58966c72859SLutz Donnerhacke FETCH(recvUnknown); 59066c72859SLutz Donnerhacke FETCH(recvRunts); 59166c72859SLutz Donnerhacke FETCH(recvInvalid); 59266c72859SLutz Donnerhacke FETCH(xmitOctets); 59366c72859SLutz Donnerhacke FETCH(xmitPackets); 59466c72859SLutz Donnerhacke FETCH(xmitMulticasts); 59566c72859SLutz Donnerhacke FETCH(xmitBroadcasts); 59666c72859SLutz Donnerhacke FETCH(loopDrops); 597f6e0c471SLutz Donnerhacke rs->loopDetects = link->stats.loopDetects; 59866c72859SLutz Donnerhacke FETCH(memoryFailures); 59966c72859SLutz Donnerhacke #undef FETCH 600ed2dbd31SArchie Cobbs } 601ed2dbd31SArchie Cobbs if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 60266c72859SLutz Donnerhacke ng_bridge_clear_link_stats(&link->stats); 603ed2dbd31SArchie Cobbs break; 604ed2dbd31SArchie Cobbs } 605ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_TABLE: 606ed2dbd31SArchie Cobbs { 607ed2dbd31SArchie Cobbs struct ng_bridge_host_ary *ary; 608ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *host; 609ed2dbd31SArchie Cobbs int i = 0, bucket; 610ed2dbd31SArchie Cobbs 611ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*ary) 612ed2dbd31SArchie Cobbs + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 613ed2dbd31SArchie Cobbs if (resp == NULL) { 614ed2dbd31SArchie Cobbs error = ENOMEM; 615ed2dbd31SArchie Cobbs break; 616ed2dbd31SArchie Cobbs } 617ed2dbd31SArchie Cobbs ary = (struct ng_bridge_host_ary *)resp->data; 618ed2dbd31SArchie Cobbs ary->numHosts = priv->numHosts; 619ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 620ccf4cd2eSLutz Donnerhacke SLIST_FOREACH(host, &priv->tab[bucket], next) { 621631cabbaSGleb Smirnoff memcpy(ary->hosts[i].addr, 622ccf4cd2eSLutz Donnerhacke host->addr, 623631cabbaSGleb Smirnoff sizeof(ary->hosts[i].addr)); 624ccf4cd2eSLutz Donnerhacke ary->hosts[i].age = host->age; 625ccf4cd2eSLutz Donnerhacke ary->hosts[i].staleness = host->staleness; 626631cabbaSGleb Smirnoff strncpy(ary->hosts[i].hook, 627ccf4cd2eSLutz Donnerhacke NG_HOOK_NAME(host->link->hook), 628631cabbaSGleb Smirnoff sizeof(ary->hosts[i].hook)); 629631cabbaSGleb Smirnoff i++; 630631cabbaSGleb Smirnoff } 631ed2dbd31SArchie Cobbs } 632ed2dbd31SArchie Cobbs break; 633ed2dbd31SArchie Cobbs } 634f8aab721SMarko Zec case NGM_BRIDGE_SET_PERSISTENT: 635f8aab721SMarko Zec { 636f8aab721SMarko Zec priv->persistent = 1; 637f8aab721SMarko Zec break; 638f8aab721SMarko Zec } 639b1bd4473SLutz Donnerhacke case NGM_BRIDGE_MOVE_HOST: 640b1bd4473SLutz Donnerhacke { 641b1bd4473SLutz Donnerhacke struct ng_bridge_move_host *mh; 642b1bd4473SLutz Donnerhacke hook_p hook; 643b1bd4473SLutz Donnerhacke 644b1bd4473SLutz Donnerhacke if (msg->header.arglen < sizeof(*mh)) { 645b1bd4473SLutz Donnerhacke error = EINVAL; 646b1bd4473SLutz Donnerhacke break; 647b1bd4473SLutz Donnerhacke } 648b1bd4473SLutz Donnerhacke mh = (struct ng_bridge_move_host *)msg->data; 649b1bd4473SLutz Donnerhacke hook = (mh->hook[0] == 0) 650b1bd4473SLutz Donnerhacke ? lasthook 651b1bd4473SLutz Donnerhacke : ng_findhook(node, mh->hook); 652b1bd4473SLutz Donnerhacke if (hook == NULL) { 653b1bd4473SLutz Donnerhacke error = ENOENT; 654b1bd4473SLutz Donnerhacke break; 655b1bd4473SLutz Donnerhacke } 656b1bd4473SLutz Donnerhacke error = ng_bridge_put(priv, mh->addr, NG_HOOK_PRIVATE(hook)); 657b1bd4473SLutz Donnerhacke break; 658b1bd4473SLutz Donnerhacke } 659ed2dbd31SArchie Cobbs default: 660ed2dbd31SArchie Cobbs error = EINVAL; 661ed2dbd31SArchie Cobbs break; 662ed2dbd31SArchie Cobbs } 663ed2dbd31SArchie Cobbs break; 664ed2dbd31SArchie Cobbs default: 665ed2dbd31SArchie Cobbs error = EINVAL; 666ed2dbd31SArchie Cobbs break; 667ed2dbd31SArchie Cobbs } 668ed2dbd31SArchie Cobbs 669ed2dbd31SArchie Cobbs /* Done */ 670069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 671069154d5SJulian Elischer NG_FREE_MSG(msg); 672ed2dbd31SArchie Cobbs return (error); 673ed2dbd31SArchie Cobbs } 674ed2dbd31SArchie Cobbs 675ed2dbd31SArchie Cobbs /* 676ed2dbd31SArchie Cobbs * Receive data on a hook 677ed2dbd31SArchie Cobbs */ 678631cabbaSGleb Smirnoff struct ng_bridge_send_ctx { 679631cabbaSGleb Smirnoff link_p foundFirst, incoming; 680631cabbaSGleb Smirnoff struct mbuf * m; 681631cabbaSGleb Smirnoff int manycast, error; 682631cabbaSGleb Smirnoff }; 683631cabbaSGleb Smirnoff 6843c958f5fSLutz Donnerhacke /* 6853c958f5fSLutz Donnerhacke * Update stats and send out 6863c958f5fSLutz Donnerhacke */ 6873c958f5fSLutz Donnerhacke static inline int 6883c958f5fSLutz Donnerhacke ng_bridge_send_data(link_cp dst, int manycast, struct mbuf *m, item_p item) { 6893c958f5fSLutz Donnerhacke int error = 0; 6903c958f5fSLutz Donnerhacke size_t len = m->m_pkthdr.len; 6913c958f5fSLutz Donnerhacke 6923c958f5fSLutz Donnerhacke if(item != NULL) 6933c958f5fSLutz Donnerhacke NG_FWD_NEW_DATA(error, item, dst->hook, m); 6943c958f5fSLutz Donnerhacke else 6953c958f5fSLutz Donnerhacke NG_SEND_DATA_ONLY(error, dst->hook, m); 6963c958f5fSLutz Donnerhacke 697a56e5ad6SLutz Donnerhacke if (error) { 698*319e9fc6SGleb Smirnoff if (error == ENOMEM) 699*319e9fc6SGleb Smirnoff counter_u64_add(dst->stats.memoryFailures, 1); 700a56e5ad6SLutz Donnerhacke /* The packet is still ours */ 701a56e5ad6SLutz Donnerhacke if (item != NULL) 702a56e5ad6SLutz Donnerhacke NG_FREE_ITEM(item); 703a56e5ad6SLutz Donnerhacke if (m != NULL) 704a56e5ad6SLutz Donnerhacke NG_FREE_M(m); 705a56e5ad6SLutz Donnerhacke return (error); 706a56e5ad6SLutz Donnerhacke } 707a56e5ad6SLutz Donnerhacke 7083c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitPackets, 1); 7093c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitOctets, len); 7103c958f5fSLutz Donnerhacke switch (manycast) { 7113c958f5fSLutz Donnerhacke default: /* unknown unicast */ 7123c958f5fSLutz Donnerhacke break; 7133c958f5fSLutz Donnerhacke case 1: /* multicast */ 7143c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitMulticasts, 1); 7153c958f5fSLutz Donnerhacke break; 7163c958f5fSLutz Donnerhacke case 2: /* broadcast */ 7173c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitBroadcasts, 1); 7183c958f5fSLutz Donnerhacke break; 7193c958f5fSLutz Donnerhacke } 720a56e5ad6SLutz Donnerhacke return (0); 7213c958f5fSLutz Donnerhacke } 7223c958f5fSLutz Donnerhacke 7233c958f5fSLutz Donnerhacke /* 7243c958f5fSLutz Donnerhacke * Loop body for sending to multiple destinations 7253c958f5fSLutz Donnerhacke * return 0 to stop looping 7263c958f5fSLutz Donnerhacke */ 727631cabbaSGleb Smirnoff static int 7280b951c55SGleb Smirnoff ng_bridge_send_ctx(hook_p dst, void *arg) 729631cabbaSGleb Smirnoff { 7300b951c55SGleb Smirnoff struct ng_bridge_send_ctx *ctx = arg; 731631cabbaSGleb Smirnoff link_p destLink = NG_HOOK_PRIVATE(dst); 732631cabbaSGleb Smirnoff struct mbuf *m2 = NULL; 733631cabbaSGleb Smirnoff int error = 0; 734631cabbaSGleb Smirnoff 735631cabbaSGleb Smirnoff /* Skip incoming link */ 736631cabbaSGleb Smirnoff if (destLink == ctx->incoming) { 737631cabbaSGleb Smirnoff return (1); 738631cabbaSGleb Smirnoff } 739631cabbaSGleb Smirnoff 740f961caf2SLutz Donnerhacke /* Skip sending unknowns to undesired links */ 741f961caf2SLutz Donnerhacke if (!ctx->manycast && !destLink->sendUnknown) 742f961caf2SLutz Donnerhacke return (1); 743f961caf2SLutz Donnerhacke 744631cabbaSGleb Smirnoff if (ctx->foundFirst == NULL) { 745631cabbaSGleb Smirnoff /* 746631cabbaSGleb Smirnoff * This is the first usable link we have found. 747631cabbaSGleb Smirnoff * Reserve it for the originals. 748631cabbaSGleb Smirnoff * If we never find another we save a copy. 749631cabbaSGleb Smirnoff */ 750631cabbaSGleb Smirnoff ctx->foundFirst = destLink; 751631cabbaSGleb Smirnoff return (1); 752631cabbaSGleb Smirnoff } 753631cabbaSGleb Smirnoff 754631cabbaSGleb Smirnoff /* 755631cabbaSGleb Smirnoff * It's usable link but not the reserved (first) one. 756631cabbaSGleb Smirnoff * Copy mbuf info for sending. 757631cabbaSGleb Smirnoff */ 758a56e5ad6SLutz Donnerhacke m2 = m_dup(ctx->m, M_NOWAIT); 759631cabbaSGleb Smirnoff if (m2 == NULL) { 76066c72859SLutz Donnerhacke counter_u64_add(ctx->incoming->stats.memoryFailures, 1); 761631cabbaSGleb Smirnoff ctx->error = ENOBUFS; 762a56e5ad6SLutz Donnerhacke return (0); /* abort loop, do not try again and again */ 763631cabbaSGleb Smirnoff } 764631cabbaSGleb Smirnoff 765631cabbaSGleb Smirnoff /* Send packet */ 7663c958f5fSLutz Donnerhacke error = ng_bridge_send_data(destLink, ctx->manycast, m2, NULL); 767631cabbaSGleb Smirnoff if (error) 768631cabbaSGleb Smirnoff ctx->error = error; 769631cabbaSGleb Smirnoff return (1); 770631cabbaSGleb Smirnoff } 771631cabbaSGleb Smirnoff 772ed2dbd31SArchie Cobbs static int 773069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item) 774ed2dbd31SArchie Cobbs { 77530400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 77630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 777ed2dbd31SArchie Cobbs struct ng_bridge_host *host; 778ed2dbd31SArchie Cobbs struct ether_header *eh; 779631cabbaSGleb Smirnoff struct ng_bridge_send_ctx ctx = { 0 }; 780631cabbaSGleb Smirnoff hook_p ret; 781ed2dbd31SArchie Cobbs 782631cabbaSGleb Smirnoff NGI_GET_M(item, ctx.m); 783ed2dbd31SArchie Cobbs 784631cabbaSGleb Smirnoff ctx.incoming = NG_HOOK_PRIVATE(hook); 785ed2dbd31SArchie Cobbs /* Sanity check packet and pull up header */ 786631cabbaSGleb Smirnoff if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) { 78766c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvRunts, 1); 788069154d5SJulian Elischer NG_FREE_ITEM(item); 789631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 790ed2dbd31SArchie Cobbs return (EINVAL); 791ed2dbd31SArchie Cobbs } 792631cabbaSGleb Smirnoff if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) { 79366c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 794069154d5SJulian Elischer NG_FREE_ITEM(item); 795ed2dbd31SArchie Cobbs return (ENOBUFS); 796ed2dbd31SArchie Cobbs } 797631cabbaSGleb Smirnoff eh = mtod(ctx.m, struct ether_header *); 798ed2dbd31SArchie Cobbs if ((eh->ether_shost[0] & 1) != 0) { 79966c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvInvalid, 1); 800069154d5SJulian Elischer NG_FREE_ITEM(item); 801631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 802ed2dbd31SArchie Cobbs return (EINVAL); 803ed2dbd31SArchie Cobbs } 804ed2dbd31SArchie Cobbs 805ed2dbd31SArchie Cobbs /* Is link disabled due to a loopback condition? */ 806631cabbaSGleb Smirnoff if (ctx.incoming->loopCount != 0) { 80766c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.loopDrops, 1); 808069154d5SJulian Elischer NG_FREE_ITEM(item); 809631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 810f6e0c471SLutz Donnerhacke return (ELOOP); 811ed2dbd31SArchie Cobbs } 812ed2dbd31SArchie Cobbs 813ed2dbd31SArchie Cobbs /* Update stats */ 81466c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvPackets, 1); 81566c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len); 816631cabbaSGleb Smirnoff if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) { 817ed2dbd31SArchie Cobbs if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 81866c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1); 819631cabbaSGleb Smirnoff ctx.manycast = 2; 820ed2dbd31SArchie Cobbs } else 82166c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvMulticasts, 1); 822ed2dbd31SArchie Cobbs } 823ed2dbd31SArchie Cobbs 824ed2dbd31SArchie Cobbs /* Look up packet's source Ethernet address in hashtable */ 825f6e0c471SLutz Donnerhacke if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) 826011b7317SLutz Donnerhacke /* Update time since last heard from this host. 827011b7317SLutz Donnerhacke * This is safe without locking, because it's 828011b7317SLutz Donnerhacke * the only operation during shared access. 829011b7317SLutz Donnerhacke */ 8304dfe70fdSLutz Donnerhacke if (__predict_false(host->staleness > 0)) 831ed2dbd31SArchie Cobbs host->staleness = 0; 832ed2dbd31SArchie Cobbs 833f6e0c471SLutz Donnerhacke if ((host == NULL && ctx.incoming->learnMac) || 834f6e0c471SLutz Donnerhacke (host != NULL && host->link != ctx.incoming)) { 835b1bd4473SLutz Donnerhacke struct ng_mesg *msg; 836b1bd4473SLutz Donnerhacke struct ng_bridge_move_host *mh; 837b1bd4473SLutz Donnerhacke int error = 0; 838b1bd4473SLutz Donnerhacke 839b1bd4473SLutz Donnerhacke NG_MKMESSAGE(msg, NGM_BRIDGE_COOKIE, NGM_BRIDGE_MOVE_HOST, 840b1bd4473SLutz Donnerhacke sizeof(*mh), M_NOWAIT); 841b1bd4473SLutz Donnerhacke if (msg == NULL) { 84266c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 843069154d5SJulian Elischer NG_FREE_ITEM(item); 844631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 845ed2dbd31SArchie Cobbs return (ENOMEM); 846ed2dbd31SArchie Cobbs } 847b1bd4473SLutz Donnerhacke mh = (struct ng_bridge_move_host *)msg->data; 848b1bd4473SLutz Donnerhacke strncpy(mh->hook, NG_HOOK_NAME(ctx.incoming->hook), 849b1bd4473SLutz Donnerhacke sizeof(mh->hook)); 850b1bd4473SLutz Donnerhacke memcpy(mh->addr, eh->ether_shost, sizeof(mh->addr)); 851b1bd4473SLutz Donnerhacke NG_SEND_MSG_ID(error, node, msg, NG_NODE_ID(node), 852b1bd4473SLutz Donnerhacke NG_NODE_ID(node)); 853b1bd4473SLutz Donnerhacke if (error) 854b1bd4473SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 855ed2dbd31SArchie Cobbs } 856ed2dbd31SArchie Cobbs 857f6e0c471SLutz Donnerhacke if (host != NULL && host->link != ctx.incoming) { 858f6e0c471SLutz Donnerhacke if (host->age < priv->conf.minStableAge) { 859f6e0c471SLutz Donnerhacke /* Drop packet on instable links */ 860f6e0c471SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.loopDrops, 1); 861f6e0c471SLutz Donnerhacke NG_FREE_ITEM(item); 862f6e0c471SLutz Donnerhacke NG_FREE_M(ctx.m); 863f6e0c471SLutz Donnerhacke return (ELOOP); 864f6e0c471SLutz Donnerhacke } 865f6e0c471SLutz Donnerhacke } 866f6e0c471SLutz Donnerhacke 867ed2dbd31SArchie Cobbs /* Run packet through ipfw processing, if enabled */ 8689b932e9eSAndre Oppermann #if 0 8690b4b0b0fSJulian Elischer if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) { 870ed2dbd31SArchie Cobbs /* XXX not implemented yet */ 871ed2dbd31SArchie Cobbs } 8729b932e9eSAndre Oppermann #endif 873ed2dbd31SArchie Cobbs 874ed2dbd31SArchie Cobbs /* 875ed2dbd31SArchie Cobbs * If unicast and destination host known, deliver to host's link, 876ed2dbd31SArchie Cobbs * unless it is the same link as the packet came in on. 877ed2dbd31SArchie Cobbs */ 878631cabbaSGleb Smirnoff if (!ctx.manycast) { 879ed2dbd31SArchie Cobbs /* Determine packet destination link */ 880ed2dbd31SArchie Cobbs if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 881631cabbaSGleb Smirnoff link_p destLink = host->link; 882ed2dbd31SArchie Cobbs 883ed2dbd31SArchie Cobbs /* If destination same as incoming link, do nothing */ 884631cabbaSGleb Smirnoff if (destLink == ctx.incoming) { 885069154d5SJulian Elischer NG_FREE_ITEM(item); 886631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 887ed2dbd31SArchie Cobbs return (0); 888ed2dbd31SArchie Cobbs } 889ed2dbd31SArchie Cobbs 890ed2dbd31SArchie Cobbs /* Deliver packet out the destination link */ 8913c958f5fSLutz Donnerhacke return (ng_bridge_send_data(destLink, ctx.manycast, ctx.m, item)); 892ed2dbd31SArchie Cobbs } 893ed2dbd31SArchie Cobbs 894ed2dbd31SArchie Cobbs /* Destination host is not known */ 89566c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvUnknown, 1); 896ed2dbd31SArchie Cobbs } 897ed2dbd31SArchie Cobbs 898ed2dbd31SArchie Cobbs /* Distribute unknown, multicast, broadcast pkts to all other links */ 899631cabbaSGleb Smirnoff NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret); 900ed2dbd31SArchie Cobbs 901a56e5ad6SLutz Donnerhacke /* Finally send out on the first link found */ 902a56e5ad6SLutz Donnerhacke if (ctx.foundFirst != NULL) { 903a56e5ad6SLutz Donnerhacke int error = ng_bridge_send_data(ctx.foundFirst, ctx.manycast, ctx.m, item); 904a56e5ad6SLutz Donnerhacke if (error) 905a56e5ad6SLutz Donnerhacke ctx.error = error; 906a56e5ad6SLutz Donnerhacke } else { /* nothing to send at all */ 907069154d5SJulian Elischer NG_FREE_ITEM(item); 908631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 909069154d5SJulian Elischer } 910069154d5SJulian Elischer 911a56e5ad6SLutz Donnerhacke return (ctx.error); 912ed2dbd31SArchie Cobbs } 913ed2dbd31SArchie Cobbs 914ed2dbd31SArchie Cobbs /* 915ed2dbd31SArchie Cobbs * Shutdown node 916ed2dbd31SArchie Cobbs */ 917ed2dbd31SArchie Cobbs static int 918069154d5SJulian Elischer ng_bridge_shutdown(node_p node) 919ed2dbd31SArchie Cobbs { 92030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 921ed2dbd31SArchie Cobbs 9226c12c2b1SArchie Cobbs /* 923195cf617SRuslan Ermilov * Shut down everything including the timer. Even if the 924195cf617SRuslan Ermilov * callout has already been dequeued and is about to be 925195cf617SRuslan Ermilov * run, ng_bridge_timeout() won't be fired as the node 926195cf617SRuslan Ermilov * is already marked NGF_INVALID, so we're safe to free 927195cf617SRuslan Ermilov * the node now. 9286c12c2b1SArchie Cobbs */ 929ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 930ed2dbd31SArchie Cobbs ("%s: numLinks=%d numHosts=%d", 9316e551fb6SDavid E. O'Brien __func__, priv->numLinks, priv->numHosts)); 932195cf617SRuslan Ermilov ng_uncallout(&priv->timer, node); 933195cf617SRuslan Ermilov NG_NODE_SET_PRIVATE(node, NULL); 934195cf617SRuslan Ermilov NG_NODE_UNREF(node); 9351ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE); 9361ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH_BRIDGE); 937ed2dbd31SArchie Cobbs return (0); 938ed2dbd31SArchie Cobbs } 939ed2dbd31SArchie Cobbs 940ed2dbd31SArchie Cobbs /* 941ed2dbd31SArchie Cobbs * Hook disconnection. 942ed2dbd31SArchie Cobbs */ 943ed2dbd31SArchie Cobbs static int 944ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook) 945ed2dbd31SArchie Cobbs { 94630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 947631cabbaSGleb Smirnoff link_p link = NG_HOOK_PRIVATE(hook); 948ed2dbd31SArchie Cobbs 949ed2dbd31SArchie Cobbs /* Remove all hosts associated with this link */ 950631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv, link); 951ed2dbd31SArchie Cobbs 952ed2dbd31SArchie Cobbs /* Free associated link information */ 953e0e3ded7SMark Johnston ng_bridge_free_link(link); 954ed2dbd31SArchie Cobbs priv->numLinks--; 955ed2dbd31SArchie Cobbs 956ed2dbd31SArchie Cobbs /* If no more hooks, go away */ 95730400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 958f8aab721SMarko Zec && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 959f8aab721SMarko Zec && !priv->persistent) { 96030400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); 96130400f03SJulian Elischer } 962ed2dbd31SArchie Cobbs return (0); 963ed2dbd31SArchie Cobbs } 964ed2dbd31SArchie Cobbs 965ed2dbd31SArchie Cobbs /****************************************************************** 966ed2dbd31SArchie Cobbs HASH TABLE FUNCTIONS 967ed2dbd31SArchie Cobbs ******************************************************************/ 968ed2dbd31SArchie Cobbs 969ed2dbd31SArchie Cobbs /* 970ed2dbd31SArchie Cobbs * Hash algorithm 971ed2dbd31SArchie Cobbs */ 972ed2dbd31SArchie Cobbs #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 973ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[1] \ 974ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 975ed2dbd31SArchie Cobbs 976ed2dbd31SArchie Cobbs /* 977ed2dbd31SArchie Cobbs * Find a host entry in the table. 978ed2dbd31SArchie Cobbs */ 979ed2dbd31SArchie Cobbs static struct ng_bridge_host * 9806117aa58SLutz Donnerhacke ng_bridge_get(priv_cp priv, const u_char *addr) 981ed2dbd31SArchie Cobbs { 982ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask); 983ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *host; 984ed2dbd31SArchie Cobbs 985ccf4cd2eSLutz Donnerhacke SLIST_FOREACH(host, &priv->tab[bucket], next) { 986ccf4cd2eSLutz Donnerhacke if (ETHER_EQUAL(host->addr, addr)) 987ccf4cd2eSLutz Donnerhacke return (host); 988ed2dbd31SArchie Cobbs } 989ed2dbd31SArchie Cobbs return (NULL); 990ed2dbd31SArchie Cobbs } 991ed2dbd31SArchie Cobbs 992ed2dbd31SArchie Cobbs /* 993f6e0c471SLutz Donnerhacke * Add a host entry to the table. If it already exists, move it 994f6e0c471SLutz Donnerhacke * to the new link. Returns 0 on success. 995ed2dbd31SArchie Cobbs */ 996ed2dbd31SArchie Cobbs static int 997631cabbaSGleb Smirnoff ng_bridge_put(priv_p priv, const u_char *addr, link_p link) 998ed2dbd31SArchie Cobbs { 999ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask); 1000ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *host; 1001ed2dbd31SArchie Cobbs 1002f6e0c471SLutz Donnerhacke if ((host = ng_bridge_get(priv, addr)) != NULL) { 1003f6e0c471SLutz Donnerhacke /* Host already on the correct link? */ 1004f6e0c471SLutz Donnerhacke if (host->link == link) 1005f6e0c471SLutz Donnerhacke return 0; 1006f6e0c471SLutz Donnerhacke 1007f6e0c471SLutz Donnerhacke /* Move old host over to new link */ 1008f6e0c471SLutz Donnerhacke if (host->age >= priv->conf.minStableAge) { 1009f6e0c471SLutz Donnerhacke host->link = link; 1010f6e0c471SLutz Donnerhacke host->age = 0; 1011f6e0c471SLutz Donnerhacke return (0); 1012f6e0c471SLutz Donnerhacke } 1013f6e0c471SLutz Donnerhacke /* 1014f6e0c471SLutz Donnerhacke * If the host was recently moved to the old link and 1015f6e0c471SLutz Donnerhacke * it's now jumping to a new link, declare a loopback 1016f6e0c471SLutz Donnerhacke * condition. 1017f6e0c471SLutz Donnerhacke */ 1018f6e0c471SLutz Donnerhacke if (priv->conf.debugLevel >= 2) 1019f6e0c471SLutz Donnerhacke log(LOG_WARNING, "ng_bridge: %s:" 1020f6e0c471SLutz Donnerhacke " loopback detected on %s\n", 1021f6e0c471SLutz Donnerhacke ng_bridge_nodename(priv->node), 1022f6e0c471SLutz Donnerhacke NG_HOOK_NAME(link->hook)); 1023f6e0c471SLutz Donnerhacke 1024f6e0c471SLutz Donnerhacke /* Mark link as linka non grata */ 1025f6e0c471SLutz Donnerhacke link->loopCount = priv->conf.loopTimeout; 1026f6e0c471SLutz Donnerhacke link->stats.loopDetects++; 1027f6e0c471SLutz Donnerhacke 1028f6e0c471SLutz Donnerhacke /* Forget all hosts on this link */ 1029f6e0c471SLutz Donnerhacke ng_bridge_remove_hosts(priv, link); 1030f6e0c471SLutz Donnerhacke return (ELOOP); 1031f6e0c471SLutz Donnerhacke } 1032ed2dbd31SArchie Cobbs 1033ed2dbd31SArchie Cobbs /* Allocate and initialize new hashtable entry */ 1034ccf4cd2eSLutz Donnerhacke host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT); 1035ccf4cd2eSLutz Donnerhacke if (host == NULL) 1036b1bd4473SLutz Donnerhacke return (ENOMEM); 1037ccf4cd2eSLutz Donnerhacke bcopy(addr, host->addr, ETHER_ADDR_LEN); 1038ccf4cd2eSLutz Donnerhacke host->link = link; 1039ccf4cd2eSLutz Donnerhacke host->staleness = 0; 1040ccf4cd2eSLutz Donnerhacke host->age = 0; 1041ed2dbd31SArchie Cobbs 1042ed2dbd31SArchie Cobbs /* Add new element to hash bucket */ 1043ccf4cd2eSLutz Donnerhacke SLIST_INSERT_HEAD(&priv->tab[bucket], host, next); 1044ed2dbd31SArchie Cobbs priv->numHosts++; 1045ed2dbd31SArchie Cobbs 1046ed2dbd31SArchie Cobbs /* Resize table if necessary */ 1047ed2dbd31SArchie Cobbs ng_bridge_rehash(priv); 1048b1bd4473SLutz Donnerhacke return (0); 1049ed2dbd31SArchie Cobbs } 1050ed2dbd31SArchie Cobbs 1051ed2dbd31SArchie Cobbs /* 1052ed2dbd31SArchie Cobbs * Resize the hash table. We try to maintain the number of buckets 1053ed2dbd31SArchie Cobbs * such that the load factor is in the range 0.25 to 1.0. 1054ed2dbd31SArchie Cobbs * 1055ed2dbd31SArchie Cobbs * If we can't get the new memory then we silently fail. This is OK 1056ed2dbd31SArchie Cobbs * because things will still work and we'll try again soon anyway. 1057ed2dbd31SArchie Cobbs */ 1058ed2dbd31SArchie Cobbs static void 1059ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv) 1060ed2dbd31SArchie Cobbs { 1061ed2dbd31SArchie Cobbs struct ng_bridge_bucket *newTab; 1062ed2dbd31SArchie Cobbs int oldBucket, newBucket; 1063ed2dbd31SArchie Cobbs int newNumBuckets; 1064ed2dbd31SArchie Cobbs u_int newMask; 1065ed2dbd31SArchie Cobbs 1066ed2dbd31SArchie Cobbs /* Is table too full or too empty? */ 1067ed2dbd31SArchie Cobbs if (priv->numHosts > priv->numBuckets 1068ed2dbd31SArchie Cobbs && (priv->numBuckets << 1) <= MAX_BUCKETS) 1069ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets << 1; 1070ed2dbd31SArchie Cobbs else if (priv->numHosts < (priv->numBuckets >> 2) 1071ed2dbd31SArchie Cobbs && (priv->numBuckets >> 2) >= MIN_BUCKETS) 1072ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets >> 2; 1073ed2dbd31SArchie Cobbs else 1074ed2dbd31SArchie Cobbs return; 1075ed2dbd31SArchie Cobbs newMask = newNumBuckets - 1; 1076ed2dbd31SArchie Cobbs 1077ed2dbd31SArchie Cobbs /* Allocate and initialize new table */ 1078ac2fffa4SPedro F. Giffuni newTab = malloc(newNumBuckets * sizeof(*newTab), 1079e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 1080ed2dbd31SArchie Cobbs if (newTab == NULL) 1081ed2dbd31SArchie Cobbs return; 1082ed2dbd31SArchie Cobbs 1083ed2dbd31SArchie Cobbs /* Move all entries from old table to new table */ 1084ed2dbd31SArchie Cobbs for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 1085ed2dbd31SArchie Cobbs struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 1086ed2dbd31SArchie Cobbs 1087ed2dbd31SArchie Cobbs while (!SLIST_EMPTY(oldList)) { 1088ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host 1089ed2dbd31SArchie Cobbs = SLIST_FIRST(oldList); 1090ed2dbd31SArchie Cobbs 1091ed2dbd31SArchie Cobbs SLIST_REMOVE_HEAD(oldList, next); 1092ccf4cd2eSLutz Donnerhacke newBucket = HASH(host->addr, newMask); 1093ccf4cd2eSLutz Donnerhacke SLIST_INSERT_HEAD(&newTab[newBucket], host, next); 1094ed2dbd31SArchie Cobbs } 1095ed2dbd31SArchie Cobbs } 1096ed2dbd31SArchie Cobbs 1097ed2dbd31SArchie Cobbs /* Replace old table with new one */ 1098ed2dbd31SArchie Cobbs if (priv->conf.debugLevel >= 3) { 1099ed2dbd31SArchie Cobbs log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 1100ed2dbd31SArchie Cobbs ng_bridge_nodename(priv->node), 1101ed2dbd31SArchie Cobbs priv->numBuckets, newNumBuckets); 1102ed2dbd31SArchie Cobbs } 11031ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE); 1104ed2dbd31SArchie Cobbs priv->numBuckets = newNumBuckets; 1105ed2dbd31SArchie Cobbs priv->hashMask = newMask; 1106ed2dbd31SArchie Cobbs priv->tab = newTab; 1107ed2dbd31SArchie Cobbs return; 1108ed2dbd31SArchie Cobbs } 1109ed2dbd31SArchie Cobbs 1110ed2dbd31SArchie Cobbs /****************************************************************** 1111ed2dbd31SArchie Cobbs MISC FUNCTIONS 1112ed2dbd31SArchie Cobbs ******************************************************************/ 1113ed2dbd31SArchie Cobbs 1114ed2dbd31SArchie Cobbs /* 1115ed2dbd31SArchie Cobbs * Remove all hosts associated with a specific link from the hashtable. 1116ed2dbd31SArchie Cobbs * If linkNum == -1, then remove all hosts in the table. 1117ed2dbd31SArchie Cobbs */ 1118ed2dbd31SArchie Cobbs static void 1119631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv_p priv, link_p link) 1120ed2dbd31SArchie Cobbs { 1121ed2dbd31SArchie Cobbs int bucket; 1122ed2dbd31SArchie Cobbs 1123ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1124ccf4cd2eSLutz Donnerhacke struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1125ed2dbd31SArchie Cobbs 1126ed2dbd31SArchie Cobbs while (*hptr != NULL) { 1127ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host = *hptr; 1128ed2dbd31SArchie Cobbs 1129ccf4cd2eSLutz Donnerhacke if (link == NULL || host->link == link) { 1130ccf4cd2eSLutz Donnerhacke *hptr = SLIST_NEXT(host, next); 1131ccf4cd2eSLutz Donnerhacke free(host, M_NETGRAPH_BRIDGE); 1132ed2dbd31SArchie Cobbs priv->numHosts--; 1133ed2dbd31SArchie Cobbs } else 1134ccf4cd2eSLutz Donnerhacke hptr = &SLIST_NEXT(host, next); 1135ed2dbd31SArchie Cobbs } 1136ed2dbd31SArchie Cobbs } 1137ed2dbd31SArchie Cobbs } 1138ed2dbd31SArchie Cobbs 1139ed2dbd31SArchie Cobbs /* 1140ed2dbd31SArchie Cobbs * Handle our once-per-second timeout event. We do two things: 1141ed2dbd31SArchie Cobbs * we decrement link->loopCount for those links being muted due to 1142ed2dbd31SArchie Cobbs * a detected loopback condition, and we remove any hosts from 1143ed2dbd31SArchie Cobbs * the hashtable whom we haven't heard from in a long while. 1144ed2dbd31SArchie Cobbs */ 1145631cabbaSGleb Smirnoff static int 11460b951c55SGleb Smirnoff ng_bridge_unmute(hook_p hook, void *arg) 1147631cabbaSGleb Smirnoff { 1148631cabbaSGleb Smirnoff link_p link = NG_HOOK_PRIVATE(hook); 1149631cabbaSGleb Smirnoff node_p node = NG_HOOK_NODE(hook); 1150631cabbaSGleb Smirnoff priv_p priv = NG_NODE_PRIVATE(node); 11510b951c55SGleb Smirnoff int *counter = arg; 1152631cabbaSGleb Smirnoff 1153631cabbaSGleb Smirnoff if (link->loopCount != 0) { 1154631cabbaSGleb Smirnoff link->loopCount--; 1155631cabbaSGleb Smirnoff if (link->loopCount == 0 && priv->conf.debugLevel >= 2) { 1156631cabbaSGleb Smirnoff log(LOG_INFO, "ng_bridge: %s:" 1157631cabbaSGleb Smirnoff " restoring looped back %s\n", 1158631cabbaSGleb Smirnoff ng_bridge_nodename(node), NG_HOOK_NAME(hook)); 1159631cabbaSGleb Smirnoff } 1160631cabbaSGleb Smirnoff } 1161abc4b11cSGleb Smirnoff (*counter)++; 1162631cabbaSGleb Smirnoff return (1); 1163631cabbaSGleb Smirnoff } 1164631cabbaSGleb Smirnoff 1165ed2dbd31SArchie Cobbs static void 1166e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) 1167ed2dbd31SArchie Cobbs { 116830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1169e0d32af7SGleb Smirnoff int bucket; 1170ed2dbd31SArchie Cobbs int counter = 0; 1171631cabbaSGleb Smirnoff hook_p ret; 1172ed2dbd31SArchie Cobbs 1173ed2dbd31SArchie Cobbs /* Update host time counters and remove stale entries */ 1174ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1175ccf4cd2eSLutz Donnerhacke struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1176ed2dbd31SArchie Cobbs 1177ed2dbd31SArchie Cobbs while (*hptr != NULL) { 1178ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host = *hptr; 1179ed2dbd31SArchie Cobbs 1180ed2dbd31SArchie Cobbs /* Remove hosts we haven't heard from in a while */ 1181ccf4cd2eSLutz Donnerhacke if (++host->staleness >= priv->conf.maxStaleness) { 1182ccf4cd2eSLutz Donnerhacke *hptr = SLIST_NEXT(host, next); 1183ccf4cd2eSLutz Donnerhacke free(host, M_NETGRAPH_BRIDGE); 1184ed2dbd31SArchie Cobbs priv->numHosts--; 1185ed2dbd31SArchie Cobbs } else { 1186ccf4cd2eSLutz Donnerhacke if (host->age < 0xffff) 1187ccf4cd2eSLutz Donnerhacke host->age++; 1188ccf4cd2eSLutz Donnerhacke hptr = &SLIST_NEXT(host, next); 1189ed2dbd31SArchie Cobbs counter++; 1190ed2dbd31SArchie Cobbs } 1191ed2dbd31SArchie Cobbs } 1192ed2dbd31SArchie Cobbs } 1193ed2dbd31SArchie Cobbs KASSERT(priv->numHosts == counter, 11946e551fb6SDavid E. O'Brien ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); 1195ed2dbd31SArchie Cobbs 1196ed2dbd31SArchie Cobbs /* Decrease table size if necessary */ 1197ed2dbd31SArchie Cobbs ng_bridge_rehash(priv); 1198ed2dbd31SArchie Cobbs 1199ed2dbd31SArchie Cobbs /* Decrease loop counter on muted looped back links */ 1200631cabbaSGleb Smirnoff counter = 0; 1201631cabbaSGleb Smirnoff NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret); 1202ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == counter, 12036e551fb6SDavid E. O'Brien ("%s: links: %d != %d", __func__, priv->numLinks, counter)); 1204ed2dbd31SArchie Cobbs 1205e0d32af7SGleb Smirnoff /* Register a new timeout, keeping the existing node reference */ 1206e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 1207ed2dbd31SArchie Cobbs } 1208ed2dbd31SArchie Cobbs 1209ed2dbd31SArchie Cobbs /* 1210ed2dbd31SArchie Cobbs * Return node's "name", even if it doesn't have one. 1211ed2dbd31SArchie Cobbs */ 1212ed2dbd31SArchie Cobbs static const char * 12136117aa58SLutz Donnerhacke ng_bridge_nodename(node_cp node) 1214ed2dbd31SArchie Cobbs { 121587e2c66aSHartmut Brandt static char name[NG_NODESIZ]; 1216ed2dbd31SArchie Cobbs 1217dd10c1e2SGleb Smirnoff if (NG_NODE_HAS_NAME(node)) 121830400f03SJulian Elischer snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 1219ed2dbd31SArchie Cobbs else 1220ed2dbd31SArchie Cobbs snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1221ed2dbd31SArchie Cobbs return name; 1222ed2dbd31SArchie Cobbs } 1223