Changeset View
Changeset View
Standalone View
Standalone View
head/sys/netgraph/ng_bridge.c
/* | |||||
* ng_bridge.c | |||||
*/ | |||||
/*- | /*- | ||||
* Copyright (c) 2000 Whistle Communications, Inc. | * Copyright (c) 2000 Whistle Communications, Inc. | ||||
* All rights reserved. | * All rights reserved. | ||||
* | * | ||||
* Subject to the following obligations and disclaimer of warranty, use and | * Subject to the following obligations and disclaimer of warranty, use and | ||||
* redistribution of this software, in source or object code forms, with or | * redistribution of this software, in source or object code forms, with or | ||||
* without modifications are expressly permitted by Whistle Communications; | * without modifications are expressly permitted by Whistle Communications; | ||||
* provided, however, that: | * provided, however, that: | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | struct ng_bridge_link { | ||||
hook_p hook; /* netgraph hook */ | hook_p hook; /* netgraph hook */ | ||||
u_int16_t loopCount; /* loop ignore timer */ | u_int16_t loopCount; /* loop ignore timer */ | ||||
struct ng_bridge_link_stats stats; /* link stats */ | struct ng_bridge_link_stats stats; /* link stats */ | ||||
}; | }; | ||||
/* Per-node private data */ | /* Per-node private data */ | ||||
struct ng_bridge_private { | struct ng_bridge_private { | ||||
struct ng_bridge_bucket *tab; /* hash table bucket array */ | struct ng_bridge_bucket *tab; /* hash table bucket array */ | ||||
struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS]; | |||||
struct ng_bridge_config conf; /* node configuration */ | struct ng_bridge_config conf; /* node configuration */ | ||||
node_p node; /* netgraph node */ | node_p node; /* netgraph node */ | ||||
u_int numHosts; /* num entries in table */ | u_int numHosts; /* num entries in table */ | ||||
u_int numBuckets; /* num buckets in table */ | u_int numBuckets; /* num buckets in table */ | ||||
u_int hashMask; /* numBuckets - 1 */ | u_int hashMask; /* numBuckets - 1 */ | ||||
int numLinks; /* num connected links */ | int numLinks; /* num connected links */ | ||||
int persistent; /* can exist w/o hooks */ | int persistent; /* can exist w/o hooks */ | ||||
struct callout timer; /* one second periodic timer */ | struct callout timer; /* one second periodic timer */ | ||||
Show All 14 Lines | |||||
static ng_rcvmsg_t ng_bridge_rcvmsg; | static ng_rcvmsg_t ng_bridge_rcvmsg; | ||||
static ng_shutdown_t ng_bridge_shutdown; | static ng_shutdown_t ng_bridge_shutdown; | ||||
static ng_newhook_t ng_bridge_newhook; | static ng_newhook_t ng_bridge_newhook; | ||||
static ng_rcvdata_t ng_bridge_rcvdata; | static ng_rcvdata_t ng_bridge_rcvdata; | ||||
static ng_disconnect_t ng_bridge_disconnect; | static ng_disconnect_t ng_bridge_disconnect; | ||||
/* Other internal functions */ | /* Other internal functions */ | ||||
static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); | static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); | ||||
static int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum); | static int ng_bridge_put(priv_p priv, const u_char *addr, link_p link); | ||||
static void ng_bridge_rehash(priv_p priv); | static void ng_bridge_rehash(priv_p priv); | ||||
static void ng_bridge_remove_hosts(priv_p priv, int linkNum); | static void ng_bridge_remove_hosts(priv_p priv, link_p link); | ||||
static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2); | static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2); | ||||
static const char *ng_bridge_nodename(node_p node); | static const char *ng_bridge_nodename(node_p node); | ||||
/* Ethernet broadcast */ | /* Ethernet broadcast */ | ||||
static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = | static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = | ||||
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; | ||||
/* Store each hook's link number in the private field */ | |||||
#define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private)) | |||||
/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ | /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ | ||||
#define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ | #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ | ||||
== ((const u_int32_t *)(b))[0] \ | == ((const u_int32_t *)(b))[0] \ | ||||
&& ((const u_int16_t *)(a))[2] \ | && ((const u_int16_t *)(a))[2] \ | ||||
== ((const u_int16_t *)(b))[2]) | == ((const u_int16_t *)(b))[2]) | ||||
/* Minimum and maximum number of hash buckets. Must be a power of two. */ | /* Minimum and maximum number of hash buckets. Must be a power of two. */ | ||||
#define MIN_BUCKETS (1 << 5) /* 32 */ | #define MIN_BUCKETS (1 << 5) /* 32 */ | ||||
Show All 39 Lines | |||||
static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[] | static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[] | ||||
= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); | = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); | ||||
static const struct ng_parse_type ng_bridge_host_ary_type = { | static const struct ng_parse_type ng_bridge_host_ary_type = { | ||||
&ng_parse_struct_type, | &ng_parse_struct_type, | ||||
&ng_bridge_host_ary_type_fields | &ng_bridge_host_ary_type_fields | ||||
}; | }; | ||||
/* Parse type for struct ng_bridge_config */ | /* Parse type for struct ng_bridge_config */ | ||||
static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = { | |||||
&ng_parse_uint8_type, | |||||
NG_BRIDGE_MAX_LINKS | |||||
}; | |||||
static const struct ng_parse_type ng_bridge_ipfwary_type = { | |||||
&ng_parse_fixedarray_type, | |||||
&ng_bridge_ipfwary_type_info | |||||
}; | |||||
static const struct ng_parse_struct_field ng_bridge_config_type_fields[] | static const struct ng_parse_struct_field ng_bridge_config_type_fields[] | ||||
= NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type); | = NG_BRIDGE_CONFIG_TYPE_INFO; | ||||
static const struct ng_parse_type ng_bridge_config_type = { | static const struct ng_parse_type ng_bridge_config_type = { | ||||
&ng_parse_struct_type, | &ng_parse_struct_type, | ||||
&ng_bridge_config_type_fields | &ng_bridge_config_type_fields | ||||
}; | }; | ||||
/* Parse type for struct ng_bridge_link_stat */ | /* Parse type for struct ng_bridge_link_stat */ | ||||
static const struct ng_parse_struct_field ng_bridge_stats_type_fields[] | static const struct ng_parse_struct_field ng_bridge_stats_type_fields[] | ||||
= NG_BRIDGE_STATS_TYPE_INFO; | = NG_BRIDGE_STATS_TYPE_INFO; | ||||
▲ Show 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | |||||
* Method for attaching a new hook | * Method for attaching a new hook | ||||
*/ | */ | ||||
static int | static int | ||||
ng_bridge_newhook(node_p node, hook_p hook, const char *name) | ng_bridge_newhook(node_p node, hook_p hook, const char *name) | ||||
{ | { | ||||
const priv_p priv = NG_NODE_PRIVATE(node); | const priv_p priv = NG_NODE_PRIVATE(node); | ||||
/* Check for a link hook */ | /* Check for a link hook */ | ||||
if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX, | if (strlen(name) > strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) { | ||||
strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) { | char linkName[NG_HOOKSIZ]; | ||||
const char *cp; | u_int32_t linkNum; | ||||
char *eptr; | link_p link; | ||||
u_long linkNum; | |||||
cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX); | /* primitive parsing */ | ||||
if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) | linkNum = strtoul(name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX), | ||||
NULL, 10); | |||||
/* validation by comparing against the reconstucted name */ | |||||
snprintf(linkName, sizeof(linkName), | |||||
"%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, | |||||
linkNum); | |||||
if (strcmp(linkName, name) != 0) | |||||
return (EINVAL); | return (EINVAL); | ||||
linkNum = strtoul(cp, &eptr, 10); | |||||
if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS) | if(NG_PEER_NODE(hook) == node) | ||||
return (EINVAL); | return (ELOOP); | ||||
if (priv->links[linkNum] != NULL) | |||||
return (EISCONN); | link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, | ||||
priv->links[linkNum] = malloc(sizeof(*priv->links[linkNum]), | M_WAITOK|M_ZERO); | ||||
M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO); | if (link == NULL) | ||||
if (priv->links[linkNum] == NULL) | |||||
return (ENOMEM); | return (ENOMEM); | ||||
priv->links[linkNum]->hook = hook; | link->hook = hook; | ||||
NG_HOOK_SET_PRIVATE(hook, (void *)linkNum); | NG_HOOK_SET_PRIVATE(hook, link); | ||||
priv->numLinks++; | priv->numLinks++; | ||||
return (0); | return (0); | ||||
} | } | ||||
/* Unknown hook name */ | /* Unknown hook name */ | ||||
return (EINVAL); | return (EINVAL); | ||||
} | } | ||||
/* | /* | ||||
* Receive a control message | * Receive a control message | ||||
*/ | */ | ||||
static int | static int | ||||
ng_bridge_reset_link(hook_p hook, int ret) | |||||
{ | |||||
link_p priv = NG_HOOK_PRIVATE(hook); | |||||
priv->loopCount = 0; | |||||
bzero(&priv->stats, sizeof(priv->stats)); | |||||
return (ret); | |||||
} | |||||
static int | |||||
ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) | ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) | ||||
{ | { | ||||
const priv_p priv = NG_NODE_PRIVATE(node); | const priv_p priv = NG_NODE_PRIVATE(node); | ||||
struct ng_mesg *resp = NULL; | struct ng_mesg *resp = NULL; | ||||
int error = 0; | int error = 0; | ||||
struct ng_mesg *msg; | struct ng_mesg *msg; | ||||
NGI_GET_MSG(item, msg); | NGI_GET_MSG(item, msg); | ||||
Show All 12 Lines | case NGM_BRIDGE_GET_CONFIG: | ||||
} | } | ||||
conf = (struct ng_bridge_config *)resp->data; | conf = (struct ng_bridge_config *)resp->data; | ||||
*conf = priv->conf; /* no sanity checking needed */ | *conf = priv->conf; /* no sanity checking needed */ | ||||
break; | break; | ||||
} | } | ||||
case NGM_BRIDGE_SET_CONFIG: | case NGM_BRIDGE_SET_CONFIG: | ||||
{ | { | ||||
struct ng_bridge_config *conf; | struct ng_bridge_config *conf; | ||||
int i; | |||||
if (msg->header.arglen | if (msg->header.arglen | ||||
!= sizeof(struct ng_bridge_config)) { | != sizeof(struct ng_bridge_config)) { | ||||
error = EINVAL; | error = EINVAL; | ||||
break; | break; | ||||
} | } | ||||
conf = (struct ng_bridge_config *)msg->data; | conf = (struct ng_bridge_config *)msg->data; | ||||
priv->conf = *conf; | priv->conf = *conf; | ||||
for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) | |||||
priv->conf.ipfw[i] = !!priv->conf.ipfw[i]; | |||||
break; | break; | ||||
} | } | ||||
case NGM_BRIDGE_RESET: | case NGM_BRIDGE_RESET: | ||||
{ | { | ||||
int i; | hook_p rethook; | ||||
/* Flush all entries in the hash table */ | /* Flush all entries in the hash table */ | ||||
ng_bridge_remove_hosts(priv, -1); | ng_bridge_remove_hosts(priv, NULL); | ||||
/* Reset all loop detection counters and stats */ | /* Reset all loop detection counters and stats */ | ||||
for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) { | NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, 1, rethook); | ||||
if (priv->links[i] == NULL) | |||||
continue; | |||||
priv->links[i]->loopCount = 0; | |||||
bzero(&priv->links[i]->stats, | |||||
sizeof(priv->links[i]->stats)); | |||||
} | |||||
break; | break; | ||||
} | } | ||||
case NGM_BRIDGE_GET_STATS: | case NGM_BRIDGE_GET_STATS: | ||||
case NGM_BRIDGE_CLR_STATS: | case NGM_BRIDGE_CLR_STATS: | ||||
case NGM_BRIDGE_GETCLR_STATS: | case NGM_BRIDGE_GETCLR_STATS: | ||||
{ | { | ||||
struct ng_bridge_link *link; | hook_p hook; | ||||
int linkNum; | link_p link; | ||||
char linkName[NG_HOOKSIZ]; | |||||
/* Get link number */ | /* Get link number */ | ||||
if (msg->header.arglen != sizeof(u_int32_t)) { | if (msg->header.arglen != sizeof(u_int32_t)) { | ||||
error = EINVAL; | error = EINVAL; | ||||
break; | break; | ||||
} | } | ||||
linkNum = *((u_int32_t *)msg->data); | snprintf(linkName, sizeof(linkName), | ||||
if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) { | "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, | ||||
error = EINVAL; | *((u_int32_t *)msg->data)); | ||||
break; | |||||
} | if ((hook = ng_findhook(node, linkName)) == NULL) { | ||||
if ((link = priv->links[linkNum]) == NULL) { | |||||
error = ENOTCONN; | error = ENOTCONN; | ||||
break; | break; | ||||
} | } | ||||
link = NG_HOOK_PRIVATE(hook); | |||||
/* Get/clear stats */ | /* Get/clear stats */ | ||||
if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { | if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { | ||||
NG_MKRESPONSE(resp, msg, | NG_MKRESPONSE(resp, msg, | ||||
sizeof(link->stats), M_NOWAIT); | sizeof(link->stats), M_NOWAIT); | ||||
if (resp == NULL) { | if (resp == NULL) { | ||||
error = ENOMEM; | error = ENOMEM; | ||||
break; | break; | ||||
Show All 15 Lines | case NGM_BRIDGE_GET_TABLE: | ||||
+ (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); | + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); | ||||
if (resp == NULL) { | if (resp == NULL) { | ||||
error = ENOMEM; | error = ENOMEM; | ||||
break; | break; | ||||
} | } | ||||
ary = (struct ng_bridge_host_ary *)resp->data; | ary = (struct ng_bridge_host_ary *)resp->data; | ||||
ary->numHosts = priv->numHosts; | ary->numHosts = priv->numHosts; | ||||
for (bucket = 0; bucket < priv->numBuckets; bucket++) { | for (bucket = 0; bucket < priv->numBuckets; bucket++) { | ||||
SLIST_FOREACH(hent, &priv->tab[bucket], next) | SLIST_FOREACH(hent, &priv->tab[bucket], next) { | ||||
ary->hosts[i++] = hent->host; | memcpy(ary->hosts[i].addr, | ||||
hent->host.addr, | |||||
sizeof(ary->hosts[i].addr)); | |||||
ary->hosts[i].age = hent->host.age; | |||||
ary->hosts[i].staleness = hent->host.staleness; | |||||
strncpy(ary->hosts[i].hook, | |||||
NG_HOOK_NAME(hent->host.link->hook), | |||||
sizeof(ary->hosts[i].hook)); | |||||
i++; | |||||
} | } | ||||
} | |||||
break; | break; | ||||
} | } | ||||
case NGM_BRIDGE_SET_PERSISTENT: | case NGM_BRIDGE_SET_PERSISTENT: | ||||
{ | { | ||||
priv->persistent = 1; | priv->persistent = 1; | ||||
break; | break; | ||||
} | } | ||||
default: | default: | ||||
Show All 10 Lines | ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) | ||||
NG_RESPOND_MSG(error, node, item, resp); | NG_RESPOND_MSG(error, node, item, resp); | ||||
NG_FREE_MSG(msg); | NG_FREE_MSG(msg); | ||||
return (error); | return (error); | ||||
} | } | ||||
/* | /* | ||||
* Receive data on a hook | * Receive data on a hook | ||||
*/ | */ | ||||
struct ng_bridge_send_ctx { | |||||
link_p foundFirst, incoming; | |||||
struct mbuf * m; | |||||
int manycast, error; | |||||
}; | |||||
static int | static int | ||||
ng_bridge_send_ctx(hook_p dst, struct ng_bridge_send_ctx * ctx) | |||||
{ | |||||
link_p destLink = NG_HOOK_PRIVATE(dst); | |||||
struct mbuf *m2 = NULL; | |||||
int error = 0; | |||||
/* Skip incoming link */ | |||||
if (destLink == ctx->incoming) { | |||||
return (1); | |||||
} | |||||
if (ctx->foundFirst == NULL) { | |||||
/* | |||||
* This is the first usable link we have found. | |||||
* Reserve it for the originals. | |||||
* If we never find another we save a copy. | |||||
*/ | |||||
ctx->foundFirst = destLink; | |||||
return (1); | |||||
} | |||||
/* | |||||
* It's usable link but not the reserved (first) one. | |||||
* Copy mbuf info for sending. | |||||
*/ | |||||
m2 = m_dup(ctx->m, M_NOWAIT); /* XXX m_copypacket() */ | |||||
if (m2 == NULL) { | |||||
ctx->incoming->stats.memoryFailures++; | |||||
ctx->error = ENOBUFS; | |||||
return (0); /* abort loop */ | |||||
} | |||||
/* Update stats */ | |||||
destLink->stats.xmitPackets++; | |||||
destLink->stats.xmitOctets += m2->m_pkthdr.len; | |||||
switch (ctx->manycast) { | |||||
default: /* unknown unicast */ | |||||
break; | |||||
case 1: /* multicast */ | |||||
destLink->stats.xmitMulticasts++; | |||||
break; | |||||
case 2: /* broadcast */ | |||||
destLink->stats.xmitBroadcasts++; | |||||
break; | |||||
} | |||||
/* Send packet */ | |||||
NG_SEND_DATA_ONLY(error, destLink->hook, m2); | |||||
if(error) | |||||
ctx->error = error; | |||||
return (1); | |||||
} | |||||
static int | |||||
ng_bridge_rcvdata(hook_p hook, item_p item) | ng_bridge_rcvdata(hook_p hook, item_p item) | ||||
{ | { | ||||
const node_p node = NG_HOOK_NODE(hook); | const node_p node = NG_HOOK_NODE(hook); | ||||
const priv_p priv = NG_NODE_PRIVATE(node); | const priv_p priv = NG_NODE_PRIVATE(node); | ||||
struct ng_bridge_host *host; | struct ng_bridge_host *host; | ||||
struct ng_bridge_link *link; | |||||
struct ether_header *eh; | struct ether_header *eh; | ||||
int error = 0, linkNum, linksSeen; | struct ng_bridge_send_ctx ctx = { 0 }; | ||||
int manycast; | hook_p ret; | ||||
struct mbuf *m; | |||||
struct ng_bridge_link *firstLink; | |||||
NGI_GET_M(item, m); | NGI_GET_M(item, ctx.m); | ||||
/* Get link number */ | |||||
linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); | |||||
KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, | |||||
("%s: linkNum=%u", __func__, linkNum)); | |||||
link = priv->links[linkNum]; | |||||
KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum)); | |||||
ctx.incoming = NG_HOOK_PRIVATE(hook); | |||||
/* Sanity check packet and pull up header */ | /* Sanity check packet and pull up header */ | ||||
if (m->m_pkthdr.len < ETHER_HDR_LEN) { | if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) { | ||||
link->stats.recvRunts++; | ctx.incoming->stats.recvRunts++; | ||||
NG_FREE_ITEM(item); | NG_FREE_ITEM(item); | ||||
NG_FREE_M(m); | NG_FREE_M(ctx.m); | ||||
return (EINVAL); | return (EINVAL); | ||||
} | } | ||||
if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) { | if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) { | ||||
link->stats.memoryFailures++; | ctx.incoming->stats.memoryFailures++; | ||||
NG_FREE_ITEM(item); | NG_FREE_ITEM(item); | ||||
return (ENOBUFS); | return (ENOBUFS); | ||||
} | } | ||||
eh = mtod(m, struct ether_header *); | eh = mtod(ctx.m, struct ether_header *); | ||||
if ((eh->ether_shost[0] & 1) != 0) { | if ((eh->ether_shost[0] & 1) != 0) { | ||||
link->stats.recvInvalid++; | ctx.incoming->stats.recvInvalid++; | ||||
NG_FREE_ITEM(item); | NG_FREE_ITEM(item); | ||||
NG_FREE_M(m); | NG_FREE_M(ctx.m); | ||||
return (EINVAL); | return (EINVAL); | ||||
} | } | ||||
/* Is link disabled due to a loopback condition? */ | /* Is link disabled due to a loopback condition? */ | ||||
if (link->loopCount != 0) { | if (ctx.incoming->loopCount != 0) { | ||||
link->stats.loopDrops++; | ctx.incoming->stats.loopDrops++; | ||||
NG_FREE_ITEM(item); | NG_FREE_ITEM(item); | ||||
NG_FREE_M(m); | NG_FREE_M(ctx.m); | ||||
return (ELOOP); /* XXX is this an appropriate error? */ | return (ELOOP); /* XXX is this an appropriate error? */ | ||||
} | } | ||||
/* Update stats */ | /* Update stats */ | ||||
link->stats.recvPackets++; | ctx.incoming->stats.recvPackets++; | ||||
link->stats.recvOctets += m->m_pkthdr.len; | ctx.incoming->stats.recvOctets += ctx.m->m_pkthdr.len; | ||||
if ((manycast = (eh->ether_dhost[0] & 1)) != 0) { | if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) { | ||||
if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { | if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { | ||||
link->stats.recvBroadcasts++; | ctx.incoming->stats.recvBroadcasts++; | ||||
manycast = 2; | ctx.manycast = 2; | ||||
} else | } else | ||||
link->stats.recvMulticasts++; | ctx.incoming->stats.recvMulticasts++; | ||||
} | } | ||||
/* Look up packet's source Ethernet address in hashtable */ | /* Look up packet's source Ethernet address in hashtable */ | ||||
if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { | if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { | ||||
/* Update time since last heard from this host */ | /* Update time since last heard from this host */ | ||||
host->staleness = 0; | host->staleness = 0; | ||||
/* Did host jump to a different link? */ | /* Did host jump to a different link? */ | ||||
if (host->linkNum != linkNum) { | if (host->link != ctx.incoming) { | ||||
/* | /* | ||||
* If the host's old link was recently established | * If the host's old link was recently established | ||||
* on the old link and it's already jumped to a new | * on the old link and it's already jumped to a new | ||||
* link, declare a loopback condition. | * link, declare a loopback condition. | ||||
*/ | */ | ||||
if (host->age < priv->conf.minStableAge) { | if (host->age < priv->conf.minStableAge) { | ||||
/* Log the problem */ | /* Log the problem */ | ||||
if (priv->conf.debugLevel >= 2) { | if (priv->conf.debugLevel >= 2) { | ||||
struct ifnet *ifp = m->m_pkthdr.rcvif; | struct ifnet *ifp = ctx.m->m_pkthdr.rcvif; | ||||
char suffix[32]; | char suffix[32]; | ||||
if (ifp != NULL) | if (ifp != NULL) | ||||
snprintf(suffix, sizeof(suffix), | snprintf(suffix, sizeof(suffix), | ||||
" (%s)", ifp->if_xname); | " (%s)", ifp->if_xname); | ||||
else | else | ||||
*suffix = '\0'; | *suffix = '\0'; | ||||
log(LOG_WARNING, "ng_bridge: %s:" | log(LOG_WARNING, "ng_bridge: %s:" | ||||
" loopback detected on %s%s\n", | " loopback detected on %s%s\n", | ||||
ng_bridge_nodename(node), | ng_bridge_nodename(node), | ||||
NG_HOOK_NAME(hook), suffix); | NG_HOOK_NAME(hook), suffix); | ||||
} | } | ||||
/* Mark link as linka non grata */ | /* Mark link as linka non grata */ | ||||
link->loopCount = priv->conf.loopTimeout; | ctx.incoming->loopCount = priv->conf.loopTimeout; | ||||
link->stats.loopDetects++; | ctx.incoming->stats.loopDetects++; | ||||
/* Forget all hosts on this link */ | /* Forget all hosts on this link */ | ||||
ng_bridge_remove_hosts(priv, linkNum); | ng_bridge_remove_hosts(priv, ctx.incoming); | ||||
/* Drop packet */ | /* Drop packet */ | ||||
link->stats.loopDrops++; | ctx.incoming->stats.loopDrops++; | ||||
NG_FREE_ITEM(item); | NG_FREE_ITEM(item); | ||||
NG_FREE_M(m); | NG_FREE_M(ctx.m); | ||||
return (ELOOP); /* XXX appropriate? */ | return (ELOOP); /* XXX appropriate? */ | ||||
} | } | ||||
/* Move host over to new link */ | /* Move host over to new link */ | ||||
host->linkNum = linkNum; | host->link = ctx.incoming; | ||||
host->age = 0; | host->age = 0; | ||||
} | } | ||||
} else { | } else { | ||||
if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) { | if (!ng_bridge_put(priv, eh->ether_shost, ctx.incoming)) { | ||||
link->stats.memoryFailures++; | ctx.incoming->stats.memoryFailures++; | ||||
NG_FREE_ITEM(item); | NG_FREE_ITEM(item); | ||||
NG_FREE_M(m); | NG_FREE_M(ctx.m); | ||||
return (ENOMEM); | return (ENOMEM); | ||||
} | } | ||||
} | } | ||||
/* Run packet through ipfw processing, if enabled */ | /* Run packet through ipfw processing, if enabled */ | ||||
#if 0 | #if 0 | ||||
if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) { | if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) { | ||||
/* XXX not implemented yet */ | /* XXX not implemented yet */ | ||||
} | } | ||||
#endif | #endif | ||||
/* | /* | ||||
* If unicast and destination host known, deliver to host's link, | * If unicast and destination host known, deliver to host's link, | ||||
* unless it is the same link as the packet came in on. | * unless it is the same link as the packet came in on. | ||||
*/ | */ | ||||
if (!manycast) { | if (!ctx.manycast) { | ||||
/* Determine packet destination link */ | /* Determine packet destination link */ | ||||
if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { | if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { | ||||
struct ng_bridge_link *const destLink | link_p destLink = host->link; | ||||
= priv->links[host->linkNum]; | |||||
/* If destination same as incoming link, do nothing */ | /* If destination same as incoming link, do nothing */ | ||||
KASSERT(destLink != NULL, | if (destLink == ctx.incoming) { | ||||
("%s: link%d null", __func__, host->linkNum)); | |||||
if (destLink == link) { | |||||
NG_FREE_ITEM(item); | NG_FREE_ITEM(item); | ||||
NG_FREE_M(m); | NG_FREE_M(ctx.m); | ||||
return (0); | return (0); | ||||
} | } | ||||
/* Deliver packet out the destination link */ | /* Deliver packet out the destination link */ | ||||
destLink->stats.xmitPackets++; | destLink->stats.xmitPackets++; | ||||
destLink->stats.xmitOctets += m->m_pkthdr.len; | destLink->stats.xmitOctets += ctx.m->m_pkthdr.len; | ||||
NG_FWD_NEW_DATA(error, item, destLink->hook, m); | NG_FWD_NEW_DATA(ctx.error, item, destLink->hook, ctx.m); | ||||
return (error); | return (ctx.error); | ||||
} | } | ||||
/* Destination host is not known */ | /* Destination host is not known */ | ||||
link->stats.recvUnknown++; | ctx.incoming->stats.recvUnknown++; | ||||
} | } | ||||
/* Distribute unknown, multicast, broadcast pkts to all other links */ | /* Distribute unknown, multicast, broadcast pkts to all other links */ | ||||
firstLink = NULL; | NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret); | ||||
for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) { | |||||
struct ng_bridge_link *destLink; | |||||
struct mbuf *m2 = NULL; | |||||
/* | |||||
* If we have checked all the links then now | |||||
* send the original on its reserved link | |||||
*/ | |||||
if (linksSeen == priv->numLinks) { | |||||
/* If we never saw a good link, leave. */ | /* If we never saw a good link, leave. */ | ||||
if (firstLink == NULL) { | if (ctx.foundFirst == NULL || ctx.error != 0) { | ||||
NG_FREE_ITEM(item); | NG_FREE_ITEM(item); | ||||
NG_FREE_M(m); | NG_FREE_M(ctx.m); | ||||
return (0); | return (ctx.error); | ||||
} | } | ||||
destLink = firstLink; | |||||
} else { | |||||
destLink = priv->links[linkNum]; | |||||
if (destLink != NULL) | |||||
linksSeen++; | |||||
/* Skip incoming link and disconnected links */ | |||||
if (destLink == NULL || destLink == link) { | |||||
continue; | |||||
} | |||||
if (firstLink == NULL) { | |||||
/* | |||||
* This is the first usable link we have found. | |||||
* Reserve it for the originals. | |||||
* If we never find another we save a copy. | |||||
*/ | |||||
firstLink = destLink; | |||||
continue; | |||||
} | |||||
/* | /* | ||||
* It's usable link but not the reserved (first) one. | |||||
* Copy mbuf info for sending. | |||||
*/ | |||||
m2 = m_dup(m, M_NOWAIT); /* XXX m_copypacket() */ | |||||
if (m2 == NULL) { | |||||
link->stats.memoryFailures++; | |||||
NG_FREE_ITEM(item); | |||||
NG_FREE_M(m); | |||||
return (ENOBUFS); | |||||
} | |||||
} | |||||
/* Update stats */ | |||||
destLink->stats.xmitPackets++; | |||||
destLink->stats.xmitOctets += m->m_pkthdr.len; | |||||
switch (manycast) { | |||||
case 0: /* unicast */ | |||||
break; | |||||
case 1: /* multicast */ | |||||
destLink->stats.xmitMulticasts++; | |||||
break; | |||||
case 2: /* broadcast */ | |||||
destLink->stats.xmitBroadcasts++; | |||||
break; | |||||
} | |||||
/* Send packet */ | |||||
if (destLink == firstLink) { | |||||
/* | |||||
* If we've sent all the others, send the original | * If we've sent all the others, send the original | ||||
* on the first link we found. | * on the first link we found. | ||||
*/ | */ | ||||
NG_FWD_NEW_DATA(error, item, destLink->hook, m); | NG_FWD_NEW_DATA(ctx.error, item, ctx.foundFirst->hook, ctx.m); | ||||
break; /* always done last - not really needed. */ | return (ctx.error); | ||||
} else { | |||||
NG_SEND_DATA_ONLY(error, destLink->hook, m2); | |||||
} | } | ||||
} | |||||
return (error); | |||||
} | |||||
/* | /* | ||||
* Shutdown node | * Shutdown node | ||||
*/ | */ | ||||
static int | static int | ||||
ng_bridge_shutdown(node_p node) | ng_bridge_shutdown(node_p node) | ||||
{ | { | ||||
const priv_p priv = NG_NODE_PRIVATE(node); | const priv_p priv = NG_NODE_PRIVATE(node); | ||||
Show All 18 Lines | |||||
/* | /* | ||||
* Hook disconnection. | * Hook disconnection. | ||||
*/ | */ | ||||
static int | static int | ||||
ng_bridge_disconnect(hook_p hook) | ng_bridge_disconnect(hook_p hook) | ||||
{ | { | ||||
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); | const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); | ||||
int linkNum; | link_p link = NG_HOOK_PRIVATE(hook); | ||||
/* Get link number */ | |||||
linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); | |||||
KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, | |||||
("%s: linkNum=%u", __func__, linkNum)); | |||||
/* Remove all hosts associated with this link */ | /* Remove all hosts associated with this link */ | ||||
ng_bridge_remove_hosts(priv, linkNum); | ng_bridge_remove_hosts(priv, link); | ||||
/* Free associated link information */ | /* Free associated link information */ | ||||
KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__)); | free(link, M_NETGRAPH_BRIDGE); | ||||
free(priv->links[linkNum], M_NETGRAPH_BRIDGE); | |||||
priv->links[linkNum] = NULL; | |||||
priv->numLinks--; | priv->numLinks--; | ||||
/* If no more hooks, go away */ | /* If no more hooks, go away */ | ||||
if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) | if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) | ||||
&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) | && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) | ||||
&& !priv->persistent) { | && !priv->persistent) { | ||||
ng_rmnode_self(NG_HOOK_NODE(hook)); | ng_rmnode_self(NG_HOOK_NODE(hook)); | ||||
} | } | ||||
Show All 28 Lines | |||||
} | } | ||||
/* | /* | ||||
* Add a new host entry to the table. This assumes the host doesn't | * Add a new host entry to the table. This assumes the host doesn't | ||||
* already exist in the table. Returns 1 on success, 0 if there | * already exist in the table. Returns 1 on success, 0 if there | ||||
* was a memory allocation failure. | * was a memory allocation failure. | ||||
*/ | */ | ||||
static int | static int | ||||
ng_bridge_put(priv_p priv, const u_char *addr, int linkNum) | ng_bridge_put(priv_p priv, const u_char *addr, link_p link) | ||||
{ | { | ||||
const int bucket = HASH(addr, priv->hashMask); | const int bucket = HASH(addr, priv->hashMask); | ||||
struct ng_bridge_hent *hent; | struct ng_bridge_hent *hent; | ||||
#ifdef INVARIANTS | #ifdef INVARIANTS | ||||
/* Assert that entry does not already exist in hashtable */ | /* Assert that entry does not already exist in hashtable */ | ||||
SLIST_FOREACH(hent, &priv->tab[bucket], next) { | SLIST_FOREACH(hent, &priv->tab[bucket], next) { | ||||
KASSERT(!ETHER_EQUAL(hent->host.addr, addr), | KASSERT(!ETHER_EQUAL(hent->host.addr, addr), | ||||
("%s: entry %6D exists in table", __func__, addr, ":")); | ("%s: entry %6D exists in table", __func__, addr, ":")); | ||||
} | } | ||||
#endif | #endif | ||||
/* Allocate and initialize new hashtable entry */ | /* Allocate and initialize new hashtable entry */ | ||||
hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT); | hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT); | ||||
if (hent == NULL) | if (hent == NULL) | ||||
return (0); | return (0); | ||||
bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); | bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); | ||||
hent->host.linkNum = linkNum; | hent->host.link = link; | ||||
hent->host.staleness = 0; | hent->host.staleness = 0; | ||||
hent->host.age = 0; | hent->host.age = 0; | ||||
/* Add new element to hash bucket */ | /* Add new element to hash bucket */ | ||||
SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); | SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); | ||||
priv->numHosts++; | priv->numHosts++; | ||||
/* Resize table if necessary */ | /* Resize table if necessary */ | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | ng_bridge_rehash(priv_p priv) | ||||
priv->tab = newTab; | priv->tab = newTab; | ||||
return; | return; | ||||
} | } | ||||
/****************************************************************** | /****************************************************************** | ||||
MISC FUNCTIONS | MISC FUNCTIONS | ||||
******************************************************************/ | ******************************************************************/ | ||||
/* | /* | ||||
* Remove all hosts associated with a specific link from the hashtable. | * Remove all hosts associated with a specific link from the hashtable. | ||||
* If linkNum == -1, then remove all hosts in the table. | * If linkNum == -1, then remove all hosts in the table. | ||||
*/ | */ | ||||
static void | static void | ||||
ng_bridge_remove_hosts(priv_p priv, int linkNum) | ng_bridge_remove_hosts(priv_p priv, link_p link) | ||||
{ | { | ||||
int bucket; | int bucket; | ||||
for (bucket = 0; bucket < priv->numBuckets; bucket++) { | for (bucket = 0; bucket < priv->numBuckets; bucket++) { | ||||
struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); | struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); | ||||
while (*hptr != NULL) { | while (*hptr != NULL) { | ||||
struct ng_bridge_hent *const hent = *hptr; | struct ng_bridge_hent *const hent = *hptr; | ||||
if (linkNum == -1 || hent->host.linkNum == linkNum) { | if (link == NULL || hent->host.link == link) { | ||||
*hptr = SLIST_NEXT(hent, next); | *hptr = SLIST_NEXT(hent, next); | ||||
free(hent, M_NETGRAPH_BRIDGE); | free(hent, M_NETGRAPH_BRIDGE); | ||||
priv->numHosts--; | priv->numHosts--; | ||||
} else | } else | ||||
hptr = &SLIST_NEXT(hent, next); | hptr = &SLIST_NEXT(hent, next); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/* | /* | ||||
* Handle our once-per-second timeout event. We do two things: | * Handle our once-per-second timeout event. We do two things: | ||||
* we decrement link->loopCount for those links being muted due to | * we decrement link->loopCount for those links being muted due to | ||||
* a detected loopback condition, and we remove any hosts from | * a detected loopback condition, and we remove any hosts from | ||||
* the hashtable whom we haven't heard from in a long while. | * the hashtable whom we haven't heard from in a long while. | ||||
*/ | */ | ||||
static int | |||||
ng_bridge_unmute(hook_p hook, int *counter) | |||||
{ | |||||
link_p link = NG_HOOK_PRIVATE(hook); | |||||
node_p node = NG_HOOK_NODE(hook); | |||||
priv_p priv = NG_NODE_PRIVATE(node); | |||||
if (link->loopCount != 0) { | |||||
link->loopCount--; | |||||
if (link->loopCount == 0 && priv->conf.debugLevel >= 2) { | |||||
log(LOG_INFO, "ng_bridge: %s:" | |||||
" restoring looped back %s\n", | |||||
ng_bridge_nodename(node), NG_HOOK_NAME(hook)); | |||||
} | |||||
} | |||||
counter++; | |||||
return (1); | |||||
} | |||||
static void | static void | ||||
ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) | ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) | ||||
{ | { | ||||
const priv_p priv = NG_NODE_PRIVATE(node); | const priv_p priv = NG_NODE_PRIVATE(node); | ||||
int bucket; | int bucket; | ||||
int counter = 0; | int counter = 0; | ||||
int linkNum; | hook_p ret; | ||||
/* Update host time counters and remove stale entries */ | /* Update host time counters and remove stale entries */ | ||||
for (bucket = 0; bucket < priv->numBuckets; bucket++) { | for (bucket = 0; bucket < priv->numBuckets; bucket++) { | ||||
struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); | struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); | ||||
while (*hptr != NULL) { | while (*hptr != NULL) { | ||||
struct ng_bridge_hent *const hent = *hptr; | struct ng_bridge_hent *const hent = *hptr; | ||||
/* Make sure host's link really exists */ | |||||
KASSERT(priv->links[hent->host.linkNum] != NULL, | |||||
("%s: host %6D on nonexistent link %d\n", | |||||
__func__, hent->host.addr, ":", | |||||
hent->host.linkNum)); | |||||
/* Remove hosts we haven't heard from in a while */ | /* Remove hosts we haven't heard from in a while */ | ||||
if (++hent->host.staleness >= priv->conf.maxStaleness) { | if (++hent->host.staleness >= priv->conf.maxStaleness) { | ||||
*hptr = SLIST_NEXT(hent, next); | *hptr = SLIST_NEXT(hent, next); | ||||
free(hent, M_NETGRAPH_BRIDGE); | free(hent, M_NETGRAPH_BRIDGE); | ||||
priv->numHosts--; | priv->numHosts--; | ||||
} else { | } else { | ||||
if (hent->host.age < 0xffff) | if (hent->host.age < 0xffff) | ||||
hent->host.age++; | hent->host.age++; | ||||
hptr = &SLIST_NEXT(hent, next); | hptr = &SLIST_NEXT(hent, next); | ||||
counter++; | counter++; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
KASSERT(priv->numHosts == counter, | KASSERT(priv->numHosts == counter, | ||||
("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); | ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); | ||||
/* Decrease table size if necessary */ | /* Decrease table size if necessary */ | ||||
ng_bridge_rehash(priv); | ng_bridge_rehash(priv); | ||||
/* Decrease loop counter on muted looped back links */ | /* Decrease loop counter on muted looped back links */ | ||||
for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) { | counter = 0; | ||||
struct ng_bridge_link *const link = priv->links[linkNum]; | NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret); | ||||
if (link != NULL) { | |||||
if (link->loopCount != 0) { | |||||
link->loopCount--; | |||||
if (link->loopCount == 0 | |||||
&& priv->conf.debugLevel >= 2) { | |||||
log(LOG_INFO, "ng_bridge: %s:" | |||||
" restoring looped back link%d\n", | |||||
ng_bridge_nodename(node), linkNum); | |||||
} | |||||
} | |||||
counter++; | |||||
} | |||||
} | |||||
KASSERT(priv->numLinks == counter, | KASSERT(priv->numLinks == counter, | ||||
("%s: links: %d != %d", __func__, priv->numLinks, counter)); | ("%s: links: %d != %d", __func__, priv->numLinks, counter)); | ||||
/* Register a new timeout, keeping the existing node reference */ | /* Register a new timeout, keeping the existing node reference */ | ||||
ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); | ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); | ||||
} | } | ||||
/* | /* | ||||
Show All 14 Lines |