1219182Sglebius/*-
2219182Sglebius * Copyright (c) 2010 Alexander V. Chernikov <melifaro@ipfw.ru>
3219182Sglebius * All rights reserved.
4219182Sglebius *
5219182Sglebius * Redistribution and use in source and binary forms, with or without
6219182Sglebius * modification, are permitted provided that the following conditions
7219182Sglebius * are met:
8219182Sglebius * 1. Redistributions of source code must retain the above copyright
9219182Sglebius *    notice, this list of conditions and the following disclaimer.
10219182Sglebius * 2. Redistributions in binary form must reproduce the above copyright
11219182Sglebius *    notice, this list of conditions and the following disclaimer in the
12219182Sglebius *    documentation and/or other materials provided with the distribution.
13219182Sglebius *
14219182Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15219182Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16219182Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17219182Sglebius * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18219182Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19219182Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20219182Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21219182Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22219182Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23219182Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24219182Sglebius * SUCH DAMAGE.
25219182Sglebius *
26219182Sglebius *	 $FreeBSD$
27219182Sglebius */
28219182Sglebius
29219182Sglebius#ifndef	_NETFLOW_V9_H_
30219182Sglebius#define	_NETFLOW_V9_H_
31219182Sglebius
32219182Sglebius#ifdef COUNTERS_64
33219182Sglebius#define CNTR		uint64_t
34219182Sglebius#define CNTR_MAX	UINT64_MAX
35219182Sglebius#else
36219182Sglebius#define CNTR		uint32_t
37219182Sglebius#define CNTR_MAX	UINT_MAX
38219182Sglebius#endif
39219182Sglebius
40219182Sglebiusstruct netflow_v9_template
41219182Sglebius{
42219182Sglebius	int	field_id;
43219182Sglebius	int	field_length;
44219182Sglebius};
45219182Sglebius
46219182Sglebius/* Template ID for tcp/udp v4 streams ID:257 (0x100 + NETFLOW_V9_FLOW_V4_L4) */
47219182Sglebiusstruct netflow_v9_record_ipv4_tcp
48219182Sglebius{
49219182Sglebius	uint32_t	src_addr;	/* Source IPv4 address (IPV4_SRC_ADDR) */
50219182Sglebius	uint32_t	dst_addr;	/* Destination IPv4 address (IPV4_DST_ADDR) */
51219182Sglebius	uint32_t	next_hop;	/* Next hop IPv4 address (IPV4_NEXT_HOP) */
52219182Sglebius	uint16_t	i_ifx;	/* Source interface index (INPUT_SNMP) */
53219182Sglebius	uint16_t	o_ifx;	/* Destination interface index (OUTPUT_SNMP) */
54219182Sglebius	CNTR		i_packets;	/* Number of incoming packets in a flow (IN_PKTS) */
55219182Sglebius	CNTR		i_octets;	/* Number of incoming octets in a flow (IN_BYTES) */
56219182Sglebius	CNTR		o_packets;	/* Number of outgoing packets in a flow (OUT_PKTS) */
57219182Sglebius	CNTR		o_octets;	/* Number of outgoing octets in a flow (OUT_BYTES) */
58219182Sglebius	uint32_t	first;	/* System uptime at start of a flow (FIRST_SWITCHED) */
59219182Sglebius	uint32_t	last;	/* System uptime at end of a flow (LAST_SWITCHED) */
60219182Sglebius	uint16_t	s_port;	/* Source port (L4_SRC_PORT) */
61219182Sglebius	uint16_t	d_port;	/* Destination port (L4_DST_PORT) */
62219182Sglebius	uint8_t		flags;	/* Cumulative OR of tcp flags (TCP_FLAGS) */
63219182Sglebius	uint8_t		prot;		/* IP protocol */
64219182Sglebius	uint8_t		tos;		/* IP type of service IN (or OUT) (TOS) */
65219182Sglebius	uint32_t	src_as;	/* Src peer/origin Autonomous System (SRC_AS) */
66219182Sglebius	uint32_t	dst_as;	/* Dst peer/origin Autonomous System (DST_AS) */
67219182Sglebius	uint8_t		src_mask;	/* Source route's mask bits (SRC_MASK) */
68219182Sglebius	uint8_t		dst_mask; 	/* Destination route's mask bits (DST_MASK) */
69219182Sglebius} __attribute__((__packed__));
70219182Sglebius
71219182Sglebius/* Template ID for tcp/udp v6 streams ID: 260 (0x100 + NETFLOW_V9_FLOW_V6_L4) */
72219182Sglebiusstruct netflow_v9_record_ipv6_tcp
73219182Sglebius{
74219182Sglebius	struct in6_addr	src_addr;	/* Source IPv6 address (IPV6_SRC_ADDR) */
75219182Sglebius	struct in6_addr	dst_addr;	/* Destination IPv6 address (IPV6_DST_ADDR) */
76219182Sglebius	struct in6_addr	next_hop;	/* Next hop IPv6 address (IPV6_NEXT_HOP) */
77219182Sglebius	uint16_t	i_ifx;	/* Source interface index (INPUT_SNMP) */
78219182Sglebius	uint16_t	o_ifx;	/* Destination interface index (OUTPUT_SNMP) */
79219182Sglebius	CNTR		i_packets;	/* Number of incoming packets in a flow (IN_PKTS) */
80219182Sglebius	CNTR		i_octets;	/* Number of incoming octets in a flow (IN_BYTES) */
81219182Sglebius	CNTR		o_packets;	/* Number of outgoing packets in a flow (OUT_PKTS) */
82219182Sglebius	CNTR		o_octets;	/* Number of outgoing octets in a flow (OUT_BYTES) */
83219182Sglebius	uint32_t	first;	/* System uptime at start of a flow (FIRST_SWITCHED) */
84219182Sglebius	uint32_t	last;	/* System uptime at end of a flow (LAST_SWITCHED) */
85219182Sglebius	uint16_t	s_port;	/* Source port (L4_SRC_PORT) */
86219182Sglebius	uint16_t	d_port;	/* Destination port (L4_DST_PORT) */
87219182Sglebius	uint8_t		flags;	/* Cumulative OR of tcp flags (TCP_FLAGS) */
88219182Sglebius	uint8_t		prot;		/* IP protocol */
89219182Sglebius	uint8_t		tos;		/* IP type of service IN (or OUT) (TOS) */
90219182Sglebius	uint32_t	src_as;	/* Src peer/origin Autonomous System (SRC_AS) */
91219182Sglebius	uint32_t	dst_as;	/* Dst peer/origin Autonomous System (DST_AS) */
92219182Sglebius	uint8_t		src_mask;	/* Source route's mask bits (SRC_MASK) */
93219182Sglebius	uint8_t		dst_mask; 	/* Destination route's mask bits (DST_MASK) */
94219182Sglebius} __attribute__((__packed__));
95219182Sglebius
96219182Sglebius/* Used in export9_add to determine max record size */
97219182Sglebiusstruct netflow_v9_record_general
98219182Sglebius{
99219182Sglebius	union {
100219182Sglebius		struct netflow_v9_record_ipv4_tcp v4_tcp;
101219182Sglebius		struct netflow_v9_record_ipv6_tcp v6_tcp;
102219182Sglebius	} rec;
103219182Sglebius};
104219182Sglebius
105219182Sglebius#define BASE_MTU	1500
106219182Sglebius#define MIN_MTU		sizeof(struct netflow_v5_header)
107219182Sglebius#define MAX_MTU		16384
108219182Sglebius#define NETFLOW_V9_MAX_SIZE	_NETFLOW_V9_MAX_SIZE(BASE_MTU)
109219182Sglebius/* Decrease MSS by 16 since there can be some IPv[46] header options */
110219182Sglebius#define _NETFLOW_V9_MAX_SIZE(x)	(x) - sizeof(struct ip6_hdr) - sizeof(struct udphdr) - 16
111219182Sglebius
112219182Sglebius/* #define NETFLOW_V9_MAX_FLOWSETS	2 */
113219182Sglebius
114219182Sglebius#define NETFLOW_V9_MAX_RECORD_SIZE	sizeof(struct netflow_v9_record_ipv6_tcp)
115219182Sglebius#define NETFLOW_V9_MAX_PACKETS_TEMPL	500	/* Send data templates every ... packets */
116219182Sglebius#define NETFLOW_V9_MAX_TIME_TEMPL	600	/* Send data templates every ... seconds */
117219182Sglebius#define NETFLOW_V9_MAX_TEMPLATES	16	/* Not a real value */
118219182Sglebius#define _NETFLOW_V9_TEMPLATE_SIZE(x)	(sizeof(x) / sizeof(struct netflow_v9_template)) * 4
119219182Sglebius//#define _NETFLOW_V9_TEMPLATE_SIZE(x)	((x) + 1) * 4
120219182Sglebius
121219182Sglebius/* Flow Templates */
122219182Sglebius#define NETFLOW_V9_FLOW_V4_L4	1 /* IPv4 TCP/UDP packet */
123219182Sglebius#define NETFLOW_V9_FLOW_V4_ICMP	2 /* IPv4 ICMP packet, currently unused */
124219182Sglebius#define NETFLOW_V9_FLOW_V4_L3	3 /* IPv4 IP packet */
125219182Sglebius#define NETFLOW_V9_FLOW_V6_L4	4 /* IPv6 TCP/UDP packet */
126219182Sglebius#define NETFLOW_V9_FLOW_V6_ICMP	5 /* IPv6 ICMP packet, currently unused */
127219182Sglebius#define NETFLOW_V9_FLOW_V6_L3	6 /* IPv6 IP packet */
128219182Sglebius
129219182Sglebius#define NETFLOW_V9_FLOW_FAKE	65535 /* Not uset used in real flowsets! */
130219182Sglebius
131219182Sglebiusstruct netflow_v9_export_dgram {
132219182Sglebius	struct netflow_v9_header	header;
133219182Sglebius	char				*data; /* MTU can change, record length is dynamic */
134219182Sglebius};
135219182Sglebius
136219182Sglebiusstruct netflow_v9_flowset_header {
137219182Sglebius	uint16_t	id; /* FlowSet id */
138219182Sglebius	uint16_t	length; /* FlowSet length */
139219182Sglebius} __attribute__((__packed__));
140219182Sglebius
141219182Sglebiusstruct netflow_v9_packet_opt {
142219182Sglebius	uint16_t	length; /* current packet length */
143219182Sglebius	uint16_t	count; /* current records count */
144219182Sglebius	uint16_t	mtu; /* max MTU shapshot */
145219182Sglebius	uint16_t	flow_type; /* current flowset */
146219182Sglebius	uint16_t	flow_header; /* offset pointing to current flow header */
147219182Sglebius};
148219182Sglebius#endif
149