xref: /freebsd/sys/netgraph/netflow/ng_netflow.c (revision 3157ba21)
1 /*-
2  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
3  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $SourceForge: ng_netflow.c,v 1.30 2004/09/05 11:37:43 glebius Exp $
28  */
29 
30 static const char rcs_id[] =
31     "@(#) $FreeBSD$";
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/syslog.h>
40 #include <sys/ctype.h>
41 
42 #include <net/if.h>
43 #include <net/ethernet.h>
44 #include <net/if_arp.h>
45 #include <net/if_var.h>
46 #include <net/if_vlan_var.h>
47 #include <net/bpf.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/tcp.h>
52 #include <netinet/udp.h>
53 
54 #include <netgraph/ng_message.h>
55 #include <netgraph/ng_parse.h>
56 #include <netgraph/netgraph.h>
57 #include <netgraph/netflow/netflow.h>
58 #include <netgraph/netflow/ng_netflow.h>
59 
60 /* Netgraph methods */
61 static ng_constructor_t	ng_netflow_constructor;
62 static ng_rcvmsg_t	ng_netflow_rcvmsg;
63 static ng_close_t	ng_netflow_close;
64 static ng_shutdown_t	ng_netflow_rmnode;
65 static ng_newhook_t	ng_netflow_newhook;
66 static ng_rcvdata_t	ng_netflow_rcvdata;
67 static ng_disconnect_t	ng_netflow_disconnect;
68 
69 /* Parse type for struct ng_netflow_info */
70 static const struct ng_parse_struct_field ng_netflow_info_type_fields[]
71 	= NG_NETFLOW_INFO_TYPE;
72 static const struct ng_parse_type ng_netflow_info_type = {
73 	&ng_parse_struct_type,
74 	&ng_netflow_info_type_fields
75 };
76 
77 /*  Parse type for struct ng_netflow_ifinfo */
78 static const struct ng_parse_struct_field ng_netflow_ifinfo_type_fields[]
79 	= NG_NETFLOW_IFINFO_TYPE;
80 static const struct ng_parse_type ng_netflow_ifinfo_type = {
81 	&ng_parse_struct_type,
82 	&ng_netflow_ifinfo_type_fields
83 };
84 
85 /* Parse type for struct ng_netflow_setdlt */
86 static const struct ng_parse_struct_field ng_netflow_setdlt_type_fields[]
87 	= NG_NETFLOW_SETDLT_TYPE;
88 static const struct ng_parse_type ng_netflow_setdlt_type = {
89 	&ng_parse_struct_type,
90 	&ng_netflow_setdlt_type_fields
91 };
92 
93 /* Parse type for ng_netflow_setifindex */
94 static const struct ng_parse_struct_field ng_netflow_setifindex_type_fields[]
95 	= NG_NETFLOW_SETIFINDEX_TYPE;
96 static const struct ng_parse_type ng_netflow_setifindex_type = {
97 	&ng_parse_struct_type,
98 	&ng_netflow_setifindex_type_fields
99 };
100 
101 /* Parse type for ng_netflow_settimeouts */
102 static const struct ng_parse_struct_field ng_netflow_settimeouts_type_fields[]
103 	= NG_NETFLOW_SETTIMEOUTS_TYPE;
104 static const struct ng_parse_type ng_netflow_settimeouts_type = {
105 	&ng_parse_struct_type,
106 	&ng_netflow_settimeouts_type_fields
107 };
108 
109 /* Parse type for ng_netflow_setconfig */
110 static const struct ng_parse_struct_field ng_netflow_setconfig_type_fields[]
111 	= NG_NETFLOW_SETCONFIG_TYPE;
112 static const struct ng_parse_type ng_netflow_setconfig_type = {
113 	&ng_parse_struct_type,
114 	&ng_netflow_setconfig_type_fields
115 };
116 
117 /* List of commands and how to convert arguments to/from ASCII */
118 static const struct ng_cmdlist ng_netflow_cmds[] = {
119        {
120 	 NGM_NETFLOW_COOKIE,
121 	 NGM_NETFLOW_INFO,
122 	 "info",
123 	 NULL,
124 	 &ng_netflow_info_type
125        },
126        {
127 	NGM_NETFLOW_COOKIE,
128 	NGM_NETFLOW_IFINFO,
129 	"ifinfo",
130 	&ng_parse_uint16_type,
131 	&ng_netflow_ifinfo_type
132        },
133        {
134 	NGM_NETFLOW_COOKIE,
135 	NGM_NETFLOW_SETDLT,
136 	"setdlt",
137 	&ng_netflow_setdlt_type,
138 	NULL
139        },
140        {
141 	NGM_NETFLOW_COOKIE,
142 	NGM_NETFLOW_SETIFINDEX,
143 	"setifindex",
144 	&ng_netflow_setifindex_type,
145 	NULL
146        },
147        {
148 	NGM_NETFLOW_COOKIE,
149 	NGM_NETFLOW_SETTIMEOUTS,
150 	"settimeouts",
151 	&ng_netflow_settimeouts_type,
152 	NULL
153        },
154        {
155 	NGM_NETFLOW_COOKIE,
156 	NGM_NETFLOW_SETCONFIG,
157 	"setconfig",
158 	&ng_netflow_setconfig_type,
159 	NULL
160        },
161        { 0 }
162 };
163 
164 
165 /* Netgraph node type descriptor */
166 static struct ng_type ng_netflow_typestruct = {
167 	.version =	NG_ABI_VERSION,
168 	.name =		NG_NETFLOW_NODE_TYPE,
169 	.constructor =	ng_netflow_constructor,
170 	.rcvmsg =	ng_netflow_rcvmsg,
171 	.close =	ng_netflow_close,
172 	.shutdown =	ng_netflow_rmnode,
173 	.newhook =	ng_netflow_newhook,
174 	.rcvdata =	ng_netflow_rcvdata,
175 	.disconnect =	ng_netflow_disconnect,
176 	.cmdlist =	ng_netflow_cmds,
177 };
178 NETGRAPH_INIT(netflow, &ng_netflow_typestruct);
179 
180 /* Called at node creation */
181 static int
182 ng_netflow_constructor(node_p node)
183 {
184 	priv_p priv;
185 	int error = 0, i;
186 
187 	/* Initialize private data */
188 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT);
189 	if (priv == NULL)
190 		return (ENOMEM);
191 	bzero(priv, sizeof(*priv));
192 
193 	/* Make node and its data point at each other */
194 	NG_NODE_SET_PRIVATE(node, priv);
195 	priv->node = node;
196 
197 	/* Initialize timeouts to default values */
198 	priv->info.nfinfo_inact_t = INACTIVE_TIMEOUT;
199 	priv->info.nfinfo_act_t = ACTIVE_TIMEOUT;
200 
201 	/* Set default config */
202 	for (i = 0; i < NG_NETFLOW_MAXIFACES; i++)
203 		priv->ifaces[i].info.conf = NG_NETFLOW_CONF_INGRESS;
204 
205 	/* Initialize callout handle */
206 	callout_init(&priv->exp_callout, CALLOUT_MPSAFE);
207 
208 	/* Allocate memory and set up flow cache */
209 	if ((error = ng_netflow_cache_init(priv)))
210 		return (error);
211 
212 	return (0);
213 }
214 
215 /*
216  * ng_netflow supports two hooks: data and export.
217  * Incoming traffic is expected on data, and expired
218  * netflow datagrams are sent to export.
219  */
220 static int
221 ng_netflow_newhook(node_p node, hook_p hook, const char *name)
222 {
223 	const priv_p priv = NG_NODE_PRIVATE(node);
224 
225 	if (strncmp(name, NG_NETFLOW_HOOK_DATA,	/* an iface hook? */
226 	    strlen(NG_NETFLOW_HOOK_DATA)) == 0) {
227 		iface_p iface;
228 		int ifnum = -1;
229 		const char *cp;
230 		char *eptr;
231 
232 		cp = name + strlen(NG_NETFLOW_HOOK_DATA);
233 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
234 			return (EINVAL);
235 
236 		ifnum = (int)strtoul(cp, &eptr, 10);
237 		if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
238 			return (EINVAL);
239 
240 		/* See if hook is already connected */
241 		if (priv->ifaces[ifnum].hook != NULL)
242 			return (EISCONN);
243 
244 		iface = &priv->ifaces[ifnum];
245 
246 		/* Link private info and hook together */
247 		NG_HOOK_SET_PRIVATE(hook, iface);
248 		iface->hook = hook;
249 
250 		/*
251 		 * In most cases traffic accounting is done on an
252 		 * Ethernet interface, so default data link type
253 		 * will be DLT_EN10MB.
254 		 */
255 		iface->info.ifinfo_dlt = DLT_EN10MB;
256 
257 	} else if (strncmp(name, NG_NETFLOW_HOOK_OUT,
258 	    strlen(NG_NETFLOW_HOOK_OUT)) == 0) {
259 		iface_p iface;
260 		int ifnum = -1;
261 		const char *cp;
262 		char *eptr;
263 
264 		cp = name + strlen(NG_NETFLOW_HOOK_OUT);
265 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
266 			return (EINVAL);
267 
268 		ifnum = (int)strtoul(cp, &eptr, 10);
269 		if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
270 			return (EINVAL);
271 
272 		/* See if hook is already connected */
273 		if (priv->ifaces[ifnum].out != NULL)
274 			return (EISCONN);
275 
276 		iface = &priv->ifaces[ifnum];
277 
278 		/* Link private info and hook together */
279 		NG_HOOK_SET_PRIVATE(hook, iface);
280 		iface->out = hook;
281 
282 	} else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT) == 0) {
283 
284 		if (priv->export != NULL)
285 			return (EISCONN);
286 
287 		priv->export = hook;
288 
289 		/* Exporter is ready. Let's schedule expiry. */
290 		callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
291 		    (void *)priv);
292 	} else
293 		return (EINVAL);
294 
295 	return (0);
296 }
297 
298 /* Get a netgraph control message. */
299 static int
300 ng_netflow_rcvmsg (node_p node, item_p item, hook_p lasthook)
301 {
302 	const priv_p priv = NG_NODE_PRIVATE(node);
303 	struct ng_mesg *resp = NULL;
304 	int error = 0;
305 	struct ng_mesg *msg;
306 
307 	NGI_GET_MSG(item, msg);
308 
309 	/* Deal with message according to cookie and command */
310 	switch (msg->header.typecookie) {
311 	case NGM_NETFLOW_COOKIE:
312 		switch (msg->header.cmd) {
313 		case NGM_NETFLOW_INFO:
314 		{
315 			struct ng_netflow_info *i;
316 
317 			NG_MKRESPONSE(resp, msg, sizeof(struct ng_netflow_info),
318 			    M_NOWAIT);
319 			i = (struct ng_netflow_info *)resp->data;
320 			ng_netflow_copyinfo(priv, i);
321 
322 			break;
323 		}
324 		case NGM_NETFLOW_IFINFO:
325 		{
326 			struct ng_netflow_ifinfo *i;
327 			const uint16_t *index;
328 
329 			if (msg->header.arglen != sizeof(uint16_t))
330 				 ERROUT(EINVAL);
331 
332 			index  = (uint16_t *)msg->data;
333 			if (*index >= NG_NETFLOW_MAXIFACES)
334 				ERROUT(EINVAL);
335 
336 			/* connected iface? */
337 			if (priv->ifaces[*index].hook == NULL)
338 				 ERROUT(EINVAL);
339 
340 			NG_MKRESPONSE(resp, msg,
341 			     sizeof(struct ng_netflow_ifinfo), M_NOWAIT);
342 			i = (struct ng_netflow_ifinfo *)resp->data;
343 			memcpy((void *)i, (void *)&priv->ifaces[*index].info,
344 			    sizeof(priv->ifaces[*index].info));
345 
346 			break;
347 		}
348 		case NGM_NETFLOW_SETDLT:
349 		{
350 			struct ng_netflow_setdlt *set;
351 			struct ng_netflow_iface *iface;
352 
353 			if (msg->header.arglen != sizeof(struct ng_netflow_setdlt))
354 				ERROUT(EINVAL);
355 
356 			set = (struct ng_netflow_setdlt *)msg->data;
357 			if (set->iface >= NG_NETFLOW_MAXIFACES)
358 				ERROUT(EINVAL);
359 			iface = &priv->ifaces[set->iface];
360 
361 			/* connected iface? */
362 			if (iface->hook == NULL)
363 				ERROUT(EINVAL);
364 
365 			switch (set->dlt) {
366 			case	DLT_EN10MB:
367 				iface->info.ifinfo_dlt = DLT_EN10MB;
368 				break;
369 			case	DLT_RAW:
370 				iface->info.ifinfo_dlt = DLT_RAW;
371 				break;
372 			default:
373 				ERROUT(EINVAL);
374 			}
375 			break;
376 		}
377 		case NGM_NETFLOW_SETIFINDEX:
378 		{
379 			struct ng_netflow_setifindex *set;
380 			struct ng_netflow_iface *iface;
381 
382 			if (msg->header.arglen != sizeof(struct ng_netflow_setifindex))
383 				ERROUT(EINVAL);
384 
385 			set = (struct ng_netflow_setifindex *)msg->data;
386 			if (set->iface >= NG_NETFLOW_MAXIFACES)
387 				ERROUT(EINVAL);
388 			iface = &priv->ifaces[set->iface];
389 
390 			/* connected iface? */
391 			if (iface->hook == NULL)
392 				ERROUT(EINVAL);
393 
394 			iface->info.ifinfo_index = set->index;
395 
396 			break;
397 		}
398 		case NGM_NETFLOW_SETTIMEOUTS:
399 		{
400 			struct ng_netflow_settimeouts *set;
401 
402 			if (msg->header.arglen != sizeof(struct ng_netflow_settimeouts))
403 				ERROUT(EINVAL);
404 
405 			set = (struct ng_netflow_settimeouts *)msg->data;
406 
407 			priv->info.nfinfo_inact_t = set->inactive_timeout;
408 			priv->info.nfinfo_act_t = set->active_timeout;
409 
410 			break;
411 		}
412 		case NGM_NETFLOW_SETCONFIG:
413 		{
414 			struct ng_netflow_setconfig *set;
415 
416 			if (msg->header.arglen != sizeof(struct ng_netflow_setconfig))
417 				ERROUT(EINVAL);
418 
419 			set = (struct ng_netflow_setconfig *)msg->data;
420 
421 			if (set->iface >= NG_NETFLOW_MAXIFACES)
422 				ERROUT(EINVAL);
423 
424 			priv->ifaces[set->iface].info.conf = set->conf;
425 
426 			break;
427 		}
428 		case NGM_NETFLOW_SHOW:
429 		{
430 			uint32_t *last;
431 
432 			if (msg->header.arglen != sizeof(uint32_t))
433 				ERROUT(EINVAL);
434 
435 			last = (uint32_t *)msg->data;
436 
437 			NG_MKRESPONSE(resp, msg, NGRESP_SIZE, M_NOWAIT);
438 
439 			if (!resp)
440 				ERROUT(ENOMEM);
441 
442 			error = ng_netflow_flow_show(priv, *last, resp);
443 
444 			break;
445 		}
446 		default:
447 			ERROUT(EINVAL);		/* unknown command */
448 			break;
449 		}
450 		break;
451 	default:
452 		ERROUT(EINVAL);		/* incorrect cookie */
453 		break;
454 	}
455 
456 	/*
457 	 * Take care of synchronous response, if any.
458 	 * Free memory and return.
459 	 */
460 done:
461 	NG_RESPOND_MSG(error, node, item, resp);
462 	NG_FREE_MSG(msg);
463 
464 	return (error);
465 }
466 
467 /* Receive data on hook. */
468 static int
469 ng_netflow_rcvdata (hook_p hook, item_p item)
470 {
471 	const node_p node = NG_HOOK_NODE(hook);
472 	const priv_p priv = NG_NODE_PRIVATE(node);
473 	const iface_p iface = NG_HOOK_PRIVATE(hook);
474 	hook_p out;
475 	struct mbuf *m = NULL;
476 	struct ip *ip;
477 	struct m_tag *mtag;
478 	int pullup_len = 0;
479 	int error = 0, bypass = 0;
480 	unsigned int src_if_index;
481 
482 	if (hook == priv->export) {
483 		/*
484 		 * Data arrived on export hook.
485 		 * This must not happen.
486 		 */
487 		log(LOG_ERR, "ng_netflow: incoming data on export hook!\n");
488 		ERROUT(EINVAL);
489 	};
490 
491 	if (hook == iface->hook) {
492 		if ((iface->info.conf & NG_NETFLOW_CONF_INGRESS) == 0)
493 			bypass = 1;
494 		out = iface->out;
495 	} else if (hook == iface->out) {
496 		if ((iface->info.conf & NG_NETFLOW_CONF_EGRESS) == 0)
497 			bypass = 1;
498 		out = iface->hook;
499 	} else
500 		ERROUT(EINVAL);
501 
502 	if ((!bypass) &&
503 	    (iface->info.conf & (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE))) {
504 		mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
505 		    MTAG_NETFLOW_CALLED, NULL);
506 		while (mtag != NULL) {
507 			if ((iface->info.conf & NG_NETFLOW_CONF_ONCE) ||
508 			    ((ng_ID_t *)(mtag + 1))[0] == NG_NODE_ID(node)) {
509 				bypass = 1;
510 				break;
511 			}
512 			mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
513 			    MTAG_NETFLOW_CALLED, mtag);
514 		}
515 	}
516 
517 	if (bypass) {
518 		if (out == NULL)
519 			ERROUT(ENOTCONN);
520 
521 		NG_FWD_ITEM_HOOK(error, item, out);
522 		return (error);
523 	}
524 
525 	if (iface->info.conf & (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE)) {
526 		mtag = m_tag_alloc(MTAG_NETFLOW, MTAG_NETFLOW_CALLED,
527 		    sizeof(ng_ID_t), M_NOWAIT);
528 		if (mtag) {
529 			((ng_ID_t *)(mtag + 1))[0] = NG_NODE_ID(node);
530 			m_tag_prepend(NGI_M(item), mtag);
531 		}
532 	}
533 
534 	NGI_GET_M(item, m);
535 
536 	/* Increase counters. */
537 	iface->info.ifinfo_packets++;
538 
539 	/*
540 	 * Depending on interface data link type and packet contents
541 	 * we pullup enough data, so that ng_netflow_flow_add() does not
542 	 * need to know about mbuf at all. We keep current length of data
543 	 * needed to be contiguous in pullup_len. mtod() is done at the
544 	 * very end one more time, since m can had changed after pulluping.
545 	 *
546 	 * In case of unrecognized data we don't return error, but just
547 	 * pass data to downstream hook, if it is available.
548 	 */
549 
550 #define	M_CHECK(length)	do {					\
551 	pullup_len += length;					\
552 	if ((m)->m_pkthdr.len < (pullup_len)) {			\
553 		error = EINVAL;					\
554 		goto bypass;					\
555 	} 							\
556 	if ((m)->m_len < (pullup_len) &&			\
557 	   (((m) = m_pullup((m),(pullup_len))) == NULL)) {	\
558 		error = ENOBUFS;				\
559 		goto done;					\
560 	}							\
561 } while (0)
562 
563 	switch (iface->info.ifinfo_dlt) {
564 	case DLT_EN10MB:	/* Ethernet */
565 	    {
566 		struct ether_header *eh;
567 		uint16_t etype;
568 
569 		M_CHECK(sizeof(struct ether_header));
570 		eh = mtod(m, struct ether_header *);
571 
572 		/* Make sure this is IP frame. */
573 		etype = ntohs(eh->ether_type);
574 		switch (etype) {
575 		case ETHERTYPE_IP:
576 			M_CHECK(sizeof(struct ip));
577 			eh = mtod(m, struct ether_header *);
578 			ip = (struct ip *)(eh + 1);
579 			break;
580 		case ETHERTYPE_VLAN:
581 		    {
582 			struct ether_vlan_header *evh;
583 
584 			M_CHECK(sizeof(struct ether_vlan_header) -
585 			    sizeof(struct ether_header));
586 			evh = mtod(m, struct ether_vlan_header *);
587 			if (ntohs(evh->evl_proto) == ETHERTYPE_IP) {
588 				M_CHECK(sizeof(struct ip));
589 				ip = (struct ip *)(evh + 1);
590 				break;
591 			}
592 		    }
593 		default:
594 			goto bypass;	/* pass this frame */
595 		}
596 		break;
597 	    }
598 	case DLT_RAW:		/* IP packets */
599 		M_CHECK(sizeof(struct ip));
600 		ip = mtod(m, struct ip *);
601 		break;
602 	default:
603 		goto bypass;
604 		break;
605 	}
606 
607 	if ((ip->ip_off & htons(IP_OFFMASK)) == 0) {
608 		/*
609 		 * In case of IP header with options, we haven't pulled
610 		 * up enough, yet.
611 		 */
612 		pullup_len += (ip->ip_hl << 2) - sizeof(struct ip);
613 
614 		switch (ip->ip_p) {
615 		case IPPROTO_TCP:
616 			M_CHECK(sizeof(struct tcphdr));
617 			break;
618 		case IPPROTO_UDP:
619 			M_CHECK(sizeof(struct udphdr));
620 			break;
621 		}
622 	}
623 
624 	switch (iface->info.ifinfo_dlt) {
625 	case DLT_EN10MB:
626 	    {
627 		struct ether_header *eh;
628 
629 		eh = mtod(m, struct ether_header *);
630 		switch (ntohs(eh->ether_type)) {
631 		case ETHERTYPE_IP:
632 			ip = (struct ip *)(eh + 1);
633 			break;
634 		case ETHERTYPE_VLAN:
635 		    {
636 			struct ether_vlan_header *evh;
637 
638 			evh = mtod(m, struct ether_vlan_header *);
639 			ip = (struct ip *)(evh + 1);
640 			break;
641 		     }
642 		default:
643 			panic("ng_netflow entered deadcode");
644 		}
645 		break;
646 	     }
647 	case DLT_RAW:
648 		ip = mtod(m, struct ip *);
649 		break;
650 	default:
651 		panic("ng_netflow entered deadcode");
652 	}
653 
654 #undef	M_CHECK
655 
656 	/* Determine packet input interface. Prefer configured. */
657 	src_if_index = 0;
658 	if (hook == iface->out || iface->info.ifinfo_index == 0) {
659 		if (m->m_pkthdr.rcvif != NULL)
660 			src_if_index = m->m_pkthdr.rcvif->if_index;
661 	} else
662 		src_if_index = iface->info.ifinfo_index;
663 
664 	error = ng_netflow_flow_add(priv, ip, src_if_index);
665 
666 bypass:
667 	if (out != NULL) {
668 		/* XXX: error gets overwritten here */
669 		NG_FWD_NEW_DATA(error, item, out, m);
670 		return (error);
671 	}
672 done:
673 	if (item)
674 		NG_FREE_ITEM(item);
675 	if (m)
676 		NG_FREE_M(m);
677 
678 	return (error);
679 }
680 
681 /* We will be shut down in a moment */
682 static int
683 ng_netflow_close(node_p node)
684 {
685 	const priv_p priv = NG_NODE_PRIVATE(node);
686 
687 	callout_drain(&priv->exp_callout);
688 	ng_netflow_cache_flush(priv);
689 
690 	return (0);
691 }
692 
693 /* Do local shutdown processing. */
694 static int
695 ng_netflow_rmnode(node_p node)
696 {
697 	const priv_p priv = NG_NODE_PRIVATE(node);
698 
699 	NG_NODE_SET_PRIVATE(node, NULL);
700 	NG_NODE_UNREF(priv->node);
701 
702 	free(priv, M_NETGRAPH);
703 
704 	return (0);
705 }
706 
707 /* Hook disconnection. */
708 static int
709 ng_netflow_disconnect(hook_p hook)
710 {
711 	node_p node = NG_HOOK_NODE(hook);
712 	priv_p priv = NG_NODE_PRIVATE(node);
713 	iface_p iface = NG_HOOK_PRIVATE(hook);
714 
715 	if (iface != NULL) {
716 		if (iface->hook == hook)
717 			iface->hook = NULL;
718 		if (iface->out == hook)
719 			iface->out = NULL;
720 	}
721 
722 	/* if export hook disconnected stop running expire(). */
723 	if (hook == priv->export) {
724 		callout_drain(&priv->exp_callout);
725 		priv->export = NULL;
726 	}
727 
728 	/* Removal of the last link destroys the node. */
729 	if (NG_NODE_NUMHOOKS(node) == 0)
730 		ng_rmnode_self(node);
731 
732 	return (0);
733 }
734