1*3b2bd0f6Slogwang /*-
2*3b2bd0f6Slogwang  * Copyright (c) 2010-2011 Alexander V. Chernikov <[email protected]>
3*3b2bd0f6Slogwang  * Copyright (c) 2004 Gleb Smirnoff <[email protected]>
4*3b2bd0f6Slogwang  * All rights reserved.
5*3b2bd0f6Slogwang  *
6*3b2bd0f6Slogwang  * Redistribution and use in source and binary forms, with or without
7*3b2bd0f6Slogwang  * modification, are permitted provided that the following conditions
8*3b2bd0f6Slogwang  * are met:
9*3b2bd0f6Slogwang  * 1. Redistributions of source code must retain the above copyright
10*3b2bd0f6Slogwang  *    notice, this list of conditions and the following disclaimer.
11*3b2bd0f6Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
12*3b2bd0f6Slogwang  *    notice, this list of conditions and the following disclaimer in the
13*3b2bd0f6Slogwang  *    documentation and/or other materials provided with the distribution.
14*3b2bd0f6Slogwang  *
15*3b2bd0f6Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*3b2bd0f6Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*3b2bd0f6Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*3b2bd0f6Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*3b2bd0f6Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*3b2bd0f6Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*3b2bd0f6Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*3b2bd0f6Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*3b2bd0f6Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*3b2bd0f6Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*3b2bd0f6Slogwang  * SUCH DAMAGE.
26*3b2bd0f6Slogwang  *
27*3b2bd0f6Slogwang  *	 $SourceForge: netflow.h,v 1.8 2004/09/16 17:05:11 glebius Exp $
28*3b2bd0f6Slogwang  *	 $FreeBSD$
29*3b2bd0f6Slogwang  */
30*3b2bd0f6Slogwang 
31*3b2bd0f6Slogwang /* netflow timeouts in seconds */
32*3b2bd0f6Slogwang 
33*3b2bd0f6Slogwang #define	ACTIVE_TIMEOUT		(30*60)	/* maximum flow lifetime is 30 min */
34*3b2bd0f6Slogwang #define	INACTIVE_TIMEOUT	15
35*3b2bd0f6Slogwang 
36*3b2bd0f6Slogwang /*
37*3b2bd0f6Slogwang  * More info can be found in these Cisco documents:
38*3b2bd0f6Slogwang  *
39*3b2bd0f6Slogwang  * Cisco IOS NetFlow, White Papers.
40*3b2bd0f6Slogwang  * http://www.cisco.com/en/US/products/ps6601/prod_white_papers_list.html
41*3b2bd0f6Slogwang  *
42*3b2bd0f6Slogwang  * Cisco CNS NetFlow Collection Engine User Guide, 5.0.2, NetFlow Export
43*3b2bd0f6Slogwang  * Datagram Formats.
44*3b2bd0f6Slogwang  * http://www.cisco.com/en/US/products/sw/netmgtsw/ps1964/products_user_guide_chapter09186a00803f3147.html#wp26453
45*3b2bd0f6Slogwang  *
46*3b2bd0f6Slogwang  * Cisco Systems NetFlow Services Export Version 9
47*3b2bd0f6Slogwang  * http://www.ietf.org/rfc/rfc3954.txt
48*3b2bd0f6Slogwang  *
49*3b2bd0f6Slogwang  */
50*3b2bd0f6Slogwang 
51*3b2bd0f6Slogwang #define NETFLOW_V1 1
52*3b2bd0f6Slogwang #define NETFLOW_V5 5
53*3b2bd0f6Slogwang #define NETFLOW_V9 9
54*3b2bd0f6Slogwang 
55*3b2bd0f6Slogwang struct netflow_v1_header
56*3b2bd0f6Slogwang {
57*3b2bd0f6Slogwang   uint16_t version;	/* NetFlow version */
58*3b2bd0f6Slogwang   uint16_t count;	/* Number of records in flow */
59*3b2bd0f6Slogwang   uint32_t sys_uptime;	/* System uptime */
60*3b2bd0f6Slogwang   uint32_t unix_secs;	/* Current seconds since 0000 UTC 1970 */
61*3b2bd0f6Slogwang   uint32_t unix_nsecs;	/* Remaining nanoseconds since 0000 UTC 1970 */
62*3b2bd0f6Slogwang } __attribute__((__packed__));
63*3b2bd0f6Slogwang 
64*3b2bd0f6Slogwang struct netflow_v5_header
65*3b2bd0f6Slogwang {
66*3b2bd0f6Slogwang   uint16_t version;	/* NetFlow version */
67*3b2bd0f6Slogwang   uint16_t count;	/* Number of records in flow */
68*3b2bd0f6Slogwang   uint32_t sys_uptime;	/* System uptime */
69*3b2bd0f6Slogwang   uint32_t unix_secs;	/* Current seconds since 0000 UTC 1970 */
70*3b2bd0f6Slogwang   uint32_t unix_nsecs;	/* Remaining nanoseconds since 0000 UTC 1970 */
71*3b2bd0f6Slogwang   uint32_t flow_seq;	/* Sequence number of the first record */
72*3b2bd0f6Slogwang   uint8_t engine_type;	/* Type of flow switching engine (RP,VIP,etc.) */
73*3b2bd0f6Slogwang   uint8_t engine_id;	/* Slot number of the flow switching engine */
74*3b2bd0f6Slogwang   uint16_t pad;		/* Pad to word boundary */
75*3b2bd0f6Slogwang } __attribute__((__packed__));
76*3b2bd0f6Slogwang 
77*3b2bd0f6Slogwang struct netflow_v9_header
78*3b2bd0f6Slogwang {
79*3b2bd0f6Slogwang   uint16_t version;	/* NetFlow version */
80*3b2bd0f6Slogwang   uint16_t count;	/* Total number of records in packet */
81*3b2bd0f6Slogwang   uint32_t sys_uptime;	/* System uptime */
82*3b2bd0f6Slogwang   uint32_t unix_secs;	/* Current seconds since 0000 UTC 1970 */
83*3b2bd0f6Slogwang   uint32_t seq_num;	/* Sequence number */
84*3b2bd0f6Slogwang   uint32_t source_id;	/* Observation Domain id */
85*3b2bd0f6Slogwang } __attribute__((__packed__));
86*3b2bd0f6Slogwang 
87*3b2bd0f6Slogwang struct netflow_v1_record
88*3b2bd0f6Slogwang {
89*3b2bd0f6Slogwang   uint32_t src_addr;	/* Source IP address */
90*3b2bd0f6Slogwang   uint32_t dst_addr;	/* Destination IP address */
91*3b2bd0f6Slogwang   uint32_t next_hop;	/* Next hop IP address */
92*3b2bd0f6Slogwang   uint16_t in_ifx;	/* Source interface index */
93*3b2bd0f6Slogwang   uint16_t out_ifx;	/* Destination interface index */
94*3b2bd0f6Slogwang   uint32_t packets;	/* Number of packets in a flow */
95*3b2bd0f6Slogwang   uint32_t octets;	/* Number of octets in a flow */
96*3b2bd0f6Slogwang   uint32_t first;	/* System uptime at start of a flow */
97*3b2bd0f6Slogwang   uint32_t last;	/* System uptime at end of a flow */
98*3b2bd0f6Slogwang   uint16_t s_port;	/* Source port */
99*3b2bd0f6Slogwang   uint16_t d_port;	/* Destination port */
100*3b2bd0f6Slogwang   uint16_t pad1;	/* Pad to word boundary */
101*3b2bd0f6Slogwang   uint8_t prot;		/* IP protocol */
102*3b2bd0f6Slogwang   uint8_t tos;		/* IP type of service */
103*3b2bd0f6Slogwang   uint8_t flags;	/* Cumulative OR of tcp flags */
104*3b2bd0f6Slogwang   uint8_t pad2;		/* Pad to word boundary */
105*3b2bd0f6Slogwang   uint16_t pad3;	/* Pad to word boundary */
106*3b2bd0f6Slogwang   uint8_t reserved[5];	/* Reserved for future use */
107*3b2bd0f6Slogwang } __attribute__((__packed__));
108*3b2bd0f6Slogwang 
109*3b2bd0f6Slogwang struct netflow_v5_record
110*3b2bd0f6Slogwang {
111*3b2bd0f6Slogwang   uint32_t src_addr;	/* Source IP address */
112*3b2bd0f6Slogwang   uint32_t dst_addr;	/* Destination IP address */
113*3b2bd0f6Slogwang   uint32_t next_hop;	/* Next hop IP address */
114*3b2bd0f6Slogwang   uint16_t i_ifx;	/* Source interface index */
115*3b2bd0f6Slogwang   uint16_t o_ifx;	/* Destination interface index */
116*3b2bd0f6Slogwang   uint32_t packets;	/* Number of packets in a flow */
117*3b2bd0f6Slogwang   uint32_t octets;	/* Number of octets in a flow */
118*3b2bd0f6Slogwang   uint32_t first;	/* System uptime at start of a flow */
119*3b2bd0f6Slogwang   uint32_t last;	/* System uptime at end of a flow */
120*3b2bd0f6Slogwang   uint16_t s_port;	/* Source port */
121*3b2bd0f6Slogwang   uint16_t d_port;	/* Destination port */
122*3b2bd0f6Slogwang   uint8_t pad1;		/* Pad to word boundary */
123*3b2bd0f6Slogwang   uint8_t flags;	/* Cumulative OR of tcp flags */
124*3b2bd0f6Slogwang   uint8_t prot;		/* IP protocol */
125*3b2bd0f6Slogwang   uint8_t tos;		/* IP type of service */
126*3b2bd0f6Slogwang   uint16_t src_as;	/* Src peer/origin Autonomous System */
127*3b2bd0f6Slogwang   uint16_t dst_as;	/* Dst peer/origin Autonomous System */
128*3b2bd0f6Slogwang   uint8_t src_mask;	/* Source route's mask bits */
129*3b2bd0f6Slogwang   uint8_t dst_mask;	/* Destination route's mask bits */
130*3b2bd0f6Slogwang   uint16_t pad2;	/* Pad to word boundary */
131*3b2bd0f6Slogwang } __attribute__((__packed__));
132*3b2bd0f6Slogwang 
133*3b2bd0f6Slogwang #define NETFLOW_V1_MAX_RECORDS 24
134*3b2bd0f6Slogwang #define NETFLOW_V5_MAX_RECORDS 30
135*3b2bd0f6Slogwang 
136*3b2bd0f6Slogwang #define NETFLOW_V1_MAX_SIZE (sizeof(netflow_v1_header)+ \
137*3b2bd0f6Slogwang 			     sizeof(netflow_v1_record)*NETFLOW_V1_MAX_RECORDS)
138*3b2bd0f6Slogwang #define NETFLOW_V5_MAX_SIZE (sizeof(netflow_v5_header)+ \
139*3b2bd0f6Slogwang 			     sizeof(netflow_v5_record)*NETFLOW_V5_MAX_RECORDS)
140*3b2bd0f6Slogwang 
141*3b2bd0f6Slogwang struct netflow_v5_export_dgram {
142*3b2bd0f6Slogwang 	struct netflow_v5_header	header;
143*3b2bd0f6Slogwang 	struct netflow_v5_record	r[NETFLOW_V5_MAX_RECORDS];
144*3b2bd0f6Slogwang } __attribute__((__packed__));
145*3b2bd0f6Slogwang 
146*3b2bd0f6Slogwang 
147*3b2bd0f6Slogwang /* RFC3954 field definitions */
148*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IN_BYTES		1	/* Input bytes count for a flow. Default 4, can be 8 */
149*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IN_PKTS		2	/* Incoming counter with number of packets associated with an IP Flow. Default 4 */
150*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_FLOWS			3	/* Number of Flows that were aggregated. Default 4 */
151*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_PROTOCOL		4	/* IP protocol byte. 1 */
152*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_TOS			5	/* Type of service byte setting when entering the incoming interface. 1 */
153*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_TCP_FLAGS		6	/* TCP flags; cumulative of all the TCP flags seen in this Flow. 1 */
154*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_L4_SRC_PORT		7	/* TCP/UDP source port number. 2 */
155*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV4_SRC_ADDR		8	/* IPv4 source address. 4 */
156*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_SRC_MASK		9	/* The number of contiguous bits in the source subnet mask (i.e., the mask in slash notation). 1 */
157*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_INPUT_SNMP		10	/* Input interface index. Default 2 */
158*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_L4_DST_PORT		11	/* TCP/UDP destination port number. 2 */
159*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV4_DST_ADDR		12	/* IPv4 destination address. 4 */
160*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_DST_MASK		13	/* The number of contiguous bits in the destination subnet mask (i.e., the mask in slash notation). 1 */
161*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_OUTPUT_SNMP		14	/* Output interface index. Default 2 */
162*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV4_NEXT_HOP		15	/* IPv4 address of the next-hop router. 4 */
163*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_SRC_AS			16	/* Source BGP autonomous system number. Default 2, can be 4 */
164*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_DST_AS			17	/* Destination BGP autonomous system number. Default 2, can be 4 */
165*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_BGP_IPV4_NEXT_HOP	18	/* Next-hop router's IP address in the BGP domain. 4 */
166*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MUL_DST_PKTS		19	/* IP multicast outgoing packet counter for packets associated with IP flow. Default 4 */
167*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MUL_DST_BYTES		20	/* IP multicast outgoing Octet (byte) counter for the number of bytes associated with IP flow. Default 4 */
168*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_LAST_SWITCHED		21	/* sysUptime in msec at which the last packet of this Flow was switched. 4 */
169*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_FIRST_SWITCHED		22	/* sysUptime in msec at which the first packet of this Flow was switched. 4 */
170*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_OUT_BYTES		23	/* Outgoing counter for the number of bytes associated with an IP Flow. Default 4 */
171*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_OUT_PKTS		24	/* Outgoing counter for the number of packets associated with an IP Flow. Default 4 */
172*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV6_SRC_ADDR		27	/* IPv6 source address. 16 */
173*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV6_DST_ADDR		28	/* IPv6 destination address. 16 */
174*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV6_SRC_MASK		29	/* Length of the IPv6 source mask in contiguous bits. 1 */
175*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV6_DST_MASK		30	/* Length of the IPv6 destination mask in contiguous bits. 1 */
176*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV6_FLOW_LABEL	31	/* IPv6 flow label as per RFC 2460 definition. 3 */
177*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_ICMP_TYPE		32	/* Internet Control Message Protocol (ICMP) packet type; reported as ICMP Type * 256 + ICMP code. 2 */
178*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MUL_IGMP_TYPE		33	/* Internet Group Management Protocol (IGMP) packet type. 1 */
179*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_SAMPLING_INTERVAL	34	/* When using sampled NetFlow, the rate at which packets are sampled; for example, a value of 100 indicates that one of every hundred packets is sampled. 4 */
180*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_SAMPLING_ALGORITHM	35	/* For sampled NetFlow platform-wide: 0x01 deterministic sampling 0x02 random sampling. 1 */
181*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_FLOW_ACTIVE_TIMEOUT	36	/* Timeout value (in seconds) for active flow entries in the NetFlow cache. 2 */
182*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_FLOW_INACTIVE_TIMEOUT	37	/* Timeout value (in seconds) for inactive Flow entries in the NetFlow cache. 2 */
183*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_ENGINE_TYPE		38	/* Type of Flow switching engine (route processor, linecard, etc...). 1 */
184*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_ENGINE_ID		39	/* ID number of the Flow switching engine. 1 */
185*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_TOTAL_BYTES_EXP	40	/* Counter with for the number of bytes exported by the Observation Domain. Default 4 */
186*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_TOTAL_PKTS_EXP		41	/* Counter with for the number of packets exported by the Observation Domain. Default 4 */
187*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_TOTAL_FLOWS_EXP	42	/* Counter with for the number of flows exported by the Observation Domain. Default 4 */
188*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_TOP_LABEL_TYPE	46	/* MPLS Top Label Type. 1 */
189*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_TOP_LABEL_IP_ADDR	47	/* Forwarding Equivalent Class corresponding to the MPLS Top Label. 4 */
190*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_FLOW_SAMPLER_ID	48	/* Identifier shown in "show flow-sampler". 1 */
191*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_FLOW_SAMPLER_MODE	49	/* The type of algorithm used for sampling data. 2 */
192*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_FLOW_SAMPLER_RANDOM_INTERVAL		50	/* Packet interval at which to sample. 4. */
193*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_DST_TOS		55	/* Type of Service byte setting when exiting outgoing interface. 1. */
194*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_SRC_MAC		56	/* Source MAC Address. 6 */
195*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_DST_MAC		57	/* Destination MAC Address. 6 */
196*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_SRC_VLAN		58	/* Virtual LAN identifier associated with ingress interface. 2 */
197*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_DST_VLAN		59	/* Virtual LAN identifier associated with egress interface. 2 */
198*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IP_PROTOCOL_VERSION	60	/* Internet Protocol Version. Set to 4 for IPv4, set to 6 for IPv6. If not present in the template, then version 4 is assumed. 1. */
199*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_DIRECTION		61	/* Flow direction: 0 - ingress flow 1 - egress flow. 1 */
200*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV6_NEXT_HOP		62	/* IPv6 address of the next-hop router. 16 */
201*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_BGP_IPV6_NEXT_HOP	63	/* Next-hop router in the BGP domain. 16 */
202*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_IPV6_OPTION_HEADERS	64	/* Bit-encoded field identifying IPv6 option headers found in the flow */
203*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_1		70	/* MPLS label at position 1 in the stack. 3 */
204*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_2		71	/* MPLS label at position 2 in the stack. 3 */
205*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_3		72	/* MPLS label at position 3 in the stack. 3 */
206*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_4		73	/* MPLS label at position 4 in the stack. 3 */
207*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_5		74	/* MPLS label at position 5 in the stack. 3 */
208*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_6		75	/* MPLS label at position 6 in the stack. 3 */
209*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_7		76	/* MPLS label at position 7 in the stack. 3 */
210*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_8		77	/* MPLS label at position 8 in the stack. 3 */
211*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_9		78	/* MPLS label at position 9 in the stack. 3 */
212*3b2bd0f6Slogwang #define NETFLOW_V9_FIELD_MPLS_LABEL_10		79	/* MPLS label at position 10 in the stack. 3 */
213*3b2bd0f6Slogwang 
214*3b2bd0f6Slogwang #define NETFLOW_V9_MAX_RESERVED_FLOWSET		0xFF	/* Clause 5.2 */
215