2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2002 Ericsson Research & Pekka Nikander
5 * Copyright (c) 2020 Nick Hibma <n_hibma@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * MACFILTER NETGRAPH NODE TYPE
34 * This node type routes packets from the ether hook to either the default hook
35 * if sender MAC address is not in the MAC table, or out over the specified
38 * Other node types can then be used to apply specific processing to the
39 * packets on each hook.
41 * If compiled with NG_MACFILTER_DEBUG the flow and resizing of the MAC table
42 * are logged to the console.
44 * If compiled with NG_MACFILTER_DEBUG_RECVDATA every packet handled is logged
48 #include <sys/param.h>
49 #include <sys/ctype.h>
50 #include <sys/systm.h>
54 #include <sys/mutex.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/socket.h>
60 #include <net/ethernet.h>
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include <netgraph/ng_parse.h>
66 #include "ng_macfilter.h"
68 #ifdef NG_SEPARATE_MALLOC
69 MALLOC_DEFINE(M_NETGRAPH_MACFILTER, "netgraph_macfilter", "netgraph macfilter node ");
71 #define M_NETGRAPH_MACFILTER M_NETGRAPH
74 #define MACTABLE_BLOCKSIZE 128 /* block size for incrementing table */
76 #ifdef NG_MACFILTER_DEBUG
77 #define MACFILTER_DEBUG(fmt, ...) printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__)
79 #define MACFILTER_DEBUG(fmt, ...)
81 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
82 #define MAC_S_ARGS(v) (v)[0], (v)[1], (v)[2], (v)[3], (v)[4], (v)[5]
85 * Parse type for struct ngm_macfilter_direct
88 static const struct ng_parse_struct_field macfilter_direct_fields[]
89 = NGM_MACFILTER_DIRECT_FIELDS;
90 static const struct ng_parse_type ng_macfilter_direct_type = {
91 &ng_parse_struct_type,
92 &macfilter_direct_fields
96 * Parse type for struct ngm_macfilter_direct_hookid.
99 static const struct ng_parse_struct_field macfilter_direct_ndx_fields[]
100 = NGM_MACFILTER_DIRECT_NDX_FIELDS;
101 static const struct ng_parse_type ng_macfilter_direct_hookid_type = {
102 &ng_parse_struct_type,
103 &macfilter_direct_ndx_fields
107 * Parse types for struct ngm_macfilter_get_macs.
110 macfilter_get_macs_count(const struct ng_parse_type *type,
111 const u_char *start, const u_char *buf)
113 const struct ngm_macfilter_macs *const ngm_macs =
114 (const struct ngm_macfilter_macs *)(buf - OFFSETOF(struct ngm_macfilter_macs, macs));
118 static const struct ng_parse_struct_field ng_macfilter_mac_fields[]
119 = NGM_MACFILTER_MAC_FIELDS;
120 static const struct ng_parse_type ng_macfilter_mac_type = {
121 &ng_parse_struct_type,
122 ng_macfilter_mac_fields,
124 static const struct ng_parse_array_info ng_macfilter_macs_array_info = {
125 &ng_macfilter_mac_type,
126 macfilter_get_macs_count
128 static const struct ng_parse_type ng_macfilter_macs_array_type = {
129 &ng_parse_array_type,
130 &ng_macfilter_macs_array_info
132 static const struct ng_parse_struct_field ng_macfilter_macs_fields[]
133 = NGM_MACFILTER_MACS_FIELDS;
134 static const struct ng_parse_type ng_macfilter_macs_type = {
135 &ng_parse_struct_type,
136 &ng_macfilter_macs_fields
140 * Parse types for struct ngm_macfilter_get_hooks.
143 macfilter_get_upper_hook_count(const struct ng_parse_type *type,
144 const u_char *start, const u_char *buf)
146 const struct ngm_macfilter_hooks *const ngm_hooks =
147 (const struct ngm_macfilter_hooks *)(buf - OFFSETOF(struct ngm_macfilter_hooks, hooks));
149 MACFILTER_DEBUG("buf %p, ngm_hooks %p, n %d", buf, ngm_hooks, ngm_hooks->n);
154 static const struct ng_parse_struct_field ng_macfilter_hook_fields[]
155 = NGM_MACFILTER_HOOK_FIELDS;
156 static const struct ng_parse_type ng_macfilter_hook_type = {
157 &ng_parse_struct_type,
158 ng_macfilter_hook_fields,
160 static const struct ng_parse_array_info ng_macfilter_hooks_array_info = {
161 &ng_macfilter_hook_type,
162 macfilter_get_upper_hook_count
164 static const struct ng_parse_type ng_macfilter_hooks_array_type = {
165 &ng_parse_array_type,
166 &ng_macfilter_hooks_array_info
168 static const struct ng_parse_struct_field ng_macfilter_hooks_fields[]
169 = NGM_MACFILTER_HOOKS_FIELDS;
170 static const struct ng_parse_type ng_macfilter_hooks_type = {
171 &ng_parse_struct_type,
172 &ng_macfilter_hooks_fields
176 * List of commands and how to convert arguments to/from ASCII
178 static const struct ng_cmdlist ng_macfilter_cmdlist[] = {
180 NGM_MACFILTER_COOKIE,
187 NGM_MACFILTER_COOKIE,
188 NGM_MACFILTER_DIRECT,
190 &ng_macfilter_direct_type,
194 NGM_MACFILTER_COOKIE,
195 NGM_MACFILTER_DIRECT_HOOKID,
197 &ng_macfilter_direct_hookid_type,
201 NGM_MACFILTER_COOKIE,
202 NGM_MACFILTER_GET_MACS,
205 &ng_macfilter_macs_type
208 NGM_MACFILTER_COOKIE,
209 NGM_MACFILTER_GETCLR_MACS,
212 &ng_macfilter_macs_type
215 NGM_MACFILTER_COOKIE,
216 NGM_MACFILTER_CLR_MACS,
222 NGM_MACFILTER_COOKIE,
223 NGM_MACFILTER_GET_HOOKS,
226 &ng_macfilter_hooks_type
232 * Netgraph node type descriptor
234 static ng_constructor_t ng_macfilter_constructor;
235 static ng_rcvmsg_t ng_macfilter_rcvmsg;
236 static ng_shutdown_t ng_macfilter_shutdown;
237 static ng_newhook_t ng_macfilter_newhook;
238 static ng_rcvdata_t ng_macfilter_rcvdata;
239 static ng_disconnect_t ng_macfilter_disconnect;
241 static struct ng_type typestruct = {
242 .version = NG_ABI_VERSION,
243 .name = NG_MACFILTER_NODE_TYPE,
244 .constructor = ng_macfilter_constructor,
245 .rcvmsg = ng_macfilter_rcvmsg,
246 .shutdown = ng_macfilter_shutdown,
247 .newhook = ng_macfilter_newhook,
248 .rcvdata = ng_macfilter_rcvdata,
249 .disconnect = ng_macfilter_disconnect,
250 .cmdlist = ng_macfilter_cmdlist
252 NETGRAPH_INIT(macfilter, &typestruct);
255 * Per MAC address info: the hook where to send to, the address
256 * Note: We use the same struct as in the netgraph message, so we can bcopy the
259 typedef struct ngm_macfilter_mac *mf_mac_p;
265 hook_p mf_ether_hook; /* Ethernet hook */
267 hook_p *mf_upper; /* Upper hooks */
268 u_int mf_upper_cnt; /* Allocated # of upper slots */
270 struct mtx mtx; /* Mutex for MACs table */
271 mf_mac_p mf_macs; /* MAC info: dynamically allocated */
272 u_int mf_mac_allocated;/* Allocated # of MAC slots */
273 u_int mf_mac_used; /* Used # of MAC slots */
277 * Resize the MAC table to accommodate at least mfp->mf_mac_used + 1 entries.
279 * Note: mtx already held
282 macfilter_mactable_resize(macfilter_p mfp)
286 int n = mfp->mf_mac_allocated;
287 if (mfp->mf_mac_used < 2*MACTABLE_BLOCKSIZE-1) /* minimum size */
288 n = 2*MACTABLE_BLOCKSIZE-1;
289 else if (mfp->mf_mac_used + 2*MACTABLE_BLOCKSIZE < mfp->mf_mac_allocated) /* reduce size */
290 n = mfp->mf_mac_allocated - MACTABLE_BLOCKSIZE;
291 else if (mfp->mf_mac_used == mfp->mf_mac_allocated) /* increase size */
292 n = mfp->mf_mac_allocated + MACTABLE_BLOCKSIZE;
294 if (n != mfp->mf_mac_allocated) {
295 MACFILTER_DEBUG("used=%d allocated=%d->%d",
296 mfp->mf_mac_used, mfp->mf_mac_allocated, n);
298 mf_mac_p mfp_new = realloc(mfp->mf_macs,
299 sizeof(mfp->mf_macs[0])*n,
300 M_NETGRAPH, M_NOWAIT | M_ZERO);
301 if (mfp_new == NULL) {
304 mfp->mf_macs = mfp_new;
305 mfp->mf_mac_allocated = n;
313 * Resets the macfilter to pass all received packets
314 * to the default hook.
316 * Note: mtx already held
319 macfilter_reset(macfilter_p mfp)
321 mfp->mf_mac_used = 0;
323 macfilter_mactable_resize(mfp);
327 * Resets the counts for each MAC address.
329 * Note: mtx already held
332 macfilter_reset_stats(macfilter_p mfp)
336 for (i = 0; i < mfp->mf_mac_used; i++) {
337 mf_mac_p p = &mfp->mf_macs[i];
338 p->packets_in = p->packets_out = 0;
339 p->bytes_in = p->bytes_out = 0;
344 * Count the number of matching macs routed to this hook.
346 * Note: mtx already held
349 macfilter_mac_count(macfilter_p mfp, int hookid)
354 for (i = 0; i < mfp->mf_mac_used; i++)
355 if (mfp->mf_macs[i].hookid == hookid)
362 * Find a MAC address in the mac table.
364 * Returns 0 on failure with *ri set to index before which to insert a new
365 * element. Or returns 1 on success with *ri set to the index of the element
368 * Note: mtx already held.
371 macfilter_find_mac(macfilter_p mfp, const u_char *ether, u_int *ri)
373 mf_mac_p mf_macs = mfp->mf_macs;
376 u_int range = mfp->mf_mac_used;
378 u_int middle = base + (range >> 1); /* middle */
379 int d = bcmp(ether, mf_macs[middle].ether, ETHER_ADDR_LEN);
380 if (d == 0) { /* match */
383 } else if (d > 0) { /* move right */
384 range -= middle - base + 1;
386 } else { /* move left */
387 range = middle - base;
396 * Change the upper hook for the given MAC address. If the hook id is zero (the
397 * default hook), the MAC address is removed from the table. Otherwise it is
398 * inserted to the table at a proper location, and the id of the hook is
401 * Note: mtx already held.
404 macfilter_mactable_change(macfilter_p mfp, u_char *ether, int hookid)
407 int found = macfilter_find_mac(mfp, ether, &i);
409 mf_mac_p mf_macs = mfp->mf_macs;
411 MACFILTER_DEBUG("ether=" MAC_FMT " found=%d i=%d ether=" MAC_FMT " hookid=%d->%d used=%d allocated=%d",
412 MAC_S_ARGS(ether), found, i, MAC_S_ARGS(mf_macs[i].ether),
413 (found? mf_macs[i].hookid:NG_MACFILTER_HOOK_DEFAULT_ID), hookid,
414 mfp->mf_mac_used, mfp->mf_mac_allocated);
417 if (hookid == NG_MACFILTER_HOOK_DEFAULT_ID) { /* drop */
420 size_t len = (mfp->mf_mac_used - i) * sizeof(mf_macs[0]);
422 bcopy(&mf_macs[i+1], &mf_macs[i], len);
424 macfilter_mactable_resize(mfp);
425 } else { /* modify */
426 mf_macs[i].hookid = hookid;
429 if (hookid == NG_MACFILTER_HOOK_DEFAULT_ID) { /* not found */
430 /* not present and not inserted */
433 if (macfilter_mactable_resize(mfp) == -1) {
436 mf_macs = mfp->mf_macs; /* reassign; might have moved during resize */
438 /* make room for new entry, unless appending */
439 size_t len = (mfp->mf_mac_used - i) * sizeof(mf_macs[0]);
441 bcopy(&mf_macs[i], &mf_macs[i+1], len);
443 mf_macs[i].hookid = hookid;
444 bcopy(ether, mf_macs[i].ether, ETHER_ADDR_LEN);
455 macfilter_mactable_remove_by_hookid(macfilter_p mfp, int hookid)
459 for (i = 0, j = 0; i < mfp->mf_mac_used; i++) {
460 if (mfp->mf_macs[i].hookid != hookid) {
462 bcopy(&mfp->mf_macs[i], &mfp->mf_macs[j], sizeof(mfp->mf_macs[0]));
468 mfp->mf_mac_used = j;
469 macfilter_mactable_resize(mfp);
475 macfilter_find_hook(macfilter_p mfp, const char *hookname)
479 for (hookid = 0; hookid < mfp->mf_upper_cnt; hookid++) {
480 if (mfp->mf_upper[hookid]) {
481 if (strncmp(NG_HOOK_NAME(mfp->mf_upper[hookid]),
482 hookname, NG_HOOKSIZ) == 0) {
492 macfilter_direct(macfilter_p mfp, struct ngm_macfilter_direct *md)
494 MACFILTER_DEBUG("ether=" MAC_FMT " hook=%s",
495 MAC_S_ARGS(md->ether), md->hookname);
497 int hookid = macfilter_find_hook(mfp, md->hookname);
501 return macfilter_mactable_change(mfp, md->ether, hookid);
505 macfilter_direct_hookid(macfilter_p mfp, struct ngm_macfilter_direct_hookid *mdi)
507 MACFILTER_DEBUG("ether=" MAC_FMT " hookid=%d",
508 MAC_S_ARGS(mdi->ether), mdi->hookid);
510 if (mdi->hookid >= mfp->mf_upper_cnt)
512 else if (mfp->mf_upper[mdi->hookid] == NULL)
515 return macfilter_mactable_change(mfp, mdi->ether, mdi->hookid);
523 * Pass packets received from any upper hook to
527 macfilter_ether_output(hook_p hook, macfilter_p mfp, struct mbuf *m, hook_p *next_hook)
529 struct ether_header *ether_header = mtod(m, struct ether_header *);
530 u_char *ether = ether_header->ether_dhost;
532 *next_hook = mfp->mf_ether_hook;
537 int found = macfilter_find_mac(mfp, ether, &i);
539 mf_mac_p mf_macs = mfp->mf_macs;
541 mf_macs[i].packets_out++;
542 if (m->m_len > ETHER_HDR_LEN)
543 mf_macs[i].bytes_out += m->m_len - ETHER_HDR_LEN;
545 #ifdef NG_MACFILTER_DEBUG_RECVDATA
546 MACFILTER_DEBUG("ether=" MAC_FMT " len=%db->%lldb: bytes: %s -> %s",
547 MAC_S_ARGS(ether), m->m_len - ETHER_HDR_LEN, mf_macs[i].bytes_out,
548 NG_HOOK_NAME(hook), NG_HOOK_NAME(*next_hook));
551 #ifdef NG_MACFILTER_DEBUG_RECVDATA
552 MACFILTER_DEBUG("ether=" MAC_FMT " len=%db->?b: bytes: %s->%s",
553 MAC_S_ARGS(ether), m->m_len - ETHER_HDR_LEN,
554 NG_HOOK_NAME(hook), NG_HOOK_NAME(*next_hook));
558 mtx_unlock(&mfp->mtx);
564 * Search for the right upper hook, based on the source ethernet
565 * address. If not found, pass to the default upper hook.
568 macfilter_ether_input(hook_p hook, macfilter_p mfp, struct mbuf *m, hook_p *next_hook)
570 struct ether_header *ether_header = mtod(m, struct ether_header *);
571 u_char *ether = ether_header->ether_shost;
572 int hookid = NG_MACFILTER_HOOK_DEFAULT_ID;
577 int found = macfilter_find_mac(mfp, ether, &i);
579 mf_mac_p mf_macs = mfp->mf_macs;
581 mf_macs[i].packets_in++;
582 if (m->m_len > ETHER_HDR_LEN)
583 mf_macs[i].bytes_in += m->m_len - ETHER_HDR_LEN;
585 hookid = mf_macs[i].hookid;
587 #ifdef NG_MACFILTER_DEBUG_RECVDATA
588 MACFILTER_DEBUG("ether=" MAC_FMT " len=%db->%lldb: bytes: %s->%s",
589 MAC_S_ARGS(ether), m->m_len - ETHER_HDR_LEN, mf_macs[i].bytes_in,
590 NG_HOOK_NAME(hook), NG_HOOK_NAME(*next_hook));
593 #ifdef NG_MACFILTER_DEBUG_RECVDATA
594 MACFILTER_DEBUG("ether=" MAC_FMT " len=%db->?b: bytes: %s->%s",
595 MAC_S_ARGS(ether), m->m_len - ETHER_HDR_LEN,
596 NG_HOOK_NAME(hook), NG_HOOK_NAME(*next_hook));
600 if (hookid >= mfp->mf_upper_cnt)
603 *next_hook = mfp->mf_upper[hookid];
605 mtx_unlock(&mfp->mtx);
611 * ======================================================================
613 * ======================================================================
617 * See basic netgraph code for comments on the individual functions.
621 ng_macfilter_constructor(node_p node)
623 macfilter_p mfp = malloc(sizeof(*mfp), M_NETGRAPH, M_NOWAIT | M_ZERO);
627 int error = macfilter_mactable_resize(mfp);
631 NG_NODE_SET_PRIVATE(node, mfp);
633 mtx_init(&mfp->mtx, "Macfilter table", NULL, MTX_DEF);
639 ng_macfilter_newhook(node_p node, hook_p hook, const char *hookname)
641 const macfilter_p mfp = NG_NODE_PRIVATE(node);
643 MACFILTER_DEBUG("%s", hookname);
645 if (strcmp(hookname, NG_MACFILTER_HOOK_ETHER) == 0) {
646 mfp->mf_ether_hook = hook;
649 if (strcmp(hookname, NG_MACFILTER_HOOK_DEFAULT) == 0) {
650 hookid = NG_MACFILTER_HOOK_DEFAULT_ID;
652 for (hookid = 1; hookid < mfp->mf_upper_cnt; hookid++)
653 if (mfp->mf_upper[hookid] == NULL)
657 if (hookid >= mfp->mf_upper_cnt) {
658 MACFILTER_DEBUG("upper cnt %d -> %d", mfp->mf_upper_cnt, hookid + 1);
660 mfp->mf_upper_cnt = hookid + 1;
661 mfp->mf_upper = realloc(mfp->mf_upper,
662 sizeof(mfp->mf_upper[0])*mfp->mf_upper_cnt,
663 M_NETGRAPH, M_NOWAIT | M_ZERO);
666 mfp->mf_upper[hookid] = hook;
673 ng_macfilter_rcvmsg(node_p node, item_p item, hook_p lasthook)
675 const macfilter_p mfp = NG_NODE_PRIVATE(node);
676 struct ng_mesg *resp = NULL;
679 struct ngm_macfilter_macs *ngm_macs;
680 struct ngm_macfilter_hooks *ngm_hooks;
681 struct ngm_macfilter_direct *md;
682 struct ngm_macfilter_direct_hookid *mdi;
687 NGI_GET_MSG(item, msg);
691 switch (msg->header.typecookie) {
692 case NGM_MACFILTER_COOKIE:
693 switch (msg->header.cmd) {
695 case NGM_MACFILTER_RESET:
696 macfilter_reset(mfp);
699 case NGM_MACFILTER_DIRECT:
700 if (msg->header.arglen != sizeof(struct ngm_macfilter_direct)) {
701 MACFILTER_DEBUG("direct: wrong type length (%d, expected %zu)",
702 msg->header.arglen, sizeof(struct ngm_macfilter_direct));
706 md = (struct ngm_macfilter_direct *)msg->data;
707 error = macfilter_direct(mfp, md);
709 case NGM_MACFILTER_DIRECT_HOOKID:
710 if (msg->header.arglen != sizeof(struct ngm_macfilter_direct_hookid)) {
711 MACFILTER_DEBUG("direct hookid: wrong type length (%d, expected %zu)",
712 msg->header.arglen, sizeof(struct ngm_macfilter_direct));
716 mdi = (struct ngm_macfilter_direct_hookid *)msg->data;
717 error = macfilter_direct_hookid(mfp, mdi);
720 case NGM_MACFILTER_GET_MACS:
721 case NGM_MACFILTER_GETCLR_MACS:
722 n = mfp->mf_mac_used;
723 resplen = sizeof(struct ngm_macfilter_macs) + n * sizeof(struct ngm_macfilter_mac);
724 NG_MKRESPONSE(resp, msg, resplen, M_NOWAIT);
729 ngm_macs = (struct ngm_macfilter_macs *)resp->data;
731 bcopy(mfp->mf_macs, &ngm_macs->macs[0], n * sizeof(struct ngm_macfilter_mac));
733 if (msg->header.cmd == NGM_MACFILTER_GETCLR_MACS)
734 macfilter_reset_stats(mfp);
737 case NGM_MACFILTER_CLR_MACS:
738 macfilter_reset_stats(mfp);
741 case NGM_MACFILTER_GET_HOOKS:
742 for (hookid = 0; hookid < mfp->mf_upper_cnt; hookid++)
743 if (mfp->mf_upper[hookid] != NULL)
745 resplen = sizeof(struct ngm_macfilter_hooks) + n * sizeof(struct ngm_macfilter_hook);
746 NG_MKRESPONSE(resp, msg, resplen, M_NOWAIT | M_ZERO);
752 ngm_hooks = (struct ngm_macfilter_hooks *)resp->data;
754 for (hookid = 0; hookid < mfp->mf_upper_cnt; hookid++) {
755 if (mfp->mf_upper[hookid] != NULL) {
756 struct ngm_macfilter_hook *ngm_hook = &ngm_hooks->hooks[i++];
757 strlcpy(ngm_hook->hookname,
758 NG_HOOK_NAME(mfp->mf_upper[hookid]),
760 ngm_hook->hookid = hookid;
761 ngm_hook->maccnt = macfilter_mac_count(mfp, hookid);
767 error = EINVAL; /* unknown command */
773 error = EINVAL; /* unknown cookie type */
777 mtx_unlock(&mfp->mtx);
779 NG_RESPOND_MSG(error, node, item, resp);
786 ng_macfilter_rcvdata(hook_p hook, item_p item)
788 const macfilter_p mfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
790 hook_p next_hook = NULL;
793 m = NGI_M(item); /* 'item' still owns it. We are peeking */
794 MACFILTER_DEBUG("%s", NG_HOOK_NAME(hook));
796 if (hook == mfp->mf_ether_hook)
797 error = macfilter_ether_input(hook, mfp, m, &next_hook);
798 else if (mfp->mf_ether_hook != NULL)
799 error = macfilter_ether_output(hook, mfp, m, &next_hook);
801 if (next_hook == NULL) {
806 NG_FWD_ITEM_HOOK(error, item, next_hook);
812 ng_macfilter_disconnect(hook_p hook)
814 const macfilter_p mfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
818 if (mfp->mf_ether_hook == hook) {
819 mfp->mf_ether_hook = NULL;
821 MACFILTER_DEBUG("%s", NG_HOOK_NAME(hook));
825 for (hookid = 0; hookid < mfp->mf_upper_cnt; hookid++) {
826 if (mfp->mf_upper[hookid] == hook) {
827 mfp->mf_upper[hookid] = NULL;
829 #ifndef NG_MACFILTER_DEBUG
830 macfilter_mactable_remove_by_hookid(mfp, hookid);
832 int cnt = macfilter_mactable_remove_by_hookid(mfp, hookid);
834 MACFILTER_DEBUG("%s: removed %d MACs", NG_HOOK_NAME(hook), cnt);
840 if (hookid == mfp->mf_upper_cnt - 1) {
841 /* Reduce the size of the array when the last element was removed */
842 for (--hookid; hookid >= 0 && mfp->mf_upper[hookid] == NULL; hookid--)
845 MACFILTER_DEBUG("upper cnt %d -> %d", mfp->mf_upper_cnt, hookid + 1);
846 mfp->mf_upper_cnt = hookid + 1;
847 mfp->mf_upper = realloc(mfp->mf_upper,
848 sizeof(mfp->mf_upper[0])*mfp->mf_upper_cnt,
849 M_NETGRAPH, M_NOWAIT | M_ZERO);
853 mtx_unlock(&mfp->mtx);
855 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
856 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
857 ng_rmnode_self(NG_HOOK_NODE(hook));
864 ng_macfilter_shutdown(node_p node)
866 const macfilter_p mfp = NG_NODE_PRIVATE(node);
868 mtx_destroy(&mfp->mtx);
869 free(mfp->mf_upper, M_NETGRAPH);
870 free(mfp->mf_macs, M_NETGRAPH);
871 free(mfp, M_NETGRAPH);