xref: /freebsd/sys/netgraph/ng_tee.c (revision 9b8db66402e94919ab47fc2d69863968e49e305a)
14cf49a43SJulian Elischer 
24cf49a43SJulian Elischer /*
34cf49a43SJulian Elischer  * ng_tee.c
4c398230bSWarner Losh  */
5c398230bSWarner Losh 
6c398230bSWarner Losh /*-
74cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
84cf49a43SJulian Elischer  * All rights reserved.
94cf49a43SJulian Elischer  *
104cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
114cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
124cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
134cf49a43SJulian Elischer  * provided, however, that:
144cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
154cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
164cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
174cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
184cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
194cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
204cf49a43SJulian Elischer  *
214cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
224cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
234cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
244cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
254cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
264cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
274cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
284cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
294cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
304cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
314cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
324cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
334cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
344cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
354cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
364cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
374cf49a43SJulian Elischer  * OF SUCH DAMAGE.
384cf49a43SJulian Elischer  *
39cc3bbd68SJulian Elischer  * Author: Julian Elischer <julian@freebsd.org>
4074f5c6aaSJulian Elischer  * $Whistle: ng_tee.c,v 1.18 1999/11/01 09:24:52 julian Exp $
414cf49a43SJulian Elischer  */
424cf49a43SJulian Elischer 
434cf49a43SJulian Elischer /*
444cf49a43SJulian Elischer  * This node is like the tee(1) command and is useful for ``snooping.''
454cf49a43SJulian Elischer  * It has 4 hooks: left, right, left2right, and right2left. Data
464cf49a43SJulian Elischer  * entering from the right is passed to the left and duplicated on
474cf49a43SJulian Elischer  * right2left, and data entering from the left is passed to the right
484cf49a43SJulian Elischer  * and duplicated on left2right. Data entering from left2right is
49722a139cSJulian Elischer  * sent to left, and data from right2left to right.
504cf49a43SJulian Elischer  */
514cf49a43SJulian Elischer 
524cf49a43SJulian Elischer #include <sys/param.h>
534cf49a43SJulian Elischer #include <sys/systm.h>
544cf49a43SJulian Elischer #include <sys/errno.h>
554cf49a43SJulian Elischer #include <sys/kernel.h>
564cf49a43SJulian Elischer #include <sys/malloc.h>
574cf49a43SJulian Elischer #include <sys/mbuf.h>
584cf49a43SJulian Elischer #include <netgraph/ng_message.h>
594cf49a43SJulian Elischer #include <netgraph/netgraph.h>
605c63f81fSArchie Cobbs #include <netgraph/ng_parse.h>
614cf49a43SJulian Elischer #include <netgraph/ng_tee.h>
624cf49a43SJulian Elischer 
63*9b8db664SDmitry Lukhtionov #ifdef NG_SEPARATE_MALLOC
64*9b8db664SDmitry Lukhtionov static MALLOC_DEFINE(M_NETGRAPH_TEE, "netgraph_tee", "netgraph tee node");
65*9b8db664SDmitry Lukhtionov #else
66*9b8db664SDmitry Lukhtionov #define M_NETGRAPH_TEE M_NETGRAPH
67*9b8db664SDmitry Lukhtionov #endif
68*9b8db664SDmitry Lukhtionov 
694cf49a43SJulian Elischer /* Per hook info */
704cf49a43SJulian Elischer struct hookinfo {
714cf49a43SJulian Elischer 	hook_p			hook;
72e632000eSAlexander Motin 	struct hookinfo		*dest, *dup;
739cb887a6SArchie Cobbs 	struct ng_tee_hookstat	stats;
744cf49a43SJulian Elischer };
75e632000eSAlexander Motin typedef struct hookinfo *hi_p;
764cf49a43SJulian Elischer 
774cf49a43SJulian Elischer /* Per node info */
784cf49a43SJulian Elischer struct privdata {
794cf49a43SJulian Elischer 	struct hookinfo		left;
804cf49a43SJulian Elischer 	struct hookinfo		right;
814cf49a43SJulian Elischer 	struct hookinfo		left2right;
824cf49a43SJulian Elischer 	struct hookinfo		right2left;
834cf49a43SJulian Elischer };
844cf49a43SJulian Elischer typedef struct privdata *sc_p;
854cf49a43SJulian Elischer 
864cf49a43SJulian Elischer /* Netgraph methods */
87e632000eSAlexander Motin static ng_constructor_t	ng_tee_constructor;
88e632000eSAlexander Motin static ng_rcvmsg_t	ng_tee_rcvmsg;
89e632000eSAlexander Motin static ng_close_t	ng_tee_close;
90e632000eSAlexander Motin static ng_shutdown_t	ng_tee_shutdown;
91e632000eSAlexander Motin static ng_newhook_t	ng_tee_newhook;
92e632000eSAlexander Motin static ng_rcvdata_t	ng_tee_rcvdata;
93e632000eSAlexander Motin static ng_disconnect_t	ng_tee_disconnect;
944cf49a43SJulian Elischer 
955c63f81fSArchie Cobbs /* Parse type for struct ng_tee_hookstat */
96f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_tee_hookstat_type_fields[]
97f0184ff8SArchie Cobbs 	= NG_TEE_HOOKSTAT_INFO;
985c63f81fSArchie Cobbs static const struct ng_parse_type ng_tee_hookstat_type = {
995c63f81fSArchie Cobbs 	&ng_parse_struct_type,
100f0184ff8SArchie Cobbs 	&ng_tee_hookstat_type_fields
1015c63f81fSArchie Cobbs };
1025c63f81fSArchie Cobbs 
1035c63f81fSArchie Cobbs /* Parse type for struct ng_tee_stats */
104f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_tee_stats_type_fields[]
105f0184ff8SArchie Cobbs 	= NG_TEE_STATS_INFO(&ng_tee_hookstat_type);
1065c63f81fSArchie Cobbs static const struct ng_parse_type ng_tee_stats_type = {
1075c63f81fSArchie Cobbs 	&ng_parse_struct_type,
108f0184ff8SArchie Cobbs 	&ng_tee_stats_type_fields
1095c63f81fSArchie Cobbs };
1105c63f81fSArchie Cobbs 
1115c63f81fSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
1125c63f81fSArchie Cobbs static const struct ng_cmdlist ng_tee_cmds[] = {
1135c63f81fSArchie Cobbs 	{
1145c63f81fSArchie Cobbs 	  NGM_TEE_COOKIE,
1155c63f81fSArchie Cobbs 	  NGM_TEE_GET_STATS,
1165c63f81fSArchie Cobbs 	  "getstats",
1175c63f81fSArchie Cobbs 	  NULL,
1185c63f81fSArchie Cobbs 	  &ng_tee_stats_type
1195c63f81fSArchie Cobbs 	},
1205c63f81fSArchie Cobbs 	{
1215c63f81fSArchie Cobbs 	  NGM_TEE_COOKIE,
1225c63f81fSArchie Cobbs 	  NGM_TEE_CLR_STATS,
1235c63f81fSArchie Cobbs 	  "clrstats",
1245c63f81fSArchie Cobbs 	  NULL,
1255c63f81fSArchie Cobbs 	  NULL
1265c63f81fSArchie Cobbs 	},
12705215785SArchie Cobbs 	{
12805215785SArchie Cobbs 	  NGM_TEE_COOKIE,
12905215785SArchie Cobbs 	  NGM_TEE_GETCLR_STATS,
13005215785SArchie Cobbs 	  "getclrstats",
13105215785SArchie Cobbs 	  NULL,
13205215785SArchie Cobbs 	  &ng_tee_stats_type
13305215785SArchie Cobbs 	},
1345c63f81fSArchie Cobbs 	{ 0 }
1355c63f81fSArchie Cobbs };
1365c63f81fSArchie Cobbs 
1374cf49a43SJulian Elischer /* Netgraph type descriptor */
1385c63f81fSArchie Cobbs static struct ng_type ng_tee_typestruct = {
139f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
140f8aae777SJulian Elischer 	.name =		NG_TEE_NODE_TYPE,
141e632000eSAlexander Motin 	.constructor =  ng_tee_constructor,
142e632000eSAlexander Motin 	.rcvmsg =	ng_tee_rcvmsg,
143e632000eSAlexander Motin 	.close =	ng_tee_close,
144e632000eSAlexander Motin 	.shutdown =	ng_tee_shutdown,
145e632000eSAlexander Motin 	.newhook =	ng_tee_newhook,
146e632000eSAlexander Motin 	.rcvdata =	ng_tee_rcvdata,
147e632000eSAlexander Motin 	.disconnect =	ng_tee_disconnect,
148f8aae777SJulian Elischer 	.cmdlist =	ng_tee_cmds,
1494cf49a43SJulian Elischer };
1505c63f81fSArchie Cobbs NETGRAPH_INIT(tee, &ng_tee_typestruct);
1514cf49a43SJulian Elischer 
1524cf49a43SJulian Elischer /*
1534cf49a43SJulian Elischer  * Node constructor
1544cf49a43SJulian Elischer  */
1554cf49a43SJulian Elischer static int
ng_tee_constructor(node_p node)156e632000eSAlexander Motin ng_tee_constructor(node_p node)
1574cf49a43SJulian Elischer {
1584cf49a43SJulian Elischer 	sc_p privdata;
1594cf49a43SJulian Elischer 
160*9b8db664SDmitry Lukhtionov 	privdata = malloc(sizeof(*privdata), M_NETGRAPH_TEE, M_WAITOK | M_ZERO);
1614cf49a43SJulian Elischer 
16230400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, privdata);
1634cf49a43SJulian Elischer 	return (0);
1644cf49a43SJulian Elischer }
1654cf49a43SJulian Elischer 
1664cf49a43SJulian Elischer /*
1674cf49a43SJulian Elischer  * Add a hook
1684cf49a43SJulian Elischer  */
1694cf49a43SJulian Elischer static int
ng_tee_newhook(node_p node,hook_p hook,const char * name)170e632000eSAlexander Motin ng_tee_newhook(node_p node, hook_p hook, const char *name)
1714cf49a43SJulian Elischer {
172e632000eSAlexander Motin 	sc_p	privdata = NG_NODE_PRIVATE(node);
173e632000eSAlexander Motin 	hi_p	hinfo;
1744cf49a43SJulian Elischer 
175053359b7SPedro F. Giffuni 	/* Precalculate internal paths. */
1764cf49a43SJulian Elischer 	if (strcmp(name, NG_TEE_HOOK_RIGHT) == 0) {
177e632000eSAlexander Motin 		hinfo = &privdata->right;
178e632000eSAlexander Motin 		if (privdata->left.dest)
179e632000eSAlexander Motin 			privdata->left.dup = privdata->left.dest;
180e632000eSAlexander Motin 		privdata->left.dest = hinfo;
181e632000eSAlexander Motin 		privdata->right2left.dest = hinfo;
1824cf49a43SJulian Elischer 	} else if (strcmp(name, NG_TEE_HOOK_LEFT) == 0) {
183e632000eSAlexander Motin 		hinfo = &privdata->left;
184e632000eSAlexander Motin 		if (privdata->right.dest)
185e632000eSAlexander Motin 			privdata->right.dup = privdata->right.dest;
186e632000eSAlexander Motin 		privdata->right.dest = hinfo;
187e632000eSAlexander Motin 		privdata->left2right.dest = hinfo;
1884cf49a43SJulian Elischer 	} else if (strcmp(name, NG_TEE_HOOK_RIGHT2LEFT) == 0) {
189e632000eSAlexander Motin 		hinfo = &privdata->right2left;
190e632000eSAlexander Motin 		if (privdata->right.dest)
191e632000eSAlexander Motin 			privdata->right.dup = hinfo;
192e632000eSAlexander Motin 		else
193e632000eSAlexander Motin 			privdata->right.dest = hinfo;
1944cf49a43SJulian Elischer 	} else if (strcmp(name, NG_TEE_HOOK_LEFT2RIGHT) == 0) {
195e632000eSAlexander Motin 		hinfo = &privdata->left2right;
196e632000eSAlexander Motin 		if (privdata->left.dest)
197e632000eSAlexander Motin 			privdata->left.dup = hinfo;
198e632000eSAlexander Motin 		else
199e632000eSAlexander Motin 			privdata->left.dest = hinfo;
2004cf49a43SJulian Elischer 	} else
2014cf49a43SJulian Elischer 		return (EINVAL);
202e632000eSAlexander Motin 	hinfo->hook = hook;
203e632000eSAlexander Motin 	bzero(&hinfo->stats, sizeof(hinfo->stats));
204e632000eSAlexander Motin 	NG_HOOK_SET_PRIVATE(hook, hinfo);
2054cf49a43SJulian Elischer 	return (0);
2064cf49a43SJulian Elischer }
2074cf49a43SJulian Elischer 
2084cf49a43SJulian Elischer /*
2099cb887a6SArchie Cobbs  * Receive a control message
2104cf49a43SJulian Elischer  */
2114cf49a43SJulian Elischer static int
ng_tee_rcvmsg(node_p node,item_p item,hook_p lasthook)212e632000eSAlexander Motin ng_tee_rcvmsg(node_p node, item_p item, hook_p lasthook)
2134cf49a43SJulian Elischer {
21430400f03SJulian Elischer 	const sc_p sc = NG_NODE_PRIVATE(node);
2159cb887a6SArchie Cobbs 	struct ng_mesg *resp = NULL;
2169cb887a6SArchie Cobbs 	int error = 0;
217069154d5SJulian Elischer 	struct ng_mesg *msg;
2189cb887a6SArchie Cobbs 
219069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
2209cb887a6SArchie Cobbs 	switch (msg->header.typecookie) {
2219cb887a6SArchie Cobbs 	case NGM_TEE_COOKIE:
2229cb887a6SArchie Cobbs 		switch (msg->header.cmd) {
2239cb887a6SArchie Cobbs 		case NGM_TEE_GET_STATS:
22405215785SArchie Cobbs 		case NGM_TEE_CLR_STATS:
22505215785SArchie Cobbs 		case NGM_TEE_GETCLR_STATS:
2269cb887a6SArchie Cobbs                     {
2279cb887a6SArchie Cobbs 			struct ng_tee_stats *stats;
2289cb887a6SArchie Cobbs 
22905215785SArchie Cobbs                         if (msg->header.cmd != NGM_TEE_CLR_STATS) {
2309cb887a6SArchie Cobbs                                 NG_MKRESPONSE(resp, msg,
23105215785SArchie Cobbs                                     sizeof(*stats), M_NOWAIT);
2329cb887a6SArchie Cobbs 				if (resp == NULL) {
2339cb887a6SArchie Cobbs 					error = ENOMEM;
2349cb887a6SArchie Cobbs 					goto done;
2359cb887a6SArchie Cobbs 				}
2369cb887a6SArchie Cobbs 				stats = (struct ng_tee_stats *)resp->data;
23705215785SArchie Cobbs 				bcopy(&sc->right.stats, &stats->right,
23805215785SArchie Cobbs 				    sizeof(stats->right));
23905215785SArchie Cobbs 				bcopy(&sc->left.stats, &stats->left,
24005215785SArchie Cobbs 				    sizeof(stats->left));
24105215785SArchie Cobbs 				bcopy(&sc->right2left.stats, &stats->right2left,
24205215785SArchie Cobbs 				    sizeof(stats->right2left));
24305215785SArchie Cobbs 				bcopy(&sc->left2right.stats, &stats->left2right,
24405215785SArchie Cobbs 				    sizeof(stats->left2right));
2459cb887a6SArchie Cobbs                         }
24605215785SArchie Cobbs                         if (msg->header.cmd != NGM_TEE_GET_STATS) {
24705215785SArchie Cobbs 				bzero(&sc->right.stats,
24805215785SArchie Cobbs 				    sizeof(sc->right.stats));
24905215785SArchie Cobbs 				bzero(&sc->left.stats,
25005215785SArchie Cobbs 				    sizeof(sc->left.stats));
2519cb887a6SArchie Cobbs 				bzero(&sc->right2left.stats,
2529cb887a6SArchie Cobbs 				    sizeof(sc->right2left.stats));
2539cb887a6SArchie Cobbs 				bzero(&sc->left2right.stats,
2549cb887a6SArchie Cobbs 				    sizeof(sc->left2right.stats));
25505215785SArchie Cobbs 			}
2569cb887a6SArchie Cobbs                         break;
25705215785SArchie Cobbs 		    }
2589cb887a6SArchie Cobbs 		default:
2599cb887a6SArchie Cobbs 			error = EINVAL;
2609cb887a6SArchie Cobbs 			break;
2619cb887a6SArchie Cobbs 		}
2629cb887a6SArchie Cobbs 		break;
263069154d5SJulian Elischer 	case NGM_FLOW_COOKIE:
264e632000eSAlexander Motin 		if (lasthook == sc->left.hook || lasthook == sc->right.hook)  {
265e632000eSAlexander Motin 			hi_p const hinfo = NG_HOOK_PRIVATE(lasthook);
266e632000eSAlexander Motin 			if (hinfo && hinfo->dest) {
267069154d5SJulian Elischer 				NGI_MSG(item) = msg;
268e632000eSAlexander Motin 				NG_FWD_ITEM_HOOK(error, item, hinfo->dest->hook);
269069154d5SJulian Elischer 				return (error);
270069154d5SJulian Elischer 			}
271069154d5SJulian Elischer 		}
272069154d5SJulian Elischer 		break;
2739cb887a6SArchie Cobbs 	default:
2749cb887a6SArchie Cobbs 		error = EINVAL;
2759cb887a6SArchie Cobbs 		break;
2769cb887a6SArchie Cobbs 	}
2779cb887a6SArchie Cobbs done:
278069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
279069154d5SJulian Elischer 	NG_FREE_MSG(msg);
2809cb887a6SArchie Cobbs 	return (error);
2814cf49a43SJulian Elischer }
2824cf49a43SJulian Elischer 
2834cf49a43SJulian Elischer /*
2844cf49a43SJulian Elischer  * Receive data on a hook
2854cf49a43SJulian Elischer  *
2864cf49a43SJulian Elischer  * If data comes in the right link send a copy out right2left, and then
2874cf49a43SJulian Elischer  * send the original onwards out through the left link.
2884cf49a43SJulian Elischer  * Do the opposite for data coming in from the left link.
2894cf49a43SJulian Elischer  * Data coming in right2left or left2right is forwarded
2904cf49a43SJulian Elischer  * on through the appropriate destination hook as if it had come
2914cf49a43SJulian Elischer  * from the other side.
2924cf49a43SJulian Elischer  */
2934cf49a43SJulian Elischer static int
ng_tee_rcvdata(hook_p hook,item_p item)294e632000eSAlexander Motin ng_tee_rcvdata(hook_p hook, item_p item)
2954cf49a43SJulian Elischer {
296e632000eSAlexander Motin 	const hi_p hinfo = NG_HOOK_PRIVATE(hook);
297e632000eSAlexander Motin 	hi_p	h;
2984cf49a43SJulian Elischer 	int	error = 0;
299069154d5SJulian Elischer 	struct mbuf *m;
3004cf49a43SJulian Elischer 
301069154d5SJulian Elischer 	m = NGI_M(item);
3024cf49a43SJulian Elischer 
3039cb887a6SArchie Cobbs 	/* Update stats on incoming hook */
3049cb887a6SArchie Cobbs 	hinfo->stats.inOctets += m->m_pkthdr.len;
3059cb887a6SArchie Cobbs 	hinfo->stats.inFrames++;
3064cf49a43SJulian Elischer 
3073ca24c28SJulian Elischer 	/* Duplicate packet if requried */
308e632000eSAlexander Motin 	if (hinfo->dup) {
3099cb887a6SArchie Cobbs 		struct mbuf *m2;
3109cb887a6SArchie Cobbs 
311069154d5SJulian Elischer 		/* Copy packet (failure will not stop the original)*/
312eb1b1807SGleb Smirnoff 		m2 = m_dup(m, M_NOWAIT);
313069154d5SJulian Elischer 		if (m2) {
3149cb887a6SArchie Cobbs 			/* Deliver duplicate */
315e632000eSAlexander Motin 			h = hinfo->dup;
316e632000eSAlexander Motin 			NG_SEND_DATA_ONLY(error, h->hook, m2);
317f4b580b1SGleb Smirnoff 			if (error == 0) {
318e632000eSAlexander Motin 				h->stats.outOctets += m->m_pkthdr.len;
319e632000eSAlexander Motin 				h->stats.outFrames++;
320f4b580b1SGleb Smirnoff 			}
3219cb887a6SArchie Cobbs 		}
322069154d5SJulian Elischer 	}
3239cb887a6SArchie Cobbs 	/* Deliver frame out destination hook */
324e632000eSAlexander Motin 	if (hinfo->dest) {
325e632000eSAlexander Motin 		h = hinfo->dest;
326e632000eSAlexander Motin 		h->stats.outOctets += m->m_pkthdr.len;
327e632000eSAlexander Motin 		h->stats.outFrames++;
328e632000eSAlexander Motin 		NG_FWD_ITEM_HOOK(error, item, h->hook);
3290598ef01SJulian Elischer 	} else
330069154d5SJulian Elischer 		NG_FREE_ITEM(item);
33153dc6459SJulian Elischer 	return (error);
3324cf49a43SJulian Elischer }
3334cf49a43SJulian Elischer 
3344cf49a43SJulian Elischer /*
335991fc65aSJulian Elischer  * We are going to be shut down soon
336991fc65aSJulian Elischer  *
337991fc65aSJulian Elischer  * If we have both a left and right hook, then we probably want to extricate
338991fc65aSJulian Elischer  * ourselves and leave the two peers still linked to each other. Otherwise we
339991fc65aSJulian Elischer  * should just shut down as a normal node would.
340991fc65aSJulian Elischer  */
341991fc65aSJulian Elischer static int
ng_tee_close(node_p node)342e632000eSAlexander Motin ng_tee_close(node_p node)
343991fc65aSJulian Elischer {
344991fc65aSJulian Elischer 	const sc_p privdata = NG_NODE_PRIVATE(node);
345991fc65aSJulian Elischer 
346991fc65aSJulian Elischer 	if (privdata->left.hook && privdata->right.hook)
347991fc65aSJulian Elischer 		ng_bypass(privdata->left.hook, privdata->right.hook);
348991fc65aSJulian Elischer 
349991fc65aSJulian Elischer 	return (0);
350991fc65aSJulian Elischer }
351991fc65aSJulian Elischer 
352991fc65aSJulian Elischer /*
3534cf49a43SJulian Elischer  * Shutdown processing
3544cf49a43SJulian Elischer  */
3554cf49a43SJulian Elischer static int
ng_tee_shutdown(node_p node)356e632000eSAlexander Motin ng_tee_shutdown(node_p node)
3574cf49a43SJulian Elischer {
35830400f03SJulian Elischer 	const sc_p privdata = NG_NODE_PRIVATE(node);
3594cf49a43SJulian Elischer 
36030400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
361*9b8db664SDmitry Lukhtionov 	free(privdata, M_NETGRAPH_TEE);
362510b7722SAlexander Motin 	NG_NODE_UNREF(node);
3634cf49a43SJulian Elischer 	return (0);
3644cf49a43SJulian Elischer }
3654cf49a43SJulian Elischer 
3664cf49a43SJulian Elischer /*
3674cf49a43SJulian Elischer  * Hook disconnection
3684cf49a43SJulian Elischer  */
3694cf49a43SJulian Elischer static int
ng_tee_disconnect(hook_p hook)370e632000eSAlexander Motin ng_tee_disconnect(hook_p hook)
3714cf49a43SJulian Elischer {
372e632000eSAlexander Motin 	sc_p	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
373e632000eSAlexander Motin 	hi_p const hinfo = NG_HOOK_PRIVATE(hook);
3744cf49a43SJulian Elischer 
3756e551fb6SDavid E. O'Brien 	KASSERT(hinfo != NULL, ("%s: null info", __func__));
3769cb887a6SArchie Cobbs 	hinfo->hook = NULL;
377e632000eSAlexander Motin 
378053359b7SPedro F. Giffuni 	/* Recalculate internal paths. */
379e632000eSAlexander Motin 	if (sc->left.dest == hinfo) {
380e632000eSAlexander Motin 		sc->left.dest = sc->left.dup;
381e632000eSAlexander Motin 		sc->left.dup = NULL;
382e632000eSAlexander Motin 	} else if (sc->left.dup == hinfo)
383e632000eSAlexander Motin 		sc->left.dup = NULL;
384e632000eSAlexander Motin 	if (sc->right.dest == hinfo) {
385e632000eSAlexander Motin 		sc->right.dest = sc->right.dup;
386e632000eSAlexander Motin 		sc->right.dup = NULL;
387e632000eSAlexander Motin 	} else if (sc->right.dup == hinfo)
388e632000eSAlexander Motin 		sc->right.dup = NULL;
389e632000eSAlexander Motin 	if (sc->left2right.dest == hinfo)
390e632000eSAlexander Motin 		sc->left2right.dest = NULL;
391e632000eSAlexander Motin 	if (sc->right2left.dest == hinfo)
392e632000eSAlexander Motin 		sc->right2left.dest = NULL;
393e632000eSAlexander Motin 
394e632000eSAlexander Motin 	/* Die when last hook disconnected. */
395e632000eSAlexander Motin 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
396e632000eSAlexander Motin 	    NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
39730400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
3984cf49a43SJulian Elischer 	return (0);
3994cf49a43SJulian Elischer }
400