xref: /dragonfly/sys/netgraph7/tee/ng_tee.c (revision b5523eac)
10147868eSNuno Antunes 
20147868eSNuno Antunes /*
30147868eSNuno Antunes  * ng_tee.c
40147868eSNuno Antunes  */
50147868eSNuno Antunes 
60147868eSNuno Antunes /*-
70147868eSNuno Antunes  * Copyright (c) 1996-1999 Whistle Communications, Inc.
80147868eSNuno Antunes  * All rights reserved.
90147868eSNuno Antunes  *
100147868eSNuno Antunes  * Subject to the following obligations and disclaimer of warranty, use and
110147868eSNuno Antunes  * redistribution of this software, in source or object code forms, with or
120147868eSNuno Antunes  * without modifications are expressly permitted by Whistle Communications;
130147868eSNuno Antunes  * provided, however, that:
140147868eSNuno Antunes  * 1. Any and all reproductions of the source or object code must include the
150147868eSNuno Antunes  *    copyright notice above and the following disclaimer of warranties; and
160147868eSNuno Antunes  * 2. No rights are granted, in any manner or form, to use Whistle
170147868eSNuno Antunes  *    Communications, Inc. trademarks, including the mark "WHISTLE
180147868eSNuno Antunes  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
190147868eSNuno Antunes  *    such appears in the above copyright notice or in the software.
200147868eSNuno Antunes  *
210147868eSNuno Antunes  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
220147868eSNuno Antunes  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
230147868eSNuno Antunes  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
240147868eSNuno Antunes  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
250147868eSNuno Antunes  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
260147868eSNuno Antunes  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
270147868eSNuno Antunes  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
280147868eSNuno Antunes  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
290147868eSNuno Antunes  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
300147868eSNuno Antunes  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
310147868eSNuno Antunes  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
320147868eSNuno Antunes  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
330147868eSNuno Antunes  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
340147868eSNuno Antunes  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
350147868eSNuno Antunes  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
360147868eSNuno Antunes  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
370147868eSNuno Antunes  * OF SUCH DAMAGE.
380147868eSNuno Antunes  *
390147868eSNuno Antunes  * Author: Julian Elischer <julian@freebsd.org>
400147868eSNuno Antunes  *
410147868eSNuno Antunes  * $FreeBSD: src/sys/netgraph/ng_tee.c,v 1.35 2008/02/24 10:13:32 mav Exp $
420147868eSNuno Antunes  * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $
430147868eSNuno Antunes  */
440147868eSNuno Antunes 
450147868eSNuno Antunes /*
460147868eSNuno Antunes  * This node is like the tee(1) command and is useful for ``snooping.''
470147868eSNuno Antunes  * It has 4 hooks: left, right, left2right, and right2left. Data
480147868eSNuno Antunes  * entering from the right is passed to the left and duplicated on
490147868eSNuno Antunes  * right2left, and data entering from the left is passed to the right
500147868eSNuno Antunes  * and duplicated on left2right. Data entering from left2right is
510147868eSNuno Antunes  * sent to left, and data from right2left to right.
520147868eSNuno Antunes  */
530147868eSNuno Antunes 
540147868eSNuno Antunes #include <sys/param.h>
550147868eSNuno Antunes #include <sys/systm.h>
560147868eSNuno Antunes #include <sys/errno.h>
570147868eSNuno Antunes #include <sys/kernel.h>
580147868eSNuno Antunes #include <sys/malloc.h>
590147868eSNuno Antunes #include <sys/mbuf.h>
600147868eSNuno Antunes #include <netgraph7/ng_message.h>
610147868eSNuno Antunes #include <netgraph7/netgraph.h>
620147868eSNuno Antunes #include <netgraph7/ng_parse.h>
630147868eSNuno Antunes #include "ng_tee.h"
640147868eSNuno Antunes 
650147868eSNuno Antunes /* Per hook info */
660147868eSNuno Antunes struct hookinfo {
670147868eSNuno Antunes 	hook_p			hook;
680147868eSNuno Antunes 	struct hookinfo		*dest, *dup;
690147868eSNuno Antunes 	struct ng_tee_hookstat	stats;
700147868eSNuno Antunes };
710147868eSNuno Antunes typedef struct hookinfo *hi_p;
720147868eSNuno Antunes 
730147868eSNuno Antunes /* Per node info */
740147868eSNuno Antunes struct privdata {
750147868eSNuno Antunes 	struct hookinfo		left;
760147868eSNuno Antunes 	struct hookinfo		right;
770147868eSNuno Antunes 	struct hookinfo		left2right;
780147868eSNuno Antunes 	struct hookinfo		right2left;
790147868eSNuno Antunes };
800147868eSNuno Antunes typedef struct privdata *sc_p;
810147868eSNuno Antunes 
820147868eSNuno Antunes /* Netgraph methods */
830147868eSNuno Antunes static ng_constructor_t	ng_tee_constructor;
840147868eSNuno Antunes static ng_rcvmsg_t	ng_tee_rcvmsg;
850147868eSNuno Antunes static ng_close_t	ng_tee_close;
860147868eSNuno Antunes static ng_shutdown_t	ng_tee_shutdown;
870147868eSNuno Antunes static ng_newhook_t	ng_tee_newhook;
880147868eSNuno Antunes static ng_rcvdata_t	ng_tee_rcvdata;
890147868eSNuno Antunes static ng_disconnect_t	ng_tee_disconnect;
900147868eSNuno Antunes 
910147868eSNuno Antunes /* Parse type for struct ng_tee_hookstat */
920147868eSNuno Antunes static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[]
930147868eSNuno Antunes 	= NG_TEE_HOOKSTAT_INFO;
940147868eSNuno Antunes static const struct ng_parse_type ng_tee_hookstat_type = {
950147868eSNuno Antunes 	&ng_parse_struct_type,
960147868eSNuno Antunes 	&ng_tee_hookstat_type_fields
970147868eSNuno Antunes };
980147868eSNuno Antunes 
990147868eSNuno Antunes /* Parse type for struct ng_tee_stats */
1000147868eSNuno Antunes static const struct ng_parse_struct_field ng_tee_stats_type_fields[]
1010147868eSNuno Antunes 	= NG_TEE_STATS_INFO(&ng_tee_hookstat_type);
1020147868eSNuno Antunes static const struct ng_parse_type ng_tee_stats_type = {
1030147868eSNuno Antunes 	&ng_parse_struct_type,
1040147868eSNuno Antunes 	&ng_tee_stats_type_fields
1050147868eSNuno Antunes };
1060147868eSNuno Antunes 
1070147868eSNuno Antunes /* List of commands and how to convert arguments to/from ASCII */
1080147868eSNuno Antunes static const struct ng_cmdlist ng_tee_cmds[] = {
1090147868eSNuno Antunes 	{
1100147868eSNuno Antunes 	  NGM_TEE_COOKIE,
1110147868eSNuno Antunes 	  NGM_TEE_GET_STATS,
1120147868eSNuno Antunes 	  "getstats",
1130147868eSNuno Antunes 	  NULL,
1140147868eSNuno Antunes 	  &ng_tee_stats_type
1150147868eSNuno Antunes 	},
1160147868eSNuno Antunes 	{
1170147868eSNuno Antunes 	  NGM_TEE_COOKIE,
1180147868eSNuno Antunes 	  NGM_TEE_CLR_STATS,
1190147868eSNuno Antunes 	  "clrstats",
1200147868eSNuno Antunes 	  NULL,
1210147868eSNuno Antunes 	  NULL
1220147868eSNuno Antunes 	},
1230147868eSNuno Antunes 	{
1240147868eSNuno Antunes 	  NGM_TEE_COOKIE,
1250147868eSNuno Antunes 	  NGM_TEE_GETCLR_STATS,
1260147868eSNuno Antunes 	  "getclrstats",
1270147868eSNuno Antunes 	  NULL,
1280147868eSNuno Antunes 	  &ng_tee_stats_type
1290147868eSNuno Antunes 	},
1300147868eSNuno Antunes 	{ 0 }
1310147868eSNuno Antunes };
1320147868eSNuno Antunes 
1330147868eSNuno Antunes /* Netgraph type descriptor */
1340147868eSNuno Antunes static struct ng_type ng_tee_typestruct = {
1350147868eSNuno Antunes 	.version =	NG_ABI_VERSION,
1360147868eSNuno Antunes 	.name =		NG_TEE_NODE_TYPE,
1370147868eSNuno Antunes 	.constructor =  ng_tee_constructor,
1380147868eSNuno Antunes 	.rcvmsg =	ng_tee_rcvmsg,
1390147868eSNuno Antunes 	.close =	ng_tee_close,
1400147868eSNuno Antunes 	.shutdown =	ng_tee_shutdown,
1410147868eSNuno Antunes 	.newhook =	ng_tee_newhook,
1420147868eSNuno Antunes 	.rcvdata =	ng_tee_rcvdata,
1430147868eSNuno Antunes 	.disconnect =	ng_tee_disconnect,
1440147868eSNuno Antunes 	.cmdlist =	ng_tee_cmds,
1450147868eSNuno Antunes };
1460147868eSNuno Antunes NETGRAPH_INIT(tee, &ng_tee_typestruct);
1470147868eSNuno Antunes 
1480147868eSNuno Antunes /*
1490147868eSNuno Antunes  * Node constructor
1500147868eSNuno Antunes  */
1510147868eSNuno Antunes static int
ng_tee_constructor(node_p node)1520147868eSNuno Antunes ng_tee_constructor(node_p node)
1530147868eSNuno Antunes {
1540147868eSNuno Antunes 	sc_p privdata;
1550147868eSNuno Antunes 
1560147868eSNuno Antunes 	privdata = kmalloc(sizeof(*privdata), M_NETGRAPH,
1570147868eSNuno Antunes 			   M_WAITOK | M_NULLOK | M_ZERO);
1580147868eSNuno Antunes 	if (privdata == NULL)
1590147868eSNuno Antunes 		return (ENOMEM);
1600147868eSNuno Antunes 
1610147868eSNuno Antunes 	NG_NODE_SET_PRIVATE(node, privdata);
1620147868eSNuno Antunes 	return (0);
1630147868eSNuno Antunes }
1640147868eSNuno Antunes 
1650147868eSNuno Antunes /*
1660147868eSNuno Antunes  * Add a hook
1670147868eSNuno Antunes  */
1680147868eSNuno Antunes static int
ng_tee_newhook(node_p node,hook_p hook,const char * name)1690147868eSNuno Antunes ng_tee_newhook(node_p node, hook_p hook, const char *name)
1700147868eSNuno Antunes {
1710147868eSNuno Antunes 	sc_p	privdata = NG_NODE_PRIVATE(node);
1720147868eSNuno Antunes 	hi_p	hinfo;
1730147868eSNuno Antunes 
1740147868eSNuno Antunes 	/* Precalculate internal pathes. */
1750147868eSNuno Antunes 	if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
1760147868eSNuno Antunes 		hinfo = &privdata->right;
1770147868eSNuno Antunes 		if (privdata->left.dest)
1780147868eSNuno Antunes 			privdata->left.dup = privdata->left.dest;
1790147868eSNuno Antunes 		privdata->left.dest = hinfo;
1800147868eSNuno Antunes 		privdata->right2left.dest = hinfo;
1810147868eSNuno Antunes 	} else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
1820147868eSNuno Antunes 		hinfo = &privdata->left;
1830147868eSNuno Antunes 		if (privdata->right.dest)
1840147868eSNuno Antunes 			privdata->right.dup = privdata->right.dest;
1850147868eSNuno Antunes 		privdata->right.dest = hinfo;
1860147868eSNuno Antunes 		privdata->left2right.dest = hinfo;
1870147868eSNuno Antunes 	} else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
1880147868eSNuno Antunes 		hinfo = &privdata->right2left;
1890147868eSNuno Antunes 		if (privdata->right.dest)
1900147868eSNuno Antunes 			privdata->right.dup = hinfo;
1910147868eSNuno Antunes 		else
1920147868eSNuno Antunes 			privdata->right.dest = hinfo;
1930147868eSNuno Antunes 	} else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
1940147868eSNuno Antunes 		hinfo = &privdata->left2right;
1950147868eSNuno Antunes 		if (privdata->left.dest)
1960147868eSNuno Antunes 			privdata->left.dup = hinfo;
1970147868eSNuno Antunes 		else
1980147868eSNuno Antunes 			privdata->left.dest = hinfo;
1990147868eSNuno Antunes 	} else
2000147868eSNuno Antunes 		return (EINVAL);
2010147868eSNuno Antunes 	hinfo->hook = hook;
2020147868eSNuno Antunes 	bzero(&hinfo->stats, sizeof(hinfo->stats));
2030147868eSNuno Antunes 	NG_HOOK_SET_PRIVATE(hook, hinfo);
2040147868eSNuno Antunes 	return (0);
2050147868eSNuno Antunes }
2060147868eSNuno Antunes 
2070147868eSNuno Antunes /*
2080147868eSNuno Antunes  * Receive a control message
2090147868eSNuno Antunes  */
2100147868eSNuno Antunes static int
ng_tee_rcvmsg(node_p node,item_p item,hook_p lasthook)2110147868eSNuno Antunes ng_tee_rcvmsg(node_p node, item_p item, hook_p lasthook)
2120147868eSNuno Antunes {
2130147868eSNuno Antunes 	const sc_p sc = NG_NODE_PRIVATE(node);
2140147868eSNuno Antunes 	struct ng_mesg *resp = NULL;
2150147868eSNuno Antunes 	int error = 0;
2160147868eSNuno Antunes 	struct ng_mesg *msg;
2170147868eSNuno Antunes 
2180147868eSNuno Antunes 	NGI_GET_MSG(item, msg);
2190147868eSNuno Antunes 	switch (msg->header.typecookie) {
2200147868eSNuno Antunes 	case NGM_TEE_COOKIE:
2210147868eSNuno Antunes 		switch (msg->header.cmd) {
2220147868eSNuno Antunes 		case NGM_TEE_GET_STATS:
2230147868eSNuno Antunes 		case NGM_TEE_CLR_STATS:
2240147868eSNuno Antunes 		case NGM_TEE_GETCLR_STATS:
2250147868eSNuno Antunes                     {
2260147868eSNuno Antunes 			struct ng_tee_stats *stats;
2270147868eSNuno Antunes 
2280147868eSNuno Antunes                         if (msg->header.cmd != NGM_TEE_CLR_STATS) {
2290147868eSNuno Antunes                                 NG_MKRESPONSE(resp, msg,
2300147868eSNuno Antunes                                     sizeof(*stats), M_WAITOK | M_NULLOK);
2310147868eSNuno Antunes 				if (resp == NULL) {
2320147868eSNuno Antunes 					error = ENOMEM;
2330147868eSNuno Antunes 					goto done;
2340147868eSNuno Antunes 				}
2350147868eSNuno Antunes 				stats = (struct ng_tee_stats *)resp->data;
2360147868eSNuno Antunes 				bcopy(&sc->right.stats, &stats->right,
2370147868eSNuno Antunes 				    sizeof(stats->right));
2380147868eSNuno Antunes 				bcopy(&sc->left.stats, &stats->left,
2390147868eSNuno Antunes 				    sizeof(stats->left));
2400147868eSNuno Antunes 				bcopy(&sc->right2left.stats, &stats->right2left,
2410147868eSNuno Antunes 				    sizeof(stats->right2left));
2420147868eSNuno Antunes 				bcopy(&sc->left2right.stats, &stats->left2right,
2430147868eSNuno Antunes 				    sizeof(stats->left2right));
2440147868eSNuno Antunes                         }
2450147868eSNuno Antunes                         if (msg->header.cmd != NGM_TEE_GET_STATS) {
2460147868eSNuno Antunes 				bzero(&sc->right.stats,
2470147868eSNuno Antunes 				    sizeof(sc->right.stats));
2480147868eSNuno Antunes 				bzero(&sc->left.stats,
2490147868eSNuno Antunes 				    sizeof(sc->left.stats));
2500147868eSNuno Antunes 				bzero(&sc->right2left.stats,
2510147868eSNuno Antunes 				    sizeof(sc->right2left.stats));
2520147868eSNuno Antunes 				bzero(&sc->left2right.stats,
2530147868eSNuno Antunes 				    sizeof(sc->left2right.stats));
2540147868eSNuno Antunes 			}
2550147868eSNuno Antunes                         break;
2560147868eSNuno Antunes 		    }
2570147868eSNuno Antunes 		default:
2580147868eSNuno Antunes 			error = EINVAL;
2590147868eSNuno Antunes 			break;
2600147868eSNuno Antunes 		}
2610147868eSNuno Antunes 		break;
2620147868eSNuno Antunes 	case NGM_FLOW_COOKIE:
2630147868eSNuno Antunes 		if (lasthook == sc->left.hook || lasthook == sc->right.hook)  {
2640147868eSNuno Antunes 			hi_p const hinfo = NG_HOOK_PRIVATE(lasthook);
2650147868eSNuno Antunes 			if (hinfo && hinfo->dest) {
2660147868eSNuno Antunes 				NGI_MSG(item) = msg;
2670147868eSNuno Antunes 				NG_FWD_ITEM_HOOK(error, item, hinfo->dest->hook);
2680147868eSNuno Antunes 				return (error);
2690147868eSNuno Antunes 			}
2700147868eSNuno Antunes 		}
2710147868eSNuno Antunes 		break;
2720147868eSNuno Antunes 	default:
2730147868eSNuno Antunes 		error = EINVAL;
2740147868eSNuno Antunes 		break;
2750147868eSNuno Antunes 	}
2760147868eSNuno Antunes done:
2770147868eSNuno Antunes 	NG_RESPOND_MSG(error, node, item, resp);
2780147868eSNuno Antunes 	NG_FREE_MSG(msg);
2790147868eSNuno Antunes 	return (error);
2800147868eSNuno Antunes }
2810147868eSNuno Antunes 
2820147868eSNuno Antunes /*
2830147868eSNuno Antunes  * Receive data on a hook
2840147868eSNuno Antunes  *
2850147868eSNuno Antunes  * If data comes in the right link send a copy out right2left, and then
2860147868eSNuno Antunes  * send the original onwards out through the left link.
2870147868eSNuno Antunes  * Do the opposite for data coming in from the left link.
2880147868eSNuno Antunes  * Data coming in right2left or left2right is forwarded
2890147868eSNuno Antunes  * on through the appropriate destination hook as if it had come
2900147868eSNuno Antunes  * from the other side.
2910147868eSNuno Antunes  */
2920147868eSNuno Antunes static int
ng_tee_rcvdata(hook_p hook,item_p item)2930147868eSNuno Antunes ng_tee_rcvdata(hook_p hook, item_p item)
2940147868eSNuno Antunes {
2950147868eSNuno Antunes 	const hi_p hinfo = NG_HOOK_PRIVATE(hook);
2960147868eSNuno Antunes 	hi_p	h;
2970147868eSNuno Antunes 	int	error = 0;
2980147868eSNuno Antunes 	struct mbuf *m;
2990147868eSNuno Antunes 
3000147868eSNuno Antunes 	m = NGI_M(item);
3010147868eSNuno Antunes 
3020147868eSNuno Antunes 	/* Update stats on incoming hook */
3030147868eSNuno Antunes 	hinfo->stats.inOctets += m->m_pkthdr.len;
3040147868eSNuno Antunes 	hinfo->stats.inFrames++;
3050147868eSNuno Antunes 
3060147868eSNuno Antunes 	/* Duplicate packet if requried */
3070147868eSNuno Antunes 	if (hinfo->dup) {
3080147868eSNuno Antunes 		struct mbuf *m2;
3090147868eSNuno Antunes 
3100147868eSNuno Antunes 		/* Copy packet (failure will not stop the original)*/
311*b5523eacSSascha Wildner 		m2 = m_dup(m, M_NOWAIT);
3120147868eSNuno Antunes 		if (m2) {
3130147868eSNuno Antunes 			/* Deliver duplicate */
3140147868eSNuno Antunes 			h = hinfo->dup;
3150147868eSNuno Antunes 			NG_SEND_DATA_ONLY(error, h->hook, m2);
3160147868eSNuno Antunes 			if (error == 0) {
3170147868eSNuno Antunes 				h->stats.outOctets += m->m_pkthdr.len;
3180147868eSNuno Antunes 				h->stats.outFrames++;
3190147868eSNuno Antunes 			}
3200147868eSNuno Antunes 		}
3210147868eSNuno Antunes 	}
3220147868eSNuno Antunes 	/* Deliver frame out destination hook */
3230147868eSNuno Antunes 	if (hinfo->dest) {
3240147868eSNuno Antunes 		h = hinfo->dest;
3250147868eSNuno Antunes 		h->stats.outOctets += m->m_pkthdr.len;
3260147868eSNuno Antunes 		h->stats.outFrames++;
3270147868eSNuno Antunes 		NG_FWD_ITEM_HOOK(error, item, h->hook);
3280147868eSNuno Antunes 	} else
3290147868eSNuno Antunes 		NG_FREE_ITEM(item);
3300147868eSNuno Antunes 	return (error);
3310147868eSNuno Antunes }
3320147868eSNuno Antunes 
3330147868eSNuno Antunes /*
3340147868eSNuno Antunes  * We are going to be shut down soon
3350147868eSNuno Antunes  *
3360147868eSNuno Antunes  * If we have both a left and right hook, then we probably want to extricate
3370147868eSNuno Antunes  * ourselves and leave the two peers still linked to each other. Otherwise we
3380147868eSNuno Antunes  * should just shut down as a normal node would.
3390147868eSNuno Antunes  */
3400147868eSNuno Antunes static int
ng_tee_close(node_p node)3410147868eSNuno Antunes ng_tee_close(node_p node)
3420147868eSNuno Antunes {
3430147868eSNuno Antunes 	const sc_p privdata = NG_NODE_PRIVATE(node);
3440147868eSNuno Antunes 
3450147868eSNuno Antunes 	if (privdata->left.hook && privdata->right.hook)
3460147868eSNuno Antunes 		ng_bypass(privdata->left.hook, privdata->right.hook);
3470147868eSNuno Antunes 
3480147868eSNuno Antunes 	return (0);
3490147868eSNuno Antunes }
3500147868eSNuno Antunes 
3510147868eSNuno Antunes /*
3520147868eSNuno Antunes  * Shutdown processing
3530147868eSNuno Antunes  */
3540147868eSNuno Antunes static int
ng_tee_shutdown(node_p node)3550147868eSNuno Antunes ng_tee_shutdown(node_p node)
3560147868eSNuno Antunes {
3570147868eSNuno Antunes 	const sc_p privdata = NG_NODE_PRIVATE(node);
3580147868eSNuno Antunes 
3590147868eSNuno Antunes 	NG_NODE_SET_PRIVATE(node, NULL);
3600147868eSNuno Antunes 	kfree(privdata, M_NETGRAPH);
3610147868eSNuno Antunes 	NG_NODE_UNREF(node);
3620147868eSNuno Antunes 	return (0);
3630147868eSNuno Antunes }
3640147868eSNuno Antunes 
3650147868eSNuno Antunes /*
3660147868eSNuno Antunes  * Hook disconnection
3670147868eSNuno Antunes  */
3680147868eSNuno Antunes static int
ng_tee_disconnect(hook_p hook)3690147868eSNuno Antunes ng_tee_disconnect(hook_p hook)
3700147868eSNuno Antunes {
3710147868eSNuno Antunes 	sc_p	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
3720147868eSNuno Antunes 	hi_p const hinfo = NG_HOOK_PRIVATE(hook);
3730147868eSNuno Antunes 
3740147868eSNuno Antunes 	KASSERT(hinfo != NULL, ("%s: null info", __func__));
3750147868eSNuno Antunes 	hinfo->hook = NULL;
3760147868eSNuno Antunes 
3770147868eSNuno Antunes 	/* Recalculate internal pathes. */
3780147868eSNuno Antunes 	if (sc->left.dest == hinfo) {
3790147868eSNuno Antunes 		sc->left.dest = sc->left.dup;
3800147868eSNuno Antunes 		sc->left.dup = NULL;
3810147868eSNuno Antunes 	} else if (sc->left.dup == hinfo)
3820147868eSNuno Antunes 		sc->left.dup = NULL;
3830147868eSNuno Antunes 	if (sc->right.dest == hinfo) {
3840147868eSNuno Antunes 		sc->right.dest = sc->right.dup;
3850147868eSNuno Antunes 		sc->right.dup = NULL;
3860147868eSNuno Antunes 	} else if (sc->right.dup == hinfo)
3870147868eSNuno Antunes 		sc->right.dup = NULL;
3880147868eSNuno Antunes 	if (sc->left2right.dest == hinfo)
3890147868eSNuno Antunes 		sc->left2right.dest = NULL;
3900147868eSNuno Antunes 	if (sc->right2left.dest == hinfo)
3910147868eSNuno Antunes 		sc->right2left.dest = NULL;
3920147868eSNuno Antunes 
3930147868eSNuno Antunes 	/* Die when last hook disconnected. */
3940147868eSNuno Antunes 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
3950147868eSNuno Antunes 	    NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
3960147868eSNuno Antunes 		ng_rmnode_self(NG_HOOK_NODE(hook));
3970147868eSNuno Antunes 	return (0);
3980147868eSNuno Antunes }
399