xref: /freebsd/sys/netgraph/netflow/ng_netflow.h (revision 5dcd9c10)
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"
365dcd9c10SGleb Smirnoff #define NGM_NETFLOW_COOKIE	1299079728
37a752e82dSGleb Smirnoff 
38dc7359b8SGleb Smirnoff #define	NG_NETFLOW_MAXIFACES	USHRT_MAX
39a752e82dSGleb Smirnoff 
40a752e82dSGleb Smirnoff /* Hook names */
41a752e82dSGleb Smirnoff 
42a752e82dSGleb Smirnoff #define	NG_NETFLOW_HOOK_DATA	"iface"
439818b82fSGleb Smirnoff #define	NG_NETFLOW_HOOK_OUT	"out"
44a752e82dSGleb Smirnoff #define NG_NETFLOW_HOOK_EXPORT	"export"
455dcd9c10SGleb Smirnoff #define NG_NETFLOW_HOOK_EXPORT9	"export9"
465dcd9c10SGleb Smirnoff 
475dcd9c10SGleb Smirnoff /* This define effectively disable (v5) netflow export hook! */
485dcd9c10SGleb Smirnoff /* #define COUNTERS_64 */
49a752e82dSGleb Smirnoff 
50a752e82dSGleb Smirnoff /* Netgraph commands understood by netflow node */
51a752e82dSGleb Smirnoff enum {
523d9dddcdSGleb Smirnoff     NGM_NETFLOW_INFO = 1|NGM_READONLY|NGM_HASREPLY,	/* get node info */
533d9dddcdSGleb Smirnoff     NGM_NETFLOW_IFINFO = 2|NGM_READONLY|NGM_HASREPLY,	/* get iface info */
543d9dddcdSGleb Smirnoff     NGM_NETFLOW_SHOW = 3|NGM_READONLY|NGM_HASREPLY,	/* show ip cache flow */
553d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETDLT		= 4,	/* set data-link type */
563d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETIFINDEX	= 5, 	/* set interface index */
573d9dddcdSGleb Smirnoff     NGM_NETFLOW_SETTIMEOUTS	= 6, 	/* set active/inactive flow timeouts */
581a6dd095SAlexander Motin     NGM_NETFLOW_SETCONFIG	= 7, 	/* set flow generation options */
595dcd9c10SGleb Smirnoff     NGM_NETFLOW_SETTEMPLATE	= 8, 	/* set v9 flow template periodic */
605dcd9c10SGleb Smirnoff     NGM_NETFLOW_SETMTU		= 9, 	/* set outgoing interface MTU */
61a752e82dSGleb Smirnoff };
62a752e82dSGleb Smirnoff 
63a752e82dSGleb Smirnoff /* This structure is returned by the NGM_NETFLOW_INFO message */
64a752e82dSGleb Smirnoff struct ng_netflow_info {
655dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_bytes;		/* accounted IPv4 bytes */
665dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_packets;		/* accounted IPv4 packets */
675dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_bytes6;		/* accounted IPv6 bytes */
685dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_packets6;	/* accounted IPv6 packets */
695dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_sbytes;		/* skipped IPv4 bytes */
705dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_spackets;	/* skipped IPv4 packets */
715dcd9c10SGleb Smirnoff 	uint64_t	nfinfo_sbytes6;		/* skipped IPv6 bytes */
725dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_spackets6;	/* skipped IPv6 packets */
7335fd572dSGleb Smirnoff 	uint32_t	nfinfo_used;		/* used cache records */
745dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_used6;		/* used IPv6 cache records */
7535fd572dSGleb Smirnoff 	uint32_t	nfinfo_alloc_failed;	/* failed allocations */
7635fd572dSGleb Smirnoff 	uint32_t	nfinfo_export_failed;	/* failed exports */
775dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_export9_failed;	/* failed exports */
785dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_realloc_mbuf;	/* reallocated mbufs */
795dcd9c10SGleb Smirnoff 	uint32_t	nfinfo_alloc_fibs;	/* fibs allocated */
8035fd572dSGleb Smirnoff 	uint32_t	nfinfo_act_exp;		/* active expiries */
8135fd572dSGleb Smirnoff 	uint32_t	nfinfo_inact_exp;	/* inactive expiries */
82a752e82dSGleb Smirnoff 	uint32_t	nfinfo_inact_t;		/* flow inactive timeout */
83a752e82dSGleb Smirnoff 	uint32_t	nfinfo_act_t;		/* flow active timeout */
84a752e82dSGleb Smirnoff };
85a752e82dSGleb Smirnoff 
86a752e82dSGleb Smirnoff /* This structure is returned by the NGM_NETFLOW_IFINFO message */
87a752e82dSGleb Smirnoff struct ng_netflow_ifinfo {
88a752e82dSGleb Smirnoff 	uint32_t	ifinfo_packets;	/* number of packets for this iface */
89a752e82dSGleb Smirnoff 	uint8_t		ifinfo_dlt;	/* Data Link Type, DLT_XXX */
90a752e82dSGleb Smirnoff #define	MAXDLTNAMELEN	20
91a752e82dSGleb Smirnoff 	u_int16_t	ifinfo_index;	/* connected iface index */
921a6dd095SAlexander Motin 	uint32_t	conf;
93a752e82dSGleb Smirnoff };
94a752e82dSGleb Smirnoff 
95a752e82dSGleb Smirnoff 
96a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETDLT message */
97a752e82dSGleb Smirnoff struct ng_netflow_setdlt {
98a752e82dSGleb Smirnoff 	uint16_t iface;		/* which iface dlt change */
99a752e82dSGleb Smirnoff 	uint8_t  dlt;			/* DLT_XXX from bpf.h */
100a752e82dSGleb Smirnoff };
101a752e82dSGleb Smirnoff 
102a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETIFINDEX */
103a752e82dSGleb Smirnoff struct ng_netflow_setifindex {
104a752e82dSGleb Smirnoff 	u_int16_t iface;		/* which iface index change */
105a752e82dSGleb Smirnoff 	u_int16_t index;		/* new index */
106a752e82dSGleb Smirnoff };
107a752e82dSGleb Smirnoff 
108a752e82dSGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETTIMEOUTS */
109a752e82dSGleb Smirnoff struct ng_netflow_settimeouts {
110a752e82dSGleb Smirnoff 	uint32_t	inactive_timeout;	/* flow inactive timeout */
111a752e82dSGleb Smirnoff 	uint32_t	active_timeout;		/* flow active timeout */
112a752e82dSGleb Smirnoff };
113a752e82dSGleb Smirnoff 
1141a6dd095SAlexander Motin #define NG_NETFLOW_CONF_INGRESS		1
1151a6dd095SAlexander Motin #define NG_NETFLOW_CONF_EGRESS		2
1161a6dd095SAlexander Motin #define NG_NETFLOW_CONF_ONCE		4
1171a6dd095SAlexander Motin #define NG_NETFLOW_CONF_THISONCE	8
1181a6dd095SAlexander Motin 
1191a6dd095SAlexander Motin /* This structure is passed to NGM_NETFLOW_SETCONFIG */
1201a6dd095SAlexander Motin struct ng_netflow_setconfig {
1211a6dd095SAlexander Motin 	u_int16_t iface;		/* which iface config change */
1221a6dd095SAlexander Motin 	u_int32_t conf;			/* new config */
1231a6dd095SAlexander Motin };
1241a6dd095SAlexander Motin 
1255dcd9c10SGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETTEMPLATE */
1265dcd9c10SGleb Smirnoff struct ng_netflow_settemplate {
1275dcd9c10SGleb Smirnoff 	uint16_t time;		/* max time between announce */
1285dcd9c10SGleb Smirnoff 	uint16_t packets;	/* max packets between announce */
1295dcd9c10SGleb Smirnoff };
1305dcd9c10SGleb Smirnoff 
1315dcd9c10SGleb Smirnoff /* This structure is passed to NGM_NETFLOW_SETMTU */
1325dcd9c10SGleb Smirnoff struct ng_netflow_setmtu {
1335dcd9c10SGleb Smirnoff 	uint16_t mtu;		/* MTU for packet */
1345dcd9c10SGleb Smirnoff };
1355dcd9c10SGleb Smirnoff 
1365dcd9c10SGleb Smirnoff 
137a752e82dSGleb Smirnoff /* This is unique data, which identifies flow */
138a752e82dSGleb Smirnoff struct flow_rec {
1395dcd9c10SGleb Smirnoff 	uint16_t	flow_type; /* IPv4 L4/L3 flow, see NETFLOW_V9_FLOW* */
1405dcd9c10SGleb Smirnoff 	uint16_t	fib;
141a752e82dSGleb Smirnoff 	struct in_addr	r_src;
142a752e82dSGleb Smirnoff 	struct in_addr	r_dst;
143a752e82dSGleb Smirnoff 	union {
144a752e82dSGleb Smirnoff 		struct {
145a752e82dSGleb Smirnoff 			uint16_t	s_port;	/* source TCP/UDP port */
146a752e82dSGleb Smirnoff 			uint16_t	d_port; /* destination TCP/UDP port */
147a752e82dSGleb Smirnoff 		} dir;
148a752e82dSGleb Smirnoff 		uint32_t both;
149a752e82dSGleb Smirnoff 	} ports;
150a752e82dSGleb Smirnoff 	union {
151a752e82dSGleb Smirnoff 		struct {
152a752e82dSGleb Smirnoff 			u_char		prot;	/* IP protocol */
153a752e82dSGleb Smirnoff 			u_char		tos;	/* IP TOS */
154a752e82dSGleb Smirnoff 			uint16_t	i_ifx;	/* input interface index */
155a752e82dSGleb Smirnoff 		} i;
156a752e82dSGleb Smirnoff 		uint32_t all;
157a752e82dSGleb Smirnoff 	} misc;
158a752e82dSGleb Smirnoff };
159a752e82dSGleb Smirnoff 
1605dcd9c10SGleb Smirnoff /* This is unique data, which identifies flow */
1615dcd9c10SGleb Smirnoff struct flow6_rec {
1625dcd9c10SGleb Smirnoff 	uint16_t	flow_type; /* IPv4 L4/L3 Ipv6 L4/L3 flow, see NETFLOW_V9_FLOW* */
1635dcd9c10SGleb Smirnoff 	uint16_t	fib;
1645dcd9c10SGleb Smirnoff 	union {
1655dcd9c10SGleb Smirnoff 		struct in_addr	r_src;
1665dcd9c10SGleb Smirnoff 		struct in6_addr	r_src6;
1675dcd9c10SGleb Smirnoff 	} src;
1685dcd9c10SGleb Smirnoff 	union {
1695dcd9c10SGleb Smirnoff 		struct in_addr	r_dst;
1705dcd9c10SGleb Smirnoff 		struct in6_addr	r_dst6;
1715dcd9c10SGleb Smirnoff 	} dst;
1725dcd9c10SGleb Smirnoff 	union {
1735dcd9c10SGleb Smirnoff 		struct {
1745dcd9c10SGleb Smirnoff 			uint16_t	s_port;	/* source TCP/UDP port */
1755dcd9c10SGleb Smirnoff 			uint16_t	d_port; /* destination TCP/UDP port */
1765dcd9c10SGleb Smirnoff 		} dir;
1775dcd9c10SGleb Smirnoff 		uint32_t both;
1785dcd9c10SGleb Smirnoff 	} ports;
1795dcd9c10SGleb Smirnoff 	union {
1805dcd9c10SGleb Smirnoff 		struct {
1815dcd9c10SGleb Smirnoff 			u_char		prot;	/* IP protocol */
1825dcd9c10SGleb Smirnoff 			u_char		tos;	/* IP TOS */
1835dcd9c10SGleb Smirnoff 			uint16_t	i_ifx;	/* input interface index */
1845dcd9c10SGleb Smirnoff 		} i;
1855dcd9c10SGleb Smirnoff 		uint32_t all;
1865dcd9c10SGleb Smirnoff 	} misc;
1875dcd9c10SGleb Smirnoff };
1885dcd9c10SGleb Smirnoff 
189a752e82dSGleb Smirnoff #define	r_ip_p	misc.i.prot
190a752e82dSGleb Smirnoff #define	r_tos	misc.i.tos
191a752e82dSGleb Smirnoff #define	r_i_ifx	misc.i.i_ifx
192a752e82dSGleb Smirnoff #define r_misc	misc.all
193a752e82dSGleb Smirnoff #define r_ports	ports.both
194a752e82dSGleb Smirnoff #define r_sport	ports.dir.s_port
195a752e82dSGleb Smirnoff #define r_dport	ports.dir.d_port
196a752e82dSGleb Smirnoff 
197a752e82dSGleb Smirnoff /* A flow entry which accumulates statistics */
198a752e82dSGleb Smirnoff struct flow_entry_data {
1995dcd9c10SGleb Smirnoff 	uint16_t		version;	/* Protocol version */
200a752e82dSGleb Smirnoff 	struct flow_rec		r;
201a752e82dSGleb Smirnoff 	struct in_addr		next_hop;
202a752e82dSGleb Smirnoff 	uint16_t		fle_o_ifx;	/* output interface index */
203a752e82dSGleb Smirnoff #define				fle_i_ifx	r.misc.i.i_ifx
204a752e82dSGleb Smirnoff 	uint8_t		dst_mask;	/* destination route mask bits */
205a752e82dSGleb Smirnoff 	uint8_t		src_mask;	/* source route mask bits */
206a752e82dSGleb Smirnoff 	u_long			packets;
207a752e82dSGleb Smirnoff 	u_long			bytes;
208a752e82dSGleb Smirnoff 	long			first;	/* uptime on first packet */
209a752e82dSGleb Smirnoff 	long			last;	/* uptime on last packet */
210a752e82dSGleb Smirnoff 	u_char			tcp_flags;	/* cumulative OR */
211a752e82dSGleb Smirnoff };
212a752e82dSGleb Smirnoff 
2135dcd9c10SGleb Smirnoff struct flow6_entry_data {
2145dcd9c10SGleb Smirnoff 	uint16_t		version;	/* Protocol version */
2155dcd9c10SGleb Smirnoff 	struct flow6_rec	r;
2165dcd9c10SGleb Smirnoff 	union {
2175dcd9c10SGleb Smirnoff 		struct in_addr		next_hop;
2185dcd9c10SGleb Smirnoff 		struct in6_addr		next_hop6;
2195dcd9c10SGleb Smirnoff 	} n;
2205dcd9c10SGleb Smirnoff 	uint16_t		fle_o_ifx;	/* output interface index */
2215dcd9c10SGleb Smirnoff #define				fle_i_ifx	r.misc.i.i_ifx
2225dcd9c10SGleb Smirnoff 	uint8_t		dst_mask;	/* destination route mask bits */
2235dcd9c10SGleb Smirnoff 	uint8_t		src_mask;	/* source route mask bits */
2245dcd9c10SGleb Smirnoff 	u_long			packets;
2255dcd9c10SGleb Smirnoff 	u_long			bytes;
2265dcd9c10SGleb Smirnoff 	long			first;	/* uptime on first packet */
2275dcd9c10SGleb Smirnoff 	long			last;	/* uptime on last packet */
2285dcd9c10SGleb Smirnoff 	u_char			tcp_flags;	/* cumulative OR */
2295dcd9c10SGleb Smirnoff };
2305dcd9c10SGleb Smirnoff 
231a752e82dSGleb Smirnoff /*
232a752e82dSGleb Smirnoff  * How many flow records we will transfer at once
233a752e82dSGleb Smirnoff  * without overflowing socket receive buffer
234a752e82dSGleb Smirnoff  */
235a752e82dSGleb Smirnoff #define NREC_AT_ONCE		1000
236a752e82dSGleb Smirnoff #define NGRESP_SIZE		(sizeof(struct ngnf_flows) + (NREC_AT_ONCE * \
237a752e82dSGleb Smirnoff 				sizeof(struct flow_entry_data)))
238a752e82dSGleb Smirnoff #define SORCVBUF_SIZE		(NGRESP_SIZE + 2 * sizeof(struct ng_mesg))
239a752e82dSGleb Smirnoff 
240a752e82dSGleb Smirnoff /* This struct is returned to userland, when "show cache ip flow" */
241a752e82dSGleb Smirnoff struct ngnf_flows {
242a752e82dSGleb Smirnoff 	uint32_t		nentries;
243a752e82dSGleb Smirnoff 	uint32_t		last;
244a752e82dSGleb Smirnoff 	struct flow_entry_data	entries[0];
245a752e82dSGleb Smirnoff };
246a752e82dSGleb Smirnoff 
247a752e82dSGleb Smirnoff /* Everything below is for kernel */
248a752e82dSGleb Smirnoff 
249a752e82dSGleb Smirnoff #ifdef _KERNEL
250a752e82dSGleb Smirnoff 
251a752e82dSGleb Smirnoff struct flow_entry {
252a752e82dSGleb Smirnoff 	struct flow_entry_data	f;
253494e177aSGleb Smirnoff 	TAILQ_ENTRY(flow_entry)	fle_hash;	/* entries in hash slot */
254a752e82dSGleb Smirnoff };
255a752e82dSGleb Smirnoff 
2565dcd9c10SGleb Smirnoff struct flow6_entry {
2575dcd9c10SGleb Smirnoff 	struct flow6_entry_data	f;
2585dcd9c10SGleb Smirnoff 	TAILQ_ENTRY(flow6_entry)	fle6_hash;	/* entries in hash slot */
2595dcd9c10SGleb Smirnoff };
260a752e82dSGleb Smirnoff /* Parsing declarations */
261a752e82dSGleb Smirnoff 
262a752e82dSGleb Smirnoff /* Parse the info structure */
263a752e82dSGleb Smirnoff #define	NG_NETFLOW_INFO_TYPE	{			\
2645dcd9c10SGleb Smirnoff 	{ "IPv4 bytes",		&ng_parse_uint64_type },	\
2655dcd9c10SGleb Smirnoff 	{ "IPv4 packets",	&ng_parse_uint32_type },	\
2665dcd9c10SGleb Smirnoff 	{ "IPv6 bytes",		&ng_parse_uint64_type },	\
2675dcd9c10SGleb Smirnoff 	{ "IPv6 packets",	&ng_parse_uint32_type },	\
2685dcd9c10SGleb Smirnoff 	{ "IPv4 skipped bytes",		&ng_parse_uint64_type },	\
2695dcd9c10SGleb Smirnoff 	{ "IPv4 skipped packets",	&ng_parse_uint32_type },	\
2705dcd9c10SGleb Smirnoff 	{ "IPv6 skipped bytes",		&ng_parse_uint64_type },	\
2715dcd9c10SGleb Smirnoff 	{ "IPv6 skipped packets",	&ng_parse_uint32_type },	\
2725dcd9c10SGleb Smirnoff 	{ "IPv4 records used",	&ng_parse_uint32_type },\
2735dcd9c10SGleb Smirnoff 	{ "IPv6 records used",	&ng_parse_uint32_type },\
274494e177aSGleb Smirnoff 	{ "Failed allocations",	&ng_parse_uint32_type },\
2755dcd9c10SGleb Smirnoff 	{ "V5 failed exports",	&ng_parse_uint32_type },\
2765dcd9c10SGleb Smirnoff 	{ "V9 failed exports",	&ng_parse_uint32_type },\
2775dcd9c10SGleb Smirnoff 	{ "mbuf reallocations",	&ng_parse_uint32_type },\
2785dcd9c10SGleb Smirnoff 	{ "fibs allocated",	&ng_parse_uint32_type },\
279494e177aSGleb Smirnoff 	{ "Active expiries",	&ng_parse_uint32_type },\
280494e177aSGleb Smirnoff 	{ "Inactive expiries",	&ng_parse_uint32_type },\
281a752e82dSGleb Smirnoff 	{ "Inactive timeout",	&ng_parse_uint32_type },\
282a752e82dSGleb Smirnoff 	{ "Active timeout",	&ng_parse_uint32_type },\
283a752e82dSGleb Smirnoff 	{ NULL }					\
284a752e82dSGleb Smirnoff }
285a752e82dSGleb Smirnoff 
286a752e82dSGleb Smirnoff /* Parse the ifinfo structure */
287a752e82dSGleb Smirnoff #define NG_NETFLOW_IFINFO_TYPE	{			\
288a752e82dSGleb Smirnoff 	{ "packets",	&ng_parse_uint32_type },	\
289a752e82dSGleb Smirnoff 	{ "data link type", &ng_parse_uint8_type },	\
290a752e82dSGleb Smirnoff 	{ "index", &ng_parse_uint16_type },		\
2911a6dd095SAlexander Motin 	{ "conf", &ng_parse_uint32_type },		\
292a752e82dSGleb Smirnoff 	{ NULL }					\
293a752e82dSGleb Smirnoff }
294a752e82dSGleb Smirnoff 
295a752e82dSGleb Smirnoff /* Parse the setdlt structure */
296a752e82dSGleb Smirnoff #define	NG_NETFLOW_SETDLT_TYPE {			\
297a752e82dSGleb Smirnoff 	{ "iface",	&ng_parse_uint16_type },	\
298a752e82dSGleb Smirnoff 	{ "dlt",	&ng_parse_uint8_type  },	\
299a752e82dSGleb Smirnoff 	{ NULL }					\
300a752e82dSGleb Smirnoff }
301a752e82dSGleb Smirnoff 
302a752e82dSGleb Smirnoff /* Parse the setifindex structure */
303a752e82dSGleb Smirnoff #define	NG_NETFLOW_SETIFINDEX_TYPE {			\
304a752e82dSGleb Smirnoff 	{ "iface",	&ng_parse_uint16_type },	\
305a752e82dSGleb Smirnoff 	{ "index",	&ng_parse_uint16_type },	\
306a752e82dSGleb Smirnoff 	{ NULL }					\
307a752e82dSGleb Smirnoff }
308a752e82dSGleb Smirnoff 
309a752e82dSGleb Smirnoff /* Parse the settimeouts structure */
310a752e82dSGleb Smirnoff #define NG_NETFLOW_SETTIMEOUTS_TYPE {			\
311a752e82dSGleb Smirnoff 	{ "inactive",	&ng_parse_uint32_type },	\
312a752e82dSGleb Smirnoff 	{ "active",	&ng_parse_uint32_type },	\
313a752e82dSGleb Smirnoff 	{ NULL }					\
314a752e82dSGleb Smirnoff }
315a752e82dSGleb Smirnoff 
3161a6dd095SAlexander Motin /* Parse the setifindex structure */
3171a6dd095SAlexander Motin #define	NG_NETFLOW_SETCONFIG_TYPE {			\
3181a6dd095SAlexander Motin 	{ "iface",	&ng_parse_uint16_type },	\
3191a6dd095SAlexander Motin 	{ "conf",	&ng_parse_uint32_type },	\
3201a6dd095SAlexander Motin 	{ NULL }					\
3211a6dd095SAlexander Motin }
3221a6dd095SAlexander Motin 
3235dcd9c10SGleb Smirnoff /* Parse the settemplate structure */
3245dcd9c10SGleb Smirnoff #define	NG_NETFLOW_SETTEMPLATE_TYPE {		\
3255dcd9c10SGleb Smirnoff 	{ "time",	&ng_parse_uint16_type },	\
3265dcd9c10SGleb Smirnoff 	{ "packets",	&ng_parse_uint16_type },	\
3275dcd9c10SGleb Smirnoff 	{ NULL }					\
3285dcd9c10SGleb Smirnoff }
3295dcd9c10SGleb Smirnoff 
3305dcd9c10SGleb Smirnoff /* Parse the setmtu structure */
3315dcd9c10SGleb Smirnoff #define	NG_NETFLOW_SETMTU_TYPE {			\
3325dcd9c10SGleb Smirnoff 	{ "mtu",	&ng_parse_uint16_type },	\
3335dcd9c10SGleb Smirnoff 	{ NULL }					\
3345dcd9c10SGleb Smirnoff }
3355dcd9c10SGleb Smirnoff 
336a752e82dSGleb Smirnoff /* Private hook data */
337a752e82dSGleb Smirnoff struct ng_netflow_iface {
338a752e82dSGleb Smirnoff 	hook_p		hook;		/* NULL when disconnected */
3399818b82fSGleb Smirnoff 	hook_p		out;		/* NULL when no bypass hook */
340a752e82dSGleb Smirnoff 	struct ng_netflow_ifinfo	info;
341a752e82dSGleb Smirnoff };
342a752e82dSGleb Smirnoff 
343a752e82dSGleb Smirnoff typedef struct ng_netflow_iface *iface_p;
344a752e82dSGleb Smirnoff typedef struct ng_netflow_ifinfo *ifinfo_p;
345a752e82dSGleb Smirnoff 
3465dcd9c10SGleb Smirnoff struct netflow_export_item {
3475dcd9c10SGleb Smirnoff 	item_p		item;
3485dcd9c10SGleb Smirnoff 	item_p		item9;
3495dcd9c10SGleb Smirnoff 	struct netflow_v9_packet_opt	*item9_opt;
3505dcd9c10SGleb Smirnoff };
3515dcd9c10SGleb Smirnoff 
3525dcd9c10SGleb Smirnoff /* Structure contatining fib-specific data */
3535dcd9c10SGleb Smirnoff struct fib_export {
3545dcd9c10SGleb Smirnoff 	uint32_t			fib;		/* kernel fib id */
3555dcd9c10SGleb Smirnoff 	struct netflow_export_item	exp;		/* Various data used for export */
3565dcd9c10SGleb Smirnoff 	struct mtx			export_mtx;	/* exp.item mutex */
3575dcd9c10SGleb Smirnoff 	struct mtx			export9_mtx;	/* exp.item9 mutex */
3585dcd9c10SGleb Smirnoff 	uint32_t			flow_seq;	/* current V5 flow sequence */
3595dcd9c10SGleb Smirnoff 	uint32_t			flow9_seq;	/* current V9 flow sequence */
3605dcd9c10SGleb Smirnoff 	uint32_t			domain_id;	/* Observartion domain id */
3615dcd9c10SGleb Smirnoff 	/* Netflow V9 counters */
3625dcd9c10SGleb Smirnoff 	uint32_t			templ_last_ts;	/* unixtime of last template announce */
3635dcd9c10SGleb Smirnoff 	uint32_t			templ_last_pkt;	/* packets count on last template announce */
3645dcd9c10SGleb Smirnoff 	uint32_t			sent_packets;	/* packets sent by exporter; */
3655dcd9c10SGleb Smirnoff 	struct netflow_v9_packet_opt	*export9_opt;	/* current packet specific options */
3665dcd9c10SGleb Smirnoff };
3675dcd9c10SGleb Smirnoff 
3685dcd9c10SGleb Smirnoff typedef struct fib_export *fib_export_p;
3695dcd9c10SGleb Smirnoff 
370a752e82dSGleb Smirnoff /* Structure describing our flow engine */
371a752e82dSGleb Smirnoff struct netflow {
372a752e82dSGleb Smirnoff 	node_p			node;		/* link to the node itself */
373a752e82dSGleb Smirnoff 	hook_p			export;		/* export data goes there */
3745dcd9c10SGleb Smirnoff 	hook_p			export9;	/* Netflow V9 export data goes there */
375a752e82dSGleb Smirnoff 
376a752e82dSGleb Smirnoff 	struct ng_netflow_info	info;
377494e177aSGleb Smirnoff 	struct callout		exp_callout;	/* expiry periodic job */
378a752e82dSGleb Smirnoff 
379494e177aSGleb Smirnoff 	/*
380494e177aSGleb Smirnoff 	 * Flow entries are allocated in uma(9) zone zone. They are
381494e177aSGleb Smirnoff 	 * indexed by hash hash. Each hash element consist of tailqueue
382494e177aSGleb Smirnoff 	 * head and mutex to protect this element.
383494e177aSGleb Smirnoff 	 */
384494e177aSGleb Smirnoff #define	CACHESIZE			(65536*4)
385a752e82dSGleb Smirnoff #define	CACHELOWAT			(CACHESIZE * 3/4)
386a752e82dSGleb Smirnoff #define	CACHEHIGHWAT			(CACHESIZE * 9/10)
387494e177aSGleb Smirnoff 	uma_zone_t		zone;
388a752e82dSGleb Smirnoff 	struct flow_hash_entry	*hash;
389a752e82dSGleb Smirnoff 
390494e177aSGleb Smirnoff 	/*
391494e177aSGleb Smirnoff 	 * NetFlow data export
392494e177aSGleb Smirnoff 	 *
393494e177aSGleb Smirnoff 	 * export_item is a data item, it has an mbuf with cluster
394494e177aSGleb Smirnoff 	 * attached to it. A thread detaches export_item from priv
395494e177aSGleb Smirnoff 	 * and works with it. If the export is full it is sent, and
396494e177aSGleb Smirnoff 	 * a new one is allocated. Before exiting thread re-attaches
397494e177aSGleb Smirnoff 	 * its current item back to priv. If there is item already,
398494e177aSGleb Smirnoff 	 * current incomplete datagram is sent.
399494e177aSGleb Smirnoff 	 * export_mtx is used for attaching/detaching.
400494e177aSGleb Smirnoff 	 */
4015dcd9c10SGleb Smirnoff 
4025dcd9c10SGleb Smirnoff 	/* IPv6 support */
4035dcd9c10SGleb Smirnoff #ifdef INET6
4045dcd9c10SGleb Smirnoff 	uma_zone_t		zone6;
4055dcd9c10SGleb Smirnoff 	struct flow6_hash_entry	*hash6;
4065dcd9c10SGleb Smirnoff #endif
4075dcd9c10SGleb Smirnoff 	/* Multiple FIB support */
4085dcd9c10SGleb Smirnoff 	fib_export_p		fib_data[RT_NUMFIBS]; /* array of pointers to fib-specific data */
4095dcd9c10SGleb Smirnoff 
4105dcd9c10SGleb Smirnoff 	/*
4115dcd9c10SGleb Smirnoff 	 * RFC 3954 clause 7.3
4125dcd9c10SGleb Smirnoff 	 * "Both options MUST be configurable by the user on the Exporter."
4135dcd9c10SGleb Smirnoff 	 */
4145dcd9c10SGleb Smirnoff 	uint16_t		templ_time;	/* time between sending templates */
4155dcd9c10SGleb Smirnoff 	uint16_t		templ_packets;	/* packets between sending templates */
4165dcd9c10SGleb Smirnoff #define NETFLOW_V9_MAX_FLOWSETS	2
4175dcd9c10SGleb Smirnoff 	u_char			flowsets_count; /* current flowsets used */
4185dcd9c10SGleb Smirnoff 	u_char			flowset_records[NETFLOW_V9_MAX_FLOWSETS - 1]; /* Count of records in each flowset */
4195dcd9c10SGleb Smirnoff 	uint16_t		mtu;		/* export interface MTU */
4205dcd9c10SGleb Smirnoff 	struct netflow_v9_flowset_header	*v9_flowsets[NETFLOW_V9_MAX_FLOWSETS - 1]; /* Pointers to pre-compiled flowsets */
4213bbbf02dSGleb Smirnoff 
4223bbbf02dSGleb Smirnoff 	struct ng_netflow_iface	ifaces[NG_NETFLOW_MAXIFACES];
423a752e82dSGleb Smirnoff };
424a752e82dSGleb Smirnoff 
425a752e82dSGleb Smirnoff typedef struct netflow *priv_p;
426a752e82dSGleb Smirnoff 
427a752e82dSGleb Smirnoff /* Header of a small list in hash cell */
428a752e82dSGleb Smirnoff struct flow_hash_entry {
429494e177aSGleb Smirnoff 	struct mtx		mtx;
430494e177aSGleb Smirnoff 	TAILQ_HEAD(fhead, flow_entry) head;
431a752e82dSGleb Smirnoff };
432a752e82dSGleb Smirnoff 
4335dcd9c10SGleb Smirnoff struct flow6_hash_entry {
4345dcd9c10SGleb Smirnoff 	struct mtx		mtx;
4355dcd9c10SGleb Smirnoff 	TAILQ_HEAD(f6head, flow6_entry) head;
4365dcd9c10SGleb Smirnoff };
4375dcd9c10SGleb Smirnoff 
438a752e82dSGleb Smirnoff #define	ERROUT(x)	{ error = (x); goto done; }
439a752e82dSGleb Smirnoff 
4401a6dd095SAlexander Motin #define MTAG_NETFLOW		1221656444
4411a6dd095SAlexander Motin #define MTAG_NETFLOW_CALLED	0
4421a6dd095SAlexander Motin 
4435dcd9c10SGleb Smirnoff #define m_pktlen(m)	((m)->m_pkthdr.len)
4445dcd9c10SGleb Smirnoff #define IP6VERSION	6
4455dcd9c10SGleb Smirnoff 
4465dcd9c10SGleb Smirnoff #define priv_to_fib(priv, fib)	(priv)->fib_data[(fib)]
4475dcd9c10SGleb Smirnoff 
4485dcd9c10SGleb Smirnoff /*
4495dcd9c10SGleb Smirnoff  * Cisco uses milliseconds for uptime. Bad idea, since it overflows
4505dcd9c10SGleb Smirnoff  * every 48+ days. But we will do same to keep compatibility. This macro
4515dcd9c10SGleb Smirnoff  * does overflowable multiplication to 1000.
4525dcd9c10SGleb Smirnoff  */
4535dcd9c10SGleb Smirnoff #define	MILLIUPTIME(t)	(((t) << 9) +	/* 512 */	\
4545dcd9c10SGleb Smirnoff 			 ((t) << 8) +	/* 256 */	\
4555dcd9c10SGleb Smirnoff 			 ((t) << 7) +	/* 128 */	\
4565dcd9c10SGleb Smirnoff 			 ((t) << 6) +	/* 64  */	\
4575dcd9c10SGleb Smirnoff 			 ((t) << 5) +	/* 32  */	\
4585dcd9c10SGleb Smirnoff 			 ((t) << 3))	/* 8   */
4595dcd9c10SGleb Smirnoff 
460a752e82dSGleb Smirnoff /* Prototypes for netflow.c */
461a752e82dSGleb Smirnoff int	ng_netflow_cache_init(priv_p);
462a752e82dSGleb Smirnoff void	ng_netflow_cache_flush(priv_p);
4635dcd9c10SGleb Smirnoff int	ng_netflow_fib_init(priv_p priv, int fib);
464a752e82dSGleb Smirnoff void	ng_netflow_copyinfo(priv_p, struct ng_netflow_info *);
465a752e82dSGleb Smirnoff timeout_t ng_netflow_expire;
4665dcd9c10SGleb Smirnoff int 	ng_netflow_flow_add(priv_p, fib_export_p, struct ip *, caddr_t, uint8_t, uint8_t, unsigned int);
4675dcd9c10SGleb Smirnoff int	ng_netflow_flow6_add(priv_p, fib_export_p, struct ip6_hdr *, caddr_t , uint8_t, uint8_t, unsigned int);
468a752e82dSGleb Smirnoff int	ng_netflow_flow_show(priv_p, uint32_t last, struct ng_mesg *);
469a752e82dSGleb Smirnoff 
4705dcd9c10SGleb Smirnoff void	ng_netflow_v9_cache_init(priv_p);
4715dcd9c10SGleb Smirnoff void	ng_netflow_v9_cache_flush(priv_p);
4725dcd9c10SGleb Smirnoff item_p	get_export9_dgram(priv_p, fib_export_p, struct netflow_v9_packet_opt **);
4735dcd9c10SGleb Smirnoff void	return_export9_dgram(priv_p, fib_export_p, item_p,
4745dcd9c10SGleb Smirnoff 	    struct netflow_v9_packet_opt *, int);
4755dcd9c10SGleb Smirnoff int	export9_add(item_p, struct netflow_v9_packet_opt *, struct flow_entry *);
4765dcd9c10SGleb Smirnoff int	export9_send(priv_p, fib_export_p, item_p, struct netflow_v9_packet_opt *,
4775dcd9c10SGleb Smirnoff 	    int);
4785dcd9c10SGleb Smirnoff 
479a752e82dSGleb Smirnoff #endif	/* _KERNEL */
480a752e82dSGleb Smirnoff #endif	/* _NG_NETFLOW_H_ */
481