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