xref: /freebsd/sys/netgraph/netflow/ng_netflow.c (revision 5dcd9c10612684d1c823670cbb5b4715028784e7)
1 /*-
2  * Copyright (c) 2010-2011 Alexander V. Chernikov <melifaro@ipfw.ru>
3  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
4  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $SourceForge: ng_netflow.c,v 1.30 2004/09/05 11:37:43 glebius Exp $
29  */
30 
31 static const char rcs_id[] =
32     "@(#) $FreeBSD$";
33 
34 #include "opt_inet6.h"
35 #include "opt_route.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/syslog.h>
44 #include <sys/ctype.h>
45 
46 #include <net/if.h>
47 #include <net/ethernet.h>
48 #include <net/route.h>
49 #include <net/if_arp.h>
50 #include <net/if_var.h>
51 #include <net/if_vlan_var.h>
52 #include <net/bpf.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip6.h>
57 #include <netinet/tcp.h>
58 #include <netinet/udp.h>
59 #include <netinet/sctp.h>
60 
61 #include <netgraph/ng_message.h>
62 #include <netgraph/ng_parse.h>
63 #include <netgraph/netgraph.h>
64 #include <netgraph/netflow/netflow.h>
65 #include <netgraph/netflow/netflow_v9.h>
66 #include <netgraph/netflow/ng_netflow.h>
67 
68 /* Netgraph methods */
69 static ng_constructor_t	ng_netflow_constructor;
70 static ng_rcvmsg_t	ng_netflow_rcvmsg;
71 static ng_close_t	ng_netflow_close;
72 static ng_shutdown_t	ng_netflow_rmnode;
73 static ng_newhook_t	ng_netflow_newhook;
74 static ng_rcvdata_t	ng_netflow_rcvdata;
75 static ng_disconnect_t	ng_netflow_disconnect;
76 
77 /* Parse type for struct ng_netflow_info */
78 static const struct ng_parse_struct_field ng_netflow_info_type_fields[]
79 	= NG_NETFLOW_INFO_TYPE;
80 static const struct ng_parse_type ng_netflow_info_type = {
81 	&ng_parse_struct_type,
82 	&ng_netflow_info_type_fields
83 };
84 
85 /*  Parse type for struct ng_netflow_ifinfo */
86 static const struct ng_parse_struct_field ng_netflow_ifinfo_type_fields[]
87 	= NG_NETFLOW_IFINFO_TYPE;
88 static const struct ng_parse_type ng_netflow_ifinfo_type = {
89 	&ng_parse_struct_type,
90 	&ng_netflow_ifinfo_type_fields
91 };
92 
93 /* Parse type for struct ng_netflow_setdlt */
94 static const struct ng_parse_struct_field ng_netflow_setdlt_type_fields[]
95 	= NG_NETFLOW_SETDLT_TYPE;
96 static const struct ng_parse_type ng_netflow_setdlt_type = {
97 	&ng_parse_struct_type,
98 	&ng_netflow_setdlt_type_fields
99 };
100 
101 /* Parse type for ng_netflow_setifindex */
102 static const struct ng_parse_struct_field ng_netflow_setifindex_type_fields[]
103 	= NG_NETFLOW_SETIFINDEX_TYPE;
104 static const struct ng_parse_type ng_netflow_setifindex_type = {
105 	&ng_parse_struct_type,
106 	&ng_netflow_setifindex_type_fields
107 };
108 
109 /* Parse type for ng_netflow_settimeouts */
110 static const struct ng_parse_struct_field ng_netflow_settimeouts_type_fields[]
111 	= NG_NETFLOW_SETTIMEOUTS_TYPE;
112 static const struct ng_parse_type ng_netflow_settimeouts_type = {
113 	&ng_parse_struct_type,
114 	&ng_netflow_settimeouts_type_fields
115 };
116 
117 /* Parse type for ng_netflow_setconfig */
118 static const struct ng_parse_struct_field ng_netflow_setconfig_type_fields[]
119 	= NG_NETFLOW_SETCONFIG_TYPE;
120 static const struct ng_parse_type ng_netflow_setconfig_type = {
121 	&ng_parse_struct_type,
122 	&ng_netflow_setconfig_type_fields
123 };
124 
125 /* Parse type for ng_netflow_settemplate */
126 static const struct ng_parse_struct_field ng_netflow_settemplate_type_fields[]
127 	= NG_NETFLOW_SETTEMPLATE_TYPE;
128 static const struct ng_parse_type ng_netflow_settemplate_type = {
129 	&ng_parse_struct_type,
130 	&ng_netflow_settemplate_type_fields
131 };
132 
133 /* Parse type for ng_netflow_setmtu */
134 static const struct ng_parse_struct_field ng_netflow_setmtu_type_fields[]
135 	= NG_NETFLOW_SETMTU_TYPE;
136 static const struct ng_parse_type ng_netflow_setmtu_type = {
137 	&ng_parse_struct_type,
138 	&ng_netflow_setmtu_type_fields
139 };
140 
141 /* List of commands and how to convert arguments to/from ASCII */
142 static const struct ng_cmdlist ng_netflow_cmds[] = {
143        {
144 	 NGM_NETFLOW_COOKIE,
145 	 NGM_NETFLOW_INFO,
146 	 "info",
147 	 NULL,
148 	 &ng_netflow_info_type
149        },
150        {
151 	NGM_NETFLOW_COOKIE,
152 	NGM_NETFLOW_IFINFO,
153 	"ifinfo",
154 	&ng_parse_uint16_type,
155 	&ng_netflow_ifinfo_type
156        },
157        {
158 	NGM_NETFLOW_COOKIE,
159 	NGM_NETFLOW_SETDLT,
160 	"setdlt",
161 	&ng_netflow_setdlt_type,
162 	NULL
163        },
164        {
165 	NGM_NETFLOW_COOKIE,
166 	NGM_NETFLOW_SETIFINDEX,
167 	"setifindex",
168 	&ng_netflow_setifindex_type,
169 	NULL
170        },
171        {
172 	NGM_NETFLOW_COOKIE,
173 	NGM_NETFLOW_SETTIMEOUTS,
174 	"settimeouts",
175 	&ng_netflow_settimeouts_type,
176 	NULL
177        },
178        {
179 	NGM_NETFLOW_COOKIE,
180 	NGM_NETFLOW_SETCONFIG,
181 	"setconfig",
182 	&ng_netflow_setconfig_type,
183 	NULL
184        },
185        {
186 	NGM_NETFLOW_COOKIE,
187 	NGM_NETFLOW_SETTEMPLATE,
188 	"settemplate",
189 	&ng_netflow_settemplate_type,
190 	NULL
191        },
192        {
193 	NGM_NETFLOW_COOKIE,
194 	NGM_NETFLOW_SETMTU,
195 	"setmtu",
196 	&ng_netflow_setmtu_type,
197 	NULL
198        },
199        { 0 }
200 };
201 
202 
203 /* Netgraph node type descriptor */
204 static struct ng_type ng_netflow_typestruct = {
205 	.version =	NG_ABI_VERSION,
206 	.name =		NG_NETFLOW_NODE_TYPE,
207 	.constructor =	ng_netflow_constructor,
208 	.rcvmsg =	ng_netflow_rcvmsg,
209 	.close =	ng_netflow_close,
210 	.shutdown =	ng_netflow_rmnode,
211 	.newhook =	ng_netflow_newhook,
212 	.rcvdata =	ng_netflow_rcvdata,
213 	.disconnect =	ng_netflow_disconnect,
214 	.cmdlist =	ng_netflow_cmds,
215 };
216 NETGRAPH_INIT(netflow, &ng_netflow_typestruct);
217 
218 /* Called at node creation */
219 static int
220 ng_netflow_constructor(node_p node)
221 {
222 	priv_p priv;
223 	int error = 0, i;
224 
225 	/* Initialize private data */
226 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT);
227 	if (priv == NULL)
228 		return (ENOMEM);
229 	bzero(priv, sizeof(*priv));
230 
231 	/* Make node and its data point at each other */
232 	NG_NODE_SET_PRIVATE(node, priv);
233 	priv->node = node;
234 
235 	/* Initialize timeouts to default values */
236 	priv->info.nfinfo_inact_t = INACTIVE_TIMEOUT;
237 	priv->info.nfinfo_act_t = ACTIVE_TIMEOUT;
238 
239 	/* Set default config */
240 	for (i = 0; i < NG_NETFLOW_MAXIFACES; i++)
241 		priv->ifaces[i].info.conf = NG_NETFLOW_CONF_INGRESS;
242 
243 	/* Initialize callout handle */
244 	callout_init(&priv->exp_callout, CALLOUT_MPSAFE);
245 
246 	/* Allocate memory and set up flow cache */
247 	if ((error = ng_netflow_cache_init(priv)))
248 		return (error);
249 
250 	return (0);
251 }
252 
253 /*
254  * ng_netflow supports two hooks: data and export.
255  * Incoming traffic is expected on data, and expired
256  * netflow datagrams are sent to export.
257  */
258 static int
259 ng_netflow_newhook(node_p node, hook_p hook, const char *name)
260 {
261 	const priv_p priv = NG_NODE_PRIVATE(node);
262 
263 	if (strncmp(name, NG_NETFLOW_HOOK_DATA,	/* an iface hook? */
264 	    strlen(NG_NETFLOW_HOOK_DATA)) == 0) {
265 		iface_p iface;
266 		int ifnum = -1;
267 		const char *cp;
268 		char *eptr;
269 
270 		cp = name + strlen(NG_NETFLOW_HOOK_DATA);
271 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
272 			return (EINVAL);
273 
274 		ifnum = (int)strtoul(cp, &eptr, 10);
275 		if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
276 			return (EINVAL);
277 
278 		/* See if hook is already connected */
279 		if (priv->ifaces[ifnum].hook != NULL)
280 			return (EISCONN);
281 
282 		iface = &priv->ifaces[ifnum];
283 
284 		/* Link private info and hook together */
285 		NG_HOOK_SET_PRIVATE(hook, iface);
286 		iface->hook = hook;
287 
288 		/*
289 		 * In most cases traffic accounting is done on an
290 		 * Ethernet interface, so default data link type
291 		 * will be DLT_EN10MB.
292 		 */
293 		iface->info.ifinfo_dlt = DLT_EN10MB;
294 
295 	} else if (strncmp(name, NG_NETFLOW_HOOK_OUT,
296 	    strlen(NG_NETFLOW_HOOK_OUT)) == 0) {
297 		iface_p iface;
298 		int ifnum = -1;
299 		const char *cp;
300 		char *eptr;
301 
302 		cp = name + strlen(NG_NETFLOW_HOOK_OUT);
303 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
304 			return (EINVAL);
305 
306 		ifnum = (int)strtoul(cp, &eptr, 10);
307 		if (*eptr != '\0' || ifnum < 0 || ifnum >= NG_NETFLOW_MAXIFACES)
308 			return (EINVAL);
309 
310 		/* See if hook is already connected */
311 		if (priv->ifaces[ifnum].out != NULL)
312 			return (EISCONN);
313 
314 		iface = &priv->ifaces[ifnum];
315 
316 		/* Link private info and hook together */
317 		NG_HOOK_SET_PRIVATE(hook, iface);
318 		iface->out = hook;
319 
320 	} else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT) == 0) {
321 
322 		if (priv->export != NULL)
323 			return (EISCONN);
324 
325 		/* Netflow version 5 supports 32-bit counters only */
326 		if (CNTR_MAX == UINT64_MAX)
327 			return (EINVAL);
328 
329 		priv->export = hook;
330 
331 		/* Exporter is ready. Let's schedule expiry. */
332 		callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
333 		    (void *)priv);
334 	} else if (strcmp(name, NG_NETFLOW_HOOK_EXPORT9) == 0) {
335 
336 		if (priv->export9 != NULL)
337 			return (EISCONN);
338 
339 		priv->export9 = hook;
340 
341 		/* Exporter is ready. Let's schedule expiry. */
342 		callout_reset(&priv->exp_callout, (1*hz), &ng_netflow_expire,
343 		    (void *)priv);
344 	} else
345 		return (EINVAL);
346 
347 	return (0);
348 }
349 
350 /* Get a netgraph control message. */
351 static int
352 ng_netflow_rcvmsg (node_p node, item_p item, hook_p lasthook)
353 {
354 	const priv_p priv = NG_NODE_PRIVATE(node);
355 	struct ng_mesg *resp = NULL;
356 	int error = 0;
357 	struct ng_mesg *msg;
358 
359 	NGI_GET_MSG(item, msg);
360 
361 	/* Deal with message according to cookie and command */
362 	switch (msg->header.typecookie) {
363 	case NGM_NETFLOW_COOKIE:
364 		switch (msg->header.cmd) {
365 		case NGM_NETFLOW_INFO:
366 		{
367 			struct ng_netflow_info *i;
368 
369 			NG_MKRESPONSE(resp, msg, sizeof(struct ng_netflow_info),
370 			    M_NOWAIT);
371 			i = (struct ng_netflow_info *)resp->data;
372 			ng_netflow_copyinfo(priv, i);
373 
374 			break;
375 		}
376 		case NGM_NETFLOW_IFINFO:
377 		{
378 			struct ng_netflow_ifinfo *i;
379 			const uint16_t *index;
380 
381 			if (msg->header.arglen != sizeof(uint16_t))
382 				 ERROUT(EINVAL);
383 
384 			index  = (uint16_t *)msg->data;
385 			if (*index >= NG_NETFLOW_MAXIFACES)
386 				ERROUT(EINVAL);
387 
388 			/* connected iface? */
389 			if (priv->ifaces[*index].hook == NULL)
390 				 ERROUT(EINVAL);
391 
392 			NG_MKRESPONSE(resp, msg,
393 			     sizeof(struct ng_netflow_ifinfo), M_NOWAIT);
394 			i = (struct ng_netflow_ifinfo *)resp->data;
395 			memcpy((void *)i, (void *)&priv->ifaces[*index].info,
396 			    sizeof(priv->ifaces[*index].info));
397 
398 			break;
399 		}
400 		case NGM_NETFLOW_SETDLT:
401 		{
402 			struct ng_netflow_setdlt *set;
403 			struct ng_netflow_iface *iface;
404 
405 			if (msg->header.arglen != sizeof(struct ng_netflow_setdlt))
406 				ERROUT(EINVAL);
407 
408 			set = (struct ng_netflow_setdlt *)msg->data;
409 			if (set->iface >= NG_NETFLOW_MAXIFACES)
410 				ERROUT(EINVAL);
411 			iface = &priv->ifaces[set->iface];
412 
413 			/* connected iface? */
414 			if (iface->hook == NULL)
415 				ERROUT(EINVAL);
416 
417 			switch (set->dlt) {
418 			case	DLT_EN10MB:
419 				iface->info.ifinfo_dlt = DLT_EN10MB;
420 				break;
421 			case	DLT_RAW:
422 				iface->info.ifinfo_dlt = DLT_RAW;
423 				break;
424 			default:
425 				ERROUT(EINVAL);
426 			}
427 			break;
428 		}
429 		case NGM_NETFLOW_SETIFINDEX:
430 		{
431 			struct ng_netflow_setifindex *set;
432 			struct ng_netflow_iface *iface;
433 
434 			if (msg->header.arglen != sizeof(struct ng_netflow_setifindex))
435 				ERROUT(EINVAL);
436 
437 			set = (struct ng_netflow_setifindex *)msg->data;
438 			if (set->iface >= NG_NETFLOW_MAXIFACES)
439 				ERROUT(EINVAL);
440 			iface = &priv->ifaces[set->iface];
441 
442 			/* connected iface? */
443 			if (iface->hook == NULL)
444 				ERROUT(EINVAL);
445 
446 			iface->info.ifinfo_index = set->index;
447 
448 			break;
449 		}
450 		case NGM_NETFLOW_SETTIMEOUTS:
451 		{
452 			struct ng_netflow_settimeouts *set;
453 
454 			if (msg->header.arglen != sizeof(struct ng_netflow_settimeouts))
455 				ERROUT(EINVAL);
456 
457 			set = (struct ng_netflow_settimeouts *)msg->data;
458 
459 			priv->info.nfinfo_inact_t = set->inactive_timeout;
460 			priv->info.nfinfo_act_t = set->active_timeout;
461 
462 			break;
463 		}
464 		case NGM_NETFLOW_SETCONFIG:
465 		{
466 			struct ng_netflow_setconfig *set;
467 
468 			if (msg->header.arglen != sizeof(struct ng_netflow_setconfig))
469 				ERROUT(EINVAL);
470 
471 			set = (struct ng_netflow_setconfig *)msg->data;
472 
473 			if (set->iface >= NG_NETFLOW_MAXIFACES)
474 				ERROUT(EINVAL);
475 
476 			priv->ifaces[set->iface].info.conf = set->conf;
477 
478 			break;
479 		}
480 		case NGM_NETFLOW_SETTEMPLATE:
481 		{
482 			struct ng_netflow_settemplate *set;
483 
484 			if (msg->header.arglen != sizeof(struct ng_netflow_settemplate))
485 				ERROUT(EINVAL);
486 
487 			set = (struct ng_netflow_settemplate *)msg->data;
488 
489 			priv->templ_packets = set->packets;
490 			priv->templ_time = set->time;
491 
492 			break;
493 		}
494 		case NGM_NETFLOW_SETMTU:
495 		{
496 			struct ng_netflow_setmtu *set;
497 
498 			if (msg->header.arglen != sizeof(struct ng_netflow_setmtu))
499 				ERROUT(EINVAL);
500 
501 			set = (struct ng_netflow_setmtu *)msg->data;
502 			if ((set->mtu < MIN_MTU) || (set->mtu > MAX_MTU))
503 				ERROUT(EINVAL);
504 
505 			priv->mtu = set->mtu;
506 
507 			break;
508 		}
509 		case NGM_NETFLOW_SHOW:
510 		{
511 			uint32_t *last;
512 
513 			if (msg->header.arglen != sizeof(uint32_t))
514 				ERROUT(EINVAL);
515 
516 			last = (uint32_t *)msg->data;
517 
518 			NG_MKRESPONSE(resp, msg, NGRESP_SIZE, M_NOWAIT);
519 
520 			if (!resp)
521 				ERROUT(ENOMEM);
522 
523 			error = ng_netflow_flow_show(priv, *last, resp);
524 
525 			break;
526 		}
527 		default:
528 			ERROUT(EINVAL);		/* unknown command */
529 			break;
530 		}
531 		break;
532 	default:
533 		ERROUT(EINVAL);		/* incorrect cookie */
534 		break;
535 	}
536 
537 	/*
538 	 * Take care of synchronous response, if any.
539 	 * Free memory and return.
540 	 */
541 done:
542 	NG_RESPOND_MSG(error, node, item, resp);
543 	NG_FREE_MSG(msg);
544 
545 	return (error);
546 }
547 
548 /* Receive data on hook. */
549 static int
550 ng_netflow_rcvdata (hook_p hook, item_p item)
551 {
552 	const node_p node = NG_HOOK_NODE(hook);
553 	const priv_p priv = NG_NODE_PRIVATE(node);
554 	const iface_p iface = NG_HOOK_PRIVATE(hook);
555 	hook_p out;
556 	struct mbuf *m = NULL, *m_old = NULL;
557 	struct ip *ip = NULL;
558 	struct ip6_hdr *ip6 = NULL;
559 	struct m_tag *mtag;
560 	int pullup_len = 0, off;
561 	uint8_t upper_proto = 0, is_frag = 0;
562 	int error = 0, bypass = 0, acct = 0;
563 	unsigned int src_if_index;
564 	caddr_t upper_ptr = NULL;
565 	fib_export_p fe;
566 	uint32_t fib;
567 
568 	if ((hook == priv->export) || (hook == priv->export9)) {
569 		/*
570 		 * Data arrived on export hook.
571 		 * This must not happen.
572 		 */
573 		log(LOG_ERR, "ng_netflow: incoming data on export hook!\n");
574 		ERROUT(EINVAL);
575 	};
576 
577 	if (hook == iface->hook) {
578 		if ((iface->info.conf & NG_NETFLOW_CONF_INGRESS) == 0)
579 			bypass = 1;
580 		out = iface->out;
581 	} else if (hook == iface->out) {
582 		if ((iface->info.conf & NG_NETFLOW_CONF_EGRESS) == 0)
583 			bypass = 1;
584 		out = iface->hook;
585 	} else
586 		ERROUT(EINVAL);
587 
588 	if ((!bypass) &&
589 	    (iface->info.conf & (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE))) {
590 		mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
591 		    MTAG_NETFLOW_CALLED, NULL);
592 		while (mtag != NULL) {
593 			if ((iface->info.conf & NG_NETFLOW_CONF_ONCE) ||
594 			    ((ng_ID_t *)(mtag + 1))[0] == NG_NODE_ID(node)) {
595 				bypass = 1;
596 				break;
597 			}
598 			mtag = m_tag_locate(NGI_M(item), MTAG_NETFLOW,
599 			    MTAG_NETFLOW_CALLED, mtag);
600 		}
601 	}
602 
603 	if (bypass) {
604 		if (out == NULL)
605 			ERROUT(ENOTCONN);
606 
607 		NG_FWD_ITEM_HOOK(error, item, out);
608 		return (error);
609 	}
610 
611 	if (iface->info.conf & (NG_NETFLOW_CONF_ONCE | NG_NETFLOW_CONF_THISONCE)) {
612 		mtag = m_tag_alloc(MTAG_NETFLOW, MTAG_NETFLOW_CALLED,
613 		    sizeof(ng_ID_t), M_NOWAIT);
614 		if (mtag) {
615 			((ng_ID_t *)(mtag + 1))[0] = NG_NODE_ID(node);
616 			m_tag_prepend(NGI_M(item), mtag);
617 		}
618 	}
619 
620 	NGI_GET_M(item, m);
621 	m_old = m;
622 
623 	/* Increase counters. */
624 	iface->info.ifinfo_packets++;
625 
626 	/*
627 	 * Depending on interface data link type and packet contents
628 	 * we pullup enough data, so that ng_netflow_flow_add() does not
629 	 * need to know about mbuf at all. We keep current length of data
630 	 * needed to be contiguous in pullup_len. mtod() is done at the
631 	 * very end one more time, since m can had changed after pulluping.
632 	 *
633 	 * In case of unrecognized data we don't return error, but just
634 	 * pass data to downstream hook, if it is available.
635 	 */
636 
637 #define	M_CHECK(length)	do {					\
638 	pullup_len += length;					\
639 	if (((m)->m_pkthdr.len < (pullup_len)) ||		\
640 	   ((pullup_len) > MHLEN)) {				\
641 		error = EINVAL;					\
642 		goto bypass;					\
643 	} 							\
644 	if ((m)->m_len < (pullup_len) &&			\
645 	   (((m) = m_pullup((m),(pullup_len))) == NULL)) {	\
646 		error = ENOBUFS;				\
647 		goto done;					\
648 	}							\
649 } while (0)
650 
651 	switch (iface->info.ifinfo_dlt) {
652 	case DLT_EN10MB:	/* Ethernet */
653 	    {
654 		struct ether_header *eh;
655 		uint16_t etype;
656 
657 		M_CHECK(sizeof(struct ether_header));
658 		eh = mtod(m, struct ether_header *);
659 
660 		/* Make sure this is IP frame. */
661 		etype = ntohs(eh->ether_type);
662 		switch (etype) {
663 		case ETHERTYPE_IP:
664 			M_CHECK(sizeof(struct ip));
665 			eh = mtod(m, struct ether_header *);
666 			ip = (struct ip *)(eh + 1);
667 			break;
668 #ifdef INET6
669 		case ETHERTYPE_IPV6:
670 			/*
671 			 * m_pullup() called by M_CHECK() pullups
672 			 * kern.ipc.max_protohdr (default 60 bytes) which is enough
673 			 */
674 			M_CHECK(sizeof(struct ip6_hdr));
675 			eh = mtod(m, struct ether_header *);
676 			ip6 = (struct ip6_hdr *)(eh + 1);
677 			break;
678 #endif
679 		case ETHERTYPE_VLAN:
680 		    {
681 			struct ether_vlan_header *evh;
682 
683 			M_CHECK(sizeof(struct ether_vlan_header) -
684 			    sizeof(struct ether_header));
685 			evh = mtod(m, struct ether_vlan_header *);
686 			etype = ntohs(evh->evl_proto);
687 
688 			if (etype == ETHERTYPE_IP) {
689 				M_CHECK(sizeof(struct ip));
690 				ip = (struct ip *)(evh + 1);
691 				break;
692 #ifdef INET6
693 			} else if (etype == ETHERTYPE_IPV6) {
694 				M_CHECK(sizeof(struct ip6_hdr));
695 				ip6 = (struct ip6_hdr *)(evh + 1);
696 				break;
697 #endif
698 			}
699 		    }
700 		default:
701 			goto bypass;	/* pass this frame */
702 		}
703 		break;
704 	    }
705 	case DLT_RAW:		/* IP packets */
706 		M_CHECK(sizeof(struct ip));
707 		ip = mtod(m, struct ip *);
708 #ifdef INET6
709 		/* If INET6 is not defined IPv6 packets will be discarded in ng_netflow_flow_add() */
710 		if (ip->ip_v == IP6VERSION) {
711 			/* IPv6 packet */
712 			ip = NULL;
713 			M_CHECK(sizeof(struct ip6_hdr));
714 			ip6 = mtod(m, struct ip6_hdr *);
715 		}
716 #endif
717 		break;
718 	default:
719 		goto bypass;
720 		break;
721 	}
722 
723 	off = pullup_len;
724 
725 	if ((ip != NULL) && ((ip->ip_off & htons(IP_OFFMASK)) == 0)) {
726 		if ((ip->ip_v != IPVERSION) ||
727 		    ((ip->ip_hl << 2) < sizeof(struct ip)))
728 			goto bypass;
729 		/*
730 		 * In case of IPv4 header with options, we haven't pulled
731 		 * up enough, yet.
732 		 */
733 		M_CHECK((ip->ip_hl << 2) - sizeof(struct ip));
734 
735 		/* Save upper layer offset and proto */
736 		off = pullup_len;
737 		upper_proto = ip->ip_p;
738 
739 		/*
740 		 * XXX: in case of wrong upper layer header we will forward this packet
741 		 * but skip this record in netflow
742 		 */
743 		switch (ip->ip_p) {
744 		case IPPROTO_TCP:
745 			M_CHECK(sizeof(struct tcphdr));
746 			break;
747 		case IPPROTO_UDP:
748 			M_CHECK(sizeof(struct udphdr));
749 			break;
750 		case IPPROTO_SCTP:
751 			M_CHECK(sizeof(struct sctphdr));
752 			break;
753 		}
754 	} else if (ip != NULL) {
755 		/* Nothing to save except upper layer proto, since this is packet fragment */
756 		is_frag = 1;
757 		upper_proto = ip->ip_p;
758 		if ((ip->ip_v != IPVERSION) ||
759 		    ((ip->ip_hl << 2) < sizeof(struct ip)))
760 			goto bypass;
761 #ifdef INET6
762 	} else if (ip6 != NULL) {
763 		/* Check if we can export */
764 		if (priv->export9 == NULL)
765 			goto bypass;
766 
767 		/* Loop thru IPv6 extended headers to get upper layer header / frag */
768 		int cur = ip6->ip6_nxt, hdr_off = 0;
769 		struct ip6_ext *ip6e;
770 		struct ip6_frag *ip6f;
771 
772 		/* Save upper layer info */
773 		off = pullup_len;
774 		upper_proto = cur;
775 
776 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
777 			goto bypass;
778 
779 		while (42) {
780 			switch (cur) {
781 			/*
782 			 * Same as in IPv4, we can forward 'bad' packet without accounting
783 			 */
784 			case IPPROTO_TCP:
785 				M_CHECK(sizeof(struct tcphdr));
786 				goto loopend;
787 			case IPPROTO_UDP:
788 				M_CHECK(sizeof(struct udphdr));
789 				goto loopend;
790 			case IPPROTO_SCTP:
791 				M_CHECK(sizeof(struct sctphdr));
792 				goto loopend;
793 
794 			/* Loop until 'real' upper layer headers */
795 			case IPPROTO_HOPOPTS:
796 			case IPPROTO_ROUTING:
797 			case IPPROTO_DSTOPTS:
798 				M_CHECK(sizeof(struct ip6_ext));
799 				ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
800 				upper_proto = ip6e->ip6e_nxt;
801 				hdr_off = (ip6e->ip6e_len + 1) << 3;
802 				break;
803 
804 			/* RFC4302, can be before DSTOPTS */
805 			case IPPROTO_AH:
806 				M_CHECK(sizeof(struct ip6_ext));
807 				ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
808 				upper_proto = ip6e->ip6e_nxt;
809 				hdr_off = (ip6e->ip6e_len + 2) << 2;
810 				break;
811 
812 			case IPPROTO_FRAGMENT:
813 				M_CHECK(sizeof(struct ip6_frag));
814 				ip6f = (struct ip6_frag *)(mtod(m, caddr_t) + off);
815 				upper_proto = ip6f->ip6f_nxt;
816 				hdr_off = sizeof(struct ip6_frag);
817 				off += hdr_off;
818 				is_frag = 1;
819 				goto loopend;
820 
821 #if 0
822 			case IPPROTO_NONE:
823 				goto loopend;
824 #endif
825 			/* Any unknow header (new extension or IPv6/IPv4 header for tunnels) */
826 			default:
827 				goto loopend;
828 			}
829 
830 			off += hdr_off;
831 			cur = upper_proto;
832 		}
833 #endif
834 	}
835 #undef	M_CHECK
836 
837 #ifdef INET6
838 loopend:
839 #endif
840 	/* Just in case of real reallocation in M_CHECK() / m_pullup() */
841 	if (m != m_old) {
842 		atomic_fetchadd_32(&priv->info.nfinfo_realloc_mbuf, 1);
843 		ip = NULL;
844 		ip6 = NULL;
845 		switch (iface->info.ifinfo_dlt) {
846 		case DLT_EN10MB:	/* Ethernet */
847 		    {
848 			struct ether_header *eh;
849 
850 			eh = mtod(m, struct ether_header *);
851 			switch (ntohs(eh->ether_type)) {
852 			case ETHERTYPE_IP:
853 				ip = (struct ip *)(eh + 1);
854 				break;
855 #ifdef INET6
856 			case ETHERTYPE_IPV6:
857 				ip6 = (struct ip6_hdr *)(eh + 1);
858 				break;
859 #endif
860 			case ETHERTYPE_VLAN:
861 			    {
862 				struct ether_vlan_header *evh;
863 
864 				evh = mtod(m, struct ether_vlan_header *);
865 				if (ntohs(evh->evl_proto) == ETHERTYPE_IP) {
866 					ip = (struct ip *)(evh + 1);
867 					break;
868 #ifdef INET6
869 				} else if (ntohs(evh->evl_proto) == ETHERTYPE_IPV6) {
870 					ip6 = (struct ip6_hdr *)(evh + 1);
871 					break;
872 #endif
873 				}
874 			    }
875 			default:
876 				panic("ng_netflow entered deadcode");
877 			}
878 			break;
879 		    }
880 		case DLT_RAW:		/* IP packets */
881 			ip = mtod(m, struct ip *);
882 #ifdef INET6
883 			if (ip->ip_v == IP6VERSION) {
884 				/* IPv6 packet */
885 				ip = NULL;
886 				ip6 = mtod(m, struct ip6_hdr *);
887 			}
888 #endif
889  			break;
890  		default:
891  			panic("ng_netflow entered deadcode");
892  		}
893  	}
894 
895 	upper_ptr = (caddr_t)(mtod(m, caddr_t) + off);
896 
897 	/* Determine packet input interface. Prefer configured. */
898 	src_if_index = 0;
899 	if (hook == iface->out || iface->info.ifinfo_index == 0) {
900 		if (m->m_pkthdr.rcvif != NULL)
901 			src_if_index = m->m_pkthdr.rcvif->if_index;
902 	} else
903 		src_if_index = iface->info.ifinfo_index;
904 
905 	/* Check packet FIB */
906 	fib = M_GETFIB(m);
907 	if (fib >= RT_NUMFIBS) {
908 		CTR2(KTR_NET, "ng_netflow_rcvdata(): packet fib %d is out of range of available fibs: 0 .. %d", fib, RT_NUMFIBS);
909 		goto bypass;
910 	}
911 
912 	if ((fe = priv_to_fib(priv, fib)) == NULL) {
913 		/* Setup new FIB */
914 		if (ng_netflow_fib_init(priv, fib) != 0) {
915 			/* malloc() failed */
916 			goto bypass;
917 		}
918 
919 		fe = priv_to_fib(priv, fib);
920 	}
921 
922 	if (ip != NULL)
923 		error = ng_netflow_flow_add(priv, fe, ip, upper_ptr, upper_proto, is_frag, src_if_index);
924 #ifdef INET6
925 	else if (ip6 != NULL)
926 		error = ng_netflow_flow6_add(priv, fe, ip6, upper_ptr, upper_proto, is_frag, src_if_index);
927 #endif
928 	else
929 		goto bypass;
930 
931 	acct = 1;
932 bypass:
933 	if (out != NULL) {
934 		if (acct == 0) {
935 			/* Accounting failure */
936 			if (ip != NULL) {
937 				atomic_fetchadd_32(&priv->info.nfinfo_spackets, 1);
938 				priv->info.nfinfo_sbytes += m_length(m, NULL);
939 			} else if (ip6 != NULL) {
940 				atomic_fetchadd_32(&priv->info.nfinfo_spackets6, 1);
941 				priv->info.nfinfo_sbytes6 += m_length(m, NULL);
942 			}
943 		}
944 
945 		/* XXX: error gets overwritten here */
946 		NG_FWD_NEW_DATA(error, item, out, m);
947 		return (error);
948 	}
949 done:
950 	if (item)
951 		NG_FREE_ITEM(item);
952 	if (m)
953 		NG_FREE_M(m);
954 
955 	return (error);
956 }
957 
958 /* We will be shut down in a moment */
959 static int
960 ng_netflow_close(node_p node)
961 {
962 	const priv_p priv = NG_NODE_PRIVATE(node);
963 
964 	callout_drain(&priv->exp_callout);
965 	ng_netflow_cache_flush(priv);
966 
967 	return (0);
968 }
969 
970 /* Do local shutdown processing. */
971 static int
972 ng_netflow_rmnode(node_p node)
973 {
974 	const priv_p priv = NG_NODE_PRIVATE(node);
975 
976 	NG_NODE_SET_PRIVATE(node, NULL);
977 	NG_NODE_UNREF(priv->node);
978 
979 	free(priv, M_NETGRAPH);
980 
981 	return (0);
982 }
983 
984 /* Hook disconnection. */
985 static int
986 ng_netflow_disconnect(hook_p hook)
987 {
988 	node_p node = NG_HOOK_NODE(hook);
989 	priv_p priv = NG_NODE_PRIVATE(node);
990 	iface_p iface = NG_HOOK_PRIVATE(hook);
991 
992 	if (iface != NULL) {
993 		if (iface->hook == hook)
994 			iface->hook = NULL;
995 		if (iface->out == hook)
996 			iface->out = NULL;
997 	}
998 
999 	/* if export hook disconnected stop running expire(). */
1000 	if (hook == priv->export) {
1001 		if (priv->export9 == NULL)
1002 			callout_drain(&priv->exp_callout);
1003 		priv->export = NULL;
1004 	}
1005 
1006 	if (hook == priv->export9) {
1007 		if (priv->export == NULL)
1008 			callout_drain(&priv->exp_callout);
1009 		priv->export9 = NULL;
1010 	}
1011 
1012 	/* Removal of the last link destroys the node. */
1013 	if (NG_NODE_NUMHOOKS(node) == 0)
1014 		ng_rmnode_self(node);
1015 
1016 	return (0);
1017 }
1018