xref: /freebsd-src/sys/netgraph/netflow/ng_netflow.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1a752e82dSGleb Smirnoff /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
45dcd9c10SGleb Smirnoff  * Copyright (c) 2010-2011 Alexander V. Chernikov <melifaro@ipfw.ru>
51d03bd16SGleb Smirnoff  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
6a752e82dSGleb Smirnoff  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
7a752e82dSGleb Smirnoff  * All rights reserved.
8a752e82dSGleb Smirnoff  *
9a752e82dSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
10a752e82dSGleb Smirnoff  * modification, are permitted provided that the following conditions
11a752e82dSGleb Smirnoff  * are met:
12a752e82dSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
13a752e82dSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
14a752e82dSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
15a752e82dSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
16a752e82dSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
17a752e82dSGleb Smirnoff  *
18a752e82dSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a752e82dSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a752e82dSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a752e82dSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a752e82dSGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a752e82dSGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a752e82dSGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a752e82dSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a752e82dSGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a752e82dSGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a752e82dSGleb Smirnoff  * SUCH DAMAGE.
29a752e82dSGleb Smirnoff  *
30a752e82dSGleb Smirnoff  * $SourceForge: ng_netflow.c,v 1.30 2004/09/05 11:37:43 glebius Exp $
31a752e82dSGleb Smirnoff  */
32a752e82dSGleb Smirnoff 
3301f6c1eaSDimitry Andric #include <sys/cdefs.h>
344e19e0d9SAlexander V. Chernikov #include "opt_inet.h"
355dcd9c10SGleb Smirnoff #include "opt_inet6.h"
365dcd9c10SGleb Smirnoff #include "opt_route.h"
375dcd9c10SGleb Smirnoff 
38a752e82dSGleb Smirnoff #include <sys/param.h>
39a752e82dSGleb Smirnoff #include <sys/systm.h>
407ee35ac9SGleb Smirnoff #include <sys/counter.h>
41a752e82dSGleb Smirnoff #include <sys/kernel.h>
42ea8d1492SAlexander V. Chernikov #include <sys/ktr.h>
43dc7359b8SGleb Smirnoff #include <sys/limits.h>
448ec07310SGleb Smirnoff #include <sys/malloc.h>
45a752e82dSGleb Smirnoff #include <sys/mbuf.h>
46a752e82dSGleb Smirnoff #include <sys/socket.h>
4718c54fe6SGleb Smirnoff #include <sys/syslog.h>
48a752e82dSGleb Smirnoff #include <sys/ctype.h>
498ec07310SGleb Smirnoff #include <vm/uma.h>
50a752e82dSGleb Smirnoff 
51a752e82dSGleb Smirnoff #include <net/if.h>
52a752e82dSGleb Smirnoff #include <net/ethernet.h>
535dcd9c10SGleb Smirnoff #include <net/route.h>
54a752e82dSGleb Smirnoff #include <net/if_arp.h>
55a752e82dSGleb Smirnoff #include <net/if_var.h>
563d0d5b21SJustin Hibbits #include <net/if_private.h>
577801dc7cSGleb Smirnoff #include <net/if_vlan_var.h>
58a752e82dSGleb Smirnoff #include <net/bpf.h>
59a752e82dSGleb Smirnoff #include <netinet/in.h>
60a752e82dSGleb Smirnoff #include <netinet/in_systm.h>
61a752e82dSGleb Smirnoff #include <netinet/ip.h>
625dcd9c10SGleb Smirnoff #include <netinet/ip6.h>
631d03bd16SGleb Smirnoff #include <netinet/tcp.h>
641d03bd16SGleb Smirnoff #include <netinet/udp.h>
655dcd9c10SGleb Smirnoff #include <netinet/sctp.h>
66a752e82dSGleb Smirnoff 
67a752e82dSGleb Smirnoff #include <netgraph/ng_message.h>
68a752e82dSGleb Smirnoff #include <netgraph/ng_parse.h>
69a752e82dSGleb Smirnoff #include <netgraph/netgraph.h>
70a752e82dSGleb Smirnoff #include <netgraph/netflow/netflow.h>
715dcd9c10SGleb Smirnoff #include <netgraph/netflow/netflow_v9.h>
72a752e82dSGleb Smirnoff #include <netgraph/netflow/ng_netflow.h>
73a752e82dSGleb Smirnoff 
74a752e82dSGleb Smirnoff /* Netgraph methods */
75a752e82dSGleb Smirnoff static ng_constructor_t	ng_netflow_constructor;
76a752e82dSGleb Smirnoff static ng_rcvmsg_t	ng_netflow_rcvmsg;
77a752e82dSGleb Smirnoff static ng_close_t	ng_netflow_close;
78a752e82dSGleb Smirnoff static ng_shutdown_t	ng_netflow_rmnode;
79a752e82dSGleb Smirnoff static ng_newhook_t	ng_netflow_newhook;
80a752e82dSGleb Smirnoff static ng_rcvdata_t	ng_netflow_rcvdata;
81a752e82dSGleb Smirnoff static ng_disconnect_t	ng_netflow_disconnect;
82a752e82dSGleb Smirnoff 
83a752e82dSGleb Smirnoff /* Parse type for struct ng_netflow_info */
84a752e82dSGleb Smirnoff static const struct ng_parse_struct_field ng_netflow_info_type_fields[]
85a752e82dSGleb Smirnoff 	= NG_NETFLOW_INFO_TYPE;
86a752e82dSGleb Smirnoff static const struct ng_parse_type ng_netflow_info_type = {
87a752e82dSGleb Smirnoff 	&ng_parse_struct_type,
88a752e82dSGleb Smirnoff 	&ng_netflow_info_type_fields
89a752e82dSGleb Smirnoff };
90a752e82dSGleb Smirnoff 
91a752e82dSGleb Smirnoff /*  Parse type for struct ng_netflow_ifinfo */
92a752e82dSGleb Smirnoff static const struct ng_parse_struct_field ng_netflow_ifinfo_type_fields[]
93a752e82dSGleb Smirnoff 	= NG_NETFLOW_IFINFO_TYPE;
94a752e82dSGleb Smirnoff static const struct ng_parse_type ng_netflow_ifinfo_type = {
95a752e82dSGleb Smirnoff 	&ng_parse_struct_type,
96a752e82dSGleb Smirnoff 	&ng_netflow_ifinfo_type_fields
97a752e82dSGleb Smirnoff };
98a752e82dSGleb Smirnoff 
99a752e82dSGleb Smirnoff /* Parse type for struct ng_netflow_setdlt */
100a752e82dSGleb Smirnoff static const struct ng_parse_struct_field ng_netflow_setdlt_type_fields[]
101a752e82dSGleb Smirnoff 	= NG_NETFLOW_SETDLT_TYPE;
102a752e82dSGleb Smirnoff static const struct ng_parse_type ng_netflow_setdlt_type = {
103a752e82dSGleb Smirnoff 	&ng_parse_struct_type,
104a752e82dSGleb Smirnoff 	&ng_netflow_setdlt_type_fields
105a752e82dSGleb Smirnoff };
106a752e82dSGleb Smirnoff 
107a752e82dSGleb Smirnoff /* Parse type for ng_netflow_setifindex */
108a752e82dSGleb Smirnoff static const struct ng_parse_struct_field ng_netflow_setifindex_type_fields[]
109a752e82dSGleb Smirnoff 	= NG_NETFLOW_SETIFINDEX_TYPE;
110a752e82dSGleb Smirnoff static const struct ng_parse_type ng_netflow_setifindex_type = {
111a752e82dSGleb Smirnoff 	&ng_parse_struct_type,
112a752e82dSGleb Smirnoff 	&ng_netflow_setifindex_type_fields
113a752e82dSGleb Smirnoff };
114a752e82dSGleb Smirnoff 
115a752e82dSGleb Smirnoff /* Parse type for ng_netflow_settimeouts */
116a752e82dSGleb Smirnoff static const struct ng_parse_struct_field ng_netflow_settimeouts_type_fields[]
117a752e82dSGleb Smirnoff 	= NG_NETFLOW_SETTIMEOUTS_TYPE;
118a752e82dSGleb Smirnoff static const struct ng_parse_type ng_netflow_settimeouts_type = {
119a752e82dSGleb Smirnoff 	&ng_parse_struct_type,
120a752e82dSGleb Smirnoff 	&ng_netflow_settimeouts_type_fields
121a752e82dSGleb Smirnoff };
122a752e82dSGleb Smirnoff 
1231a6dd095SAlexander Motin /* Parse type for ng_netflow_setconfig */
1241a6dd095SAlexander Motin static const struct ng_parse_struct_field ng_netflow_setconfig_type_fields[]
1251a6dd095SAlexander Motin 	= NG_NETFLOW_SETCONFIG_TYPE;
1261a6dd095SAlexander Motin static const struct ng_parse_type ng_netflow_setconfig_type = {
1271a6dd095SAlexander Motin 	&ng_parse_struct_type,
1281a6dd095SAlexander Motin 	&ng_netflow_setconfig_type_fields
1291a6dd095SAlexander Motin };
1301a6dd095SAlexander Motin 
1315dcd9c10SGleb Smirnoff /* Parse type for ng_netflow_settemplate */
1325dcd9c10SGleb Smirnoff static const struct ng_parse_struct_field ng_netflow_settemplate_type_fields[]
1335dcd9c10SGleb Smirnoff 	= NG_NETFLOW_SETTEMPLATE_TYPE;
1345dcd9c10SGleb Smirnoff static const struct ng_parse_type ng_netflow_settemplate_type = {
1355dcd9c10SGleb Smirnoff 	&ng_parse_struct_type,
1365dcd9c10SGleb Smirnoff 	&ng_netflow_settemplate_type_fields
1375dcd9c10SGleb Smirnoff };
1385dcd9c10SGleb Smirnoff 
1395dcd9c10SGleb Smirnoff /* Parse type for ng_netflow_setmtu */
1405dcd9c10SGleb Smirnoff static const struct ng_parse_struct_field ng_netflow_setmtu_type_fields[]
1415dcd9c10SGleb Smirnoff 	= NG_NETFLOW_SETMTU_TYPE;
1425dcd9c10SGleb Smirnoff static const struct ng_parse_type ng_netflow_setmtu_type = {
1435dcd9c10SGleb Smirnoff 	&ng_parse_struct_type,
1445dcd9c10SGleb Smirnoff 	&ng_netflow_setmtu_type_fields
1455dcd9c10SGleb Smirnoff };
1465dcd9c10SGleb Smirnoff 
14710fcb07cSAlexander V. Chernikov /* Parse type for struct ng_netflow_v9info */
14810fcb07cSAlexander V. Chernikov static const struct ng_parse_struct_field ng_netflow_v9info_type_fields[]
14910fcb07cSAlexander V. Chernikov 	= NG_NETFLOW_V9INFO_TYPE;
15010fcb07cSAlexander V. Chernikov static const struct ng_parse_type ng_netflow_v9info_type = {
15110fcb07cSAlexander V. Chernikov 	&ng_parse_struct_type,
15210fcb07cSAlexander V. Chernikov 	&ng_netflow_v9info_type_fields
15310fcb07cSAlexander V. Chernikov };
15410fcb07cSAlexander V. Chernikov 
155a752e82dSGleb Smirnoff /* List of commands and how to convert arguments to/from ASCII */
156a752e82dSGleb Smirnoff static const struct ng_cmdlist ng_netflow_cmds[] = {
157a752e82dSGleb Smirnoff        {
158a752e82dSGleb Smirnoff 	 NGM_NETFLOW_COOKIE,
159a752e82dSGleb Smirnoff 	 NGM_NETFLOW_INFO,
160a752e82dSGleb Smirnoff 	 "info",
161a752e82dSGleb Smirnoff 	 NULL,
162a752e82dSGleb Smirnoff 	 &ng_netflow_info_type
163a752e82dSGleb Smirnoff        },
164a752e82dSGleb Smirnoff        {
165a752e82dSGleb Smirnoff 	NGM_NETFLOW_COOKIE,
166a752e82dSGleb Smirnoff 	NGM_NETFLOW_IFINFO,
167a752e82dSGleb Smirnoff 	"ifinfo",
168176119c4SGleb Smirnoff 	&ng_parse_uint16_type,
169a752e82dSGleb Smirnoff 	&ng_netflow_ifinfo_type
170a752e82dSGleb Smirnoff        },
171a752e82dSGleb Smirnoff        {
172a752e82dSGleb Smirnoff 	NGM_NETFLOW_COOKIE,
173a752e82dSGleb Smirnoff 	NGM_NETFLOW_SETDLT,
174a752e82dSGleb Smirnoff 	"setdlt",
175a752e82dSGleb Smirnoff 	&ng_netflow_setdlt_type,
176a752e82dSGleb Smirnoff 	NULL
177a752e82dSGleb Smirnoff        },
178a752e82dSGleb Smirnoff        {
179a752e82dSGleb Smirnoff 	NGM_NETFLOW_COOKIE,
180a752e82dSGleb Smirnoff 	NGM_NETFLOW_SETIFINDEX,
181a752e82dSGleb Smirnoff 	"setifindex",
182a752e82dSGleb Smirnoff 	&ng_netflow_setifindex_type,
183a752e82dSGleb Smirnoff 	NULL
184a752e82dSGleb Smirnoff        },
185a752e82dSGleb Smirnoff        {
186a752e82dSGleb Smirnoff 	NGM_NETFLOW_COOKIE,
187a752e82dSGleb Smirnoff 	NGM_NETFLOW_SETTIMEOUTS,
188a752e82dSGleb Smirnoff 	"settimeouts",
189a752e82dSGleb Smirnoff 	&ng_netflow_settimeouts_type,
190a752e82dSGleb Smirnoff 	NULL
191a752e82dSGleb Smirnoff        },
1921a6dd095SAlexander Motin        {
1931a6dd095SAlexander Motin 	NGM_NETFLOW_COOKIE,
1941a6dd095SAlexander Motin 	NGM_NETFLOW_SETCONFIG,
1951a6dd095SAlexander Motin 	"setconfig",
1961a6dd095SAlexander Motin 	&ng_netflow_setconfig_type,
1971a6dd095SAlexander Motin 	NULL
1981a6dd095SAlexander Motin        },
1995dcd9c10SGleb Smirnoff        {
2005dcd9c10SGleb Smirnoff 	NGM_NETFLOW_COOKIE,
2015dcd9c10SGleb Smirnoff 	NGM_NETFLOW_SETTEMPLATE,
2025dcd9c10SGleb Smirnoff 	"settemplate",
2035dcd9c10SGleb Smirnoff 	&ng_netflow_settemplate_type,
2045dcd9c10SGleb Smirnoff 	NULL
2055dcd9c10SGleb Smirnoff        },
2065dcd9c10SGleb Smirnoff        {
2075dcd9c10SGleb Smirnoff 	NGM_NETFLOW_COOKIE,
2085dcd9c10SGleb Smirnoff 	NGM_NETFLOW_SETMTU,
2095dcd9c10SGleb Smirnoff 	"setmtu",
2105dcd9c10SGleb Smirnoff 	&ng_netflow_setmtu_type,
2115dcd9c10SGleb Smirnoff 	NULL
2125dcd9c10SGleb Smirnoff        },
21310fcb07cSAlexander V. Chernikov        {
21410fcb07cSAlexander V. Chernikov 	 NGM_NETFLOW_COOKIE,
21510fcb07cSAlexander V. Chernikov 	 NGM_NETFLOW_V9INFO,
21610fcb07cSAlexander V. Chernikov 	 "v9info",
21710fcb07cSAlexander V. Chernikov 	 NULL,
21810fcb07cSAlexander V. Chernikov 	 &ng_netflow_v9info_type
21910fcb07cSAlexander V. Chernikov        },
220a752e82dSGleb Smirnoff        { 0 }
221a752e82dSGleb Smirnoff };
222a752e82dSGleb Smirnoff 
223a752e82dSGleb Smirnoff /* Netgraph node type descriptor */
224a752e82dSGleb Smirnoff static struct ng_type ng_netflow_typestruct = {
225a752e82dSGleb Smirnoff 	.version =	NG_ABI_VERSION,
226a752e82dSGleb Smirnoff 	.name =		NG_NETFLOW_NODE_TYPE,
227a752e82dSGleb Smirnoff 	.constructor =	ng_netflow_constructor,
228a752e82dSGleb Smirnoff 	.rcvmsg =	ng_netflow_rcvmsg,
229a752e82dSGleb Smirnoff 	.close =	ng_netflow_close,
230a752e82dSGleb Smirnoff 	.shutdown =	ng_netflow_rmnode,
231a752e82dSGleb Smirnoff 	.newhook =	ng_netflow_newhook,
232a752e82dSGleb Smirnoff 	.rcvdata =	ng_netflow_rcvdata,
233a752e82dSGleb Smirnoff 	.disconnect =	ng_netflow_disconnect,
234a752e82dSGleb Smirnoff 	.cmdlist =	ng_netflow_cmds,
235a752e82dSGleb Smirnoff };
236a752e82dSGleb Smirnoff NETGRAPH_INIT(netflow, &ng_netflow_typestruct);
237a752e82dSGleb Smirnoff 
238a752e82dSGleb Smirnoff /* Called at node creation */
239a752e82dSGleb Smirnoff static int
ng_netflow_constructor(node_p node)240a752e82dSGleb Smirnoff ng_netflow_constructor(node_p node)
241a752e82dSGleb Smirnoff {
242a752e82dSGleb Smirnoff 	priv_p priv;
243b6770143SGleb Smirnoff 	int i;
244a752e82dSGleb Smirnoff 
245a752e82dSGleb Smirnoff 	/* Initialize private data */
246674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
247a752e82dSGleb Smirnoff 
24814797255SAlexander V. Chernikov 	/* Initialize fib data */
24914797255SAlexander V. Chernikov 	priv->maxfibs = rt_numfibs;
25014797255SAlexander V. Chernikov 	priv->fib_data = malloc(sizeof(fib_export_p) * priv->maxfibs,
25114797255SAlexander V. Chernikov 	    M_NETGRAPH, M_WAITOK | M_ZERO);
25214797255SAlexander V. Chernikov 
253a752e82dSGleb Smirnoff 	/* Make node and its data point at each other */
254a752e82dSGleb Smirnoff 	NG_NODE_SET_PRIVATE(node, priv);
255a752e82dSGleb Smirnoff 	priv->node = node;
256a752e82dSGleb Smirnoff 
257a752e82dSGleb Smirnoff 	/* Initialize timeouts to default values */
2587ee35ac9SGleb Smirnoff 	priv->nfinfo_inact_t = INACTIVE_TIMEOUT;
2597ee35ac9SGleb Smirnoff 	priv->nfinfo_act_t = ACTIVE_TIMEOUT;
260a752e82dSGleb Smirnoff 
2611a6dd095SAlexander Motin 	/* Set default config */
2621a6dd095SAlexander Motin 	for (i = 0; i < NG_NETFLOW_MAXIFACES; i++)
2631a6dd095SAlexander Motin 		priv->ifaces[i].info.conf = NG_NETFLOW_CONF_INGRESS;
2641a6dd095SAlexander Motin 
265a752e82dSGleb Smirnoff 	/* Initialize callout handle */
266fd90e2edSJung-uk Kim 	callout_init(&priv->exp_callout, 1);
267a752e82dSGleb Smirnoff 
268a752e82dSGleb Smirnoff 	/* Allocate memory and set up flow cache */
269b6770143SGleb Smirnoff 	ng_netflow_cache_init(priv);
270a752e82dSGleb Smirnoff 
271a752e82dSGleb Smirnoff 	return (0);
272a752e82dSGleb Smirnoff }
273a752e82dSGleb Smirnoff 
274a752e82dSGleb Smirnoff /*
275a752e82dSGleb Smirnoff  * ng_netflow supports two hooks: data and export.
276a752e82dSGleb Smirnoff  * Incoming traffic is expected on data, and expired
277a752e82dSGleb Smirnoff  * netflow datagrams are sent to export.
278a752e82dSGleb Smirnoff  */
279a752e82dSGleb Smirnoff static int
ng_netflow_newhook(node_p node,hook_p hook,const char * name)280a752e82dSGleb Smirnoff ng_netflow_newhook(node_p node, hook_p hook, const char *name)
281a752e82dSGleb Smirnoff {
282a752e82dSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
283a752e82dSGleb Smirnoff 
284a752e82dSGleb Smirnoff 	if (strncmp(name, NG_NETFLOW_HOOK_DATA,	/* an iface hook? */
285a752e82dSGleb Smirnoff 	    strlen(NG_NETFLOW_HOOK_DATA)) == 0) {
286a752e82dSGleb Smirnoff 		iface_p iface;
287a752e82dSGleb Smirnoff 		int ifnum = -1;
288a752e82dSGleb Smirnoff 		const char *cp;
289a752e82dSGleb Smirnoff 		char *eptr;
290a752e82dSGleb Smirnoff 
291a752e82dSGleb Smirnoff 		cp = name + strlen(NG_NETFLOW_HOOK_DATA);
292a752e82dSGleb Smirnoff 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
293a752e82dSGleb Smirnoff 			return (EINVAL);
294a752e82dSGleb Smirnoff 
295a752e82dSGleb Smirnoff 		ifnum = (int)strtoul(cp, &eptr, 10);
296a752e82dSGleb Smirnoff 		if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
297a752e82dSGleb Smirnoff 			return (EINVAL);
298a752e82dSGleb Smirnoff 
299a752e82dSGleb Smirnoff 		/* See if hook is already connected */
300a752e82dSGleb Smirnoff 		if (priv->ifaces[ifnum].hook != NULL)
301a752e82dSGleb Smirnoff 			return (EISCONN);
302a752e82dSGleb Smirnoff 
303a752e82dSGleb Smirnoff 		iface = &priv->ifaces[ifnum];
304a752e82dSGleb Smirnoff 
305a752e82dSGleb Smirnoff 		/* Link private info and hook together */
306a752e82dSGleb Smirnoff 		NG_HOOK_SET_PRIVATE(hook, iface);
307a752e82dSGleb Smirnoff 		iface->hook = hook;
308a752e82dSGleb Smirnoff 
309a752e82dSGleb Smirnoff 		/*
310a752e82dSGleb Smirnoff 		 * In most cases traffic accounting is done on an
311a752e82dSGleb Smirnoff 		 * Ethernet interface, so default data link type
312a752e82dSGleb Smirnoff 		 * will be DLT_EN10MB.
313a752e82dSGleb Smirnoff 		 */
314a752e82dSGleb Smirnoff 		iface->info.ifinfo_dlt = DLT_EN10MB;
315a752e82dSGleb Smirnoff 
3169818b82fSGleb Smirnoff 	} else if (strncmp(name, NG_NETFLOW_HOOK_OUT,
3179818b82fSGleb Smirnoff 	    strlen(NG_NETFLOW_HOOK_OUT)) == 0) {
3189818b82fSGleb Smirnoff 		iface_p iface;
3199818b82fSGleb Smirnoff 		int ifnum = -1;
3209818b82fSGleb Smirnoff 		const char *cp;
3219818b82fSGleb Smirnoff 		char *eptr;
3229818b82fSGleb Smirnoff 
3239818b82fSGleb Smirnoff 		cp = name + strlen(NG_NETFLOW_HOOK_OUT);
3249818b82fSGleb Smirnoff 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
3259818b82fSGleb Smirnoff 			return (EINVAL);
3269818b82fSGleb Smirnoff 
3279818b82fSGleb Smirnoff 		ifnum = (int)strtoul(cp, &eptr, 10);
3289818b82fSGleb Smirnoff 		if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
3299818b82fSGleb Smirnoff 			return (EINVAL);
3309818b82fSGleb Smirnoff 
3319818b82fSGleb Smirnoff 		/* See if hook is already connected */
3329818b82fSGleb Smirnoff 		if (priv->ifaces[ifnum].out != NULL)
3339818b82fSGleb Smirnoff 			return (EISCONN);
3349818b82fSGleb Smirnoff 
3359818b82fSGleb Smirnoff 		iface = &priv->ifaces[ifnum];
3369818b82fSGleb Smirnoff 
3379818b82fSGleb Smirnoff 		/* Link private info and hook together */
3389818b82fSGleb Smirnoff 		NG_HOOK_SET_PRIVATE(hook, iface);
3399818b82fSGleb Smirnoff 		iface->out = hook;
3409818b82fSGleb Smirnoff 
341a752e82dSGleb Smirnoff 	} else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT) == 0) {
342a752e82dSGleb Smirnoff 		if (priv->export != NULL)
343a752e82dSGleb Smirnoff 			return (EISCONN);
344a752e82dSGleb Smirnoff 
3455dcd9c10SGleb Smirnoff 		/* Netflow version 5 supports 32-bit counters only */
3465dcd9c10SGleb Smirnoff 		if (CNTR_MAX == UINT64_MAX)
3475dcd9c10SGleb Smirnoff 			return (EINVAL);
3485dcd9c10SGleb Smirnoff 
349a752e82dSGleb Smirnoff 		priv->export = hook;
350a752e82dSGleb Smirnoff 
351102fe25eSAlexander Motin 		/* Exporter is ready. Let's schedule expiry. */
352102fe25eSAlexander Motin 		callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
353102fe25eSAlexander Motin 		    (void *)priv);
3545dcd9c10SGleb Smirnoff 	} else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT9) == 0) {
3555dcd9c10SGleb Smirnoff 		if (priv->export9 != NULL)
3565dcd9c10SGleb Smirnoff 			return (EISCONN);
3575dcd9c10SGleb Smirnoff 
3585dcd9c10SGleb Smirnoff 		priv->export9 = hook;
3595dcd9c10SGleb Smirnoff 
3605dcd9c10SGleb Smirnoff 		/* Exporter is ready. Let's schedule expiry. */
3615dcd9c10SGleb Smirnoff 		callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
3625dcd9c10SGleb Smirnoff 		    (void *)priv);
363a752e82dSGleb Smirnoff 	} else
364a752e82dSGleb Smirnoff 		return (EINVAL);
365a752e82dSGleb Smirnoff 
366a752e82dSGleb Smirnoff 	return (0);
367a752e82dSGleb Smirnoff }
368a752e82dSGleb Smirnoff 
369a752e82dSGleb Smirnoff /* Get a netgraph control message. */
370a752e82dSGleb Smirnoff static int
ng_netflow_rcvmsg(node_p node,item_p item,hook_p lasthook)371a752e82dSGleb Smirnoff ng_netflow_rcvmsg (node_p node, item_p item, hook_p lasthook)
372a752e82dSGleb Smirnoff {
373a752e82dSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
374a752e82dSGleb Smirnoff 	struct ng_mesg *resp = NULL;
375a752e82dSGleb Smirnoff 	int error = 0;
376a752e82dSGleb Smirnoff 	struct ng_mesg *msg;
377a752e82dSGleb Smirnoff 
378a752e82dSGleb Smirnoff 	NGI_GET_MSG(item, msg);
379a752e82dSGleb Smirnoff 
380a752e82dSGleb Smirnoff 	/* Deal with message according to cookie and command */
381a752e82dSGleb Smirnoff 	switch (msg->header.typecookie) {
382a752e82dSGleb Smirnoff 	case NGM_NETFLOW_COOKIE:
383a752e82dSGleb Smirnoff 		switch (msg->header.cmd) {
384a752e82dSGleb Smirnoff 		case NGM_NETFLOW_INFO:
385a752e82dSGleb Smirnoff 		    {
386a752e82dSGleb Smirnoff 			struct ng_netflow_info *i;
387a752e82dSGleb Smirnoff 
388a752e82dSGleb Smirnoff 			NG_MKRESPONSE(resp, msg, sizeof(struct ng_netflow_info),
389a752e82dSGleb Smirnoff 			    M_NOWAIT);
390a752e82dSGleb Smirnoff 			i = (struct ng_netflow_info *)resp->data;
391a752e82dSGleb Smirnoff 			ng_netflow_copyinfo(priv, i);
392a752e82dSGleb Smirnoff 
393a752e82dSGleb Smirnoff 			break;
394a752e82dSGleb Smirnoff 		    }
395a752e82dSGleb Smirnoff 		case NGM_NETFLOW_IFINFO:
396a752e82dSGleb Smirnoff 		    {
397a752e82dSGleb Smirnoff 			struct ng_netflow_ifinfo *i;
398176119c4SGleb Smirnoff 			const uint16_t *index;
399a752e82dSGleb Smirnoff 
400176119c4SGleb Smirnoff 			if (msg->header.arglen != sizeof(uint16_t))
401a752e82dSGleb Smirnoff 				 ERROUT(EINVAL);
402a752e82dSGleb Smirnoff 
403176119c4SGleb Smirnoff 			index  = (uint16_t *)msg->data;
404f3d231b4SGleb Smirnoff 			if (*index >= NG_NETFLOW_MAXIFACES)
405176119c4SGleb Smirnoff 				ERROUT(EINVAL);
406a752e82dSGleb Smirnoff 
407a752e82dSGleb Smirnoff 			/* connected iface? */
408a752e82dSGleb Smirnoff 			if (priv->ifaces[*index].hook == NULL)
409a752e82dSGleb Smirnoff 				 ERROUT(EINVAL);
410a752e82dSGleb Smirnoff 
411a752e82dSGleb Smirnoff 			NG_MKRESPONSE(resp, msg,
412a752e82dSGleb Smirnoff 			     sizeof(struct ng_netflow_ifinfo), M_NOWAIT);
413a752e82dSGleb Smirnoff 			i = (struct ng_netflow_ifinfo *)resp->data;
414a752e82dSGleb Smirnoff 			memcpy((void *)i, (void *)&priv->ifaces[*index].info,
415a752e82dSGleb Smirnoff 			    sizeof(priv->ifaces[*index].info));
416a752e82dSGleb Smirnoff 
417a752e82dSGleb Smirnoff 			break;
418a752e82dSGleb Smirnoff 		    }
419a752e82dSGleb Smirnoff 		case NGM_NETFLOW_SETDLT:
420a752e82dSGleb Smirnoff 		    {
421a752e82dSGleb Smirnoff 			struct ng_netflow_setdlt *set;
422a752e82dSGleb Smirnoff 			struct ng_netflow_iface *iface;
423a752e82dSGleb Smirnoff 
424a23a2dd1SGleb Smirnoff 			if (msg->header.arglen !=
425a23a2dd1SGleb Smirnoff 			    sizeof(struct ng_netflow_setdlt))
426a752e82dSGleb Smirnoff 				ERROUT(EINVAL);
427a752e82dSGleb Smirnoff 
428a752e82dSGleb Smirnoff 			set = (struct ng_netflow_setdlt *)msg->data;
429f3d231b4SGleb Smirnoff 			if (set->iface >= NG_NETFLOW_MAXIFACES)
430176119c4SGleb Smirnoff 				ERROUT(EINVAL);
431a752e82dSGleb Smirnoff 			iface = &priv->ifaces[set->iface];
432a752e82dSGleb Smirnoff 
433a752e82dSGleb Smirnoff 			/* connected iface? */
434a752e82dSGleb Smirnoff 			if (iface->hook == NULL)
435a752e82dSGleb Smirnoff 				ERROUT(EINVAL);
436a752e82dSGleb Smirnoff 
437a752e82dSGleb Smirnoff 			switch (set->dlt) {
438a752e82dSGleb Smirnoff 			case	DLT_EN10MB:
439a752e82dSGleb Smirnoff 				iface->info.ifinfo_dlt = DLT_EN10MB;
440a752e82dSGleb Smirnoff 				break;
441a752e82dSGleb Smirnoff 			case	DLT_RAW:
442a752e82dSGleb Smirnoff 				iface->info.ifinfo_dlt = DLT_RAW;
443a752e82dSGleb Smirnoff 				break;
444a752e82dSGleb Smirnoff 			default:
445a752e82dSGleb Smirnoff 				ERROUT(EINVAL);
446a752e82dSGleb Smirnoff 			}
447a752e82dSGleb Smirnoff 			break;
448a752e82dSGleb Smirnoff 		    }
449a752e82dSGleb Smirnoff 		case NGM_NETFLOW_SETIFINDEX:
450a752e82dSGleb Smirnoff 		    {
451a752e82dSGleb Smirnoff 			struct ng_netflow_setifindex *set;
452a752e82dSGleb Smirnoff 			struct ng_netflow_iface *iface;
453a752e82dSGleb Smirnoff 
454a23a2dd1SGleb Smirnoff 			if (msg->header.arglen !=
455a23a2dd1SGleb Smirnoff 			    sizeof(struct ng_netflow_setifindex))
456a752e82dSGleb Smirnoff 				ERROUT(EINVAL);
457a752e82dSGleb Smirnoff 
458a752e82dSGleb Smirnoff 			set = (struct ng_netflow_setifindex *)msg->data;
459f3d231b4SGleb Smirnoff 			if (set->iface >= NG_NETFLOW_MAXIFACES)
460176119c4SGleb Smirnoff 				ERROUT(EINVAL);
461a752e82dSGleb Smirnoff 			iface = &priv->ifaces[set->iface];
462a752e82dSGleb Smirnoff 
463a752e82dSGleb Smirnoff 			/* connected iface? */
464a752e82dSGleb Smirnoff 			if (iface->hook == NULL)
465a752e82dSGleb Smirnoff 				ERROUT(EINVAL);
466a752e82dSGleb Smirnoff 
467a752e82dSGleb Smirnoff 			iface->info.ifinfo_index = set->index;
468a752e82dSGleb Smirnoff 
469a752e82dSGleb Smirnoff 			break;
470a752e82dSGleb Smirnoff 		    }
471a752e82dSGleb Smirnoff 		case NGM_NETFLOW_SETTIMEOUTS:
472a752e82dSGleb Smirnoff 		    {
473a752e82dSGleb Smirnoff 			struct ng_netflow_settimeouts *set;
474a752e82dSGleb Smirnoff 
475a23a2dd1SGleb Smirnoff 			if (msg->header.arglen !=
476a23a2dd1SGleb Smirnoff 			    sizeof(struct ng_netflow_settimeouts))
477a752e82dSGleb Smirnoff 				ERROUT(EINVAL);
478a752e82dSGleb Smirnoff 
479a752e82dSGleb Smirnoff 			set = (struct ng_netflow_settimeouts *)msg->data;
480a752e82dSGleb Smirnoff 
4817ee35ac9SGleb Smirnoff 			priv->nfinfo_inact_t = set->inactive_timeout;
4827ee35ac9SGleb Smirnoff 			priv->nfinfo_act_t = set->active_timeout;
483a752e82dSGleb Smirnoff 
484a752e82dSGleb Smirnoff 			break;
485a752e82dSGleb Smirnoff 		    }
4861a6dd095SAlexander Motin 		case NGM_NETFLOW_SETCONFIG:
4871a6dd095SAlexander Motin 		    {
4881a6dd095SAlexander Motin 			struct ng_netflow_setconfig *set;
4891a6dd095SAlexander Motin 
490a23a2dd1SGleb Smirnoff 			if (msg->header.arglen !=
491a23a2dd1SGleb Smirnoff 			    sizeof(struct ng_netflow_setconfig))
4921a6dd095SAlexander Motin 				ERROUT(EINVAL);
4931a6dd095SAlexander Motin 
4941a6dd095SAlexander Motin 			set = (struct ng_netflow_setconfig *)msg->data;
4951a6dd095SAlexander Motin 
4961a6dd095SAlexander Motin 			if (set->iface >= NG_NETFLOW_MAXIFACES)
4971a6dd095SAlexander Motin 				ERROUT(EINVAL);
4981a6dd095SAlexander Motin 
4991a6dd095SAlexander Motin 			priv->ifaces[set->iface].info.conf = set->conf;
5001a6dd095SAlexander Motin 
5011a6dd095SAlexander Motin 			break;
5021a6dd095SAlexander Motin 		    }
5035dcd9c10SGleb Smirnoff 		case NGM_NETFLOW_SETTEMPLATE:
5045dcd9c10SGleb Smirnoff 		    {
5055dcd9c10SGleb Smirnoff 			struct ng_netflow_settemplate *set;
5065dcd9c10SGleb Smirnoff 
507a23a2dd1SGleb Smirnoff 			if (msg->header.arglen !=
508a23a2dd1SGleb Smirnoff 			    sizeof(struct ng_netflow_settemplate))
5095dcd9c10SGleb Smirnoff 				ERROUT(EINVAL);
5105dcd9c10SGleb Smirnoff 
5115dcd9c10SGleb Smirnoff 			set = (struct ng_netflow_settemplate *)msg->data;
5125dcd9c10SGleb Smirnoff 
5135dcd9c10SGleb Smirnoff 			priv->templ_packets = set->packets;
5145dcd9c10SGleb Smirnoff 			priv->templ_time = set->time;
5155dcd9c10SGleb Smirnoff 
5165dcd9c10SGleb Smirnoff 			break;
5175dcd9c10SGleb Smirnoff 		    }
5185dcd9c10SGleb Smirnoff 		case NGM_NETFLOW_SETMTU:
5195dcd9c10SGleb Smirnoff 		    {
5205dcd9c10SGleb Smirnoff 			struct ng_netflow_setmtu *set;
5215dcd9c10SGleb Smirnoff 
522a23a2dd1SGleb Smirnoff 			if (msg->header.arglen !=
523a23a2dd1SGleb Smirnoff 			    sizeof(struct ng_netflow_setmtu))
5245dcd9c10SGleb Smirnoff 				ERROUT(EINVAL);
5255dcd9c10SGleb Smirnoff 
5265dcd9c10SGleb Smirnoff 			set = (struct ng_netflow_setmtu *)msg->data;
5275dcd9c10SGleb Smirnoff 			if ((set->mtu < MIN_MTU) || (set->mtu > MAX_MTU))
5285dcd9c10SGleb Smirnoff 				ERROUT(EINVAL);
5295dcd9c10SGleb Smirnoff 
5305dcd9c10SGleb Smirnoff 			priv->mtu = set->mtu;
5315dcd9c10SGleb Smirnoff 
5325dcd9c10SGleb Smirnoff 			break;
5335dcd9c10SGleb Smirnoff 		    }
534a752e82dSGleb Smirnoff 		case NGM_NETFLOW_SHOW:
535a23a2dd1SGleb Smirnoff 			if (msg->header.arglen !=
536a23a2dd1SGleb Smirnoff 			    sizeof(struct ngnf_show_header))
537a752e82dSGleb Smirnoff 				ERROUT(EINVAL);
538a752e82dSGleb Smirnoff 
539a752e82dSGleb Smirnoff 			NG_MKRESPONSE(resp, msg, NGRESP_SIZE, M_NOWAIT);
540a752e82dSGleb Smirnoff 
541a752e82dSGleb Smirnoff 			if (!resp)
542a752e82dSGleb Smirnoff 				ERROUT(ENOMEM);
543a752e82dSGleb Smirnoff 
544ea7e1638SGleb Smirnoff 			error = ng_netflow_flow_show(priv,
545ea7e1638SGleb Smirnoff 			    (struct ngnf_show_header *)msg->data,
546ea7e1638SGleb Smirnoff 			    (struct ngnf_show_header *)resp->data);
547ea7e1638SGleb Smirnoff 
548ea7e1638SGleb Smirnoff 			if (error)
549ea7e1638SGleb Smirnoff 				NG_FREE_MSG(resp);
550a752e82dSGleb Smirnoff 
551a752e82dSGleb Smirnoff 			break;
55210fcb07cSAlexander V. Chernikov 		case NGM_NETFLOW_V9INFO:
55310fcb07cSAlexander V. Chernikov 		    {
55410fcb07cSAlexander V. Chernikov 			struct ng_netflow_v9info *i;
55510fcb07cSAlexander V. Chernikov 
556a23a2dd1SGleb Smirnoff 			NG_MKRESPONSE(resp, msg,
557a23a2dd1SGleb Smirnoff 			    sizeof(struct ng_netflow_v9info), M_NOWAIT);
55810fcb07cSAlexander V. Chernikov 			i = (struct ng_netflow_v9info *)resp->data;
55910fcb07cSAlexander V. Chernikov 			ng_netflow_copyv9info(priv, i);
56010fcb07cSAlexander V. Chernikov 
56110fcb07cSAlexander V. Chernikov 			break;
56210fcb07cSAlexander V. Chernikov 		    }
563a752e82dSGleb Smirnoff 		default:
564a752e82dSGleb Smirnoff 			ERROUT(EINVAL);		/* unknown command */
565a752e82dSGleb Smirnoff 			break;
566a752e82dSGleb Smirnoff 		}
567a752e82dSGleb Smirnoff 		break;
568a752e82dSGleb Smirnoff 	default:
569a752e82dSGleb Smirnoff 		ERROUT(EINVAL);		/* incorrect cookie */
570a752e82dSGleb Smirnoff 		break;
571a752e82dSGleb Smirnoff 	}
572a752e82dSGleb Smirnoff 
573a752e82dSGleb Smirnoff 	/*
574a752e82dSGleb Smirnoff 	 * Take care of synchronous response, if any.
575a752e82dSGleb Smirnoff 	 * Free memory and return.
576a752e82dSGleb Smirnoff 	 */
577a752e82dSGleb Smirnoff done:
578a752e82dSGleb Smirnoff 	NG_RESPOND_MSG(error, node, item, resp);
579a752e82dSGleb Smirnoff 	NG_FREE_MSG(msg);
580a752e82dSGleb Smirnoff 
581a752e82dSGleb Smirnoff 	return (error);
582a752e82dSGleb Smirnoff }
583a752e82dSGleb Smirnoff 
584a752e82dSGleb Smirnoff /* Receive data on hook. */
585a752e82dSGleb Smirnoff static int
ng_netflow_rcvdata(hook_p hook,item_p item)586a752e82dSGleb Smirnoff ng_netflow_rcvdata (hook_p hook, item_p item)
587a752e82dSGleb Smirnoff {
588a752e82dSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
589a752e82dSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
590a752e82dSGleb Smirnoff 	const iface_p iface = NG_HOOK_PRIVATE(hook);
5911a6dd095SAlexander Motin 	hook_p out;
5925dcd9c10SGleb Smirnoff 	struct mbuf *m = NULL, *m_old = NULL;
5935dcd9c10SGleb Smirnoff 	struct ip *ip = NULL;
5945dcd9c10SGleb Smirnoff 	struct ip6_hdr *ip6 = NULL;
5951a6dd095SAlexander Motin 	struct m_tag *mtag;
5962fdf3ed1SJohn Baldwin 	int pullup_len = 0;
5972fdf3ed1SJohn Baldwin 	uint8_t acct = 0, bypass = 0;
5980bd6bb6bSAlexander V. Chernikov 	int error = 0, l3_off = 0;
5992fdf3ed1SJohn Baldwin #if defined(INET) || defined(INET6)
6002fdf3ed1SJohn Baldwin 	int off;
6012fdf3ed1SJohn Baldwin 	uint8_t flags = 0, upper_proto = 0;
6021a6dd095SAlexander Motin 	unsigned int src_if_index;
6035dcd9c10SGleb Smirnoff 	caddr_t upper_ptr = NULL;
6042fdf3ed1SJohn Baldwin #endif
6055dcd9c10SGleb Smirnoff 	fib_export_p fe;
6065dcd9c10SGleb Smirnoff 	uint32_t fib;
607a752e82dSGleb Smirnoff 
6085dcd9c10SGleb Smirnoff 	if ((hook == priv->export) || (hook == priv->export9)) {
609a752e82dSGleb Smirnoff 		/*
610a752e82dSGleb Smirnoff 		 * Data arrived on export hook.
611a752e82dSGleb Smirnoff 		 * This must not happen.
612a752e82dSGleb Smirnoff 		 */
61318c54fe6SGleb Smirnoff 		log(LOG_ERR, "ng_netflow: incoming data on export hook!\n");
614a752e82dSGleb Smirnoff 		ERROUT(EINVAL);
61574b8d63dSPedro F. Giffuni 	}
616a752e82dSGleb Smirnoff 
6171a6dd095SAlexander Motin 	if (hook == iface->hook) {
6181a6dd095SAlexander Motin 		if ((iface->info.conf & NG_NETFLOW_CONF_INGRESS) == 0)
6191a6dd095SAlexander Motin 			bypass = 1;
6201a6dd095SAlexander Motin 		out = iface->out;
6211a6dd095SAlexander Motin 	} else if (hook == iface->out) {
6221a6dd095SAlexander Motin 		if ((iface->info.conf & NG_NETFLOW_CONF_EGRESS) == 0)
6231a6dd095SAlexander Motin 			bypass = 1;
6241a6dd095SAlexander Motin 		out = iface->hook;
6251a6dd095SAlexander Motin 	} else
6261a6dd095SAlexander Motin 		ERROUT(EINVAL);
6271a6dd095SAlexander Motin 
628a23a2dd1SGleb Smirnoff 	if ((!bypass) && (iface->info.conf &
629a23a2dd1SGleb Smirnoff 	    (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE))) {
6301a6dd095SAlexander Motin 		mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
6311a6dd095SAlexander Motin 		    MTAG_NETFLOW_CALLED, NULL);
6321a6dd095SAlexander Motin 		while (mtag != NULL) {
6331a6dd095SAlexander Motin 			if ((iface->info.conf & NG_NETFLOW_CONF_ONCE) ||
6341a6dd095SAlexander Motin 			    ((ng_ID_t *)(mtag + 1))[0] == NG_NODE_ID(node)) {
6351a6dd095SAlexander Motin 				bypass = 1;
6361a6dd095SAlexander Motin 				break;
6371a6dd095SAlexander Motin 			}
6381a6dd095SAlexander Motin 			mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
6391a6dd095SAlexander Motin 			    MTAG_NETFLOW_CALLED, mtag);
6401a6dd095SAlexander Motin 		}
6411a6dd095SAlexander Motin 	}
6421a6dd095SAlexander Motin 
6431a6dd095SAlexander Motin 	if (bypass) {
6441a6dd095SAlexander Motin 		if (out == NULL)
6459818b82fSGleb Smirnoff 			ERROUT(ENOTCONN);
6469818b82fSGleb Smirnoff 
6471a6dd095SAlexander Motin 		NG_FWD_ITEM_HOOK(error, item, out);
6489818b82fSGleb Smirnoff 		return (error);
6499818b82fSGleb Smirnoff 	}
6509818b82fSGleb Smirnoff 
651a23a2dd1SGleb Smirnoff 	if (iface->info.conf &
652a23a2dd1SGleb Smirnoff 	    (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE)) {
6531a6dd095SAlexander Motin 		mtag = m_tag_alloc(MTAG_NETFLOW, MTAG_NETFLOW_CALLED,
6541a6dd095SAlexander Motin 		    sizeof(ng_ID_t), M_NOWAIT);
6551a6dd095SAlexander Motin 		if (mtag) {
6561a6dd095SAlexander Motin 			((ng_ID_t *)(mtag + 1))[0] = NG_NODE_ID(node);
6571a6dd095SAlexander Motin 			m_tag_prepend(NGI_M(item), mtag);
6581a6dd095SAlexander Motin 		}
6591a6dd095SAlexander Motin 	}
6601a6dd095SAlexander Motin 
6612fdf3ed1SJohn Baldwin #if defined(INET) || defined(INET6)
66236374fcfSAlexander V. Chernikov 	/* Import configuration flags related to flow creation */
66336374fcfSAlexander V. Chernikov 	flags = iface->info.conf & NG_NETFLOW_FLOW_FLAGS;
6642fdf3ed1SJohn Baldwin #endif
66536374fcfSAlexander V. Chernikov 
6669818b82fSGleb Smirnoff 	NGI_GET_M(item, m);
6675dcd9c10SGleb Smirnoff 	m_old = m;
6689818b82fSGleb Smirnoff 
6692b38b687SGleb Smirnoff 	/* Increase counters. */
670a752e82dSGleb Smirnoff 	iface->info.ifinfo_packets++;
671a752e82dSGleb Smirnoff 
6722b38b687SGleb Smirnoff 	/*
6732b38b687SGleb Smirnoff 	 * Depending on interface data link type and packet contents
6742b38b687SGleb Smirnoff 	 * we pullup enough data, so that ng_netflow_flow_add() does not
6752b38b687SGleb Smirnoff 	 * need to know about mbuf at all. We keep current length of data
6762b38b687SGleb Smirnoff 	 * needed to be contiguous in pullup_len. mtod() is done at the
6772b38b687SGleb Smirnoff 	 * very end one more time, since m can had changed after pulluping.
6782b38b687SGleb Smirnoff 	 *
6792b38b687SGleb Smirnoff 	 * In case of unrecognized data we don't return error, but just
6802b38b687SGleb Smirnoff 	 * pass data to downstream hook, if it is available.
6812b38b687SGleb Smirnoff 	 */
6822b38b687SGleb Smirnoff 
6832b38b687SGleb Smirnoff #define	M_CHECK(length)	do {					\
6842b38b687SGleb Smirnoff 	pullup_len += length;					\
6855dcd9c10SGleb Smirnoff 	if (((m)->m_pkthdr.len < (pullup_len)) ||		\
6865dcd9c10SGleb Smirnoff 	   ((pullup_len) > MHLEN)) {				\
6872b38b687SGleb Smirnoff 		error = EINVAL;					\
6889818b82fSGleb Smirnoff 		goto bypass;					\
6892b38b687SGleb Smirnoff 	} 							\
6902b38b687SGleb Smirnoff 	if ((m)->m_len < (pullup_len) &&			\
6912b38b687SGleb Smirnoff 	   (((m) = m_pullup((m),(pullup_len))) == NULL)) {	\
6922b38b687SGleb Smirnoff 		error = ENOBUFS;				\
6932b38b687SGleb Smirnoff 		goto done;					\
6942b38b687SGleb Smirnoff 	}							\
6952b38b687SGleb Smirnoff } while (0)
6962b38b687SGleb Smirnoff 
697a752e82dSGleb Smirnoff 	switch (iface->info.ifinfo_dlt) {
698a752e82dSGleb Smirnoff 	case DLT_EN10MB:	/* Ethernet */
699a752e82dSGleb Smirnoff 	    {
700a752e82dSGleb Smirnoff 		struct ether_header *eh;
701a752e82dSGleb Smirnoff 		uint16_t etype;
702a752e82dSGleb Smirnoff 
7032b38b687SGleb Smirnoff 		M_CHECK(sizeof(struct ether_header));
704a752e82dSGleb Smirnoff 		eh = mtod(m, struct ether_header *);
705a752e82dSGleb Smirnoff 
7062b38b687SGleb Smirnoff 		/* Make sure this is IP frame. */
707a752e82dSGleb Smirnoff 		etype = ntohs(eh->ether_type);
708a752e82dSGleb Smirnoff 		switch (etype) {
7094e19e0d9SAlexander V. Chernikov #ifdef INET
710a752e82dSGleb Smirnoff 		case ETHERTYPE_IP:
7112b38b687SGleb Smirnoff 			M_CHECK(sizeof(struct ip));
7122b38b687SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
7132b38b687SGleb Smirnoff 			ip = (struct ip *)(eh + 1);
7140bd6bb6bSAlexander V. Chernikov 			l3_off = sizeof(struct ether_header);
715a752e82dSGleb Smirnoff 			break;
7164e19e0d9SAlexander V. Chernikov #endif
7175dcd9c10SGleb Smirnoff #ifdef INET6
7185dcd9c10SGleb Smirnoff 		case ETHERTYPE_IPV6:
7195dcd9c10SGleb Smirnoff 			/*
7205dcd9c10SGleb Smirnoff 			 * m_pullup() called by M_CHECK() pullups
721a23a2dd1SGleb Smirnoff 			 * kern.ipc.max_protohdr (default 60 bytes)
722a23a2dd1SGleb Smirnoff 			 * which is enough.
7235dcd9c10SGleb Smirnoff 			 */
7245dcd9c10SGleb Smirnoff 			M_CHECK(sizeof(struct ip6_hdr));
7255dcd9c10SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
7265dcd9c10SGleb Smirnoff 			ip6 = (struct ip6_hdr *)(eh + 1);
7270bd6bb6bSAlexander V. Chernikov 			l3_off = sizeof(struct ether_header);
7285dcd9c10SGleb Smirnoff 			break;
7295dcd9c10SGleb Smirnoff #endif
7307801dc7cSGleb Smirnoff 		case ETHERTYPE_VLAN:
7317801dc7cSGleb Smirnoff 		    {
7327801dc7cSGleb Smirnoff 			struct ether_vlan_header *evh;
7337801dc7cSGleb Smirnoff 
7347801dc7cSGleb Smirnoff 			M_CHECK(sizeof(struct ether_vlan_header) -
7357801dc7cSGleb Smirnoff 			    sizeof(struct ether_header));
7367801dc7cSGleb Smirnoff 			evh = mtod(m, struct ether_vlan_header *);
7375dcd9c10SGleb Smirnoff 			etype = ntohs(evh->evl_proto);
7380bd6bb6bSAlexander V. Chernikov 			l3_off = sizeof(struct ether_vlan_header);
7395dcd9c10SGleb Smirnoff 
7405dcd9c10SGleb Smirnoff 			if (etype == ETHERTYPE_IP) {
7414e19e0d9SAlexander V. Chernikov #ifdef INET
7427801dc7cSGleb Smirnoff 				M_CHECK(sizeof(struct ip));
7437801dc7cSGleb Smirnoff 				ip = (struct ip *)(evh + 1);
7447801dc7cSGleb Smirnoff 				break;
7454e19e0d9SAlexander V. Chernikov #endif
7465dcd9c10SGleb Smirnoff #ifdef INET6
7475dcd9c10SGleb Smirnoff 			} else if (etype == ETHERTYPE_IPV6) {
7485dcd9c10SGleb Smirnoff 				M_CHECK(sizeof(struct ip6_hdr));
7495dcd9c10SGleb Smirnoff 				ip6 = (struct ip6_hdr *)(evh + 1);
7505dcd9c10SGleb Smirnoff 				break;
7515dcd9c10SGleb Smirnoff #endif
7527801dc7cSGleb Smirnoff 			}
7537801dc7cSGleb Smirnoff 		    }
754a752e82dSGleb Smirnoff 		default:
7559818b82fSGleb Smirnoff 			goto bypass;	/* pass this frame */
7562b38b687SGleb Smirnoff 		}
7572b38b687SGleb Smirnoff 		break;
7582b38b687SGleb Smirnoff 	    }
7592b38b687SGleb Smirnoff 	case DLT_RAW:		/* IP packets */
7602b38b687SGleb Smirnoff 		M_CHECK(sizeof(struct ip));
7612b38b687SGleb Smirnoff 		ip = mtod(m, struct ip *);
7620bd6bb6bSAlexander V. Chernikov 		/* l3_off is already zero */
7635dcd9c10SGleb Smirnoff #ifdef INET6
764a23a2dd1SGleb Smirnoff 		/*
765a23a2dd1SGleb Smirnoff 		 * If INET6 is not defined IPv6 packets
766a23a2dd1SGleb Smirnoff 		 * will be discarded in ng_netflow_flow_add().
767a23a2dd1SGleb Smirnoff 		 */
7685dcd9c10SGleb Smirnoff 		if (ip->ip_v == IP6VERSION) {
7695dcd9c10SGleb Smirnoff 			ip = NULL;
770f75083f0SAlexander V. Chernikov 			M_CHECK(sizeof(struct ip6_hdr) - sizeof(struct ip));
7715dcd9c10SGleb Smirnoff 			ip6 = mtod(m, struct ip6_hdr *);
7725dcd9c10SGleb Smirnoff 		}
7735dcd9c10SGleb Smirnoff #endif
7744e19e0d9SAlexander V. Chernikov #ifndef INET
7754e19e0d9SAlexander V. Chernikov 		ip = NULL;
7764e19e0d9SAlexander V. Chernikov #endif
7772b38b687SGleb Smirnoff 		break;
7782b38b687SGleb Smirnoff 	default:
7799818b82fSGleb Smirnoff 		goto bypass;
7802b38b687SGleb Smirnoff 		break;
781a752e82dSGleb Smirnoff 	}
782a752e82dSGleb Smirnoff 
7832fdf3ed1SJohn Baldwin #if defined(INET) || defined(INET6)
7845dcd9c10SGleb Smirnoff 	off = pullup_len;
7852fdf3ed1SJohn Baldwin #endif
7865dcd9c10SGleb Smirnoff 
7875dcd9c10SGleb Smirnoff 	if ((ip != NULL) && ((ip->ip_off & htons(IP_OFFMASK)) == 0)) {
7885dcd9c10SGleb Smirnoff 		if ((ip->ip_v != IPVERSION) ||
7895dcd9c10SGleb Smirnoff 		    ((ip->ip_hl << 2) < sizeof(struct ip)))
7905dcd9c10SGleb Smirnoff 			goto bypass;
7912b38b687SGleb Smirnoff 		/*
7925dcd9c10SGleb Smirnoff 		 * In case of IPv4 header with options, we haven't pulled
7932b38b687SGleb Smirnoff 		 * up enough, yet.
7942b38b687SGleb Smirnoff 		 */
7955dcd9c10SGleb Smirnoff 		M_CHECK((ip->ip_hl << 2) - sizeof(struct ip));
7962b38b687SGleb Smirnoff 
7972fdf3ed1SJohn Baldwin #if defined(INET) || defined(INET6)
7985dcd9c10SGleb Smirnoff 		/* Save upper layer offset and proto */
7995dcd9c10SGleb Smirnoff 		off = pullup_len;
8005dcd9c10SGleb Smirnoff 		upper_proto = ip->ip_p;
8012fdf3ed1SJohn Baldwin #endif
8025dcd9c10SGleb Smirnoff 
8035dcd9c10SGleb Smirnoff 		/*
804a23a2dd1SGleb Smirnoff 		 * XXX: in case of wrong upper layer header we will
805a23a2dd1SGleb Smirnoff 		 * forward this packet but skip this record in netflow.
8065dcd9c10SGleb Smirnoff 		 */
8072b38b687SGleb Smirnoff 		switch (ip->ip_p) {
8082b38b687SGleb Smirnoff 		case IPPROTO_TCP:
8092b38b687SGleb Smirnoff 			M_CHECK(sizeof(struct tcphdr));
8102b38b687SGleb Smirnoff 			break;
8112b38b687SGleb Smirnoff 		case IPPROTO_UDP:
8122b38b687SGleb Smirnoff 			M_CHECK(sizeof(struct udphdr));
8132b38b687SGleb Smirnoff 			break;
8145dcd9c10SGleb Smirnoff 		case IPPROTO_SCTP:
8155dcd9c10SGleb Smirnoff 			M_CHECK(sizeof(struct sctphdr));
8165dcd9c10SGleb Smirnoff 			break;
8172b38b687SGleb Smirnoff 		}
8185dcd9c10SGleb Smirnoff 	} else if (ip != NULL) {
819a23a2dd1SGleb Smirnoff 		/*
820a23a2dd1SGleb Smirnoff 		 * Nothing to save except upper layer proto,
821a23a2dd1SGleb Smirnoff 		 * since this is a packet fragment.
822a23a2dd1SGleb Smirnoff 		 */
8232fdf3ed1SJohn Baldwin #if defined(INET) || defined(INET6)
82436374fcfSAlexander V. Chernikov 		flags |= NG_NETFLOW_IS_FRAG;
8255dcd9c10SGleb Smirnoff 		upper_proto = ip->ip_p;
8262fdf3ed1SJohn Baldwin #endif
8275dcd9c10SGleb Smirnoff 		if ((ip->ip_v != IPVERSION) ||
8285dcd9c10SGleb Smirnoff 		    ((ip->ip_hl << 2) < sizeof(struct ip)))
8295dcd9c10SGleb Smirnoff 			goto bypass;
8305dcd9c10SGleb Smirnoff #ifdef INET6
8315dcd9c10SGleb Smirnoff 	} else if (ip6 != NULL) {
8325dcd9c10SGleb Smirnoff 		int cur = ip6->ip6_nxt, hdr_off = 0;
8335dcd9c10SGleb Smirnoff 		struct ip6_ext *ip6e;
8345dcd9c10SGleb Smirnoff 		struct ip6_frag *ip6f;
8355dcd9c10SGleb Smirnoff 
836a23a2dd1SGleb Smirnoff 		if (priv->export9 == NULL)
837a23a2dd1SGleb Smirnoff 			goto bypass;
838a23a2dd1SGleb Smirnoff 
839a23a2dd1SGleb Smirnoff 		/* Save upper layer info. */
8405dcd9c10SGleb Smirnoff 		off = pullup_len;
8415dcd9c10SGleb Smirnoff 		upper_proto = cur;
8425dcd9c10SGleb Smirnoff 
8435dcd9c10SGleb Smirnoff 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
8445dcd9c10SGleb Smirnoff 			goto bypass;
8455dcd9c10SGleb Smirnoff 
846a23a2dd1SGleb Smirnoff 		/*
847053359b7SPedro F. Giffuni 		 * Loop through IPv6 extended headers to get upper
848a23a2dd1SGleb Smirnoff 		 * layer header / frag.
849a23a2dd1SGleb Smirnoff 		 */
850a23a2dd1SGleb Smirnoff 		for (;;) {
8515dcd9c10SGleb Smirnoff 			switch (cur) {
8525dcd9c10SGleb Smirnoff 			/*
853a23a2dd1SGleb Smirnoff 			 * Same as in IPv4, we can forward a 'bad'
854a23a2dd1SGleb Smirnoff 			 * packet without accounting.
8555dcd9c10SGleb Smirnoff 			 */
8565dcd9c10SGleb Smirnoff 			case IPPROTO_TCP:
8575dcd9c10SGleb Smirnoff 				M_CHECK(sizeof(struct tcphdr));
8585dcd9c10SGleb Smirnoff 				goto loopend;
8595dcd9c10SGleb Smirnoff 			case IPPROTO_UDP:
8605dcd9c10SGleb Smirnoff 				M_CHECK(sizeof(struct udphdr));
8615dcd9c10SGleb Smirnoff 				goto loopend;
8625dcd9c10SGleb Smirnoff 			case IPPROTO_SCTP:
8635dcd9c10SGleb Smirnoff 				M_CHECK(sizeof(struct sctphdr));
8645dcd9c10SGleb Smirnoff 				goto loopend;
8655dcd9c10SGleb Smirnoff 
8665dcd9c10SGleb Smirnoff 			/* Loop until 'real' upper layer headers */
8675dcd9c10SGleb Smirnoff 			case IPPROTO_HOPOPTS:
8685dcd9c10SGleb Smirnoff 			case IPPROTO_ROUTING:
8695dcd9c10SGleb Smirnoff 			case IPPROTO_DSTOPTS:
8705dcd9c10SGleb Smirnoff 				M_CHECK(sizeof(struct ip6_ext));
871a23a2dd1SGleb Smirnoff 				ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
872a23a2dd1SGleb Smirnoff 				    off);
8735dcd9c10SGleb Smirnoff 				upper_proto = ip6e->ip6e_nxt;
8745dcd9c10SGleb Smirnoff 				hdr_off = (ip6e->ip6e_len + 1) << 3;
8755dcd9c10SGleb Smirnoff 				break;
8765dcd9c10SGleb Smirnoff 
8775dcd9c10SGleb Smirnoff 			/* RFC4302, can be before DSTOPTS */
8785dcd9c10SGleb Smirnoff 			case IPPROTO_AH:
8795dcd9c10SGleb Smirnoff 				M_CHECK(sizeof(struct ip6_ext));
880a23a2dd1SGleb Smirnoff 				ip6e = (struct ip6_ext *)(mtod(m, caddr_t) +
881a23a2dd1SGleb Smirnoff 				    off);
8825dcd9c10SGleb Smirnoff 				upper_proto = ip6e->ip6e_nxt;
8835dcd9c10SGleb Smirnoff 				hdr_off = (ip6e->ip6e_len + 2) << 2;
8845dcd9c10SGleb Smirnoff 				break;
8855dcd9c10SGleb Smirnoff 
8865dcd9c10SGleb Smirnoff 			case IPPROTO_FRAGMENT:
8875dcd9c10SGleb Smirnoff 				M_CHECK(sizeof(struct ip6_frag));
888a23a2dd1SGleb Smirnoff 				ip6f = (struct ip6_frag *)(mtod(m, caddr_t) +
889a23a2dd1SGleb Smirnoff 				    off);
8905dcd9c10SGleb Smirnoff 				upper_proto = ip6f->ip6f_nxt;
8915dcd9c10SGleb Smirnoff 				hdr_off = sizeof(struct ip6_frag);
8925dcd9c10SGleb Smirnoff 				off += hdr_off;
89336374fcfSAlexander V. Chernikov 				flags |= NG_NETFLOW_IS_FRAG;
8945dcd9c10SGleb Smirnoff 				goto loopend;
8955dcd9c10SGleb Smirnoff 
8965dcd9c10SGleb Smirnoff #if 0
8975dcd9c10SGleb Smirnoff 			case IPPROTO_NONE:
8985dcd9c10SGleb Smirnoff 				goto loopend;
8995dcd9c10SGleb Smirnoff #endif
9000bd6bb6bSAlexander V. Chernikov 			/*
90110d66948SKevin Lo 			 * Any unknown header (new extension or IPv6/IPv4
9020bd6bb6bSAlexander V. Chernikov 			 * header for tunnels) ends loop.
9030bd6bb6bSAlexander V. Chernikov 			 */
9045dcd9c10SGleb Smirnoff 			default:
9055dcd9c10SGleb Smirnoff 				goto loopend;
906ed2fc967SGleb Smirnoff 			}
9072b38b687SGleb Smirnoff 
9085dcd9c10SGleb Smirnoff 			off += hdr_off;
9095dcd9c10SGleb Smirnoff 			cur = upper_proto;
9105dcd9c10SGleb Smirnoff 		}
9115dcd9c10SGleb Smirnoff #endif
9125dcd9c10SGleb Smirnoff 	}
9135dcd9c10SGleb Smirnoff #undef	M_CHECK
9145dcd9c10SGleb Smirnoff 
9155dcd9c10SGleb Smirnoff #ifdef INET6
9165dcd9c10SGleb Smirnoff loopend:
9175dcd9c10SGleb Smirnoff #endif
9185dcd9c10SGleb Smirnoff 	/* Just in case of real reallocation in M_CHECK() / m_pullup() */
9195dcd9c10SGleb Smirnoff 	if (m != m_old) {
9207ee35ac9SGleb Smirnoff 		priv->nfinfo_realloc_mbuf++;
9210bd6bb6bSAlexander V. Chernikov 		/* Restore ip/ipv6 pointer */
9220bd6bb6bSAlexander V. Chernikov 		if (ip != NULL)
9230bd6bb6bSAlexander V. Chernikov 			ip = (struct ip *)(mtod(m, caddr_t) + l3_off);
9240bd6bb6bSAlexander V. Chernikov 		else if (ip6 != NULL)
9250bd6bb6bSAlexander V. Chernikov 			ip6 = (struct ip6_hdr *)(mtod(m, caddr_t) + l3_off);
9265dcd9c10SGleb Smirnoff  	}
927a752e82dSGleb Smirnoff 
9282fdf3ed1SJohn Baldwin #if defined(INET) || defined(INET6)
9295dcd9c10SGleb Smirnoff 	upper_ptr = (caddr_t)(mtod(m, caddr_t) + off);
930a752e82dSGleb Smirnoff 
9311a6dd095SAlexander Motin 	/* Determine packet input interface. Prefer configured. */
9321a6dd095SAlexander Motin 	src_if_index = 0;
9331a6dd095SAlexander Motin 	if (hook == iface->out || iface->info.ifinfo_index == 0) {
9341a6dd095SAlexander Motin 		if (m->m_pkthdr.rcvif != NULL)
9351a6dd095SAlexander Motin 			src_if_index = m->m_pkthdr.rcvif->if_index;
9361a6dd095SAlexander Motin 	} else
9371a6dd095SAlexander Motin 		src_if_index = iface->info.ifinfo_index;
9382fdf3ed1SJohn Baldwin #endif
9391a6dd095SAlexander Motin 
9405dcd9c10SGleb Smirnoff 	/* Check packet FIB */
9415dcd9c10SGleb Smirnoff 	fib = M_GETFIB(m);
94214797255SAlexander V. Chernikov 	if (fib >= priv->maxfibs) {
94314797255SAlexander V. Chernikov 		CTR2(KTR_NET, "ng_netflow_rcvdata(): packet fib %d is out of "
94414797255SAlexander V. Chernikov 		    "range of available fibs: 0 .. %d",
94514797255SAlexander V. Chernikov 		    fib, priv->maxfibs);
9465dcd9c10SGleb Smirnoff 		goto bypass;
9475dcd9c10SGleb Smirnoff 	}
948a752e82dSGleb Smirnoff 
9495dcd9c10SGleb Smirnoff 	if ((fe = priv_to_fib(priv, fib)) == NULL) {
9505dcd9c10SGleb Smirnoff 		/* Setup new FIB */
9515dcd9c10SGleb Smirnoff 		if (ng_netflow_fib_init(priv, fib) != 0) {
9525dcd9c10SGleb Smirnoff 			/* malloc() failed */
9535dcd9c10SGleb Smirnoff 			goto bypass;
9545dcd9c10SGleb Smirnoff 		}
9555dcd9c10SGleb Smirnoff 
9565dcd9c10SGleb Smirnoff 		fe = priv_to_fib(priv, fib);
9575dcd9c10SGleb Smirnoff 	}
9585dcd9c10SGleb Smirnoff 
9594e19e0d9SAlexander V. Chernikov #ifdef INET
9605dcd9c10SGleb Smirnoff 	if (ip != NULL)
961a23a2dd1SGleb Smirnoff 		error = ng_netflow_flow_add(priv, fe, ip, upper_ptr,
962a23a2dd1SGleb Smirnoff 		    upper_proto, flags, src_if_index);
9634e19e0d9SAlexander V. Chernikov #endif
9644e19e0d9SAlexander V. Chernikov #if defined(INET6) && defined(INET)
9654e19e0d9SAlexander V. Chernikov 	else
9664e19e0d9SAlexander V. Chernikov #endif
9675dcd9c10SGleb Smirnoff #ifdef INET6
9684e19e0d9SAlexander V. Chernikov 	if (ip6 != NULL)
969a23a2dd1SGleb Smirnoff 		error = ng_netflow_flow6_add(priv, fe, ip6, upper_ptr,
970a23a2dd1SGleb Smirnoff 		    upper_proto, flags, src_if_index);
9715dcd9c10SGleb Smirnoff #endif
9725dcd9c10SGleb Smirnoff 	else
9735dcd9c10SGleb Smirnoff 		goto bypass;
9745dcd9c10SGleb Smirnoff 
9755dcd9c10SGleb Smirnoff 	acct = 1;
9769818b82fSGleb Smirnoff bypass:
9771a6dd095SAlexander Motin 	if (out != NULL) {
9785dcd9c10SGleb Smirnoff 		if (acct == 0) {
9795dcd9c10SGleb Smirnoff 			/* Accounting failure */
9805dcd9c10SGleb Smirnoff 			if (ip != NULL) {
9817ee35ac9SGleb Smirnoff 				counter_u64_add(priv->nfinfo_spackets, 1);
9827ee35ac9SGleb Smirnoff 				counter_u64_add(priv->nfinfo_sbytes,
9837ee35ac9SGleb Smirnoff 				    m->m_pkthdr.len);
9845dcd9c10SGleb Smirnoff 			} else if (ip6 != NULL) {
9857ee35ac9SGleb Smirnoff 				counter_u64_add(priv->nfinfo_spackets6, 1);
9867ee35ac9SGleb Smirnoff 				counter_u64_add(priv->nfinfo_sbytes6,
9877ee35ac9SGleb Smirnoff 				    m->m_pkthdr.len);
9885dcd9c10SGleb Smirnoff 			}
9895dcd9c10SGleb Smirnoff 		}
9905dcd9c10SGleb Smirnoff 
9919818b82fSGleb Smirnoff 		/* XXX: error gets overwritten here */
9921a6dd095SAlexander Motin 		NG_FWD_NEW_DATA(error, item, out, m);
9939818b82fSGleb Smirnoff 		return (error);
9949818b82fSGleb Smirnoff 	}
995a752e82dSGleb Smirnoff done:
996a752e82dSGleb Smirnoff 	if (item)
997a752e82dSGleb Smirnoff 		NG_FREE_ITEM(item);
9980e406d0fSGleb Smirnoff 	if (m)
999a752e82dSGleb Smirnoff 		NG_FREE_M(m);
1000a752e82dSGleb Smirnoff 
1001a752e82dSGleb Smirnoff 	return (error);
1002a752e82dSGleb Smirnoff }
1003a752e82dSGleb Smirnoff 
1004a752e82dSGleb Smirnoff /* We will be shut down in a moment */
1005a752e82dSGleb Smirnoff static int
ng_netflow_close(node_p node)1006a752e82dSGleb Smirnoff ng_netflow_close(node_p node)
1007a752e82dSGleb Smirnoff {
1008a752e82dSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1009a752e82dSGleb Smirnoff 
1010a752e82dSGleb Smirnoff 	callout_drain(&priv->exp_callout);
1011a752e82dSGleb Smirnoff 	ng_netflow_cache_flush(priv);
1012a752e82dSGleb Smirnoff 
1013a752e82dSGleb Smirnoff 	return (0);
1014a752e82dSGleb Smirnoff }
1015a752e82dSGleb Smirnoff 
1016a752e82dSGleb Smirnoff /* Do local shutdown processing. */
1017a752e82dSGleb Smirnoff static int
ng_netflow_rmnode(node_p node)1018a752e82dSGleb Smirnoff ng_netflow_rmnode(node_p node)
1019a752e82dSGleb Smirnoff {
1020a752e82dSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1021a752e82dSGleb Smirnoff 
1022a752e82dSGleb Smirnoff 	NG_NODE_SET_PRIVATE(node, NULL);
1023a752e82dSGleb Smirnoff 	NG_NODE_UNREF(priv->node);
1024a752e82dSGleb Smirnoff 
102514797255SAlexander V. Chernikov 	free(priv->fib_data, M_NETGRAPH);
10261ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH);
1027a752e82dSGleb Smirnoff 
1028a752e82dSGleb Smirnoff 	return (0);
1029a752e82dSGleb Smirnoff }
1030a752e82dSGleb Smirnoff 
1031a752e82dSGleb Smirnoff /* Hook disconnection. */
1032a752e82dSGleb Smirnoff static int
ng_netflow_disconnect(hook_p hook)1033a752e82dSGleb Smirnoff ng_netflow_disconnect(hook_p hook)
1034a752e82dSGleb Smirnoff {
1035a752e82dSGleb Smirnoff 	node_p node = NG_HOOK_NODE(hook);
1036a752e82dSGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(node);
1037a752e82dSGleb Smirnoff 	iface_p iface = NG_HOOK_PRIVATE(hook);
1038a752e82dSGleb Smirnoff 
1039747cdba4SGleb Smirnoff 	if (iface != NULL) {
1040747cdba4SGleb Smirnoff 		if (iface->hook == hook)
1041a752e82dSGleb Smirnoff 			iface->hook = NULL;
1042747cdba4SGleb Smirnoff 		if (iface->out == hook)
1043747cdba4SGleb Smirnoff 			iface->out = NULL;
1044747cdba4SGleb Smirnoff 	}
1045a752e82dSGleb Smirnoff 
1046102fe25eSAlexander Motin 	/* if export hook disconnected stop running expire(). */
1047102fe25eSAlexander Motin 	if (hook == priv->export) {
10485dcd9c10SGleb Smirnoff 		if (priv->export9 == NULL)
1049102fe25eSAlexander Motin 			callout_drain(&priv->exp_callout);
1050a752e82dSGleb Smirnoff 		priv->export = NULL;
1051102fe25eSAlexander Motin 	}
1052a752e82dSGleb Smirnoff 
10535dcd9c10SGleb Smirnoff 	if (hook == priv->export9) {
10545dcd9c10SGleb Smirnoff 		if (priv->export == NULL)
10555dcd9c10SGleb Smirnoff 			callout_drain(&priv->exp_callout);
10565dcd9c10SGleb Smirnoff 		priv->export9 = NULL;
10575dcd9c10SGleb Smirnoff 	}
10585dcd9c10SGleb Smirnoff 
1059a752e82dSGleb Smirnoff 	/* Removal of the last link destroys the node. */
1060a752e82dSGleb Smirnoff 	if (NG_NODE_NUMHOOKS(node) == 0)
1061a752e82dSGleb Smirnoff 		ng_rmnode_self(node);
1062a752e82dSGleb Smirnoff 
1063a752e82dSGleb Smirnoff 	return (0);
1064a752e82dSGleb Smirnoff }
1065