/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2010 Alexander V. Chernikov <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#ifndef _NETFLOW_V9_H_29#define _NETFLOW_V9_H_3031#ifdef COUNTERS_6432#define CNTR uint64_t33#define CNTR_MAX UINT64_MAX34#else35#define CNTR uint32_t36#define CNTR_MAX UINT_MAX37#endif3839struct netflow_v9_template40{41int field_id;42int field_length;43};4445/* Template ID for tcp/udp v4 streams ID:257 (0x100 + NETFLOW_V9_FLOW_V4_L4) */46struct netflow_v9_record_ipv4_tcp47{48uint32_t src_addr; /* Source IPv4 address (IPV4_SRC_ADDR) */49uint32_t dst_addr; /* Destination IPv4 address (IPV4_DST_ADDR) */50uint32_t next_hop; /* Next hop IPv4 address (IPV4_NEXT_HOP) */51uint16_t i_ifx; /* Source interface index (INPUT_SNMP) */52uint16_t o_ifx; /* Destination interface index (OUTPUT_SNMP) */53CNTR i_packets; /* Number of incoming packets in a flow (IN_PKTS) */54CNTR i_octets; /* Number of incoming octets in a flow (IN_BYTES) */55CNTR o_packets; /* Number of outgoing packets in a flow (OUT_PKTS) */56CNTR o_octets; /* Number of outgoing octets in a flow (OUT_BYTES) */57uint32_t first; /* System uptime at start of a flow (FIRST_SWITCHED) */58uint32_t last; /* System uptime at end of a flow (LAST_SWITCHED) */59uint16_t s_port; /* Source port (L4_SRC_PORT) */60uint16_t d_port; /* Destination port (L4_DST_PORT) */61uint8_t flags; /* Cumulative OR of tcp flags (TCP_FLAGS) */62uint8_t prot; /* IP protocol */63uint8_t tos; /* IP type of service IN (or OUT) (TOS) */64uint32_t src_as; /* Src peer/origin Autonomous System (SRC_AS) */65uint32_t dst_as; /* Dst peer/origin Autonomous System (DST_AS) */66uint8_t src_mask; /* Source route's mask bits (SRC_MASK) */67uint8_t dst_mask; /* Destination route's mask bits (DST_MASK) */68} __attribute__((__packed__));6970/* Template ID for tcp/udp v6 streams ID: 260 (0x100 + NETFLOW_V9_FLOW_V6_L4) */71struct netflow_v9_record_ipv6_tcp72{73struct in6_addr src_addr; /* Source IPv6 address (IPV6_SRC_ADDR) */74struct in6_addr dst_addr; /* Destination IPv6 address (IPV6_DST_ADDR) */75struct in6_addr next_hop; /* Next hop IPv6 address (IPV6_NEXT_HOP) */76uint16_t i_ifx; /* Source interface index (INPUT_SNMP) */77uint16_t o_ifx; /* Destination interface index (OUTPUT_SNMP) */78CNTR i_packets; /* Number of incoming packets in a flow (IN_PKTS) */79CNTR i_octets; /* Number of incoming octets in a flow (IN_BYTES) */80CNTR o_packets; /* Number of outgoing packets in a flow (OUT_PKTS) */81CNTR o_octets; /* Number of outgoing octets in a flow (OUT_BYTES) */82uint32_t first; /* System uptime at start of a flow (FIRST_SWITCHED) */83uint32_t last; /* System uptime at end of a flow (LAST_SWITCHED) */84uint16_t s_port; /* Source port (L4_SRC_PORT) */85uint16_t d_port; /* Destination port (L4_DST_PORT) */86uint8_t flags; /* Cumulative OR of tcp flags (TCP_FLAGS) */87uint8_t prot; /* IP protocol */88uint8_t tos; /* IP type of service IN (or OUT) (TOS) */89uint32_t src_as; /* Src peer/origin Autonomous System (SRC_AS) */90uint32_t dst_as; /* Dst peer/origin Autonomous System (DST_AS) */91uint8_t src_mask; /* Source route's mask bits (SRC_MASK) */92uint8_t dst_mask; /* Destination route's mask bits (DST_MASK) */93} __attribute__((__packed__));9495/* Used in export9_add to determine max record size */96struct netflow_v9_record_general97{98union {99struct netflow_v9_record_ipv4_tcp v4_tcp;100struct netflow_v9_record_ipv6_tcp v6_tcp;101} rec;102};103104#define BASE_MTU 1500105#define MIN_MTU sizeof(struct netflow_v5_header)106#define MAX_MTU 16384107#define NETFLOW_V9_MAX_SIZE _NETFLOW_V9_MAX_SIZE(BASE_MTU)108/* Decrease MSS by 16 since there can be some IPv[46] header options */109#define _NETFLOW_V9_MAX_SIZE(x) (x) - sizeof(struct ip6_hdr) - sizeof(struct udphdr) - 16110111/* #define NETFLOW_V9_MAX_FLOWSETS 2 */112113#define NETFLOW_V9_MAX_RECORD_SIZE sizeof(struct netflow_v9_record_ipv6_tcp)114#define NETFLOW_V9_MAX_PACKETS_TEMPL 500 /* Send data templates every ... packets */115#define NETFLOW_V9_MAX_TIME_TEMPL 600 /* Send data templates every ... seconds */116#define NETFLOW_V9_MAX_TEMPLATES 16 /* Not a real value */117#define _NETFLOW_V9_TEMPLATE_SIZE(x) (sizeof(x) / sizeof(struct netflow_v9_template)) * 4118//#define _NETFLOW_V9_TEMPLATE_SIZE(x) ((x) + 1) * 4119120/* Flow Templates */121#define NETFLOW_V9_FLOW_V4_L4 1 /* IPv4 TCP/UDP packet */122#define NETFLOW_V9_FLOW_V4_ICMP 2 /* IPv4 ICMP packet, currently unused */123#define NETFLOW_V9_FLOW_V4_L3 3 /* IPv4 IP packet */124#define NETFLOW_V9_FLOW_V6_L4 4 /* IPv6 TCP/UDP packet */125#define NETFLOW_V9_FLOW_V6_ICMP 5 /* IPv6 ICMP packet, currently unused */126#define NETFLOW_V9_FLOW_V6_L3 6 /* IPv6 IP packet */127128#define NETFLOW_V9_FLOW_FAKE 65535 /* Not uset used in real flowsets! */129130struct netflow_v9_export_dgram {131struct netflow_v9_header header;132char *data; /* MTU can change, record length is dynamic */133};134135struct netflow_v9_flowset_header {136uint16_t id; /* FlowSet id */137uint16_t length; /* FlowSet length */138} __attribute__((__packed__));139140struct netflow_v9_packet_opt {141uint16_t length; /* current packet length */142uint16_t count; /* current records count */143uint16_t mtu; /* max MTU snapshot */144uint16_t flow_type; /* current flowset */145uint16_t flow_header; /* offset pointing to current flow header */146};147#endif148149150