xref: /freebsd/sys/netgraph/netflow/ng_netflow.h (revision 2ff63af9)
1a752e82dSGleb Smirnoff /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
45dcd9c10SGleb Smirnoff  * Copyright (c) 2010-2011 Alexander V. Chernikov <melifaro@ipfw.ru>
51d03bd16SGleb Smirnoff  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
6a752e82dSGleb Smirnoff  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
7a752e82dSGleb Smirnoff  * All rights reserved.
8a752e82dSGleb Smirnoff  *
9a752e82dSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
10a752e82dSGleb Smirnoff  * modification, are permitted provided that the following conditions
11a752e82dSGleb Smirnoff  * are met:
12a752e82dSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
13a752e82dSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
14a752e82dSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
15a752e82dSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
16a752e82dSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
17a752e82dSGleb Smirnoff  *
18a752e82dSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a752e82dSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a752e82dSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a752e82dSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a752e82dSGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a752e82dSGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a752e82dSGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a752e82dSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a752e82dSGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a752e82dSGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a752e82dSGleb Smirnoff  * SUCH DAMAGE.
29a752e82dSGleb Smirnoff  *
30a752e82dSGleb Smirnoff  *	 $SourceForge: ng_netflow.h,v 1.26 2004/09/04 15:44:55 glebius Exp $
31a752e82dSGleb Smirnoff  */
32a752e82dSGleb Smirnoff 
33a752e82dSGleb Smirnoff #ifndef	_NG_NETFLOW_H_
34a752e82dSGleb Smirnoff #define	_NG_NETFLOW_H_
35a752e82dSGleb Smirnoff 
36a752e82dSGleb Smirnoff #define NG_NETFLOW_NODE_TYPE	"netflow"
377ee35ac9SGleb Smirnoff #define NGM_NETFLOW_COOKIE	1365756954
3810fcb07cSAlexander V. Chernikov #define NGM_NETFLOW_V9_COOKIE	1349865386
39a752e82dSGleb Smirnoff 
40dc7359b8SGleb Smirnoff #define	NG_NETFLOW_MAXIFACES	USHRT_MAX
41a752e82dSGleb Smirnoff 
42a752e82dSGleb Smirnoff /* Hook names */
43a752e82dSGleb Smirnoff 
44a752e82dSGleb Smirnoff #define	NG_NETFLOW_HOOK_DATA	"iface"
459818b82fSGleb Smirnoff #define	NG_NETFLOW_HOOK_OUT	"out"
46a752e82dSGleb Smirnoff #define NG_NETFLOW_HOOK_EXPORT	"export"
475dcd9c10SGleb Smirnoff #define NG_NETFLOW_HOOK_EXPORT9	"export9"
485dcd9c10SGleb Smirnoff 
495dcd9c10SGleb Smirnoff /* This define effectively disable (v5) netflow export hook! */
505dcd9c10SGleb Smirnoff /* #define COUNTERS_64 */
51a752e82dSGleb Smirnoff 
52a752e82dSGleb Smirnoff /* Netgraph commands understood by netflow node */
53a752e82dSGleb Smirnoff enum {
543d9dddcdSGleb Smirnoff     NGM_NETFLOW_INFO = 1|NGM_READONLY|NGM_HASREPLY,	/* get node info */
553d9dddcdSGleb Smirnoff     NGM_NETFLOW_IFINFO = 2|NGM_READONLY|NGM_HASREPLY,	/* get iface info */
563d9dddcdSGleb Smirnoff     NGM_NETFLOW_SHOW = 3|NGM_READONLY|NGM_HASREPLY,	/* show ip cache flow */
573d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETDLT		= 4,	/* set data-link type */
583d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETIFINDEX	= 5, 	/* set interface index */
593d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETTIMEOUTS	= 6, 	/* set active/inactive flow timeouts */
601a6dd095SAlexander Motin     NGM_NETFLOW_SETCONFIG	= 7, 	/* set flow generation options */
615dcd9c10SGleb Smirnoff     NGM_NETFLOW_SETTEMPLATE	= 8, 	/* set v9 flow template periodic */
625dcd9c10SGleb Smirnoff     NGM_NETFLOW_SETMTU		= 9, 	/* set outgoing interface MTU */
6310fcb07cSAlexander V. Chernikov     NGM_NETFLOW_V9INFO = 10|NGM_READONLY|NGM_HASREPLY, 	/* get v9 info */
64a752e82dSGleb Smirnoff };
65a752e82dSGleb Smirnoff 
66a752e82dSGleb Smirnoff /* This structure is returned by the NGM_NETFLOW_INFO message */
67a752e82dSGleb Smirnoff struct ng_netflow_info {
685dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_bytes;		/* accounted IPv4 bytes */
697ee35ac9SGleb Smirnoff 	uint64_t	nfinfo_packets;		/* accounted IPv4 packets */
705dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_bytes6;		/* accounted IPv6 bytes */
717ee35ac9SGleb Smirnoff 	uint64_t	nfinfo_packets6;	/* accounted IPv6 packets */
725dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_sbytes;		/* skipped IPv4 bytes */
737ee35ac9SGleb Smirnoff 	uint64_t	nfinfo_spackets;	/* skipped IPv4 packets */
745dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_sbytes6;		/* skipped IPv6 bytes */
757ee35ac9SGleb Smirnoff 	uint64_t	nfinfo_spackets6;	/* skipped IPv6 packets */
767ee35ac9SGleb Smirnoff 	uint64_t	nfinfo_act_exp;		/* active expiries */
777ee35ac9SGleb Smirnoff 	uint64_t	nfinfo_inact_exp;	/* inactive expiries */
7835fd572dSGleb Smirnoff 	uint32_t	nfinfo_used;		/* used cache records */
795dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_used6;		/* used IPv6 cache records */
8035fd572dSGleb Smirnoff 	uint32_t	nfinfo_alloc_failed;	/* failed allocations */
8135fd572dSGleb Smirnoff 	uint32_t	nfinfo_export_failed;	/* failed exports */
825dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_export9_failed;	/* failed exports */
835dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_realloc_mbuf;	/* reallocated mbufs */
845dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_alloc_fibs;	/* fibs allocated */
85a752e82dSGleb Smirnoff 	uint32_t	nfinfo_inact_t;		/* flow inactive timeout */
86a752e82dSGleb Smirnoff 	uint32_t	nfinfo_act_t;		/* flow active timeout */
87a752e82dSGleb Smirnoff };
88a752e82dSGleb Smirnoff 
897ee35ac9SGleb Smirnoff /* Parse the info structure */
907ee35ac9SGleb Smirnoff #define	NG_NETFLOW_INFO_TYPE {					\
917ee35ac9SGleb Smirnoff 	{ "IPv4 bytes",			&ng_parse_uint64_type },\
927ee35ac9SGleb Smirnoff 	{ "IPv4 packets",		&ng_parse_uint64_type },\
937ee35ac9SGleb Smirnoff 	{ "IPv6 bytes",			&ng_parse_uint64_type },\
947ee35ac9SGleb Smirnoff 	{ "IPv6 packets",		&ng_parse_uint64_type },\
957ee35ac9SGleb Smirnoff 	{ "IPv4 skipped bytes",		&ng_parse_uint64_type },\
967ee35ac9SGleb Smirnoff 	{ "IPv4 skipped packets",	&ng_parse_uint64_type },\
977ee35ac9SGleb Smirnoff 	{ "IPv6 skipped bytes",		&ng_parse_uint64_type },\
987ee35ac9SGleb Smirnoff 	{ "IPv6 skipped packets",	&ng_parse_uint64_type },\
997ee35ac9SGleb Smirnoff 	{ "Active expiries",		&ng_parse_uint64_type },\
1007ee35ac9SGleb Smirnoff 	{ "Inactive expiries",		&ng_parse_uint64_type },\
1017ee35ac9SGleb Smirnoff 	{ "IPv4 records used",		&ng_parse_uint32_type },\
1027ee35ac9SGleb Smirnoff 	{ "IPv6 records used",		&ng_parse_uint32_type },\
1037ee35ac9SGleb Smirnoff 	{ "Failed allocations",		&ng_parse_uint32_type },\
1047ee35ac9SGleb Smirnoff 	{ "V5 failed exports",		&ng_parse_uint32_type },\
1057ee35ac9SGleb Smirnoff 	{ "V9 failed exports",		&ng_parse_uint32_type },\
1067ee35ac9SGleb Smirnoff 	{ "mbuf reallocations",		&ng_parse_uint32_type },\
1077ee35ac9SGleb Smirnoff 	{ "fibs allocated",		&ng_parse_uint32_type },\
1087ee35ac9SGleb Smirnoff 	{ "Inactive timeout",		&ng_parse_uint32_type },\
1097ee35ac9SGleb Smirnoff 	{ "Active timeout",		&ng_parse_uint32_type },\
1107ee35ac9SGleb Smirnoff 	{ NULL }						\
1117ee35ac9SGleb Smirnoff }
1127ee35ac9SGleb Smirnoff 
113a752e82dSGleb Smirnoff /* This structure is returned by the NGM_NETFLOW_IFINFO message */
114a752e82dSGleb Smirnoff struct ng_netflow_ifinfo {
115a752e82dSGleb Smirnoff 	uint32_t	ifinfo_packets;	/* number of packets for this iface */
116a752e82dSGleb Smirnoff 	uint8_t		ifinfo_dlt;	/* Data Link Type, DLT_XXX */
117a752e82dSGleb Smirnoff #define	MAXDLTNAMELEN	20
11871336d8cSGleb Smirnoff 	uint16_t	ifinfo_index;	/* connected iface index */
1191a6dd095SAlexander Motin 	uint32_t	conf;
120a752e82dSGleb Smirnoff };
121a752e82dSGleb Smirnoff 
122a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETDLT message */
123a752e82dSGleb Smirnoff struct ng_netflow_setdlt {
124a752e82dSGleb Smirnoff 	uint16_t iface;		/* which iface dlt change */
125a752e82dSGleb Smirnoff 	uint8_t  dlt;		/* DLT_XXX from bpf.h */
126a752e82dSGleb Smirnoff };
127a752e82dSGleb Smirnoff 
128a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETIFINDEX */
129a752e82dSGleb Smirnoff struct ng_netflow_setifindex {
13071336d8cSGleb Smirnoff 	uint16_t iface;		/* which iface index change */
13171336d8cSGleb Smirnoff 	uint16_t index;		/* new index */
132a752e82dSGleb Smirnoff };
133a752e82dSGleb Smirnoff 
134a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETTIMEOUTS */
135a752e82dSGleb Smirnoff struct ng_netflow_settimeouts {
136a752e82dSGleb Smirnoff 	uint32_t	inactive_timeout;	/* flow inactive timeout */
137a752e82dSGleb Smirnoff 	uint32_t	active_timeout;		/* flow active timeout */
138a752e82dSGleb Smirnoff };
139a752e82dSGleb Smirnoff 
14036374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_INGRESS		0x01	/* Account on ingress */
14136374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_EGRESS		0x02	/* Account on egress */
14236374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_ONCE		0x04	/* Add tag to account only once */
14336374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_THISONCE	0x08	/* Account once in current node */
14436374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_NOSRCLOOKUP	0x10	/* No radix lookup on src */
14536374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_NODSTLOOKUP	0x20	/* No radix lookup on dst */
14636374fcfSAlexander V. Chernikov 
14736374fcfSAlexander V. Chernikov #define NG_NETFLOW_IS_FRAG		0x01
14836374fcfSAlexander V. Chernikov #define NG_NETFLOW_FLOW_FLAGS		(NG_NETFLOW_CONF_NOSRCLOOKUP|\
14936374fcfSAlexander V. Chernikov 					NG_NETFLOW_CONF_NODSTLOOKUP)
1501a6dd095SAlexander Motin 
1511a6dd095SAlexander Motin /* This structure is passed to NGM_NETFLOW_SETCONFIG */
1521a6dd095SAlexander Motin struct ng_netflow_setconfig {
15371336d8cSGleb Smirnoff 	uint16_t iface;		/* which iface config change */
15471336d8cSGleb Smirnoff 	uint32_t conf;		/* new config */
1551a6dd095SAlexander Motin };
1561a6dd095SAlexander Motin 
1575dcd9c10SGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETTEMPLATE */
1585dcd9c10SGleb Smirnoff struct ng_netflow_settemplate {
1595dcd9c10SGleb Smirnoff 	uint16_t time;		/* max time between announce */
1605dcd9c10SGleb Smirnoff 	uint16_t packets;	/* max packets between announce */
1615dcd9c10SGleb Smirnoff };
1625dcd9c10SGleb Smirnoff 
1635dcd9c10SGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETMTU */
1645dcd9c10SGleb Smirnoff struct ng_netflow_setmtu {
1655dcd9c10SGleb Smirnoff 	uint16_t mtu;		/* MTU for packet */
1665dcd9c10SGleb Smirnoff };
1675dcd9c10SGleb Smirnoff 
168053359b7SPedro F. Giffuni /* This structure is used in NGM_NETFLOW_SHOW request/response */
169ea7e1638SGleb Smirnoff struct ngnf_show_header {
170ea7e1638SGleb Smirnoff 	u_char		version;	/* IPv4 or IPv6 */
171ea7e1638SGleb Smirnoff 	uint32_t	hash_id;	/* current hash index */
17271336d8cSGleb Smirnoff 	uint32_t	list_id;	/* current record number in hash */
173ea7e1638SGleb Smirnoff 	uint32_t	nentries;	/* number of records in response */
174ea7e1638SGleb Smirnoff };
175ea7e1638SGleb Smirnoff 
17610fcb07cSAlexander V. Chernikov /* This structure is used in NGM_NETFLOW_V9INFO message */
17710fcb07cSAlexander V. Chernikov struct ng_netflow_v9info {
17810fcb07cSAlexander V. Chernikov 	uint16_t	templ_packets;	/* v9 template packets */
17910fcb07cSAlexander V. Chernikov 	uint16_t	templ_time;	/* v9 template time */
18010fcb07cSAlexander V. Chernikov 	uint16_t	mtu;		/* v9 MTU */
18110fcb07cSAlexander V. Chernikov };
18210fcb07cSAlexander V. Chernikov 
183ea7e1638SGleb Smirnoff /* XXXGL
184ea7e1638SGleb Smirnoff  * Somewhere flow_rec6 is casted to flow_rec, and flow6_entry_data is
185ea7e1638SGleb Smirnoff  * casted to flow_entry_data. After casting, fle->r.fib is accessed.
186ea7e1638SGleb Smirnoff  * So beginning of these structs up to fib should be kept common.
187ea7e1638SGleb Smirnoff  */
1885dcd9c10SGleb Smirnoff 
189a752e82dSGleb Smirnoff /* This is unique data, which identifies flow */
190a752e82dSGleb Smirnoff struct flow_rec {
19171336d8cSGleb Smirnoff 	uint16_t	flow_type;
1925dcd9c10SGleb Smirnoff 	uint16_t	fib;
193a752e82dSGleb Smirnoff 	struct in_addr	r_src;
194a752e82dSGleb Smirnoff 	struct in_addr	r_dst;
195a752e82dSGleb Smirnoff 	union {
196a752e82dSGleb Smirnoff 		struct {
197a752e82dSGleb Smirnoff 			uint16_t	s_port;	/* source TCP/UDP port */
198a752e82dSGleb Smirnoff 			uint16_t	d_port; /* destination TCP/UDP port */
199a752e82dSGleb Smirnoff 		} dir;
200a752e82dSGleb Smirnoff 		uint32_t both;
201a752e82dSGleb Smirnoff 	} ports;
202a752e82dSGleb Smirnoff 	union {
203a752e82dSGleb Smirnoff 		struct {
204a752e82dSGleb Smirnoff 			u_char		prot;	/* IP protocol */
205a752e82dSGleb Smirnoff 			u_char		tos;	/* IP TOS */
206a752e82dSGleb Smirnoff 			uint16_t	i_ifx;	/* input interface index */
207a752e82dSGleb Smirnoff 		} i;
208a752e82dSGleb Smirnoff 		uint32_t all;
209a752e82dSGleb Smirnoff 	} misc;
210a752e82dSGleb Smirnoff };
211a752e82dSGleb Smirnoff 
2125dcd9c10SGleb Smirnoff /* This is unique data, which identifies flow */
2135dcd9c10SGleb Smirnoff struct flow6_rec {
21471336d8cSGleb Smirnoff 	uint16_t	flow_type;
2155dcd9c10SGleb Smirnoff 	uint16_t	fib;
2165dcd9c10SGleb Smirnoff 	union {
2175dcd9c10SGleb Smirnoff 		struct in_addr	r_src;
2185dcd9c10SGleb Smirnoff 		struct in6_addr	r_src6;
2195dcd9c10SGleb Smirnoff 	} src;
2205dcd9c10SGleb Smirnoff 	union {
2215dcd9c10SGleb Smirnoff 		struct in_addr	r_dst;
2225dcd9c10SGleb Smirnoff 		struct in6_addr	r_dst6;
2235dcd9c10SGleb Smirnoff 	} dst;
2245dcd9c10SGleb Smirnoff 	union {
2255dcd9c10SGleb Smirnoff 		struct {
2265dcd9c10SGleb Smirnoff 			uint16_t	s_port;	/* source TCP/UDP port */
2275dcd9c10SGleb Smirnoff 			uint16_t	d_port; /* destination TCP/UDP port */
2285dcd9c10SGleb Smirnoff 		} dir;
2295dcd9c10SGleb Smirnoff 		uint32_t both;
2305dcd9c10SGleb Smirnoff 	} ports;
2315dcd9c10SGleb Smirnoff 	union {
2325dcd9c10SGleb Smirnoff 		struct {
2335dcd9c10SGleb Smirnoff 			u_char		prot;	/* IP protocol */
2345dcd9c10SGleb Smirnoff 			u_char		tos;	/* IP TOS */
2355dcd9c10SGleb Smirnoff 			uint16_t	i_ifx;	/* input interface index */
2365dcd9c10SGleb Smirnoff 		} i;
2375dcd9c10SGleb Smirnoff 		uint32_t all;
2385dcd9c10SGleb Smirnoff 	} misc;
2395dcd9c10SGleb Smirnoff };
2405dcd9c10SGleb Smirnoff 
241a752e82dSGleb Smirnoff #define	r_ip_p	misc.i.prot
242a752e82dSGleb Smirnoff #define	r_tos	misc.i.tos
243a752e82dSGleb Smirnoff #define	r_i_ifx	misc.i.i_ifx
244a752e82dSGleb Smirnoff #define r_misc	misc.all
245a752e82dSGleb Smirnoff #define r_ports	ports.both
246a752e82dSGleb Smirnoff #define r_sport	ports.dir.s_port
247a752e82dSGleb Smirnoff #define r_dport	ports.dir.d_port
248a752e82dSGleb Smirnoff 
249a752e82dSGleb Smirnoff /* A flow entry which accumulates statistics */
250a752e82dSGleb Smirnoff struct flow_entry_data {
2515dcd9c10SGleb Smirnoff 	uint16_t	version;	/* Protocol version */
252a752e82dSGleb Smirnoff 	struct flow_rec	r;
253a752e82dSGleb Smirnoff 	struct in_addr	next_hop;
254a752e82dSGleb Smirnoff 	uint16_t	fle_o_ifx;	/* output interface index */
255a752e82dSGleb Smirnoff #define	fle_i_ifx	r.misc.i.i_ifx
256a752e82dSGleb Smirnoff 	uint8_t		dst_mask;	/* destination route mask bits */
257a752e82dSGleb Smirnoff 	uint8_t		src_mask;	/* source route mask bits */
258a752e82dSGleb Smirnoff 	u_long		packets;
259a752e82dSGleb Smirnoff 	u_long		bytes;
260a752e82dSGleb Smirnoff 	long		first;		/* uptime on first packet */
261a752e82dSGleb Smirnoff 	long		last;		/* uptime on last packet */
262a752e82dSGleb Smirnoff 	u_char		tcp_flags;	/* cumulative OR */
263a752e82dSGleb Smirnoff };
264a752e82dSGleb Smirnoff 
2655dcd9c10SGleb Smirnoff struct flow6_entry_data {
2665dcd9c10SGleb Smirnoff 	uint16_t		version;	/* Protocol version */
2675dcd9c10SGleb Smirnoff 	struct flow6_rec	r;
2685dcd9c10SGleb Smirnoff 	union {
2695dcd9c10SGleb Smirnoff 		struct in_addr	next_hop;
2705dcd9c10SGleb Smirnoff 		struct in6_addr	next_hop6;
2715dcd9c10SGleb Smirnoff 	} n;
2725dcd9c10SGleb Smirnoff 	uint16_t	fle_o_ifx;	/* output interface index */
2735dcd9c10SGleb Smirnoff #define	fle_i_ifx	r.misc.i.i_ifx
2745dcd9c10SGleb Smirnoff 	uint8_t		dst_mask;	/* destination route mask bits */
2755dcd9c10SGleb Smirnoff 	uint8_t		src_mask;	/* source route mask bits */
2765dcd9c10SGleb Smirnoff 	u_long		packets;
2775dcd9c10SGleb Smirnoff 	u_long		bytes;
2785dcd9c10SGleb Smirnoff 	long		first;		/* uptime on first packet */
2795dcd9c10SGleb Smirnoff 	long		last;		/* uptime on last packet */
2805dcd9c10SGleb Smirnoff 	u_char		tcp_flags;	/* cumulative OR */
2815dcd9c10SGleb Smirnoff };
2825dcd9c10SGleb Smirnoff 
283a752e82dSGleb Smirnoff /*
284a752e82dSGleb Smirnoff  * How many flow records we will transfer at once
285a752e82dSGleb Smirnoff  * without overflowing socket receive buffer
286a752e82dSGleb Smirnoff  */
287a752e82dSGleb Smirnoff #define NREC_AT_ONCE	 1000
288ea7e1638SGleb Smirnoff #define NREC6_AT_ONCE	(NREC_AT_ONCE * sizeof(struct flow_entry_data) / \
289ea7e1638SGleb Smirnoff 			sizeof(struct flow6_entry_data))
290ea7e1638SGleb Smirnoff #define NGRESP_SIZE	(sizeof(struct ngnf_show_header) + (NREC_AT_ONCE * \
291a752e82dSGleb Smirnoff 			sizeof(struct flow_entry_data)))
292a752e82dSGleb Smirnoff #define SORCVBUF_SIZE	(NGRESP_SIZE + 2 * sizeof(struct ng_mesg))
293a752e82dSGleb Smirnoff 
294a752e82dSGleb Smirnoff /* Everything below is for kernel */
295a752e82dSGleb Smirnoff 
296a752e82dSGleb Smirnoff #ifdef _KERNEL
297a752e82dSGleb Smirnoff 
298a752e82dSGleb Smirnoff struct flow_entry {
299494e177aSGleb Smirnoff 	TAILQ_ENTRY(flow_entry)	fle_hash;	/* entries in hash slot */
300ea7e1638SGleb Smirnoff 	struct flow_entry_data	f;
301a752e82dSGleb Smirnoff };
302a752e82dSGleb Smirnoff 
3035dcd9c10SGleb Smirnoff struct flow6_entry {
304ea7e1638SGleb Smirnoff 	TAILQ_ENTRY(flow_entry)	fle_hash;	/* entries in hash slot */
3055dcd9c10SGleb Smirnoff 	struct flow6_entry_data	f;
3065dcd9c10SGleb Smirnoff };
307a752e82dSGleb Smirnoff /* Parsing declarations */
308a752e82dSGleb Smirnoff 
309a752e82dSGleb Smirnoff /* Parse the ifinfo structure */
310a752e82dSGleb Smirnoff #define NG_NETFLOW_IFINFO_TYPE	{			\
311a752e82dSGleb Smirnoff 	{ "packets",		&ng_parse_uint32_type },\
312a752e82dSGleb Smirnoff 	{ "data link type",	&ng_parse_uint8_type },	\
313a752e82dSGleb Smirnoff 	{ "index",		&ng_parse_uint16_type },\
3141a6dd095SAlexander Motin 	{ "conf",		&ng_parse_uint32_type },\
315a752e82dSGleb Smirnoff 	{ NULL }					\
316a752e82dSGleb Smirnoff }
317a752e82dSGleb Smirnoff 
318a752e82dSGleb Smirnoff /* Parse the setdlt structure */
319a752e82dSGleb Smirnoff #define	NG_NETFLOW_SETDLT_TYPE {			\
320a752e82dSGleb Smirnoff 	{ "iface",	&ng_parse_uint16_type },	\
321a752e82dSGleb Smirnoff 	{ "dlt",	&ng_parse_uint8_type },		\
322a752e82dSGleb Smirnoff 	{ NULL }					\
323a752e82dSGleb Smirnoff }
324a752e82dSGleb Smirnoff 
325a752e82dSGleb Smirnoff /* Parse the setifindex structure */
326a752e82dSGleb Smirnoff #define	NG_NETFLOW_SETIFINDEX_TYPE {			\
327a752e82dSGleb Smirnoff 	{ "iface",	&ng_parse_uint16_type },	\
328a752e82dSGleb Smirnoff 	{ "index",	&ng_parse_uint16_type },	\
329a752e82dSGleb Smirnoff 	{ NULL }					\
330a752e82dSGleb Smirnoff }
331a752e82dSGleb Smirnoff 
332a752e82dSGleb Smirnoff /* Parse the settimeouts structure */
333a752e82dSGleb Smirnoff #define NG_NETFLOW_SETTIMEOUTS_TYPE {			\
334a752e82dSGleb Smirnoff 	{ "inactive",	&ng_parse_uint32_type },	\
335a752e82dSGleb Smirnoff 	{ "active",	&ng_parse_uint32_type },	\
336a752e82dSGleb Smirnoff 	{ NULL }					\
337a752e82dSGleb Smirnoff }
338a752e82dSGleb Smirnoff 
3391a6dd095SAlexander Motin /* Parse the setifindex structure */
3401a6dd095SAlexander Motin #define	NG_NETFLOW_SETCONFIG_TYPE {			\
3411a6dd095SAlexander Motin 	{ "iface",	&ng_parse_uint16_type },	\
3421a6dd095SAlexander Motin 	{ "conf",	&ng_parse_uint32_type },	\
3431a6dd095SAlexander Motin 	{ NULL }					\
3441a6dd095SAlexander Motin }
3451a6dd095SAlexander Motin 
3465dcd9c10SGleb Smirnoff /* Parse the settemplate structure */
3475dcd9c10SGleb Smirnoff #define	NG_NETFLOW_SETTEMPLATE_TYPE {		\
3485dcd9c10SGleb Smirnoff 	{ "time",	&ng_parse_uint16_type },	\
3495dcd9c10SGleb Smirnoff 	{ "packets",	&ng_parse_uint16_type },	\
3505dcd9c10SGleb Smirnoff 	{ NULL }					\
3515dcd9c10SGleb Smirnoff }
3525dcd9c10SGleb Smirnoff 
3535dcd9c10SGleb Smirnoff /* Parse the setmtu structure */
3545dcd9c10SGleb Smirnoff #define	NG_NETFLOW_SETMTU_TYPE {			\
3555dcd9c10SGleb Smirnoff 	{ "mtu",	&ng_parse_uint16_type },	\
3565dcd9c10SGleb Smirnoff 	{ NULL }					\
3575dcd9c10SGleb Smirnoff }
3585dcd9c10SGleb Smirnoff 
35910fcb07cSAlexander V. Chernikov /* Parse the v9info structure */
36010fcb07cSAlexander V. Chernikov #define	NG_NETFLOW_V9INFO_TYPE {				\
36110fcb07cSAlexander V. Chernikov 	{ "v9 template packets",	&ng_parse_uint16_type },\
36210fcb07cSAlexander V. Chernikov 	{ "v9 template time",		&ng_parse_uint16_type },\
36310fcb07cSAlexander V. Chernikov 	{ "v9 MTU",			&ng_parse_uint16_type },\
36410fcb07cSAlexander V. Chernikov 	{ NULL }						\
36510fcb07cSAlexander V. Chernikov }
36610fcb07cSAlexander V. Chernikov 
367a752e82dSGleb Smirnoff /* Private hook data */
368a752e82dSGleb Smirnoff struct ng_netflow_iface {
369a752e82dSGleb Smirnoff 	hook_p		hook;		/* NULL when disconnected */
3709818b82fSGleb Smirnoff 	hook_p		out;		/* NULL when no bypass hook */
371a752e82dSGleb Smirnoff 	struct ng_netflow_ifinfo	info;
372a752e82dSGleb Smirnoff };
373a752e82dSGleb Smirnoff 
374a752e82dSGleb Smirnoff typedef struct ng_netflow_iface *iface_p;
375a752e82dSGleb Smirnoff typedef struct ng_netflow_ifinfo *ifinfo_p;
376a752e82dSGleb Smirnoff 
3775dcd9c10SGleb Smirnoff struct netflow_export_item {
3785dcd9c10SGleb Smirnoff 	item_p		item;
3795dcd9c10SGleb Smirnoff 	item_p		item9;
3805dcd9c10SGleb Smirnoff 	struct netflow_v9_packet_opt	*item9_opt;
3815dcd9c10SGleb Smirnoff };
3825dcd9c10SGleb Smirnoff 
3835dcd9c10SGleb Smirnoff /* Structure contatining fib-specific data */
3845dcd9c10SGleb Smirnoff struct fib_export {
3855dcd9c10SGleb Smirnoff 	uint32_t	fib;		/* kernel fib id */
38671336d8cSGleb Smirnoff 
38771336d8cSGleb Smirnoff 	/* Various data used for export */
38871336d8cSGleb Smirnoff 	struct netflow_export_item exp;
38971336d8cSGleb Smirnoff 
3905dcd9c10SGleb Smirnoff 	struct mtx	export_mtx;	/* exp.item mutex */
3915dcd9c10SGleb Smirnoff 	struct mtx	export9_mtx;	/* exp.item9 mutex */
3925dcd9c10SGleb Smirnoff 	uint32_t	flow_seq;	/* current V5 flow sequence */
3935dcd9c10SGleb Smirnoff 	uint32_t	flow9_seq;	/* current V9 flow sequence */
3945dcd9c10SGleb Smirnoff 	uint32_t	domain_id;	/* Observartion domain id */
3955dcd9c10SGleb Smirnoff 	/* Netflow V9 counters */
3965dcd9c10SGleb Smirnoff 	uint32_t	templ_last_ts;	/* unixtime of last template announce */
39771336d8cSGleb Smirnoff 	uint32_t	templ_last_pkt;	/* packet count on last announce */
3985dcd9c10SGleb Smirnoff 	uint32_t	sent_packets;	/* packets sent by exporter; */
39971336d8cSGleb Smirnoff 
40071336d8cSGleb Smirnoff 	/* Current packet specific options */
40171336d8cSGleb Smirnoff 	struct netflow_v9_packet_opt *export9_opt;
4025dcd9c10SGleb Smirnoff };
4035dcd9c10SGleb Smirnoff 
4045dcd9c10SGleb Smirnoff typedef struct fib_export *fib_export_p;
4055dcd9c10SGleb Smirnoff 
406a752e82dSGleb Smirnoff /* Structure describing our flow engine */
407a752e82dSGleb Smirnoff struct netflow {
408a752e82dSGleb Smirnoff 	node_p		node;		/* link to the node itself */
409a752e82dSGleb Smirnoff 	hook_p		export;		/* export data goes there */
4105dcd9c10SGleb Smirnoff 	hook_p		export9;	/* Netflow V9 export data goes there */
411494e177aSGleb Smirnoff 	struct callout	exp_callout;	/* expiry periodic job */
412a752e82dSGleb Smirnoff 
413494e177aSGleb Smirnoff 	/*
414494e177aSGleb Smirnoff 	 * Flow entries are allocated in uma(9) zone zone. They are
415494e177aSGleb Smirnoff 	 * indexed by hash hash. Each hash element consist of tailqueue
416494e177aSGleb Smirnoff 	 * head and mutex to protect this element.
417494e177aSGleb Smirnoff 	 */
41851856405SGleb Smirnoff #define	CACHESIZE			(65536*16)
419a752e82dSGleb Smirnoff #define	CACHELOWAT			(CACHESIZE * 3/4)
420a752e82dSGleb Smirnoff #define	CACHEHIGHWAT			(CACHESIZE * 9/10)
421494e177aSGleb Smirnoff 	uma_zone_t		zone;
422a752e82dSGleb Smirnoff 	struct flow_hash_entry	*hash;
423a752e82dSGleb Smirnoff 
424494e177aSGleb Smirnoff 	/*
425494e177aSGleb Smirnoff 	 * NetFlow data export
426494e177aSGleb Smirnoff 	 *
427494e177aSGleb Smirnoff 	 * export_item is a data item, it has an mbuf with cluster
428494e177aSGleb Smirnoff 	 * attached to it. A thread detaches export_item from priv
429494e177aSGleb Smirnoff 	 * and works with it. If the export is full it is sent, and
430494e177aSGleb Smirnoff 	 * a new one is allocated. Before exiting thread re-attaches
431494e177aSGleb Smirnoff 	 * its current item back to priv. If there is item already,
432494e177aSGleb Smirnoff 	 * current incomplete datagram is sent.
433494e177aSGleb Smirnoff 	 * export_mtx is used for attaching/detaching.
434494e177aSGleb Smirnoff 	 */
4355dcd9c10SGleb Smirnoff 
4365dcd9c10SGleb Smirnoff 	/* IPv6 support */
4375dcd9c10SGleb Smirnoff #ifdef INET6
4385dcd9c10SGleb Smirnoff 	uma_zone_t		zone6;
439ea7e1638SGleb Smirnoff 	struct flow_hash_entry	*hash6;
4405dcd9c10SGleb Smirnoff #endif
44171336d8cSGleb Smirnoff 
4427ee35ac9SGleb Smirnoff 	/* Statistics. */
4437ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_bytes;		/* accounted IPv4 bytes */
4447ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_packets;		/* accounted IPv4 packets */
4457ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_bytes6;		/* accounted IPv6 bytes */
4467ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_packets6;	/* accounted IPv6 packets */
4477ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_sbytes;		/* skipped IPv4 bytes */
4487ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_spackets;	/* skipped IPv4 packets */
4497ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_sbytes6;		/* skipped IPv6 bytes */
4507ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_spackets6;	/* skipped IPv6 packets */
4517ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_act_exp;		/* active expiries */
4527ee35ac9SGleb Smirnoff 	counter_u64_t	nfinfo_inact_exp;	/* inactive expiries */
4537ee35ac9SGleb Smirnoff 	uint32_t	nfinfo_alloc_failed;	/* failed allocations */
4547ee35ac9SGleb Smirnoff 	uint32_t	nfinfo_export_failed;	/* failed exports */
4557ee35ac9SGleb Smirnoff 	uint32_t	nfinfo_export9_failed;	/* failed exports */
4567ee35ac9SGleb Smirnoff 	uint32_t	nfinfo_realloc_mbuf;	/* reallocated mbufs */
4577ee35ac9SGleb Smirnoff 	uint32_t	nfinfo_alloc_fibs;	/* fibs allocated */
4587ee35ac9SGleb Smirnoff 	uint32_t	nfinfo_inact_t;		/* flow inactive timeout */
4597ee35ac9SGleb Smirnoff 	uint32_t	nfinfo_act_t;		/* flow active timeout */
4607ee35ac9SGleb Smirnoff 
4615dcd9c10SGleb Smirnoff 	/* Multiple FIB support */
46271336d8cSGleb Smirnoff 	fib_export_p	*fib_data;	/* vector to per-fib data */
46314797255SAlexander V. Chernikov 	uint16_t	maxfibs;	/* number of allocated fibs */
4645dcd9c10SGleb Smirnoff 
46510fcb07cSAlexander V. Chernikov 	/* Netflow v9 configuration options */
4665dcd9c10SGleb Smirnoff 	/*
4675dcd9c10SGleb Smirnoff 	 * RFC 3954 clause 7.3
4685dcd9c10SGleb Smirnoff 	 * "Both options MUST be configurable by the user on the Exporter."
4695dcd9c10SGleb Smirnoff 	 */
4705dcd9c10SGleb Smirnoff 	uint16_t	templ_time;	/* time between sending templates */
4715dcd9c10SGleb Smirnoff 	uint16_t	templ_packets;	/* packets between sending templates */
4725dcd9c10SGleb Smirnoff #define NETFLOW_V9_MAX_FLOWSETS	2
4735dcd9c10SGleb Smirnoff 	u_char		flowsets_count; /* current flowsets used */
47471336d8cSGleb Smirnoff 
47571336d8cSGleb Smirnoff 	/* Count of records in each flowset */
47671336d8cSGleb Smirnoff 	u_char		flowset_records[NETFLOW_V9_MAX_FLOWSETS - 1];
4775dcd9c10SGleb Smirnoff 	uint16_t	mtu;		/* export interface MTU */
47871336d8cSGleb Smirnoff 
47971336d8cSGleb Smirnoff 	/* Pointers to pre-compiled flowsets */
48071336d8cSGleb Smirnoff 	struct netflow_v9_flowset_header
48171336d8cSGleb Smirnoff 	    *v9_flowsets[NETFLOW_V9_MAX_FLOWSETS - 1];
4823bbbf02dSGleb Smirnoff 
4833bbbf02dSGleb Smirnoff 	struct ng_netflow_iface	ifaces[NG_NETFLOW_MAXIFACES];
484a752e82dSGleb Smirnoff };
485a752e82dSGleb Smirnoff 
486a752e82dSGleb Smirnoff typedef struct netflow *priv_p;
487a752e82dSGleb Smirnoff 
488a752e82dSGleb Smirnoff /* Header of a small list in hash cell */
489a752e82dSGleb Smirnoff struct flow_hash_entry {
490494e177aSGleb Smirnoff 	struct mtx		mtx;
491494e177aSGleb Smirnoff 	TAILQ_HEAD(fhead, flow_entry) head;
492a752e82dSGleb Smirnoff };
493a752e82dSGleb Smirnoff #define	ERROUT(x)	{ error = (x); goto done; }
494a752e82dSGleb Smirnoff 
4951a6dd095SAlexander Motin #define MTAG_NETFLOW		1221656444
4961a6dd095SAlexander Motin #define MTAG_NETFLOW_CALLED	0
4971a6dd095SAlexander Motin 
4985dcd9c10SGleb Smirnoff #define m_pktlen(m)	((m)->m_pkthdr.len)
4995dcd9c10SGleb Smirnoff #define IP6VERSION	6
5005dcd9c10SGleb Smirnoff 
5015dcd9c10SGleb Smirnoff #define priv_to_fib(priv, fib)	(priv)->fib_data[(fib)]
5025dcd9c10SGleb Smirnoff 
5035dcd9c10SGleb Smirnoff /*
5045dcd9c10SGleb Smirnoff  * Cisco uses milliseconds for uptime. Bad idea, since it overflows
5055dcd9c10SGleb Smirnoff  * every 48+ days. But we will do same to keep compatibility. This macro
5065dcd9c10SGleb Smirnoff  * does overflowable multiplication to 1000.
5075dcd9c10SGleb Smirnoff  */
5085dcd9c10SGleb Smirnoff #define	MILLIUPTIME(t)	(((t) << 9) +	/* 512 */	\
5095dcd9c10SGleb Smirnoff 			 ((t) << 8) +	/* 256 */	\
5105dcd9c10SGleb Smirnoff 			 ((t) << 7) +	/* 128 */	\
5115dcd9c10SGleb Smirnoff 			 ((t) << 6) +	/* 64  */	\
5125dcd9c10SGleb Smirnoff 			 ((t) << 5) +	/* 32  */	\
5135dcd9c10SGleb Smirnoff 			 ((t) << 3))	/* 8   */
5145dcd9c10SGleb Smirnoff 
515a752e82dSGleb Smirnoff /* Prototypes for netflow.c */
516b6770143SGleb Smirnoff void	ng_netflow_cache_init(priv_p);
517a752e82dSGleb Smirnoff void	ng_netflow_cache_flush(priv_p);
5185dcd9c10SGleb Smirnoff int	ng_netflow_fib_init(priv_p priv, int fib);
519a752e82dSGleb Smirnoff void	ng_netflow_copyinfo(priv_p, struct ng_netflow_info *);
52010fcb07cSAlexander V. Chernikov void	ng_netflow_copyv9info(priv_p, struct ng_netflow_v9info *);
5215773ac11SJohn Baldwin callout_func_t ng_netflow_expire;
52271336d8cSGleb Smirnoff int 	ng_netflow_flow_add(priv_p, fib_export_p, struct ip *, caddr_t,
52371336d8cSGleb Smirnoff 	uint8_t, uint8_t, unsigned int);
52471336d8cSGleb Smirnoff int	ng_netflow_flow6_add(priv_p, fib_export_p, struct ip6_hdr *, caddr_t,
52571336d8cSGleb Smirnoff 	uint8_t, uint8_t, unsigned int);
52671336d8cSGleb Smirnoff int	ng_netflow_flow_show(priv_p, struct ngnf_show_header *req,
52771336d8cSGleb Smirnoff 	struct ngnf_show_header *resp);
5285dcd9c10SGleb Smirnoff void	ng_netflow_v9_cache_init(priv_p);
5295dcd9c10SGleb Smirnoff void	ng_netflow_v9_cache_flush(priv_p);
53071336d8cSGleb Smirnoff item_p	get_export9_dgram(priv_p, fib_export_p,
53171336d8cSGleb Smirnoff 	struct netflow_v9_packet_opt **);
5325dcd9c10SGleb Smirnoff void	return_export9_dgram(priv_p, fib_export_p, item_p,
5335dcd9c10SGleb Smirnoff 	struct netflow_v9_packet_opt *, int);
53471336d8cSGleb Smirnoff int	export9_add(item_p, struct netflow_v9_packet_opt *,
53571336d8cSGleb Smirnoff 	struct flow_entry *);
53671336d8cSGleb Smirnoff int	export9_send(priv_p, fib_export_p, item_p,
53771336d8cSGleb Smirnoff 	struct netflow_v9_packet_opt *, int);
5385dcd9c10SGleb Smirnoff 
539a752e82dSGleb Smirnoff #endif	/* _KERNEL */
540a752e82dSGleb Smirnoff #endif	/* _NG_NETFLOW_H_ */
541