58#include <sys/kernel.h>
59#include <sys/malloc.h>
61#include <sys/socket.h>
62#include <net/ethernet.h>
70#ifdef NG_SEPARATE_MALLOC
73#define M_NETGRAPH_MACFILTER M_NETGRAPH
76#define MACTABLE_BLOCKSIZE 128
78#ifdef NG_MACFILTER_DEBUG
79#define MACFILTER_DEBUG(fmt, ...) printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__)
81#define MACFILTER_DEBUG(fmt, ...)
83#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
84#define MAC_S_ARGS(v) (v)[0], (v)[1], (v)[2], (v)[3], (v)[4], (v)[5]
113 const u_char *start,
const u_char *buf)
146 const u_char *start,
const u_char *buf)
151 MACFILTER_DEBUG(
"buf %p, ngm_hooks %p, n %d", buf, ngm_hooks, ngm_hooks->
n);
291 else if (mfp->
mf_mac_used + 2*MACTABLE_BLOCKSIZE < mfp->mf_mac_allocated)
302 M_NETGRAPH, M_NOWAIT | M_ZERO);
303 if (mfp_new == NULL) {
380 u_int middle = base + (range >> 1);
381 int d = bcmp(ether, mf_macs[middle].ether, ETHER_ADDR_LEN);
386 range -= middle - base + 1;
389 range = middle - base;
422 size_t len = (mfp->
mf_mac_used - i) *
sizeof(mf_macs[0]);
424 bcopy(&mf_macs[i+1], &mf_macs[i], len);
428 mf_macs[i].
hookid = hookid;
441 size_t len = (mfp->
mf_mac_used - i) *
sizeof(mf_macs[0]);
443 bcopy(&mf_macs[i], &mf_macs[i+1], len);
445 mf_macs[i].
hookid = hookid;
446 bcopy(ether, mf_macs[i].ether, ETHER_ADDR_LEN);
481 for (hookid = 0; hookid < mfp->
mf_upper_cnt; hookid++) {
531 struct ether_header *ether_header = mtod(m,
struct ether_header *);
532 u_char *ether = ether_header->ether_dhost;
544 if (m->m_len > ETHER_HDR_LEN)
545 mf_macs[i].
bytes_out += m->m_len - ETHER_HDR_LEN;
547#ifdef NG_MACFILTER_DEBUG_RECVDATA
553#ifdef NG_MACFILTER_DEBUG_RECVDATA
560 mtx_unlock(&mfp->
mtx);
572 struct ether_header *ether_header = mtod(m,
struct ether_header *);
573 u_char *ether = ether_header->ether_shost;
584 if (m->m_len > ETHER_HDR_LEN)
585 mf_macs[i].
bytes_in += m->m_len - ETHER_HDR_LEN;
587 hookid = mf_macs[i].
hookid;
589#ifdef NG_MACFILTER_DEBUG_RECVDATA
595#ifdef NG_MACFILTER_DEBUG_RECVDATA
607 mtx_unlock(&mfp->
mtx);
625 macfilter_p mfp = malloc(
sizeof(*mfp), M_NETGRAPH, M_NOWAIT | M_ZERO);
635 mtx_init(&mfp->
mtx,
"Macfilter table", NULL, MTX_DEF);
665 M_NETGRAPH, M_NOWAIT | M_ZERO);
756 for (hookid = 0; hookid < mfp->
mf_upper_cnt; hookid++) {
757 if (mfp->
mf_upper[hookid] != NULL) {
779 mtx_unlock(&mfp->
mtx);
803 if (next_hook == NULL) {
827 for (hookid = 0; hookid < mfp->
mf_upper_cnt; hookid++) {
828 if (mfp->
mf_upper[hookid] == hook) {
831#ifndef NG_MACFILTER_DEBUG
844 for (--hookid; hookid >= 0 && mfp->
mf_upper[hookid] == NULL; hookid--)
851 M_NETGRAPH, M_NOWAIT | M_ZERO);
855 mtx_unlock(&mfp->
mtx);
870 mtx_destroy(&mfp->
mtx);
872 free(mfp->
mf_macs, M_NETGRAPH);
873 free(mfp, M_NETGRAPH);
#define NG_HOOK_NODE(hook)
int ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook)
#define NG_FWD_ITEM_HOOK(error, item, hook)
int ng_disconnect_t(hook_p hook)
#define NG_NODE_SET_PRIVATE(node, val)
#define NG_RESPOND_MSG(error, here, item, resp)
#define NG_NODE_IS_VALID(node)
#define NG_NODE_UNREF(node)
int ng_rmnode_self(node_p here)
int ng_rcvdata_t(hook_p hook, item_p item)
int ng_shutdown_t(node_p node)
#define NG_HOOK_NAME(hook)
#define NG_FREE_ITEM(item)
int ng_constructor_t(node_p node)
#define NG_NODE_NUMHOOKS(node)
#define NGI_GET_MSG(i, m)
#define NG_NODE_PRIVATE(node)
int ng_newhook_t(node_p node, hook_p hook, const char *name)
MALLOC_DEFINE(M_NG_CCATM, "ng_ccatm", "netgraph uni api node")
static u_int macfilter_find_mac(macfilter_p mfp, const u_char *ether, u_int *ri)
static int macfilter_find_hook(macfilter_p mfp, const char *hookname)
static const struct ng_parse_type ng_macfilter_direct_hookid_type
static const struct ng_parse_array_info ng_macfilter_hooks_array_info
static int macfilter_mactable_remove_by_hookid(macfilter_p mfp, int hookid)
#define MACTABLE_BLOCKSIZE
static const struct ng_cmdlist ng_macfilter_cmdlist[]
static ng_disconnect_t ng_macfilter_disconnect
static int macfilter_get_upper_hook_count(const struct ng_parse_type *type, const u_char *start, const u_char *buf)
static const struct ng_parse_struct_field ng_macfilter_hook_fields[]
static const struct ng_parse_type ng_macfilter_macs_array_type
static int macfilter_ether_output(hook_p hook, macfilter_p mfp, struct mbuf *m, hook_p *next_hook)
static int macfilter_direct(macfilter_p mfp, struct ngm_macfilter_direct *md)
static ng_rcvdata_t ng_macfilter_rcvdata
static const struct ng_parse_struct_field macfilter_direct_fields[]
static const struct ng_parse_type ng_macfilter_hooks_array_type
static ng_rcvmsg_t ng_macfilter_rcvmsg
static ng_newhook_t ng_macfilter_newhook
#define MACFILTER_DEBUG(fmt,...)
static int macfilter_mac_count(macfilter_p mfp, int hookid)
struct ngm_macfilter_mac * mf_mac_p
NETGRAPH_INIT(macfilter, &typestruct)
static int macfilter_direct_hookid(macfilter_p mfp, struct ngm_macfilter_direct_hookid *mdi)
static void macfilter_reset_stats(macfilter_p mfp)
static struct ng_type typestruct
static int macfilter_ether_input(hook_p hook, macfilter_p mfp, struct mbuf *m, hook_p *next_hook)
static const struct ng_parse_struct_field ng_macfilter_macs_fields[]
static const struct ng_parse_type ng_macfilter_hooks_type
static const struct ng_parse_array_info ng_macfilter_macs_array_info
static const struct ng_parse_type ng_macfilter_hook_type
#define M_NETGRAPH_MACFILTER
static ng_shutdown_t ng_macfilter_shutdown
static const struct ng_parse_struct_field ng_macfilter_mac_fields[]
static const struct ng_parse_type ng_macfilter_macs_type
static const struct ng_parse_type ng_macfilter_mac_type
static void macfilter_reset(macfilter_p mfp)
static int macfilter_get_macs_count(const struct ng_parse_type *type, const u_char *start, const u_char *buf)
static ng_constructor_t ng_macfilter_constructor
static int macfilter_mactable_resize(macfilter_p mfp)
static int macfilter_mactable_change(macfilter_p mfp, u_char *ether, int hookid)
static const struct ng_parse_struct_field macfilter_direct_ndx_fields[]
static const struct ng_parse_type ng_macfilter_direct_type
static const struct ng_parse_struct_field ng_macfilter_hooks_fields[]
#define NG_MACFILTER_NODE_TYPE
#define NGM_MACFILTER_MAC_FIELDS
#define NGM_MACFILTER_HOOK_FIELDS
#define NGM_MACFILTER_DIRECT_FIELDS
#define NGM_MACFILTER_HOOKS_FIELDS
#define NG_MACFILTER_HOOK_DEFAULT
#define NG_MACFILTER_HOOK_ETHER
#define NGM_MACFILTER_MACS_FIELDS
#define NGM_MACFILTER_COOKIE
#define NGM_MACFILTER_DIRECT_NDX_FIELDS
#define NG_MACFILTER_HOOK_DEFAULT_ID
@ NGM_MACFILTER_GET_HOOKS
@ NGM_MACFILTER_DIRECT_HOOKID
@ NGM_MACFILTER_GETCLR_MACS
#define NG_MKRESPONSE(rsp, msg, len, how)
const struct ng_parse_type ng_parse_array_type
const struct ng_parse_type ng_parse_struct_type
struct ng_mesg::ng_msghdr header
u_char ether[ETHER_ADDR_LEN]
u_char ether[ETHER_ADDR_LEN]
u_char hookname[NG_HOOKSIZ]
u_char hookname[NG_HOOKSIZ]
struct ngm_macfilter_hook hooks[]
struct ngm_macfilter_mac macs[]