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