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 */ 157*e0e3ded7SMark 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 399*e0e3ded7SMark Johnston link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 400*e0e3ded7SMark Johnston if (link == NULL) 401*e0e3ded7SMark Johnston return (ENOMEM); 402f961caf2SLutz Donnerhacke 403*e0e3ded7SMark Johnston #define NG_BRIDGE_COUNTER_ALLOC(f) do { \ 404*e0e3ded7SMark Johnston link->stats.f = counter_u64_alloc(M_NOWAIT); \ 405*e0e3ded7SMark Johnston if (link->stats.f == NULL) \ 406*e0e3ded7SMark Johnston goto nomem; \ 407*e0e3ded7SMark Johnston } while (0) 408*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvOctets); 409*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvPackets); 410*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvMulticasts); 411*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvBroadcasts); 412*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvUnknown); 413*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvRunts); 414*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(recvInvalid); 415*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitOctets); 416*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitPackets); 417*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitMulticasts); 418*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(xmitBroadcasts); 419*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(loopDrops); 420*e0e3ded7SMark Johnston NG_BRIDGE_COUNTER_ALLOC(memoryFailures); 421*e0e3ded7SMark 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); 437*e0e3ded7SMark Johnston 438*e0e3ded7SMark Johnston nomem: 439*e0e3ded7SMark Johnston ng_bridge_free_link(link); 440*e0e3ded7SMark Johnston return (ENOMEM); 441ed2dbd31SArchie Cobbs } 442ed2dbd31SArchie Cobbs 443ed2dbd31SArchie Cobbs /* 444ed2dbd31SArchie Cobbs * Receive a control message 445ed2dbd31SArchie Cobbs */ 446*e0e3ded7SMark Johnston static void 447*e0e3ded7SMark 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); 463*e0e3ded7SMark Johnston } 464*e0e3ded7SMark Johnston 465*e0e3ded7SMark Johnston static void 466*e0e3ded7SMark Johnston ng_bridge_free_link(link_p link) 467*e0e3ded7SMark Johnston { 468*e0e3ded7SMark Johnston counter_u64_free(link->stats.recvOctets); 469*e0e3ded7SMark Johnston counter_u64_free(link->stats.recvPackets); 470*e0e3ded7SMark Johnston counter_u64_free(link->stats.recvMulticasts); 471*e0e3ded7SMark Johnston counter_u64_free(link->stats.recvBroadcasts); 472*e0e3ded7SMark Johnston counter_u64_free(link->stats.recvUnknown); 473*e0e3ded7SMark Johnston counter_u64_free(link->stats.recvRunts); 474*e0e3ded7SMark Johnston counter_u64_free(link->stats.recvInvalid); 475*e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitOctets); 476*e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitPackets); 477*e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitMulticasts); 478*e0e3ded7SMark Johnston counter_u64_free(link->stats.xmitBroadcasts); 479*e0e3ded7SMark Johnston counter_u64_free(link->stats.loopDrops); 480*e0e3ded7SMark Johnston counter_u64_free(link->stats.memoryFailures); 481*e0e3ded7SMark Johnston free(link, M_NETGRAPH_BRIDGE); 482*e0e3ded7SMark 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) { 698a56e5ad6SLutz Donnerhacke /* The packet is still ours */ 699a56e5ad6SLutz Donnerhacke if (item != NULL) 700a56e5ad6SLutz Donnerhacke NG_FREE_ITEM(item); 701a56e5ad6SLutz Donnerhacke if (m != NULL) 702a56e5ad6SLutz Donnerhacke NG_FREE_M(m); 703a56e5ad6SLutz Donnerhacke return (error); 704a56e5ad6SLutz Donnerhacke } 705a56e5ad6SLutz Donnerhacke 7063c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitPackets, 1); 7073c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitOctets, len); 7083c958f5fSLutz Donnerhacke switch (manycast) { 7093c958f5fSLutz Donnerhacke default: /* unknown unicast */ 7103c958f5fSLutz Donnerhacke break; 7113c958f5fSLutz Donnerhacke case 1: /* multicast */ 7123c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitMulticasts, 1); 7133c958f5fSLutz Donnerhacke break; 7143c958f5fSLutz Donnerhacke case 2: /* broadcast */ 7153c958f5fSLutz Donnerhacke counter_u64_add(dst->stats.xmitBroadcasts, 1); 7163c958f5fSLutz Donnerhacke break; 7173c958f5fSLutz Donnerhacke } 718a56e5ad6SLutz Donnerhacke return (0); 7193c958f5fSLutz Donnerhacke } 7203c958f5fSLutz Donnerhacke 7213c958f5fSLutz Donnerhacke /* 7223c958f5fSLutz Donnerhacke * Loop body for sending to multiple destinations 7233c958f5fSLutz Donnerhacke * return 0 to stop looping 7243c958f5fSLutz Donnerhacke */ 725631cabbaSGleb Smirnoff static int 7260b951c55SGleb Smirnoff ng_bridge_send_ctx(hook_p dst, void *arg) 727631cabbaSGleb Smirnoff { 7280b951c55SGleb Smirnoff struct ng_bridge_send_ctx *ctx = arg; 729631cabbaSGleb Smirnoff link_p destLink = NG_HOOK_PRIVATE(dst); 730631cabbaSGleb Smirnoff struct mbuf *m2 = NULL; 731631cabbaSGleb Smirnoff int error = 0; 732631cabbaSGleb Smirnoff 733631cabbaSGleb Smirnoff /* Skip incoming link */ 734631cabbaSGleb Smirnoff if (destLink == ctx->incoming) { 735631cabbaSGleb Smirnoff return (1); 736631cabbaSGleb Smirnoff } 737631cabbaSGleb Smirnoff 738f961caf2SLutz Donnerhacke /* Skip sending unknowns to undesired links */ 739f961caf2SLutz Donnerhacke if (!ctx->manycast && !destLink->sendUnknown) 740f961caf2SLutz Donnerhacke return (1); 741f961caf2SLutz Donnerhacke 742631cabbaSGleb Smirnoff if (ctx->foundFirst == NULL) { 743631cabbaSGleb Smirnoff /* 744631cabbaSGleb Smirnoff * This is the first usable link we have found. 745631cabbaSGleb Smirnoff * Reserve it for the originals. 746631cabbaSGleb Smirnoff * If we never find another we save a copy. 747631cabbaSGleb Smirnoff */ 748631cabbaSGleb Smirnoff ctx->foundFirst = destLink; 749631cabbaSGleb Smirnoff return (1); 750631cabbaSGleb Smirnoff } 751631cabbaSGleb Smirnoff 752631cabbaSGleb Smirnoff /* 753631cabbaSGleb Smirnoff * It's usable link but not the reserved (first) one. 754631cabbaSGleb Smirnoff * Copy mbuf info for sending. 755631cabbaSGleb Smirnoff */ 756a56e5ad6SLutz Donnerhacke m2 = m_dup(ctx->m, M_NOWAIT); 757631cabbaSGleb Smirnoff if (m2 == NULL) { 75866c72859SLutz Donnerhacke counter_u64_add(ctx->incoming->stats.memoryFailures, 1); 759631cabbaSGleb Smirnoff ctx->error = ENOBUFS; 760a56e5ad6SLutz Donnerhacke return (0); /* abort loop, do not try again and again */ 761631cabbaSGleb Smirnoff } 762631cabbaSGleb Smirnoff 763631cabbaSGleb Smirnoff /* Send packet */ 7643c958f5fSLutz Donnerhacke error = ng_bridge_send_data(destLink, ctx->manycast, m2, NULL); 765631cabbaSGleb Smirnoff if (error) 766631cabbaSGleb Smirnoff ctx->error = error; 767631cabbaSGleb Smirnoff return (1); 768631cabbaSGleb Smirnoff } 769631cabbaSGleb Smirnoff 770ed2dbd31SArchie Cobbs static int 771069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item) 772ed2dbd31SArchie Cobbs { 77330400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 77430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 775ed2dbd31SArchie Cobbs struct ng_bridge_host *host; 776ed2dbd31SArchie Cobbs struct ether_header *eh; 777631cabbaSGleb Smirnoff struct ng_bridge_send_ctx ctx = { 0 }; 778631cabbaSGleb Smirnoff hook_p ret; 779ed2dbd31SArchie Cobbs 780631cabbaSGleb Smirnoff NGI_GET_M(item, ctx.m); 781ed2dbd31SArchie Cobbs 782631cabbaSGleb Smirnoff ctx.incoming = NG_HOOK_PRIVATE(hook); 783ed2dbd31SArchie Cobbs /* Sanity check packet and pull up header */ 784631cabbaSGleb Smirnoff if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) { 78566c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvRunts, 1); 786069154d5SJulian Elischer NG_FREE_ITEM(item); 787631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 788ed2dbd31SArchie Cobbs return (EINVAL); 789ed2dbd31SArchie Cobbs } 790631cabbaSGleb Smirnoff if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) { 79166c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 792069154d5SJulian Elischer NG_FREE_ITEM(item); 793ed2dbd31SArchie Cobbs return (ENOBUFS); 794ed2dbd31SArchie Cobbs } 795631cabbaSGleb Smirnoff eh = mtod(ctx.m, struct ether_header *); 796ed2dbd31SArchie Cobbs if ((eh->ether_shost[0] & 1) != 0) { 79766c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvInvalid, 1); 798069154d5SJulian Elischer NG_FREE_ITEM(item); 799631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 800ed2dbd31SArchie Cobbs return (EINVAL); 801ed2dbd31SArchie Cobbs } 802ed2dbd31SArchie Cobbs 803ed2dbd31SArchie Cobbs /* Is link disabled due to a loopback condition? */ 804631cabbaSGleb Smirnoff if (ctx.incoming->loopCount != 0) { 80566c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.loopDrops, 1); 806069154d5SJulian Elischer NG_FREE_ITEM(item); 807631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 808f6e0c471SLutz Donnerhacke return (ELOOP); 809ed2dbd31SArchie Cobbs } 810ed2dbd31SArchie Cobbs 811ed2dbd31SArchie Cobbs /* Update stats */ 81266c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvPackets, 1); 81366c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len); 814631cabbaSGleb Smirnoff if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) { 815ed2dbd31SArchie Cobbs if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 81666c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1); 817631cabbaSGleb Smirnoff ctx.manycast = 2; 818ed2dbd31SArchie Cobbs } else 81966c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvMulticasts, 1); 820ed2dbd31SArchie Cobbs } 821ed2dbd31SArchie Cobbs 822ed2dbd31SArchie Cobbs /* Look up packet's source Ethernet address in hashtable */ 823f6e0c471SLutz Donnerhacke if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) 824011b7317SLutz Donnerhacke /* Update time since last heard from this host. 825011b7317SLutz Donnerhacke * This is safe without locking, because it's 826011b7317SLutz Donnerhacke * the only operation during shared access. 827011b7317SLutz Donnerhacke */ 8284dfe70fdSLutz Donnerhacke if (__predict_false(host->staleness > 0)) 829ed2dbd31SArchie Cobbs host->staleness = 0; 830ed2dbd31SArchie Cobbs 831f6e0c471SLutz Donnerhacke if ((host == NULL && ctx.incoming->learnMac) || 832f6e0c471SLutz Donnerhacke (host != NULL && host->link != ctx.incoming)) { 833b1bd4473SLutz Donnerhacke struct ng_mesg *msg; 834b1bd4473SLutz Donnerhacke struct ng_bridge_move_host *mh; 835b1bd4473SLutz Donnerhacke int error = 0; 836b1bd4473SLutz Donnerhacke 837b1bd4473SLutz Donnerhacke NG_MKMESSAGE(msg, NGM_BRIDGE_COOKIE, NGM_BRIDGE_MOVE_HOST, 838b1bd4473SLutz Donnerhacke sizeof(*mh), M_NOWAIT); 839b1bd4473SLutz Donnerhacke if (msg == NULL) { 84066c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 841069154d5SJulian Elischer NG_FREE_ITEM(item); 842631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 843ed2dbd31SArchie Cobbs return (ENOMEM); 844ed2dbd31SArchie Cobbs } 845b1bd4473SLutz Donnerhacke mh = (struct ng_bridge_move_host *)msg->data; 846b1bd4473SLutz Donnerhacke strncpy(mh->hook, NG_HOOK_NAME(ctx.incoming->hook), 847b1bd4473SLutz Donnerhacke sizeof(mh->hook)); 848b1bd4473SLutz Donnerhacke memcpy(mh->addr, eh->ether_shost, sizeof(mh->addr)); 849b1bd4473SLutz Donnerhacke NG_SEND_MSG_ID(error, node, msg, NG_NODE_ID(node), 850b1bd4473SLutz Donnerhacke NG_NODE_ID(node)); 851b1bd4473SLutz Donnerhacke if (error) 852b1bd4473SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 853ed2dbd31SArchie Cobbs } 854ed2dbd31SArchie Cobbs 855f6e0c471SLutz Donnerhacke if (host != NULL && host->link != ctx.incoming) { 856f6e0c471SLutz Donnerhacke if (host->age < priv->conf.minStableAge) { 857f6e0c471SLutz Donnerhacke /* Drop packet on instable links */ 858f6e0c471SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.loopDrops, 1); 859f6e0c471SLutz Donnerhacke NG_FREE_ITEM(item); 860f6e0c471SLutz Donnerhacke NG_FREE_M(ctx.m); 861f6e0c471SLutz Donnerhacke return (ELOOP); 862f6e0c471SLutz Donnerhacke } 863f6e0c471SLutz Donnerhacke } 864f6e0c471SLutz Donnerhacke 865ed2dbd31SArchie Cobbs /* Run packet through ipfw processing, if enabled */ 8669b932e9eSAndre Oppermann #if 0 8670b4b0b0fSJulian Elischer if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) { 868ed2dbd31SArchie Cobbs /* XXX not implemented yet */ 869ed2dbd31SArchie Cobbs } 8709b932e9eSAndre Oppermann #endif 871ed2dbd31SArchie Cobbs 872ed2dbd31SArchie Cobbs /* 873ed2dbd31SArchie Cobbs * If unicast and destination host known, deliver to host's link, 874ed2dbd31SArchie Cobbs * unless it is the same link as the packet came in on. 875ed2dbd31SArchie Cobbs */ 876631cabbaSGleb Smirnoff if (!ctx.manycast) { 877ed2dbd31SArchie Cobbs /* Determine packet destination link */ 878ed2dbd31SArchie Cobbs if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 879631cabbaSGleb Smirnoff link_p destLink = host->link; 880ed2dbd31SArchie Cobbs 881ed2dbd31SArchie Cobbs /* If destination same as incoming link, do nothing */ 882631cabbaSGleb Smirnoff if (destLink == ctx.incoming) { 883069154d5SJulian Elischer NG_FREE_ITEM(item); 884631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 885ed2dbd31SArchie Cobbs return (0); 886ed2dbd31SArchie Cobbs } 887ed2dbd31SArchie Cobbs 888ed2dbd31SArchie Cobbs /* Deliver packet out the destination link */ 8893c958f5fSLutz Donnerhacke return (ng_bridge_send_data(destLink, ctx.manycast, ctx.m, item)); 890ed2dbd31SArchie Cobbs } 891ed2dbd31SArchie Cobbs 892ed2dbd31SArchie Cobbs /* Destination host is not known */ 89366c72859SLutz Donnerhacke counter_u64_add(ctx.incoming->stats.recvUnknown, 1); 894ed2dbd31SArchie Cobbs } 895ed2dbd31SArchie Cobbs 896ed2dbd31SArchie Cobbs /* Distribute unknown, multicast, broadcast pkts to all other links */ 897631cabbaSGleb Smirnoff NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret); 898ed2dbd31SArchie Cobbs 899a56e5ad6SLutz Donnerhacke /* Finally send out on the first link found */ 900a56e5ad6SLutz Donnerhacke if (ctx.foundFirst != NULL) { 901a56e5ad6SLutz Donnerhacke int error = ng_bridge_send_data(ctx.foundFirst, ctx.manycast, ctx.m, item); 902a56e5ad6SLutz Donnerhacke if (error) 903a56e5ad6SLutz Donnerhacke ctx.error = error; 904a56e5ad6SLutz Donnerhacke } else { /* nothing to send at all */ 905069154d5SJulian Elischer NG_FREE_ITEM(item); 906631cabbaSGleb Smirnoff NG_FREE_M(ctx.m); 907069154d5SJulian Elischer } 908069154d5SJulian Elischer 909a56e5ad6SLutz Donnerhacke return (ctx.error); 910ed2dbd31SArchie Cobbs } 911ed2dbd31SArchie Cobbs 912ed2dbd31SArchie Cobbs /* 913ed2dbd31SArchie Cobbs * Shutdown node 914ed2dbd31SArchie Cobbs */ 915ed2dbd31SArchie Cobbs static int 916069154d5SJulian Elischer ng_bridge_shutdown(node_p node) 917ed2dbd31SArchie Cobbs { 91830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 919ed2dbd31SArchie Cobbs 9206c12c2b1SArchie Cobbs /* 921195cf617SRuslan Ermilov * Shut down everything including the timer. Even if the 922195cf617SRuslan Ermilov * callout has already been dequeued and is about to be 923195cf617SRuslan Ermilov * run, ng_bridge_timeout() won't be fired as the node 924195cf617SRuslan Ermilov * is already marked NGF_INVALID, so we're safe to free 925195cf617SRuslan Ermilov * the node now. 9266c12c2b1SArchie Cobbs */ 927ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 928ed2dbd31SArchie Cobbs ("%s: numLinks=%d numHosts=%d", 9296e551fb6SDavid E. O'Brien __func__, priv->numLinks, priv->numHosts)); 930195cf617SRuslan Ermilov ng_uncallout(&priv->timer, node); 931195cf617SRuslan Ermilov NG_NODE_SET_PRIVATE(node, NULL); 932195cf617SRuslan Ermilov NG_NODE_UNREF(node); 9331ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE); 9341ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH_BRIDGE); 935ed2dbd31SArchie Cobbs return (0); 936ed2dbd31SArchie Cobbs } 937ed2dbd31SArchie Cobbs 938ed2dbd31SArchie Cobbs /* 939ed2dbd31SArchie Cobbs * Hook disconnection. 940ed2dbd31SArchie Cobbs */ 941ed2dbd31SArchie Cobbs static int 942ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook) 943ed2dbd31SArchie Cobbs { 94430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 945631cabbaSGleb Smirnoff link_p link = NG_HOOK_PRIVATE(hook); 946ed2dbd31SArchie Cobbs 947ed2dbd31SArchie Cobbs /* Remove all hosts associated with this link */ 948631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv, link); 949ed2dbd31SArchie Cobbs 950ed2dbd31SArchie Cobbs /* Free associated link information */ 951*e0e3ded7SMark Johnston ng_bridge_free_link(link); 952ed2dbd31SArchie Cobbs priv->numLinks--; 953ed2dbd31SArchie Cobbs 954ed2dbd31SArchie Cobbs /* If no more hooks, go away */ 95530400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 956f8aab721SMarko Zec && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 957f8aab721SMarko Zec && !priv->persistent) { 95830400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); 95930400f03SJulian Elischer } 960ed2dbd31SArchie Cobbs return (0); 961ed2dbd31SArchie Cobbs } 962ed2dbd31SArchie Cobbs 963ed2dbd31SArchie Cobbs /****************************************************************** 964ed2dbd31SArchie Cobbs HASH TABLE FUNCTIONS 965ed2dbd31SArchie Cobbs ******************************************************************/ 966ed2dbd31SArchie Cobbs 967ed2dbd31SArchie Cobbs /* 968ed2dbd31SArchie Cobbs * Hash algorithm 969ed2dbd31SArchie Cobbs */ 970ed2dbd31SArchie Cobbs #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 971ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[1] \ 972ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 973ed2dbd31SArchie Cobbs 974ed2dbd31SArchie Cobbs /* 975ed2dbd31SArchie Cobbs * Find a host entry in the table. 976ed2dbd31SArchie Cobbs */ 977ed2dbd31SArchie Cobbs static struct ng_bridge_host * 9786117aa58SLutz Donnerhacke ng_bridge_get(priv_cp priv, const u_char *addr) 979ed2dbd31SArchie Cobbs { 980ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask); 981ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *host; 982ed2dbd31SArchie Cobbs 983ccf4cd2eSLutz Donnerhacke SLIST_FOREACH(host, &priv->tab[bucket], next) { 984ccf4cd2eSLutz Donnerhacke if (ETHER_EQUAL(host->addr, addr)) 985ccf4cd2eSLutz Donnerhacke return (host); 986ed2dbd31SArchie Cobbs } 987ed2dbd31SArchie Cobbs return (NULL); 988ed2dbd31SArchie Cobbs } 989ed2dbd31SArchie Cobbs 990ed2dbd31SArchie Cobbs /* 991f6e0c471SLutz Donnerhacke * Add a host entry to the table. If it already exists, move it 992f6e0c471SLutz Donnerhacke * to the new link. Returns 0 on success. 993ed2dbd31SArchie Cobbs */ 994ed2dbd31SArchie Cobbs static int 995631cabbaSGleb Smirnoff ng_bridge_put(priv_p priv, const u_char *addr, link_p link) 996ed2dbd31SArchie Cobbs { 997ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask); 998ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *host; 999ed2dbd31SArchie Cobbs 1000f6e0c471SLutz Donnerhacke if ((host = ng_bridge_get(priv, addr)) != NULL) { 1001f6e0c471SLutz Donnerhacke /* Host already on the correct link? */ 1002f6e0c471SLutz Donnerhacke if (host->link == link) 1003f6e0c471SLutz Donnerhacke return 0; 1004f6e0c471SLutz Donnerhacke 1005f6e0c471SLutz Donnerhacke /* Move old host over to new link */ 1006f6e0c471SLutz Donnerhacke if (host->age >= priv->conf.minStableAge) { 1007f6e0c471SLutz Donnerhacke host->link = link; 1008f6e0c471SLutz Donnerhacke host->age = 0; 1009f6e0c471SLutz Donnerhacke return (0); 1010f6e0c471SLutz Donnerhacke } 1011f6e0c471SLutz Donnerhacke /* 1012f6e0c471SLutz Donnerhacke * If the host was recently moved to the old link and 1013f6e0c471SLutz Donnerhacke * it's now jumping to a new link, declare a loopback 1014f6e0c471SLutz Donnerhacke * condition. 1015f6e0c471SLutz Donnerhacke */ 1016f6e0c471SLutz Donnerhacke if (priv->conf.debugLevel >= 2) 1017f6e0c471SLutz Donnerhacke log(LOG_WARNING, "ng_bridge: %s:" 1018f6e0c471SLutz Donnerhacke " loopback detected on %s\n", 1019f6e0c471SLutz Donnerhacke ng_bridge_nodename(priv->node), 1020f6e0c471SLutz Donnerhacke NG_HOOK_NAME(link->hook)); 1021f6e0c471SLutz Donnerhacke 1022f6e0c471SLutz Donnerhacke /* Mark link as linka non grata */ 1023f6e0c471SLutz Donnerhacke link->loopCount = priv->conf.loopTimeout; 1024f6e0c471SLutz Donnerhacke link->stats.loopDetects++; 1025f6e0c471SLutz Donnerhacke 1026f6e0c471SLutz Donnerhacke /* Forget all hosts on this link */ 1027f6e0c471SLutz Donnerhacke ng_bridge_remove_hosts(priv, link); 1028f6e0c471SLutz Donnerhacke return (ELOOP); 1029f6e0c471SLutz Donnerhacke } 1030ed2dbd31SArchie Cobbs 1031ed2dbd31SArchie Cobbs /* Allocate and initialize new hashtable entry */ 1032ccf4cd2eSLutz Donnerhacke host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT); 1033ccf4cd2eSLutz Donnerhacke if (host == NULL) 1034b1bd4473SLutz Donnerhacke return (ENOMEM); 1035ccf4cd2eSLutz Donnerhacke bcopy(addr, host->addr, ETHER_ADDR_LEN); 1036ccf4cd2eSLutz Donnerhacke host->link = link; 1037ccf4cd2eSLutz Donnerhacke host->staleness = 0; 1038ccf4cd2eSLutz Donnerhacke host->age = 0; 1039ed2dbd31SArchie Cobbs 1040ed2dbd31SArchie Cobbs /* Add new element to hash bucket */ 1041ccf4cd2eSLutz Donnerhacke SLIST_INSERT_HEAD(&priv->tab[bucket], host, next); 1042ed2dbd31SArchie Cobbs priv->numHosts++; 1043ed2dbd31SArchie Cobbs 1044ed2dbd31SArchie Cobbs /* Resize table if necessary */ 1045ed2dbd31SArchie Cobbs ng_bridge_rehash(priv); 1046b1bd4473SLutz Donnerhacke return (0); 1047ed2dbd31SArchie Cobbs } 1048ed2dbd31SArchie Cobbs 1049ed2dbd31SArchie Cobbs /* 1050ed2dbd31SArchie Cobbs * Resize the hash table. We try to maintain the number of buckets 1051ed2dbd31SArchie Cobbs * such that the load factor is in the range 0.25 to 1.0. 1052ed2dbd31SArchie Cobbs * 1053ed2dbd31SArchie Cobbs * If we can't get the new memory then we silently fail. This is OK 1054ed2dbd31SArchie Cobbs * because things will still work and we'll try again soon anyway. 1055ed2dbd31SArchie Cobbs */ 1056ed2dbd31SArchie Cobbs static void 1057ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv) 1058ed2dbd31SArchie Cobbs { 1059ed2dbd31SArchie Cobbs struct ng_bridge_bucket *newTab; 1060ed2dbd31SArchie Cobbs int oldBucket, newBucket; 1061ed2dbd31SArchie Cobbs int newNumBuckets; 1062ed2dbd31SArchie Cobbs u_int newMask; 1063ed2dbd31SArchie Cobbs 1064ed2dbd31SArchie Cobbs /* Is table too full or too empty? */ 1065ed2dbd31SArchie Cobbs if (priv->numHosts > priv->numBuckets 1066ed2dbd31SArchie Cobbs && (priv->numBuckets << 1) <= MAX_BUCKETS) 1067ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets << 1; 1068ed2dbd31SArchie Cobbs else if (priv->numHosts < (priv->numBuckets >> 2) 1069ed2dbd31SArchie Cobbs && (priv->numBuckets >> 2) >= MIN_BUCKETS) 1070ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets >> 2; 1071ed2dbd31SArchie Cobbs else 1072ed2dbd31SArchie Cobbs return; 1073ed2dbd31SArchie Cobbs newMask = newNumBuckets - 1; 1074ed2dbd31SArchie Cobbs 1075ed2dbd31SArchie Cobbs /* Allocate and initialize new table */ 1076ac2fffa4SPedro F. Giffuni newTab = malloc(newNumBuckets * sizeof(*newTab), 1077e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 1078ed2dbd31SArchie Cobbs if (newTab == NULL) 1079ed2dbd31SArchie Cobbs return; 1080ed2dbd31SArchie Cobbs 1081ed2dbd31SArchie Cobbs /* Move all entries from old table to new table */ 1082ed2dbd31SArchie Cobbs for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 1083ed2dbd31SArchie Cobbs struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 1084ed2dbd31SArchie Cobbs 1085ed2dbd31SArchie Cobbs while (!SLIST_EMPTY(oldList)) { 1086ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host 1087ed2dbd31SArchie Cobbs = SLIST_FIRST(oldList); 1088ed2dbd31SArchie Cobbs 1089ed2dbd31SArchie Cobbs SLIST_REMOVE_HEAD(oldList, next); 1090ccf4cd2eSLutz Donnerhacke newBucket = HASH(host->addr, newMask); 1091ccf4cd2eSLutz Donnerhacke SLIST_INSERT_HEAD(&newTab[newBucket], host, next); 1092ed2dbd31SArchie Cobbs } 1093ed2dbd31SArchie Cobbs } 1094ed2dbd31SArchie Cobbs 1095ed2dbd31SArchie Cobbs /* Replace old table with new one */ 1096ed2dbd31SArchie Cobbs if (priv->conf.debugLevel >= 3) { 1097ed2dbd31SArchie Cobbs log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 1098ed2dbd31SArchie Cobbs ng_bridge_nodename(priv->node), 1099ed2dbd31SArchie Cobbs priv->numBuckets, newNumBuckets); 1100ed2dbd31SArchie Cobbs } 11011ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE); 1102ed2dbd31SArchie Cobbs priv->numBuckets = newNumBuckets; 1103ed2dbd31SArchie Cobbs priv->hashMask = newMask; 1104ed2dbd31SArchie Cobbs priv->tab = newTab; 1105ed2dbd31SArchie Cobbs return; 1106ed2dbd31SArchie Cobbs } 1107ed2dbd31SArchie Cobbs 1108ed2dbd31SArchie Cobbs /****************************************************************** 1109ed2dbd31SArchie Cobbs MISC FUNCTIONS 1110ed2dbd31SArchie Cobbs ******************************************************************/ 1111ed2dbd31SArchie Cobbs 1112ed2dbd31SArchie Cobbs /* 1113ed2dbd31SArchie Cobbs * Remove all hosts associated with a specific link from the hashtable. 1114ed2dbd31SArchie Cobbs * If linkNum == -1, then remove all hosts in the table. 1115ed2dbd31SArchie Cobbs */ 1116ed2dbd31SArchie Cobbs static void 1117631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv_p priv, link_p link) 1118ed2dbd31SArchie Cobbs { 1119ed2dbd31SArchie Cobbs int bucket; 1120ed2dbd31SArchie Cobbs 1121ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1122ccf4cd2eSLutz Donnerhacke struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1123ed2dbd31SArchie Cobbs 1124ed2dbd31SArchie Cobbs while (*hptr != NULL) { 1125ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host = *hptr; 1126ed2dbd31SArchie Cobbs 1127ccf4cd2eSLutz Donnerhacke if (link == NULL || host->link == link) { 1128ccf4cd2eSLutz Donnerhacke *hptr = SLIST_NEXT(host, next); 1129ccf4cd2eSLutz Donnerhacke free(host, M_NETGRAPH_BRIDGE); 1130ed2dbd31SArchie Cobbs priv->numHosts--; 1131ed2dbd31SArchie Cobbs } else 1132ccf4cd2eSLutz Donnerhacke hptr = &SLIST_NEXT(host, next); 1133ed2dbd31SArchie Cobbs } 1134ed2dbd31SArchie Cobbs } 1135ed2dbd31SArchie Cobbs } 1136ed2dbd31SArchie Cobbs 1137ed2dbd31SArchie Cobbs /* 1138ed2dbd31SArchie Cobbs * Handle our once-per-second timeout event. We do two things: 1139ed2dbd31SArchie Cobbs * we decrement link->loopCount for those links being muted due to 1140ed2dbd31SArchie Cobbs * a detected loopback condition, and we remove any hosts from 1141ed2dbd31SArchie Cobbs * the hashtable whom we haven't heard from in a long while. 1142ed2dbd31SArchie Cobbs */ 1143631cabbaSGleb Smirnoff static int 11440b951c55SGleb Smirnoff ng_bridge_unmute(hook_p hook, void *arg) 1145631cabbaSGleb Smirnoff { 1146631cabbaSGleb Smirnoff link_p link = NG_HOOK_PRIVATE(hook); 1147631cabbaSGleb Smirnoff node_p node = NG_HOOK_NODE(hook); 1148631cabbaSGleb Smirnoff priv_p priv = NG_NODE_PRIVATE(node); 11490b951c55SGleb Smirnoff int *counter = arg; 1150631cabbaSGleb Smirnoff 1151631cabbaSGleb Smirnoff if (link->loopCount != 0) { 1152631cabbaSGleb Smirnoff link->loopCount--; 1153631cabbaSGleb Smirnoff if (link->loopCount == 0 && priv->conf.debugLevel >= 2) { 1154631cabbaSGleb Smirnoff log(LOG_INFO, "ng_bridge: %s:" 1155631cabbaSGleb Smirnoff " restoring looped back %s\n", 1156631cabbaSGleb Smirnoff ng_bridge_nodename(node), NG_HOOK_NAME(hook)); 1157631cabbaSGleb Smirnoff } 1158631cabbaSGleb Smirnoff } 1159abc4b11cSGleb Smirnoff (*counter)++; 1160631cabbaSGleb Smirnoff return (1); 1161631cabbaSGleb Smirnoff } 1162631cabbaSGleb Smirnoff 1163ed2dbd31SArchie Cobbs static void 1164e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) 1165ed2dbd31SArchie Cobbs { 116630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 1167e0d32af7SGleb Smirnoff int bucket; 1168ed2dbd31SArchie Cobbs int counter = 0; 1169631cabbaSGleb Smirnoff hook_p ret; 1170ed2dbd31SArchie Cobbs 1171ed2dbd31SArchie Cobbs /* Update host time counters and remove stale entries */ 1172ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1173ccf4cd2eSLutz Donnerhacke struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1174ed2dbd31SArchie Cobbs 1175ed2dbd31SArchie Cobbs while (*hptr != NULL) { 1176ccf4cd2eSLutz Donnerhacke struct ng_bridge_host *const host = *hptr; 1177ed2dbd31SArchie Cobbs 1178ed2dbd31SArchie Cobbs /* Remove hosts we haven't heard from in a while */ 1179ccf4cd2eSLutz Donnerhacke if (++host->staleness >= priv->conf.maxStaleness) { 1180ccf4cd2eSLutz Donnerhacke *hptr = SLIST_NEXT(host, next); 1181ccf4cd2eSLutz Donnerhacke free(host, M_NETGRAPH_BRIDGE); 1182ed2dbd31SArchie Cobbs priv->numHosts--; 1183ed2dbd31SArchie Cobbs } else { 1184ccf4cd2eSLutz Donnerhacke if (host->age < 0xffff) 1185ccf4cd2eSLutz Donnerhacke host->age++; 1186ccf4cd2eSLutz Donnerhacke hptr = &SLIST_NEXT(host, next); 1187ed2dbd31SArchie Cobbs counter++; 1188ed2dbd31SArchie Cobbs } 1189ed2dbd31SArchie Cobbs } 1190ed2dbd31SArchie Cobbs } 1191ed2dbd31SArchie Cobbs KASSERT(priv->numHosts == counter, 11926e551fb6SDavid E. O'Brien ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); 1193ed2dbd31SArchie Cobbs 1194ed2dbd31SArchie Cobbs /* Decrease table size if necessary */ 1195ed2dbd31SArchie Cobbs ng_bridge_rehash(priv); 1196ed2dbd31SArchie Cobbs 1197ed2dbd31SArchie Cobbs /* Decrease loop counter on muted looped back links */ 1198631cabbaSGleb Smirnoff counter = 0; 1199631cabbaSGleb Smirnoff NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret); 1200ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == counter, 12016e551fb6SDavid E. O'Brien ("%s: links: %d != %d", __func__, priv->numLinks, counter)); 1202ed2dbd31SArchie Cobbs 1203e0d32af7SGleb Smirnoff /* Register a new timeout, keeping the existing node reference */ 1204e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 1205ed2dbd31SArchie Cobbs } 1206ed2dbd31SArchie Cobbs 1207ed2dbd31SArchie Cobbs /* 1208ed2dbd31SArchie Cobbs * Return node's "name", even if it doesn't have one. 1209ed2dbd31SArchie Cobbs */ 1210ed2dbd31SArchie Cobbs static const char * 12116117aa58SLutz Donnerhacke ng_bridge_nodename(node_cp node) 1212ed2dbd31SArchie Cobbs { 121387e2c66aSHartmut Brandt static char name[NG_NODESIZ]; 1214ed2dbd31SArchie Cobbs 1215dd10c1e2SGleb Smirnoff if (NG_NODE_HAS_NAME(node)) 121630400f03SJulian Elischer snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 1217ed2dbd31SArchie Cobbs else 1218ed2dbd31SArchie Cobbs snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1219ed2dbd31SArchie Cobbs return name; 1220ed2dbd31SArchie Cobbs } 1221