15dcd9c10SGleb Smirnoff /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 45dcd9c10SGleb Smirnoff * Copyright (c) 2010 Alexander V. Chernikov <melifaro@ipfw.ru> 55dcd9c10SGleb Smirnoff * All rights reserved. 65dcd9c10SGleb Smirnoff * 75dcd9c10SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 85dcd9c10SGleb Smirnoff * modification, are permitted provided that the following conditions 95dcd9c10SGleb Smirnoff * are met: 105dcd9c10SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright 115dcd9c10SGleb Smirnoff * notice, this list of conditions and the following disclaimer. 125dcd9c10SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright 135dcd9c10SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the 145dcd9c10SGleb Smirnoff * documentation and/or other materials provided with the distribution. 155dcd9c10SGleb Smirnoff * 165dcd9c10SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 175dcd9c10SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 185dcd9c10SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 195dcd9c10SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 205dcd9c10SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 215dcd9c10SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 225dcd9c10SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 235dcd9c10SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 245dcd9c10SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 255dcd9c10SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 265dcd9c10SGleb Smirnoff * SUCH DAMAGE. 275dcd9c10SGleb Smirnoff */ 285dcd9c10SGleb Smirnoff 295dcd9c10SGleb Smirnoff #ifndef _NETFLOW_V9_H_ 305dcd9c10SGleb Smirnoff #define _NETFLOW_V9_H_ 315dcd9c10SGleb Smirnoff 325dcd9c10SGleb Smirnoff #ifdef COUNTERS_64 335dcd9c10SGleb Smirnoff #define CNTR uint64_t 345dcd9c10SGleb Smirnoff #define CNTR_MAX UINT64_MAX 355dcd9c10SGleb Smirnoff #else 365dcd9c10SGleb Smirnoff #define CNTR uint32_t 375dcd9c10SGleb Smirnoff #define CNTR_MAX UINT_MAX 385dcd9c10SGleb Smirnoff #endif 395dcd9c10SGleb Smirnoff 405dcd9c10SGleb Smirnoff struct netflow_v9_template 415dcd9c10SGleb Smirnoff { 425dcd9c10SGleb Smirnoff int field_id; 435dcd9c10SGleb Smirnoff int field_length; 445dcd9c10SGleb Smirnoff }; 455dcd9c10SGleb Smirnoff 465dcd9c10SGleb Smirnoff /* Template ID for tcp/udp v4 streams ID:257 (0x100 + NETFLOW_V9_FLOW_V4_L4) */ 475dcd9c10SGleb Smirnoff struct netflow_v9_record_ipv4_tcp 485dcd9c10SGleb Smirnoff { 495dcd9c10SGleb Smirnoff uint32_t src_addr; /* Source IPv4 address (IPV4_SRC_ADDR) */ 505dcd9c10SGleb Smirnoff uint32_t dst_addr; /* Destination IPv4 address (IPV4_DST_ADDR) */ 515dcd9c10SGleb Smirnoff uint32_t next_hop; /* Next hop IPv4 address (IPV4_NEXT_HOP) */ 525dcd9c10SGleb Smirnoff uint16_t i_ifx; /* Source interface index (INPUT_SNMP) */ 535dcd9c10SGleb Smirnoff uint16_t o_ifx; /* Destination interface index (OUTPUT_SNMP) */ 545dcd9c10SGleb Smirnoff CNTR i_packets; /* Number of incoming packets in a flow (IN_PKTS) */ 555dcd9c10SGleb Smirnoff CNTR i_octets; /* Number of incoming octets in a flow (IN_BYTES) */ 565dcd9c10SGleb Smirnoff CNTR o_packets; /* Number of outgoing packets in a flow (OUT_PKTS) */ 575dcd9c10SGleb Smirnoff CNTR o_octets; /* Number of outgoing octets in a flow (OUT_BYTES) */ 585dcd9c10SGleb Smirnoff uint32_t first; /* System uptime at start of a flow (FIRST_SWITCHED) */ 595dcd9c10SGleb Smirnoff uint32_t last; /* System uptime at end of a flow (LAST_SWITCHED) */ 605dcd9c10SGleb Smirnoff uint16_t s_port; /* Source port (L4_SRC_PORT) */ 615dcd9c10SGleb Smirnoff uint16_t d_port; /* Destination port (L4_DST_PORT) */ 625dcd9c10SGleb Smirnoff uint8_t flags; /* Cumulative OR of tcp flags (TCP_FLAGS) */ 635dcd9c10SGleb Smirnoff uint8_t prot; /* IP protocol */ 645dcd9c10SGleb Smirnoff uint8_t tos; /* IP type of service IN (or OUT) (TOS) */ 655dcd9c10SGleb Smirnoff uint32_t src_as; /* Src peer/origin Autonomous System (SRC_AS) */ 665dcd9c10SGleb Smirnoff uint32_t dst_as; /* Dst peer/origin Autonomous System (DST_AS) */ 675dcd9c10SGleb Smirnoff uint8_t src_mask; /* Source route's mask bits (SRC_MASK) */ 685dcd9c10SGleb Smirnoff uint8_t dst_mask; /* Destination route's mask bits (DST_MASK) */ 695dcd9c10SGleb Smirnoff } __attribute__((__packed__)); 705dcd9c10SGleb Smirnoff 715dcd9c10SGleb Smirnoff /* Template ID for tcp/udp v6 streams ID: 260 (0x100 + NETFLOW_V9_FLOW_V6_L4) */ 725dcd9c10SGleb Smirnoff struct netflow_v9_record_ipv6_tcp 735dcd9c10SGleb Smirnoff { 745dcd9c10SGleb Smirnoff struct in6_addr src_addr; /* Source IPv6 address (IPV6_SRC_ADDR) */ 755dcd9c10SGleb Smirnoff struct in6_addr dst_addr; /* Destination IPv6 address (IPV6_DST_ADDR) */ 765dcd9c10SGleb Smirnoff struct in6_addr next_hop; /* Next hop IPv6 address (IPV6_NEXT_HOP) */ 775dcd9c10SGleb Smirnoff uint16_t i_ifx; /* Source interface index (INPUT_SNMP) */ 785dcd9c10SGleb Smirnoff uint16_t o_ifx; /* Destination interface index (OUTPUT_SNMP) */ 795dcd9c10SGleb Smirnoff CNTR i_packets; /* Number of incoming packets in a flow (IN_PKTS) */ 805dcd9c10SGleb Smirnoff CNTR i_octets; /* Number of incoming octets in a flow (IN_BYTES) */ 815dcd9c10SGleb Smirnoff CNTR o_packets; /* Number of outgoing packets in a flow (OUT_PKTS) */ 825dcd9c10SGleb Smirnoff CNTR o_octets; /* Number of outgoing octets in a flow (OUT_BYTES) */ 835dcd9c10SGleb Smirnoff uint32_t first; /* System uptime at start of a flow (FIRST_SWITCHED) */ 845dcd9c10SGleb Smirnoff uint32_t last; /* System uptime at end of a flow (LAST_SWITCHED) */ 855dcd9c10SGleb Smirnoff uint16_t s_port; /* Source port (L4_SRC_PORT) */ 865dcd9c10SGleb Smirnoff uint16_t d_port; /* Destination port (L4_DST_PORT) */ 875dcd9c10SGleb Smirnoff uint8_t flags; /* Cumulative OR of tcp flags (TCP_FLAGS) */ 885dcd9c10SGleb Smirnoff uint8_t prot; /* IP protocol */ 895dcd9c10SGleb Smirnoff uint8_t tos; /* IP type of service IN (or OUT) (TOS) */ 905dcd9c10SGleb Smirnoff uint32_t src_as; /* Src peer/origin Autonomous System (SRC_AS) */ 915dcd9c10SGleb Smirnoff uint32_t dst_as; /* Dst peer/origin Autonomous System (DST_AS) */ 925dcd9c10SGleb Smirnoff uint8_t src_mask; /* Source route's mask bits (SRC_MASK) */ 935dcd9c10SGleb Smirnoff uint8_t dst_mask; /* Destination route's mask bits (DST_MASK) */ 945dcd9c10SGleb Smirnoff } __attribute__((__packed__)); 955dcd9c10SGleb Smirnoff 965dcd9c10SGleb Smirnoff /* Used in export9_add to determine max record size */ 975dcd9c10SGleb Smirnoff struct netflow_v9_record_general 985dcd9c10SGleb Smirnoff { 995dcd9c10SGleb Smirnoff union { 1005dcd9c10SGleb Smirnoff struct netflow_v9_record_ipv4_tcp v4_tcp; 1015dcd9c10SGleb Smirnoff struct netflow_v9_record_ipv6_tcp v6_tcp; 1025dcd9c10SGleb Smirnoff } rec; 1035dcd9c10SGleb Smirnoff }; 1045dcd9c10SGleb Smirnoff 1055dcd9c10SGleb Smirnoff #define BASE_MTU 1500 1065dcd9c10SGleb Smirnoff #define MIN_MTU sizeof(struct netflow_v5_header) 1075dcd9c10SGleb Smirnoff #define MAX_MTU 16384 1085dcd9c10SGleb Smirnoff #define NETFLOW_V9_MAX_SIZE _NETFLOW_V9_MAX_SIZE(BASE_MTU) 1095dcd9c10SGleb Smirnoff /* Decrease MSS by 16 since there can be some IPv[46] header options */ 1105dcd9c10SGleb Smirnoff #define _NETFLOW_V9_MAX_SIZE(x) (x) - sizeof(struct ip6_hdr) - sizeof(struct udphdr) - 16 1115dcd9c10SGleb Smirnoff 1125dcd9c10SGleb Smirnoff /* #define NETFLOW_V9_MAX_FLOWSETS 2 */ 1135dcd9c10SGleb Smirnoff 1145dcd9c10SGleb Smirnoff #define NETFLOW_V9_MAX_RECORD_SIZE sizeof(struct netflow_v9_record_ipv6_tcp) 1155dcd9c10SGleb Smirnoff #define NETFLOW_V9_MAX_PACKETS_TEMPL 500 /* Send data templates every ... packets */ 1165dcd9c10SGleb Smirnoff #define NETFLOW_V9_MAX_TIME_TEMPL 600 /* Send data templates every ... seconds */ 1175dcd9c10SGleb Smirnoff #define NETFLOW_V9_MAX_TEMPLATES 16 /* Not a real value */ 1185dcd9c10SGleb Smirnoff #define _NETFLOW_V9_TEMPLATE_SIZE(x) (sizeof(x) / sizeof(struct netflow_v9_template)) * 4 1195dcd9c10SGleb Smirnoff //#define _NETFLOW_V9_TEMPLATE_SIZE(x) ((x) + 1) * 4 1205dcd9c10SGleb Smirnoff 1215dcd9c10SGleb Smirnoff /* Flow Templates */ 1225dcd9c10SGleb Smirnoff #define NETFLOW_V9_FLOW_V4_L4 1 /* IPv4 TCP/UDP packet */ 1235dcd9c10SGleb Smirnoff #define NETFLOW_V9_FLOW_V4_ICMP 2 /* IPv4 ICMP packet, currently unused */ 1245dcd9c10SGleb Smirnoff #define NETFLOW_V9_FLOW_V4_L3 3 /* IPv4 IP packet */ 1255dcd9c10SGleb Smirnoff #define NETFLOW_V9_FLOW_V6_L4 4 /* IPv6 TCP/UDP packet */ 1265dcd9c10SGleb Smirnoff #define NETFLOW_V9_FLOW_V6_ICMP 5 /* IPv6 ICMP packet, currently unused */ 1275dcd9c10SGleb Smirnoff #define NETFLOW_V9_FLOW_V6_L3 6 /* IPv6 IP packet */ 1285dcd9c10SGleb Smirnoff 1295dcd9c10SGleb Smirnoff #define NETFLOW_V9_FLOW_FAKE 65535 /* Not uset used in real flowsets! */ 1305dcd9c10SGleb Smirnoff 1315dcd9c10SGleb Smirnoff struct netflow_v9_export_dgram { 1325dcd9c10SGleb Smirnoff struct netflow_v9_header header; 1335dcd9c10SGleb Smirnoff char *data; /* MTU can change, record length is dynamic */ 1345dcd9c10SGleb Smirnoff }; 1355dcd9c10SGleb Smirnoff 1365dcd9c10SGleb Smirnoff struct netflow_v9_flowset_header { 1375dcd9c10SGleb Smirnoff uint16_t id; /* FlowSet id */ 1385dcd9c10SGleb Smirnoff uint16_t length; /* FlowSet length */ 1395dcd9c10SGleb Smirnoff } __attribute__((__packed__)); 1405dcd9c10SGleb Smirnoff 1415dcd9c10SGleb Smirnoff struct netflow_v9_packet_opt { 1425dcd9c10SGleb Smirnoff uint16_t length; /* current packet length */ 1435dcd9c10SGleb Smirnoff uint16_t count; /* current records count */ 1441dc2c71eSGordon Bergling uint16_t mtu; /* max MTU snapshot */ 1455dcd9c10SGleb Smirnoff uint16_t flow_type; /* current flowset */ 1465dcd9c10SGleb Smirnoff uint16_t flow_header; /* offset pointing to current flow header */ 1475dcd9c10SGleb Smirnoff }; 1485dcd9c10SGleb Smirnoff #endif 149