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>
35*805c8e8eSzrj #include <sys/malloc.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
535a975a3dSMatthew Dillon #include "ng_message.h"
545a975a3dSMatthew Dillon #include "ng_parse.h"
555a975a3dSMatthew Dillon #include "netgraph.h"
565a975a3dSMatthew Dillon #include "netflow/netflow.h"
575a975a3dSMatthew 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
ng_netflow_constructor(node_p node)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 */
172a2d0f739SSascha Wildner priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
173b06ebda0SMatthew Dillon if (priv == NULL)
174b06ebda0SMatthew Dillon return (ENOMEM);
175b06ebda0SMatthew Dillon
176b06ebda0SMatthew Dillon /* Make node and its data point at each other */
177b06ebda0SMatthew Dillon NG_NODE_SET_PRIVATE(node, priv);
178b06ebda0SMatthew Dillon priv->node = node;
179b06ebda0SMatthew Dillon
180b06ebda0SMatthew Dillon /* Initialize timeouts to default values */
181b06ebda0SMatthew Dillon priv->info.nfinfo_inact_t = INACTIVE_TIMEOUT;
182b06ebda0SMatthew Dillon priv->info.nfinfo_act_t = ACTIVE_TIMEOUT;
183b06ebda0SMatthew Dillon
184b06ebda0SMatthew Dillon /* Initialize callout handle */
185b06ebda0SMatthew Dillon callout_init(&priv->exp_callout, CALLOUT_MPSAFE);
186b06ebda0SMatthew Dillon
187b06ebda0SMatthew Dillon /* Allocate memory and set up flow cache */
188b06ebda0SMatthew Dillon if ((error = ng_netflow_cache_init(priv)))
189b06ebda0SMatthew Dillon return (error);
190b06ebda0SMatthew Dillon
191b06ebda0SMatthew Dillon return (0);
192b06ebda0SMatthew Dillon }
193b06ebda0SMatthew Dillon
194b06ebda0SMatthew Dillon /*
195b06ebda0SMatthew Dillon * ng_netflow supports two hooks: data and export.
196b06ebda0SMatthew Dillon * Incoming traffic is expected on data, and expired
197b06ebda0SMatthew Dillon * netflow datagrams are sent to export.
198b06ebda0SMatthew Dillon */
199b06ebda0SMatthew Dillon static int
ng_netflow_newhook(node_p node,hook_p hook,const char * name)200b06ebda0SMatthew Dillon ng_netflow_newhook(node_p node, hook_p hook, const char *name)
201b06ebda0SMatthew Dillon {
202b06ebda0SMatthew Dillon const priv_p priv = NG_NODE_PRIVATE(node);
203b06ebda0SMatthew Dillon
204b06ebda0SMatthew Dillon if (strncmp(name, NG_NETFLOW_HOOK_DATA, /* an iface hook? */
205b06ebda0SMatthew Dillon strlen(NG_NETFLOW_HOOK_DATA)) == 0) {
206b06ebda0SMatthew Dillon iface_p iface;
207b06ebda0SMatthew Dillon int ifnum = -1;
208b06ebda0SMatthew Dillon const char *cp;
209b06ebda0SMatthew Dillon char *eptr;
210b06ebda0SMatthew Dillon
211b06ebda0SMatthew Dillon cp = name + strlen(NG_NETFLOW_HOOK_DATA);
212b06ebda0SMatthew Dillon if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
213b06ebda0SMatthew Dillon return (EINVAL);
214b06ebda0SMatthew Dillon
215b06ebda0SMatthew Dillon ifnum = (int)strtoul(cp, &eptr, 10);
216b06ebda0SMatthew Dillon if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
217b06ebda0SMatthew Dillon return (EINVAL);
218b06ebda0SMatthew Dillon
219b06ebda0SMatthew Dillon /* See if hook is already connected */
220b06ebda0SMatthew Dillon if (priv->ifaces[ifnum].hook != NULL)
221b06ebda0SMatthew Dillon return (EISCONN);
222b06ebda0SMatthew Dillon
223b06ebda0SMatthew Dillon iface = &priv->ifaces[ifnum];
224b06ebda0SMatthew Dillon
225b06ebda0SMatthew Dillon /* Link private info and hook together */
226b06ebda0SMatthew Dillon NG_HOOK_SET_PRIVATE(hook, iface);
227b06ebda0SMatthew Dillon iface->hook = hook;
228b06ebda0SMatthew Dillon
229b06ebda0SMatthew Dillon /*
230b06ebda0SMatthew Dillon * In most cases traffic accounting is done on an
231b06ebda0SMatthew Dillon * Ethernet interface, so default data link type
232b06ebda0SMatthew Dillon * will be DLT_EN10MB.
233b06ebda0SMatthew Dillon */
234b06ebda0SMatthew Dillon iface->info.ifinfo_dlt = DLT_EN10MB;
235b06ebda0SMatthew Dillon
236b06ebda0SMatthew Dillon } else if (strncmp(name, NG_NETFLOW_HOOK_OUT,
237b06ebda0SMatthew Dillon strlen(NG_NETFLOW_HOOK_OUT)) == 0) {
238b06ebda0SMatthew Dillon iface_p iface;
239b06ebda0SMatthew Dillon int ifnum = -1;
240b06ebda0SMatthew Dillon const char *cp;
241b06ebda0SMatthew Dillon char *eptr;
242b06ebda0SMatthew Dillon
243b06ebda0SMatthew Dillon cp = name + strlen(NG_NETFLOW_HOOK_OUT);
244b06ebda0SMatthew Dillon if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
245b06ebda0SMatthew Dillon return (EINVAL);
246b06ebda0SMatthew Dillon
247b06ebda0SMatthew Dillon ifnum = (int)strtoul(cp, &eptr, 10);
248b06ebda0SMatthew Dillon if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
249b06ebda0SMatthew Dillon return (EINVAL);
250b06ebda0SMatthew Dillon
251b06ebda0SMatthew Dillon /* See if hook is already connected */
252b06ebda0SMatthew Dillon if (priv->ifaces[ifnum].out != NULL)
253b06ebda0SMatthew Dillon return (EISCONN);
254b06ebda0SMatthew Dillon
255b06ebda0SMatthew Dillon iface = &priv->ifaces[ifnum];
256b06ebda0SMatthew Dillon
257b06ebda0SMatthew Dillon /* Link private info and hook together */
258b06ebda0SMatthew Dillon NG_HOOK_SET_PRIVATE(hook, iface);
259b06ebda0SMatthew Dillon iface->out = hook;
260b06ebda0SMatthew Dillon
261b06ebda0SMatthew Dillon } else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT) == 0) {
262b06ebda0SMatthew Dillon
263b06ebda0SMatthew Dillon if (priv->export != NULL)
264b06ebda0SMatthew Dillon return (EISCONN);
265b06ebda0SMatthew Dillon
266b06ebda0SMatthew Dillon priv->export = hook;
267b06ebda0SMatthew Dillon
268b06ebda0SMatthew Dillon #if 0 /* TODO: profile & test first */
269b06ebda0SMatthew Dillon /*
270b06ebda0SMatthew Dillon * We send export dgrams in interrupt handlers and in
271b06ebda0SMatthew Dillon * callout threads. We'd better queue data for later
272b06ebda0SMatthew Dillon * netgraph ISR processing.
273b06ebda0SMatthew Dillon */
274b06ebda0SMatthew Dillon NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
275b06ebda0SMatthew Dillon #endif
276b06ebda0SMatthew Dillon
277b06ebda0SMatthew Dillon /* Exporter is ready. Let's schedule expiry. */
278b06ebda0SMatthew Dillon callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
279b06ebda0SMatthew Dillon (void *)priv);
280b06ebda0SMatthew Dillon } else
281b06ebda0SMatthew Dillon return (EINVAL);
282b06ebda0SMatthew Dillon
283b06ebda0SMatthew Dillon return (0);
284b06ebda0SMatthew Dillon }
285b06ebda0SMatthew Dillon
286b06ebda0SMatthew Dillon /* Get a netgraph control message. */
287b06ebda0SMatthew Dillon static int
ng_netflow_rcvmsg(node_p node,item_p item,hook_p lasthook)288b06ebda0SMatthew Dillon ng_netflow_rcvmsg (node_p node, item_p item, hook_p lasthook)
289b06ebda0SMatthew Dillon {
290b06ebda0SMatthew Dillon const priv_p priv = NG_NODE_PRIVATE(node);
291b06ebda0SMatthew Dillon struct ng_mesg *resp = NULL;
292b06ebda0SMatthew Dillon int error = 0;
293b06ebda0SMatthew Dillon struct ng_mesg *msg;
294b06ebda0SMatthew Dillon
295b06ebda0SMatthew Dillon NGI_GET_MSG(item, msg);
296b06ebda0SMatthew Dillon
297b06ebda0SMatthew Dillon /* Deal with message according to cookie and command */
298b06ebda0SMatthew Dillon switch (msg->header.typecookie) {
299b06ebda0SMatthew Dillon case NGM_NETFLOW_COOKIE:
300b06ebda0SMatthew Dillon switch (msg->header.cmd) {
301b06ebda0SMatthew Dillon case NGM_NETFLOW_INFO:
302b06ebda0SMatthew Dillon {
303b06ebda0SMatthew Dillon struct ng_netflow_info *i;
304b06ebda0SMatthew Dillon
305b06ebda0SMatthew Dillon NG_MKRESPONSE(resp, msg, sizeof(struct ng_netflow_info),
3065a975a3dSMatthew Dillon M_WAITOK | M_NULLOK);
307b06ebda0SMatthew Dillon i = (struct ng_netflow_info *)resp->data;
308b06ebda0SMatthew Dillon ng_netflow_copyinfo(priv, i);
309b06ebda0SMatthew Dillon
310b06ebda0SMatthew Dillon break;
311b06ebda0SMatthew Dillon }
312b06ebda0SMatthew Dillon case NGM_NETFLOW_IFINFO:
313b06ebda0SMatthew Dillon {
314b06ebda0SMatthew Dillon struct ng_netflow_ifinfo *i;
315b06ebda0SMatthew Dillon const uint16_t *index;
316b06ebda0SMatthew Dillon
317b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(uint16_t))
318b06ebda0SMatthew Dillon ERROUT(EINVAL);
319b06ebda0SMatthew Dillon
320b06ebda0SMatthew Dillon index = (uint16_t *)msg->data;
321b06ebda0SMatthew Dillon if (*index >= NG_NETFLOW_MAXIFACES)
322b06ebda0SMatthew Dillon ERROUT(EINVAL);
323b06ebda0SMatthew Dillon
324b06ebda0SMatthew Dillon /* connected iface? */
325b06ebda0SMatthew Dillon if (priv->ifaces[*index].hook == NULL)
326b06ebda0SMatthew Dillon ERROUT(EINVAL);
327b06ebda0SMatthew Dillon
328b06ebda0SMatthew Dillon NG_MKRESPONSE(resp, msg,
3295a975a3dSMatthew Dillon sizeof(struct ng_netflow_ifinfo), M_WAITOK | M_NULLOK);
330b06ebda0SMatthew Dillon i = (struct ng_netflow_ifinfo *)resp->data;
331b06ebda0SMatthew Dillon memcpy((void *)i, (void *)&priv->ifaces[*index].info,
332b06ebda0SMatthew Dillon sizeof(priv->ifaces[*index].info));
333b06ebda0SMatthew Dillon
334b06ebda0SMatthew Dillon break;
335b06ebda0SMatthew Dillon }
336b06ebda0SMatthew Dillon case NGM_NETFLOW_SETDLT:
337b06ebda0SMatthew Dillon {
338b06ebda0SMatthew Dillon struct ng_netflow_setdlt *set;
339b06ebda0SMatthew Dillon struct ng_netflow_iface *iface;
340b06ebda0SMatthew Dillon
341b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(struct ng_netflow_setdlt))
342b06ebda0SMatthew Dillon ERROUT(EINVAL);
343b06ebda0SMatthew Dillon
344b06ebda0SMatthew Dillon set = (struct ng_netflow_setdlt *)msg->data;
345b06ebda0SMatthew Dillon if (set->iface >= NG_NETFLOW_MAXIFACES)
346b06ebda0SMatthew Dillon ERROUT(EINVAL);
347b06ebda0SMatthew Dillon iface = &priv->ifaces[set->iface];
348b06ebda0SMatthew Dillon
349b06ebda0SMatthew Dillon /* connected iface? */
350b06ebda0SMatthew Dillon if (iface->hook == NULL)
351b06ebda0SMatthew Dillon ERROUT(EINVAL);
352b06ebda0SMatthew Dillon
353b06ebda0SMatthew Dillon switch (set->dlt) {
354b06ebda0SMatthew Dillon case DLT_EN10MB:
355b06ebda0SMatthew Dillon iface->info.ifinfo_dlt = DLT_EN10MB;
356b06ebda0SMatthew Dillon break;
357b06ebda0SMatthew Dillon case DLT_RAW:
358b06ebda0SMatthew Dillon iface->info.ifinfo_dlt = DLT_RAW;
359b06ebda0SMatthew Dillon break;
360b06ebda0SMatthew Dillon default:
361b06ebda0SMatthew Dillon ERROUT(EINVAL);
362b06ebda0SMatthew Dillon }
363b06ebda0SMatthew Dillon break;
364b06ebda0SMatthew Dillon }
365b06ebda0SMatthew Dillon case NGM_NETFLOW_SETIFINDEX:
366b06ebda0SMatthew Dillon {
367b06ebda0SMatthew Dillon struct ng_netflow_setifindex *set;
368b06ebda0SMatthew Dillon struct ng_netflow_iface *iface;
369b06ebda0SMatthew Dillon
370b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(struct ng_netflow_setifindex))
371b06ebda0SMatthew Dillon ERROUT(EINVAL);
372b06ebda0SMatthew Dillon
373b06ebda0SMatthew Dillon set = (struct ng_netflow_setifindex *)msg->data;
374b06ebda0SMatthew Dillon if (set->iface >= NG_NETFLOW_MAXIFACES)
375b06ebda0SMatthew Dillon ERROUT(EINVAL);
376b06ebda0SMatthew Dillon iface = &priv->ifaces[set->iface];
377b06ebda0SMatthew Dillon
378b06ebda0SMatthew Dillon /* connected iface? */
379b06ebda0SMatthew Dillon if (iface->hook == NULL)
380b06ebda0SMatthew Dillon ERROUT(EINVAL);
381b06ebda0SMatthew Dillon
382b06ebda0SMatthew Dillon iface->info.ifinfo_index = set->index;
383b06ebda0SMatthew Dillon
384b06ebda0SMatthew Dillon break;
385b06ebda0SMatthew Dillon }
386b06ebda0SMatthew Dillon case NGM_NETFLOW_SETTIMEOUTS:
387b06ebda0SMatthew Dillon {
388b06ebda0SMatthew Dillon struct ng_netflow_settimeouts *set;
389b06ebda0SMatthew Dillon
390b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(struct ng_netflow_settimeouts))
391b06ebda0SMatthew Dillon ERROUT(EINVAL);
392b06ebda0SMatthew Dillon
393b06ebda0SMatthew Dillon set = (struct ng_netflow_settimeouts *)msg->data;
394b06ebda0SMatthew Dillon
395b06ebda0SMatthew Dillon priv->info.nfinfo_inact_t = set->inactive_timeout;
396b06ebda0SMatthew Dillon priv->info.nfinfo_act_t = set->active_timeout;
397b06ebda0SMatthew Dillon
398b06ebda0SMatthew Dillon break;
399b06ebda0SMatthew Dillon }
400b06ebda0SMatthew Dillon case NGM_NETFLOW_SHOW:
401b06ebda0SMatthew Dillon {
402b06ebda0SMatthew Dillon uint32_t *last;
403b06ebda0SMatthew Dillon
404b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(uint32_t))
405b06ebda0SMatthew Dillon ERROUT(EINVAL);
406b06ebda0SMatthew Dillon
407b06ebda0SMatthew Dillon last = (uint32_t *)msg->data;
408b06ebda0SMatthew Dillon
4095a975a3dSMatthew Dillon NG_MKRESPONSE(resp, msg, NGRESP_SIZE, M_WAITOK | M_NULLOK);
410b06ebda0SMatthew Dillon
411b06ebda0SMatthew Dillon if (!resp)
412b06ebda0SMatthew Dillon ERROUT(ENOMEM);
413b06ebda0SMatthew Dillon
414b06ebda0SMatthew Dillon error = ng_netflow_flow_show(priv, *last, resp);
415b06ebda0SMatthew Dillon
416b06ebda0SMatthew Dillon break;
417b06ebda0SMatthew Dillon }
418b06ebda0SMatthew Dillon default:
419b06ebda0SMatthew Dillon ERROUT(EINVAL); /* unknown command */
420b06ebda0SMatthew Dillon break;
421b06ebda0SMatthew Dillon }
422b06ebda0SMatthew Dillon break;
423b06ebda0SMatthew Dillon default:
424b06ebda0SMatthew Dillon ERROUT(EINVAL); /* incorrect cookie */
425b06ebda0SMatthew Dillon break;
426b06ebda0SMatthew Dillon }
427b06ebda0SMatthew Dillon
428b06ebda0SMatthew Dillon /*
429b06ebda0SMatthew Dillon * Take care of synchronous response, if any.
430b06ebda0SMatthew Dillon * Free memory and return.
431b06ebda0SMatthew Dillon */
432b06ebda0SMatthew Dillon done:
433b06ebda0SMatthew Dillon NG_RESPOND_MSG(error, node, item, resp);
434b06ebda0SMatthew Dillon NG_FREE_MSG(msg);
435b06ebda0SMatthew Dillon
436b06ebda0SMatthew Dillon return (error);
437b06ebda0SMatthew Dillon }
438b06ebda0SMatthew Dillon
439b06ebda0SMatthew Dillon /* Receive data on hook. */
440b06ebda0SMatthew Dillon static int
ng_netflow_rcvdata(hook_p hook,item_p item)441b06ebda0SMatthew Dillon ng_netflow_rcvdata (hook_p hook, item_p item)
442b06ebda0SMatthew Dillon {
443b06ebda0SMatthew Dillon const node_p node = NG_HOOK_NODE(hook);
444b06ebda0SMatthew Dillon const priv_p priv = NG_NODE_PRIVATE(node);
445b06ebda0SMatthew Dillon const iface_p iface = NG_HOOK_PRIVATE(hook);
446b06ebda0SMatthew Dillon struct mbuf *m = NULL;
447b06ebda0SMatthew Dillon struct ip *ip;
448b06ebda0SMatthew Dillon int pullup_len = 0;
449b06ebda0SMatthew Dillon int error = 0;
450b06ebda0SMatthew Dillon
451b06ebda0SMatthew Dillon if (hook == priv->export) {
452b06ebda0SMatthew Dillon /*
453b06ebda0SMatthew Dillon * Data arrived on export hook.
454b06ebda0SMatthew Dillon * This must not happen.
455b06ebda0SMatthew Dillon */
456b06ebda0SMatthew Dillon log(LOG_ERR, "ng_netflow: incoming data on export hook!\n");
457b06ebda0SMatthew Dillon ERROUT(EINVAL);
4580fdb7d01SSascha Wildner }
459b06ebda0SMatthew Dillon
460b06ebda0SMatthew Dillon if (hook == iface->out) {
461b06ebda0SMatthew Dillon /*
462b06ebda0SMatthew Dillon * Data arrived on out hook. Bypass it.
463b06ebda0SMatthew Dillon */
464b06ebda0SMatthew Dillon if (iface->hook == NULL)
465b06ebda0SMatthew Dillon ERROUT(ENOTCONN);
466b06ebda0SMatthew Dillon
467b06ebda0SMatthew Dillon NG_FWD_ITEM_HOOK(error, item, iface->hook);
468b06ebda0SMatthew Dillon return (error);
469b06ebda0SMatthew Dillon }
470b06ebda0SMatthew Dillon
471b06ebda0SMatthew Dillon NGI_GET_M(item, m);
472b06ebda0SMatthew Dillon
473b06ebda0SMatthew Dillon /* Increase counters. */
474b06ebda0SMatthew Dillon iface->info.ifinfo_packets++;
475b06ebda0SMatthew Dillon
476b06ebda0SMatthew Dillon /*
477b06ebda0SMatthew Dillon * Depending on interface data link type and packet contents
478b06ebda0SMatthew Dillon * we pullup enough data, so that ng_netflow_flow_add() does not
479b06ebda0SMatthew Dillon * need to know about mbuf at all. We keep current length of data
480b06ebda0SMatthew Dillon * needed to be contiguous in pullup_len. mtod() is done at the
481b06ebda0SMatthew Dillon * very end one more time, since m can had changed after pulluping.
482b06ebda0SMatthew Dillon *
483b06ebda0SMatthew Dillon * In case of unrecognized data we don't return error, but just
484b06ebda0SMatthew Dillon * pass data to downstream hook, if it is available.
485b06ebda0SMatthew Dillon */
486b06ebda0SMatthew Dillon
487b06ebda0SMatthew Dillon #define M_CHECK(length) do { \
488b06ebda0SMatthew Dillon pullup_len += length; \
489b06ebda0SMatthew Dillon if ((m)->m_pkthdr.len < (pullup_len)) { \
490b06ebda0SMatthew Dillon error = EINVAL; \
491b06ebda0SMatthew Dillon goto bypass; \
492b06ebda0SMatthew Dillon } \
493b06ebda0SMatthew Dillon if ((m)->m_len < (pullup_len) && \
494b06ebda0SMatthew Dillon (((m) = m_pullup((m),(pullup_len))) == NULL)) { \
495b06ebda0SMatthew Dillon error = ENOBUFS; \
496b06ebda0SMatthew Dillon goto done; \
497b06ebda0SMatthew Dillon } \
498b06ebda0SMatthew Dillon } while (0)
499b06ebda0SMatthew Dillon
500b06ebda0SMatthew Dillon switch (iface->info.ifinfo_dlt) {
501b06ebda0SMatthew Dillon case DLT_EN10MB: /* Ethernet */
502b06ebda0SMatthew Dillon {
503b06ebda0SMatthew Dillon struct ether_header *eh;
504b06ebda0SMatthew Dillon uint16_t etype;
505b06ebda0SMatthew Dillon
506b06ebda0SMatthew Dillon M_CHECK(sizeof(struct ether_header));
507b06ebda0SMatthew Dillon eh = mtod(m, struct ether_header *);
508b06ebda0SMatthew Dillon
509b06ebda0SMatthew Dillon /* Make sure this is IP frame. */
510b06ebda0SMatthew Dillon etype = ntohs(eh->ether_type);
511b06ebda0SMatthew Dillon switch (etype) {
512b06ebda0SMatthew Dillon case ETHERTYPE_IP:
513b06ebda0SMatthew Dillon M_CHECK(sizeof(struct ip));
514b06ebda0SMatthew Dillon eh = mtod(m, struct ether_header *);
515b06ebda0SMatthew Dillon ip = (struct ip *)(eh + 1);
516b06ebda0SMatthew Dillon break;
517b06ebda0SMatthew Dillon case ETHERTYPE_VLAN:
518b06ebda0SMatthew Dillon {
519b06ebda0SMatthew Dillon struct ether_vlan_header *evh;
520b06ebda0SMatthew Dillon
521b06ebda0SMatthew Dillon M_CHECK(sizeof(struct ether_vlan_header) -
522b06ebda0SMatthew Dillon sizeof(struct ether_header));
523b06ebda0SMatthew Dillon evh = mtod(m, struct ether_vlan_header *);
524b06ebda0SMatthew Dillon if (ntohs(evh->evl_proto) == ETHERTYPE_IP) {
525b06ebda0SMatthew Dillon M_CHECK(sizeof(struct ip));
526b06ebda0SMatthew Dillon ip = (struct ip *)(evh + 1);
527b06ebda0SMatthew Dillon break;
528b06ebda0SMatthew Dillon }
529b06ebda0SMatthew Dillon }
530b06ebda0SMatthew Dillon default:
531b06ebda0SMatthew Dillon goto bypass; /* pass this frame */
532b06ebda0SMatthew Dillon }
533b06ebda0SMatthew Dillon break;
534b06ebda0SMatthew Dillon }
535b06ebda0SMatthew Dillon case DLT_RAW: /* IP packets */
536b06ebda0SMatthew Dillon M_CHECK(sizeof(struct ip));
537b06ebda0SMatthew Dillon ip = mtod(m, struct ip *);
538b06ebda0SMatthew Dillon break;
539b06ebda0SMatthew Dillon default:
540b06ebda0SMatthew Dillon goto bypass;
541b06ebda0SMatthew Dillon break;
542b06ebda0SMatthew Dillon }
543b06ebda0SMatthew Dillon
544b06ebda0SMatthew Dillon if ((ip->ip_off & htons(IP_OFFMASK)) == 0) {
545b06ebda0SMatthew Dillon /*
546b06ebda0SMatthew Dillon * In case of IP header with options, we haven't pulled
547b06ebda0SMatthew Dillon * up enough, yet.
548b06ebda0SMatthew Dillon */
549b06ebda0SMatthew Dillon pullup_len += (ip->ip_hl << 2) - sizeof(struct ip);
550b06ebda0SMatthew Dillon
551b06ebda0SMatthew Dillon switch (ip->ip_p) {
552b06ebda0SMatthew Dillon case IPPROTO_TCP:
553b06ebda0SMatthew Dillon M_CHECK(sizeof(struct tcphdr));
554b06ebda0SMatthew Dillon break;
555b06ebda0SMatthew Dillon case IPPROTO_UDP:
556b06ebda0SMatthew Dillon M_CHECK(sizeof(struct udphdr));
557b06ebda0SMatthew Dillon break;
558b06ebda0SMatthew Dillon }
559b06ebda0SMatthew Dillon }
560b06ebda0SMatthew Dillon
561b06ebda0SMatthew Dillon switch (iface->info.ifinfo_dlt) {
562b06ebda0SMatthew Dillon case DLT_EN10MB:
563b06ebda0SMatthew Dillon {
564b06ebda0SMatthew Dillon struct ether_header *eh;
565b06ebda0SMatthew Dillon
566b06ebda0SMatthew Dillon eh = mtod(m, struct ether_header *);
567b06ebda0SMatthew Dillon switch (ntohs(eh->ether_type)) {
568b06ebda0SMatthew Dillon case ETHERTYPE_IP:
569b06ebda0SMatthew Dillon ip = (struct ip *)(eh + 1);
570b06ebda0SMatthew Dillon break;
571b06ebda0SMatthew Dillon case ETHERTYPE_VLAN:
572b06ebda0SMatthew Dillon {
573b06ebda0SMatthew Dillon struct ether_vlan_header *evh;
574b06ebda0SMatthew Dillon
575b06ebda0SMatthew Dillon evh = mtod(m, struct ether_vlan_header *);
576b06ebda0SMatthew Dillon ip = (struct ip *)(evh + 1);
577b06ebda0SMatthew Dillon break;
578b06ebda0SMatthew Dillon }
579b06ebda0SMatthew Dillon default:
580b06ebda0SMatthew Dillon panic("ng_netflow entered deadcode");
581b06ebda0SMatthew Dillon }
582b06ebda0SMatthew Dillon break;
583b06ebda0SMatthew Dillon }
584b06ebda0SMatthew Dillon case DLT_RAW:
585b06ebda0SMatthew Dillon ip = mtod(m, struct ip *);
586b06ebda0SMatthew Dillon break;
587b06ebda0SMatthew Dillon default:
588b06ebda0SMatthew Dillon panic("ng_netflow entered deadcode");
589b06ebda0SMatthew Dillon }
590b06ebda0SMatthew Dillon
591b06ebda0SMatthew Dillon #undef M_CHECK
592b06ebda0SMatthew Dillon
593b06ebda0SMatthew Dillon error = ng_netflow_flow_add(priv, ip, iface, m->m_pkthdr.rcvif);
594b06ebda0SMatthew Dillon
595b06ebda0SMatthew Dillon bypass:
596b06ebda0SMatthew Dillon if (iface->out != NULL) {
597b06ebda0SMatthew Dillon /* XXX: error gets overwritten here */
598b06ebda0SMatthew Dillon NG_FWD_NEW_DATA(error, item, iface->out, m);
599b06ebda0SMatthew Dillon return (error);
600b06ebda0SMatthew Dillon }
601b06ebda0SMatthew Dillon done:
602b06ebda0SMatthew Dillon if (item)
603b06ebda0SMatthew Dillon NG_FREE_ITEM(item);
604b06ebda0SMatthew Dillon if (m)
605b06ebda0SMatthew Dillon NG_FREE_M(m);
606b06ebda0SMatthew Dillon
607b06ebda0SMatthew Dillon return (error);
608b06ebda0SMatthew Dillon }
609b06ebda0SMatthew Dillon
610b06ebda0SMatthew Dillon /* We will be shut down in a moment */
611b06ebda0SMatthew Dillon static int
ng_netflow_close(node_p node)612b06ebda0SMatthew Dillon ng_netflow_close(node_p node)
613b06ebda0SMatthew Dillon {
614b06ebda0SMatthew Dillon const priv_p priv = NG_NODE_PRIVATE(node);
615b06ebda0SMatthew Dillon
616b06ebda0SMatthew Dillon callout_drain(&priv->exp_callout);
617b06ebda0SMatthew Dillon ng_netflow_cache_flush(priv);
618b06ebda0SMatthew Dillon
619b06ebda0SMatthew Dillon return (0);
620b06ebda0SMatthew Dillon }
621b06ebda0SMatthew Dillon
622b06ebda0SMatthew Dillon /* Do local shutdown processing. */
623b06ebda0SMatthew Dillon static int
ng_netflow_rmnode(node_p node)624b06ebda0SMatthew Dillon ng_netflow_rmnode(node_p node)
625b06ebda0SMatthew Dillon {
626b06ebda0SMatthew Dillon const priv_p priv = NG_NODE_PRIVATE(node);
627b06ebda0SMatthew Dillon
628b06ebda0SMatthew Dillon NG_NODE_SET_PRIVATE(node, NULL);
629b06ebda0SMatthew Dillon NG_NODE_UNREF(priv->node);
630b06ebda0SMatthew Dillon
631fc025606SSascha Wildner kfree(priv, M_NETGRAPH);
632b06ebda0SMatthew Dillon
633b06ebda0SMatthew Dillon return (0);
634b06ebda0SMatthew Dillon }
635b06ebda0SMatthew Dillon
636b06ebda0SMatthew Dillon /* Hook disconnection. */
637b06ebda0SMatthew Dillon static int
ng_netflow_disconnect(hook_p hook)638b06ebda0SMatthew Dillon ng_netflow_disconnect(hook_p hook)
639b06ebda0SMatthew Dillon {
640b06ebda0SMatthew Dillon node_p node = NG_HOOK_NODE(hook);
641b06ebda0SMatthew Dillon priv_p priv = NG_NODE_PRIVATE(node);
642b06ebda0SMatthew Dillon iface_p iface = NG_HOOK_PRIVATE(hook);
643b06ebda0SMatthew Dillon
644b06ebda0SMatthew Dillon if (iface != NULL) {
645b06ebda0SMatthew Dillon if (iface->hook == hook)
646b06ebda0SMatthew Dillon iface->hook = NULL;
647b06ebda0SMatthew Dillon if (iface->out == hook)
648b06ebda0SMatthew Dillon iface->out = NULL;
649b06ebda0SMatthew Dillon }
650b06ebda0SMatthew Dillon
651b06ebda0SMatthew Dillon /* if export hook disconnected stop running expire(). */
652b06ebda0SMatthew Dillon if (hook == priv->export) {
653b06ebda0SMatthew Dillon callout_drain(&priv->exp_callout);
654b06ebda0SMatthew Dillon priv->export = NULL;
655b06ebda0SMatthew Dillon }
656b06ebda0SMatthew Dillon
657b06ebda0SMatthew Dillon /* Removal of the last link destroys the node. */
658b06ebda0SMatthew Dillon if (NG_NODE_NUMHOOKS(node) == 0)
659b06ebda0SMatthew Dillon ng_rmnode_self(node);
660b06ebda0SMatthew Dillon
661b06ebda0SMatthew Dillon return (0);
662b06ebda0SMatthew Dillon }
663