xref: /freebsd/sys/netgraph/netflow/ng_netflow.h (revision 51856405)
1a752e82dSGleb Smirnoff /*-
25dcd9c10SGleb Smirnoff  * Copyright (c) 2010-2011 Alexander V. Chernikov <melifaro@ipfw.ru>
31d03bd16SGleb Smirnoff  * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
4a752e82dSGleb Smirnoff  * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
5a752e82dSGleb Smirnoff  * All rights reserved.
6a752e82dSGleb Smirnoff  *
7a752e82dSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
8a752e82dSGleb Smirnoff  * modification, are permitted provided that the following conditions
9a752e82dSGleb Smirnoff  * are met:
10a752e82dSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
11a752e82dSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
12a752e82dSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
13a752e82dSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
14a752e82dSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
15a752e82dSGleb Smirnoff  *
16a752e82dSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a752e82dSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a752e82dSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a752e82dSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a752e82dSGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a752e82dSGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a752e82dSGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a752e82dSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a752e82dSGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a752e82dSGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a752e82dSGleb Smirnoff  * SUCH DAMAGE.
27a752e82dSGleb Smirnoff  *
28a752e82dSGleb Smirnoff  *	 $SourceForge: ng_netflow.h,v 1.26 2004/09/04 15:44:55 glebius Exp $
29a752e82dSGleb Smirnoff  *	 $FreeBSD$
30a752e82dSGleb Smirnoff  */
31a752e82dSGleb Smirnoff 
32a752e82dSGleb Smirnoff #ifndef	_NG_NETFLOW_H_
33a752e82dSGleb Smirnoff #define	_NG_NETFLOW_H_
34a752e82dSGleb Smirnoff 
35a752e82dSGleb Smirnoff #define NG_NETFLOW_NODE_TYPE	"netflow"
36ea7e1638SGleb Smirnoff #define NGM_NETFLOW_COOKIE	1309868867
3710fcb07cSAlexander V. Chernikov #define NGM_NETFLOW_V9_COOKIE	1349865386
38a752e82dSGleb Smirnoff 
39dc7359b8SGleb Smirnoff #define	NG_NETFLOW_MAXIFACES	USHRT_MAX
40a752e82dSGleb Smirnoff 
41a752e82dSGleb Smirnoff /* Hook names */
42a752e82dSGleb Smirnoff 
43a752e82dSGleb Smirnoff #define	NG_NETFLOW_HOOK_DATA	"iface"
449818b82fSGleb Smirnoff #define	NG_NETFLOW_HOOK_OUT	"out"
45a752e82dSGleb Smirnoff #define NG_NETFLOW_HOOK_EXPORT	"export"
465dcd9c10SGleb Smirnoff #define NG_NETFLOW_HOOK_EXPORT9	"export9"
475dcd9c10SGleb Smirnoff 
485dcd9c10SGleb Smirnoff /* This define effectively disable (v5) netflow export hook! */
495dcd9c10SGleb Smirnoff /* #define COUNTERS_64 */
50a752e82dSGleb Smirnoff 
51a752e82dSGleb Smirnoff /* Netgraph commands understood by netflow node */
52a752e82dSGleb Smirnoff enum {
533d9dddcdSGleb Smirnoff     NGM_NETFLOW_INFO = 1|NGM_READONLY|NGM_HASREPLY,	/* get node info */
543d9dddcdSGleb Smirnoff     NGM_NETFLOW_IFINFO = 2|NGM_READONLY|NGM_HASREPLY,	/* get iface info */
553d9dddcdSGleb Smirnoff     NGM_NETFLOW_SHOW = 3|NGM_READONLY|NGM_HASREPLY,	/* show ip cache flow */
563d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETDLT		= 4,	/* set data-link type */
573d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETIFINDEX	= 5, 	/* set interface index */
583d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETTIMEOUTS	= 6, 	/* set active/inactive flow timeouts */
591a6dd095SAlexander Motin     NGM_NETFLOW_SETCONFIG	= 7, 	/* set flow generation options */
605dcd9c10SGleb Smirnoff     NGM_NETFLOW_SETTEMPLATE	= 8, 	/* set v9 flow template periodic */
615dcd9c10SGleb Smirnoff     NGM_NETFLOW_SETMTU		= 9, 	/* set outgoing interface MTU */
6210fcb07cSAlexander V. Chernikov     NGM_NETFLOW_V9INFO = 10|NGM_READONLY|NGM_HASREPLY, 	/* get v9 info */
63a752e82dSGleb Smirnoff };
64a752e82dSGleb Smirnoff 
65a752e82dSGleb Smirnoff /* This structure is returned by the NGM_NETFLOW_INFO message */
66a752e82dSGleb Smirnoff struct ng_netflow_info {
675dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_bytes;		/* accounted IPv4 bytes */
685dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_packets;		/* accounted IPv4 packets */
695dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_bytes6;		/* accounted IPv6 bytes */
705dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_packets6;	/* accounted IPv6 packets */
715dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_sbytes;		/* skipped IPv4 bytes */
725dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_spackets;	/* skipped IPv4 packets */
735dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_sbytes6;		/* skipped IPv6 bytes */
745dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_spackets6;	/* skipped IPv6 packets */
7535fd572dSGleb Smirnoff 	uint32_t	nfinfo_used;		/* used cache records */
765dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_used6;		/* used IPv6 cache records */
7735fd572dSGleb Smirnoff 	uint32_t	nfinfo_alloc_failed;	/* failed allocations */
7835fd572dSGleb Smirnoff 	uint32_t	nfinfo_export_failed;	/* failed exports */
795dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_export9_failed;	/* failed exports */
805dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_realloc_mbuf;	/* reallocated mbufs */
815dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_alloc_fibs;	/* fibs allocated */
8235fd572dSGleb Smirnoff 	uint32_t	nfinfo_act_exp;		/* active expiries */
8335fd572dSGleb Smirnoff 	uint32_t	nfinfo_inact_exp;	/* inactive expiries */
84a752e82dSGleb Smirnoff 	uint32_t	nfinfo_inact_t;		/* flow inactive timeout */
85a752e82dSGleb Smirnoff 	uint32_t	nfinfo_act_t;		/* flow active timeout */
86a752e82dSGleb Smirnoff };
87a752e82dSGleb Smirnoff 
88a752e82dSGleb Smirnoff /* This structure is returned by the NGM_NETFLOW_IFINFO message */
89a752e82dSGleb Smirnoff struct ng_netflow_ifinfo {
90a752e82dSGleb Smirnoff 	uint32_t	ifinfo_packets;	/* number of packets for this iface */
91a752e82dSGleb Smirnoff 	uint8_t		ifinfo_dlt;	/* Data Link Type, DLT_XXX */
92a752e82dSGleb Smirnoff #define	MAXDLTNAMELEN	20
9371336d8cSGleb Smirnoff 	uint16_t	ifinfo_index;	/* connected iface index */
941a6dd095SAlexander Motin 	uint32_t	conf;
95a752e82dSGleb Smirnoff };
96a752e82dSGleb Smirnoff 
97a752e82dSGleb Smirnoff 
98a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETDLT message */
99a752e82dSGleb Smirnoff struct ng_netflow_setdlt {
100a752e82dSGleb Smirnoff 	uint16_t iface;		/* which iface dlt change */
101a752e82dSGleb Smirnoff 	uint8_t  dlt;		/* DLT_XXX from bpf.h */
102a752e82dSGleb Smirnoff };
103a752e82dSGleb Smirnoff 
104a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETIFINDEX */
105a752e82dSGleb Smirnoff struct ng_netflow_setifindex {
10671336d8cSGleb Smirnoff 	uint16_t iface;		/* which iface index change */
10771336d8cSGleb Smirnoff 	uint16_t index;		/* new index */
108a752e82dSGleb Smirnoff };
109a752e82dSGleb Smirnoff 
110a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETTIMEOUTS */
111a752e82dSGleb Smirnoff struct ng_netflow_settimeouts {
112a752e82dSGleb Smirnoff 	uint32_t	inactive_timeout;	/* flow inactive timeout */
113a752e82dSGleb Smirnoff 	uint32_t	active_timeout;		/* flow active timeout */
114a752e82dSGleb Smirnoff };
115a752e82dSGleb Smirnoff 
11636374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_INGRESS		0x01	/* Account on ingress */
11736374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_EGRESS		0x02	/* Account on egress */
11836374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_ONCE		0x04	/* Add tag to account only once */
11936374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_THISONCE	0x08	/* Account once in current node */
12036374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_NOSRCLOOKUP	0x10	/* No radix lookup on src */
12136374fcfSAlexander V. Chernikov #define NG_NETFLOW_CONF_NODSTLOOKUP	0x20	/* No radix lookup on dst */
12236374fcfSAlexander V. Chernikov 
12336374fcfSAlexander V. Chernikov #define NG_NETFLOW_IS_FRAG		0x01
12436374fcfSAlexander V. Chernikov #define NG_NETFLOW_FLOW_FLAGS		(NG_NETFLOW_CONF_NOSRCLOOKUP|\
12536374fcfSAlexander V. Chernikov 					NG_NETFLOW_CONF_NODSTLOOKUP)
1261a6dd095SAlexander Motin 
1271a6dd095SAlexander Motin /* This structure is passed to NGM_NETFLOW_SETCONFIG */
1281a6dd095SAlexander Motin struct ng_netflow_setconfig {
12971336d8cSGleb Smirnoff 	uint16_t iface;		/* which iface config change */
13071336d8cSGleb Smirnoff 	uint32_t conf;		/* new config */
1311a6dd095SAlexander Motin };
1321a6dd095SAlexander Motin 
1335dcd9c10SGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETTEMPLATE */
1345dcd9c10SGleb Smirnoff struct ng_netflow_settemplate {
1355dcd9c10SGleb Smirnoff 	uint16_t time;		/* max time between announce */
1365dcd9c10SGleb Smirnoff 	uint16_t packets;	/* max packets between announce */
1375dcd9c10SGleb Smirnoff };
1385dcd9c10SGleb Smirnoff 
1395dcd9c10SGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETMTU */
1405dcd9c10SGleb Smirnoff struct ng_netflow_setmtu {
1415dcd9c10SGleb Smirnoff 	uint16_t mtu;		/* MTU for packet */
1425dcd9c10SGleb Smirnoff };
1435dcd9c10SGleb Smirnoff 
144ea7e1638SGleb Smirnoff /* This structure is used in NGM_NETFLOW_SHOW request/responce */
145ea7e1638SGleb Smirnoff struct ngnf_show_header {
146ea7e1638SGleb Smirnoff 	u_char		version;	/* IPv4 or IPv6 */
147ea7e1638SGleb Smirnoff 	uint32_t	hash_id;	/* current hash index */
14871336d8cSGleb Smirnoff 	uint32_t	list_id;	/* current record number in hash */
149ea7e1638SGleb Smirnoff 	uint32_t	nentries;	/* number of records in response */
150ea7e1638SGleb Smirnoff };
151ea7e1638SGleb Smirnoff 
15210fcb07cSAlexander V. Chernikov /* This structure is used in NGM_NETFLOW_V9INFO message */
15310fcb07cSAlexander V. Chernikov struct ng_netflow_v9info {
15410fcb07cSAlexander V. Chernikov 	uint16_t	templ_packets;	/* v9 template packets */
15510fcb07cSAlexander V. Chernikov 	uint16_t	templ_time;	/* v9 template time */
15610fcb07cSAlexander V. Chernikov 	uint16_t	mtu;		/* v9 MTU */
15710fcb07cSAlexander V. Chernikov };
15810fcb07cSAlexander V. Chernikov 
159ea7e1638SGleb Smirnoff /* XXXGL
160ea7e1638SGleb Smirnoff  * Somewhere flow_rec6 is casted to flow_rec, and flow6_entry_data is
161ea7e1638SGleb Smirnoff  * casted to flow_entry_data. After casting, fle->r.fib is accessed.
162ea7e1638SGleb Smirnoff  * So beginning of these structs up to fib should be kept common.
163ea7e1638SGleb Smirnoff  */
1645dcd9c10SGleb Smirnoff 
165a752e82dSGleb Smirnoff /* This is unique data, which identifies flow */
166a752e82dSGleb Smirnoff struct flow_rec {
16771336d8cSGleb Smirnoff 	uint16_t	flow_type;
1685dcd9c10SGleb Smirnoff 	uint16_t	fib;
169a752e82dSGleb Smirnoff 	struct in_addr	r_src;
170a752e82dSGleb Smirnoff 	struct in_addr	r_dst;
171a752e82dSGleb Smirnoff 	union {
172a752e82dSGleb Smirnoff 		struct {
173a752e82dSGleb Smirnoff 			uint16_t	s_port;	/* source TCP/UDP port */
174a752e82dSGleb Smirnoff 			uint16_t	d_port; /* destination TCP/UDP port */
175a752e82dSGleb Smirnoff 		} dir;
176a752e82dSGleb Smirnoff 		uint32_t both;
177a752e82dSGleb Smirnoff 	} ports;
178a752e82dSGleb Smirnoff 	union {
179a752e82dSGleb Smirnoff 		struct {
180a752e82dSGleb Smirnoff 			u_char		prot;	/* IP protocol */
181a752e82dSGleb Smirnoff 			u_char		tos;	/* IP TOS */
182a752e82dSGleb Smirnoff 			uint16_t	i_ifx;	/* input interface index */
183a752e82dSGleb Smirnoff 		} i;
184a752e82dSGleb Smirnoff 		uint32_t all;
185a752e82dSGleb Smirnoff 	} misc;
186a752e82dSGleb Smirnoff };
187a752e82dSGleb Smirnoff 
1885dcd9c10SGleb Smirnoff /* This is unique data, which identifies flow */
1895dcd9c10SGleb Smirnoff struct flow6_rec {
19071336d8cSGleb Smirnoff 	uint16_t	flow_type;
1915dcd9c10SGleb Smirnoff 	uint16_t	fib;
1925dcd9c10SGleb Smirnoff 	union {
1935dcd9c10SGleb Smirnoff 		struct in_addr	r_src;
1945dcd9c10SGleb Smirnoff 		struct in6_addr	r_src6;
1955dcd9c10SGleb Smirnoff 	} src;
1965dcd9c10SGleb Smirnoff 	union {
1975dcd9c10SGleb Smirnoff 		struct in_addr	r_dst;
1985dcd9c10SGleb Smirnoff 		struct in6_addr	r_dst6;
1995dcd9c10SGleb Smirnoff 	} dst;
2005dcd9c10SGleb Smirnoff 	union {
2015dcd9c10SGleb Smirnoff 		struct {
2025dcd9c10SGleb Smirnoff 			uint16_t	s_port;	/* source TCP/UDP port */
2035dcd9c10SGleb Smirnoff 			uint16_t	d_port; /* destination TCP/UDP port */
2045dcd9c10SGleb Smirnoff 		} dir;
2055dcd9c10SGleb Smirnoff 		uint32_t both;
2065dcd9c10SGleb Smirnoff 	} ports;
2075dcd9c10SGleb Smirnoff 	union {
2085dcd9c10SGleb Smirnoff 		struct {
2095dcd9c10SGleb Smirnoff 			u_char		prot;	/* IP protocol */
2105dcd9c10SGleb Smirnoff 			u_char		tos;	/* IP TOS */
2115dcd9c10SGleb Smirnoff 			uint16_t	i_ifx;	/* input interface index */
2125dcd9c10SGleb Smirnoff 		} i;
2135dcd9c10SGleb Smirnoff 		uint32_t all;
2145dcd9c10SGleb Smirnoff 	} misc;
2155dcd9c10SGleb Smirnoff };
2165dcd9c10SGleb Smirnoff 
217a752e82dSGleb Smirnoff #define	r_ip_p	misc.i.prot
218a752e82dSGleb Smirnoff #define	r_tos	misc.i.tos
219a752e82dSGleb Smirnoff #define	r_i_ifx	misc.i.i_ifx
220a752e82dSGleb Smirnoff #define r_misc	misc.all
221a752e82dSGleb Smirnoff #define r_ports	ports.both
222a752e82dSGleb Smirnoff #define r_sport	ports.dir.s_port
223a752e82dSGleb Smirnoff #define r_dport	ports.dir.d_port
224a752e82dSGleb Smirnoff 
225a752e82dSGleb Smirnoff /* A flow entry which accumulates statistics */
226a752e82dSGleb Smirnoff struct flow_entry_data {
2275dcd9c10SGleb Smirnoff 	uint16_t	version;	/* Protocol version */
228a752e82dSGleb Smirnoff 	struct flow_rec	r;
229a752e82dSGleb Smirnoff 	struct in_addr	next_hop;
230a752e82dSGleb Smirnoff 	uint16_t	fle_o_ifx;	/* output interface index */
231a752e82dSGleb Smirnoff #define	fle_i_ifx	r.misc.i.i_ifx
232a752e82dSGleb Smirnoff 	uint8_t		dst_mask;	/* destination route mask bits */
233a752e82dSGleb Smirnoff 	uint8_t		src_mask;	/* source route mask bits */
234a752e82dSGleb Smirnoff 	u_long		packets;
235a752e82dSGleb Smirnoff 	u_long		bytes;
236a752e82dSGleb Smirnoff 	long		first;		/* uptime on first packet */
237a752e82dSGleb Smirnoff 	long		last;		/* uptime on last packet */
238a752e82dSGleb Smirnoff 	u_char		tcp_flags;	/* cumulative OR */
239a752e82dSGleb Smirnoff };
240a752e82dSGleb Smirnoff 
2415dcd9c10SGleb Smirnoff struct flow6_entry_data {
2425dcd9c10SGleb Smirnoff 	uint16_t		version;	/* Protocol version */
2435dcd9c10SGleb Smirnoff 	struct flow6_rec	r;
2445dcd9c10SGleb Smirnoff 	union {
2455dcd9c10SGleb Smirnoff 		struct in_addr	next_hop;
2465dcd9c10SGleb Smirnoff 		struct in6_addr	next_hop6;
2475dcd9c10SGleb Smirnoff 	} n;
2485dcd9c10SGleb Smirnoff 	uint16_t	fle_o_ifx;	/* output interface index */
2495dcd9c10SGleb Smirnoff #define	fle_i_ifx	r.misc.i.i_ifx
2505dcd9c10SGleb Smirnoff 	uint8_t		dst_mask;	/* destination route mask bits */
2515dcd9c10SGleb Smirnoff 	uint8_t		src_mask;	/* source route mask bits */
2525dcd9c10SGleb Smirnoff 	u_long		packets;
2535dcd9c10SGleb Smirnoff 	u_long		bytes;
2545dcd9c10SGleb Smirnoff 	long		first;		/* uptime on first packet */
2555dcd9c10SGleb Smirnoff 	long		last;		/* uptime on last packet */
2565dcd9c10SGleb Smirnoff 	u_char		tcp_flags;	/* cumulative OR */
2575dcd9c10SGleb Smirnoff };
2585dcd9c10SGleb Smirnoff 
259a752e82dSGleb Smirnoff /*
260a752e82dSGleb Smirnoff  * How many flow records we will transfer at once
261a752e82dSGleb Smirnoff  * without overflowing socket receive buffer
262a752e82dSGleb Smirnoff  */
263a752e82dSGleb Smirnoff #define NREC_AT_ONCE	 1000
264ea7e1638SGleb Smirnoff #define NREC6_AT_ONCE	(NREC_AT_ONCE * sizeof(struct flow_entry_data) / \
265ea7e1638SGleb Smirnoff 			sizeof(struct flow6_entry_data))
266ea7e1638SGleb Smirnoff #define NGRESP_SIZE	(sizeof(struct ngnf_show_header) + (NREC_AT_ONCE * \
267a752e82dSGleb Smirnoff 			sizeof(struct flow_entry_data)))
268a752e82dSGleb Smirnoff #define SORCVBUF_SIZE	(NGRESP_SIZE + 2 * sizeof(struct ng_mesg))
269a752e82dSGleb Smirnoff 
270a752e82dSGleb Smirnoff /* Everything below is for kernel */
271a752e82dSGleb Smirnoff 
272a752e82dSGleb Smirnoff #ifdef _KERNEL
273a752e82dSGleb Smirnoff 
274a752e82dSGleb Smirnoff struct flow_entry {
275494e177aSGleb Smirnoff 	TAILQ_ENTRY(flow_entry)	fle_hash;	/* entries in hash slot */
276ea7e1638SGleb Smirnoff 	struct flow_entry_data	f;
277a752e82dSGleb Smirnoff };
278a752e82dSGleb Smirnoff 
2795dcd9c10SGleb Smirnoff struct flow6_entry {
280ea7e1638SGleb Smirnoff 	TAILQ_ENTRY(flow_entry)	fle_hash;	/* entries in hash slot */
2815dcd9c10SGleb Smirnoff 	struct flow6_entry_data	f;
2825dcd9c10SGleb Smirnoff };
283a752e82dSGleb Smirnoff /* Parsing declarations */
284a752e82dSGleb Smirnoff 
285a752e82dSGleb Smirnoff /* Parse the info structure */
286a752e82dSGleb Smirnoff #define	NG_NETFLOW_INFO_TYPE {					\
2875dcd9c10SGleb Smirnoff 	{ "IPv4 bytes",			&ng_parse_uint64_type },\
2885dcd9c10SGleb Smirnoff 	{ "IPv4 packets",		&ng_parse_uint32_type },\
2895dcd9c10SGleb Smirnoff 	{ "IPv6 bytes",			&ng_parse_uint64_type },\
2905dcd9c10SGleb Smirnoff 	{ "IPv6 packets",		&ng_parse_uint32_type },\
2915dcd9c10SGleb Smirnoff 	{ "IPv4 skipped bytes",		&ng_parse_uint64_type },\
2925dcd9c10SGleb Smirnoff 	{ "IPv4 skipped packets",	&ng_parse_uint32_type },\
2935dcd9c10SGleb Smirnoff 	{ "IPv6 skipped bytes",		&ng_parse_uint64_type },\
2945dcd9c10SGleb Smirnoff 	{ "IPv6 skipped packets",	&ng_parse_uint32_type },\
2955dcd9c10SGleb Smirnoff 	{ "IPv4 records used",		&ng_parse_uint32_type },\
2965dcd9c10SGleb Smirnoff 	{ "IPv6 records used",		&ng_parse_uint32_type },\
297494e177aSGleb Smirnoff 	{ "Failed allocations",		&ng_parse_uint32_type },\
2985dcd9c10SGleb Smirnoff 	{ "V5 failed exports",		&ng_parse_uint32_type },\
2995dcd9c10SGleb Smirnoff 	{ "V9 failed exports",		&ng_parse_uint32_type },\
3005dcd9c10SGleb Smirnoff 	{ "mbuf reallocations",		&ng_parse_uint32_type },\
3015dcd9c10SGleb Smirnoff 	{ "fibs allocated",		&ng_parse_uint32_type },\
302494e177aSGleb Smirnoff 	{ "Active expiries",		&ng_parse_uint32_type },\
303494e177aSGleb Smirnoff 	{ "Inactive expiries",		&ng_parse_uint32_type },\
304a752e82dSGleb Smirnoff 	{ "Inactive timeout",		&ng_parse_uint32_type },\
305a752e82dSGleb Smirnoff 	{ "Active timeout",		&ng_parse_uint32_type },\
306a752e82dSGleb Smirnoff 	{ NULL }						\
307a752e82dSGleb Smirnoff }
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 */
411a752e82dSGleb Smirnoff 	struct ng_netflow_info  info;
412494e177aSGleb Smirnoff 	struct callout	exp_callout;	/* expiry periodic job */
413a752e82dSGleb Smirnoff 
414494e177aSGleb Smirnoff 	/*
415494e177aSGleb Smirnoff 	 * Flow entries are allocated in uma(9) zone zone. They are
416494e177aSGleb Smirnoff 	 * indexed by hash hash. Each hash element consist of tailqueue
417494e177aSGleb Smirnoff 	 * head and mutex to protect this element.
418494e177aSGleb Smirnoff 	 */
41951856405SGleb Smirnoff #define	CACHESIZE			(65536*16)
420a752e82dSGleb Smirnoff #define	CACHELOWAT			(CACHESIZE * 3/4)
421a752e82dSGleb Smirnoff #define	CACHEHIGHWAT			(CACHESIZE * 9/10)
422494e177aSGleb Smirnoff 	uma_zone_t		zone;
423a752e82dSGleb Smirnoff 	struct flow_hash_entry	*hash;
424a752e82dSGleb Smirnoff 
425494e177aSGleb Smirnoff 	/*
426494e177aSGleb Smirnoff 	 * NetFlow data export
427494e177aSGleb Smirnoff 	 *
428494e177aSGleb Smirnoff 	 * export_item is a data item, it has an mbuf with cluster
429494e177aSGleb Smirnoff 	 * attached to it. A thread detaches export_item from priv
430494e177aSGleb Smirnoff 	 * and works with it. If the export is full it is sent, and
431494e177aSGleb Smirnoff 	 * a new one is allocated. Before exiting thread re-attaches
432494e177aSGleb Smirnoff 	 * its current item back to priv. If there is item already,
433494e177aSGleb Smirnoff 	 * current incomplete datagram is sent.
434494e177aSGleb Smirnoff 	 * export_mtx is used for attaching/detaching.
435494e177aSGleb Smirnoff 	 */
4365dcd9c10SGleb Smirnoff 
4375dcd9c10SGleb Smirnoff 	/* IPv6 support */
4385dcd9c10SGleb Smirnoff #ifdef INET6
4395dcd9c10SGleb Smirnoff 	uma_zone_t		zone6;
440ea7e1638SGleb Smirnoff 	struct flow_hash_entry	*hash6;
4415dcd9c10SGleb Smirnoff #endif
44271336d8cSGleb Smirnoff 
4435dcd9c10SGleb Smirnoff 	/* Multiple FIB support */
44471336d8cSGleb Smirnoff 	fib_export_p	*fib_data;	/* vector to per-fib data */
44514797255SAlexander V. Chernikov 	uint16_t	maxfibs;	/* number of allocated fibs */
4465dcd9c10SGleb Smirnoff 
44710fcb07cSAlexander V. Chernikov 	/* Netflow v9 configuration options */
4485dcd9c10SGleb Smirnoff 	/*
4495dcd9c10SGleb Smirnoff 	 * RFC 3954 clause 7.3
4505dcd9c10SGleb Smirnoff 	 * "Both options MUST be configurable by the user on the Exporter."
4515dcd9c10SGleb Smirnoff 	 */
4525dcd9c10SGleb Smirnoff 	uint16_t	templ_time;	/* time between sending templates */
4535dcd9c10SGleb Smirnoff 	uint16_t	templ_packets;	/* packets between sending templates */
4545dcd9c10SGleb Smirnoff #define NETFLOW_V9_MAX_FLOWSETS	2
4555dcd9c10SGleb Smirnoff 	u_char		flowsets_count; /* current flowsets used */
45671336d8cSGleb Smirnoff 
45771336d8cSGleb Smirnoff 	/* Count of records in each flowset */
45871336d8cSGleb Smirnoff 	u_char		flowset_records[NETFLOW_V9_MAX_FLOWSETS - 1];
4595dcd9c10SGleb Smirnoff 	uint16_t	mtu;		/* export interface MTU */
46071336d8cSGleb Smirnoff 
46171336d8cSGleb Smirnoff 	/* Pointers to pre-compiled flowsets */
46271336d8cSGleb Smirnoff 	struct netflow_v9_flowset_header
46371336d8cSGleb Smirnoff 	    *v9_flowsets[NETFLOW_V9_MAX_FLOWSETS - 1];
4643bbbf02dSGleb Smirnoff 
4653bbbf02dSGleb Smirnoff 	struct ng_netflow_iface	ifaces[NG_NETFLOW_MAXIFACES];
466a752e82dSGleb Smirnoff };
467a752e82dSGleb Smirnoff 
468a752e82dSGleb Smirnoff typedef struct netflow *priv_p;
469a752e82dSGleb Smirnoff 
470a752e82dSGleb Smirnoff /* Header of a small list in hash cell */
471a752e82dSGleb Smirnoff struct flow_hash_entry {
472494e177aSGleb Smirnoff 	struct mtx		mtx;
473494e177aSGleb Smirnoff 	TAILQ_HEAD(fhead, flow_entry) head;
474a752e82dSGleb Smirnoff };
475a752e82dSGleb Smirnoff 
476a752e82dSGleb Smirnoff #define	ERROUT(x)	{ error = (x); goto done; }
477a752e82dSGleb Smirnoff 
4781a6dd095SAlexander Motin #define MTAG_NETFLOW		1221656444
4791a6dd095SAlexander Motin #define MTAG_NETFLOW_CALLED	0
4801a6dd095SAlexander Motin 
4815dcd9c10SGleb Smirnoff #define m_pktlen(m)	((m)->m_pkthdr.len)
4825dcd9c10SGleb Smirnoff #define IP6VERSION	6
4835dcd9c10SGleb Smirnoff 
4845dcd9c10SGleb Smirnoff #define priv_to_fib(priv, fib)	(priv)->fib_data[(fib)]
4855dcd9c10SGleb Smirnoff 
4865dcd9c10SGleb Smirnoff /*
4875dcd9c10SGleb Smirnoff  * Cisco uses milliseconds for uptime. Bad idea, since it overflows
4885dcd9c10SGleb Smirnoff  * every 48+ days. But we will do same to keep compatibility. This macro
4895dcd9c10SGleb Smirnoff  * does overflowable multiplication to 1000.
4905dcd9c10SGleb Smirnoff  */
4915dcd9c10SGleb Smirnoff #define	MILLIUPTIME(t)	(((t) << 9) +	/* 512 */	\
4925dcd9c10SGleb Smirnoff 			 ((t) << 8) +	/* 256 */	\
4935dcd9c10SGleb Smirnoff 			 ((t) << 7) +	/* 128 */	\
4945dcd9c10SGleb Smirnoff 			 ((t) << 6) +	/* 64  */	\
4955dcd9c10SGleb Smirnoff 			 ((t) << 5) +	/* 32  */	\
4965dcd9c10SGleb Smirnoff 			 ((t) << 3))	/* 8   */
4975dcd9c10SGleb Smirnoff 
498a752e82dSGleb Smirnoff /* Prototypes for netflow.c */
499b6770143SGleb Smirnoff void	ng_netflow_cache_init(priv_p);
500a752e82dSGleb Smirnoff void	ng_netflow_cache_flush(priv_p);
5015dcd9c10SGleb Smirnoff int	ng_netflow_fib_init(priv_p priv, int fib);
502a752e82dSGleb Smirnoff void	ng_netflow_copyinfo(priv_p, struct ng_netflow_info *);
50310fcb07cSAlexander V. Chernikov void	ng_netflow_copyv9info(priv_p, struct ng_netflow_v9info *);
504a752e82dSGleb Smirnoff timeout_t ng_netflow_expire;
50571336d8cSGleb Smirnoff int 	ng_netflow_flow_add(priv_p, fib_export_p, struct ip *, caddr_t,
50671336d8cSGleb Smirnoff 	uint8_t, uint8_t, unsigned int);
50771336d8cSGleb Smirnoff int	ng_netflow_flow6_add(priv_p, fib_export_p, struct ip6_hdr *, caddr_t,
50871336d8cSGleb Smirnoff 	uint8_t, uint8_t, unsigned int);
50971336d8cSGleb Smirnoff int	ng_netflow_flow_show(priv_p, struct ngnf_show_header *req,
51071336d8cSGleb Smirnoff 	struct ngnf_show_header *resp);
5115dcd9c10SGleb Smirnoff void	ng_netflow_v9_cache_init(priv_p);
5125dcd9c10SGleb Smirnoff void	ng_netflow_v9_cache_flush(priv_p);
51371336d8cSGleb Smirnoff item_p	get_export9_dgram(priv_p, fib_export_p,
51471336d8cSGleb Smirnoff 	struct netflow_v9_packet_opt **);
5155dcd9c10SGleb Smirnoff void	return_export9_dgram(priv_p, fib_export_p, item_p,
5165dcd9c10SGleb Smirnoff 	struct netflow_v9_packet_opt *, int);
51771336d8cSGleb Smirnoff int	export9_add(item_p, struct netflow_v9_packet_opt *,
51871336d8cSGleb Smirnoff 	struct flow_entry *);
51971336d8cSGleb Smirnoff int	export9_send(priv_p, fib_export_p, item_p,
52071336d8cSGleb Smirnoff 	struct netflow_v9_packet_opt *, int);
5215dcd9c10SGleb Smirnoff 
522a752e82dSGleb Smirnoff #endif	/* _KERNEL */
523a752e82dSGleb Smirnoff #endif	/* _NG_NETFLOW_H_ */
524