/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2010-2011 Alexander V. Chernikov <[email protected]>4* Copyright (c) 2004-2005 Gleb Smirnoff <[email protected]>5* Copyright (c) 2001-2003 Roman V. Palagin <[email protected]>6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*29* $SourceForge: ng_netflow.h,v 1.26 2004/09/04 15:44:55 glebius Exp $30*/3132#ifndef _NG_NETFLOW_H_33#define _NG_NETFLOW_H_3435#define NG_NETFLOW_NODE_TYPE "netflow"36#define NGM_NETFLOW_COOKIE 136575695437#define NGM_NETFLOW_V9_COOKIE 13498653863839#define NG_NETFLOW_MAXIFACES USHRT_MAX4041/* Hook names */4243#define NG_NETFLOW_HOOK_DATA "iface"44#define NG_NETFLOW_HOOK_OUT "out"45#define NG_NETFLOW_HOOK_EXPORT "export"46#define NG_NETFLOW_HOOK_EXPORT9 "export9"4748/* This define effectively disable (v5) netflow export hook! */49/* #define COUNTERS_64 */5051/* Netgraph commands understood by netflow node */52enum {53NGM_NETFLOW_INFO = 1|NGM_READONLY|NGM_HASREPLY, /* get node info */54NGM_NETFLOW_IFINFO = 2|NGM_READONLY|NGM_HASREPLY, /* get iface info */55NGM_NETFLOW_SHOW = 3|NGM_READONLY|NGM_HASREPLY, /* show ip cache flow */56NGM_NETFLOW_SETDLT = 4, /* set data-link type */57NGM_NETFLOW_SETIFINDEX = 5, /* set interface index */58NGM_NETFLOW_SETTIMEOUTS = 6, /* set active/inactive flow timeouts */59NGM_NETFLOW_SETCONFIG = 7, /* set flow generation options */60NGM_NETFLOW_SETTEMPLATE = 8, /* set v9 flow template periodic */61NGM_NETFLOW_SETMTU = 9, /* set outgoing interface MTU */62NGM_NETFLOW_V9INFO = 10|NGM_READONLY|NGM_HASREPLY, /* get v9 info */63};6465/* This structure is returned by the NGM_NETFLOW_INFO message */66struct ng_netflow_info {67uint64_t nfinfo_bytes; /* accounted IPv4 bytes */68uint64_t nfinfo_packets; /* accounted IPv4 packets */69uint64_t nfinfo_bytes6; /* accounted IPv6 bytes */70uint64_t nfinfo_packets6; /* accounted IPv6 packets */71uint64_t nfinfo_sbytes; /* skipped IPv4 bytes */72uint64_t nfinfo_spackets; /* skipped IPv4 packets */73uint64_t nfinfo_sbytes6; /* skipped IPv6 bytes */74uint64_t nfinfo_spackets6; /* skipped IPv6 packets */75uint64_t nfinfo_act_exp; /* active expiries */76uint64_t nfinfo_inact_exp; /* inactive expiries */77uint32_t nfinfo_used; /* used cache records */78uint32_t nfinfo_used6; /* used IPv6 cache records */79uint32_t nfinfo_alloc_failed; /* failed allocations */80uint32_t nfinfo_export_failed; /* failed exports */81uint32_t nfinfo_export9_failed; /* failed exports */82uint32_t nfinfo_realloc_mbuf; /* reallocated mbufs */83uint32_t nfinfo_alloc_fibs; /* fibs allocated */84uint32_t nfinfo_inact_t; /* flow inactive timeout */85uint32_t nfinfo_act_t; /* flow active timeout */86};8788/* Parse the info structure */89#define NG_NETFLOW_INFO_TYPE { \90{ "IPv4 bytes", &ng_parse_uint64_type },\91{ "IPv4 packets", &ng_parse_uint64_type },\92{ "IPv6 bytes", &ng_parse_uint64_type },\93{ "IPv6 packets", &ng_parse_uint64_type },\94{ "IPv4 skipped bytes", &ng_parse_uint64_type },\95{ "IPv4 skipped packets", &ng_parse_uint64_type },\96{ "IPv6 skipped bytes", &ng_parse_uint64_type },\97{ "IPv6 skipped packets", &ng_parse_uint64_type },\98{ "Active expiries", &ng_parse_uint64_type },\99{ "Inactive expiries", &ng_parse_uint64_type },\100{ "IPv4 records used", &ng_parse_uint32_type },\101{ "IPv6 records used", &ng_parse_uint32_type },\102{ "Failed allocations", &ng_parse_uint32_type },\103{ "V5 failed exports", &ng_parse_uint32_type },\104{ "V9 failed exports", &ng_parse_uint32_type },\105{ "mbuf reallocations", &ng_parse_uint32_type },\106{ "fibs allocated", &ng_parse_uint32_type },\107{ "Inactive timeout", &ng_parse_uint32_type },\108{ "Active timeout", &ng_parse_uint32_type },\109{ NULL } \110}111112/* This structure is returned by the NGM_NETFLOW_IFINFO message */113struct ng_netflow_ifinfo {114uint32_t ifinfo_packets; /* number of packets for this iface */115uint8_t ifinfo_dlt; /* Data Link Type, DLT_XXX */116#define MAXDLTNAMELEN 20117uint16_t ifinfo_index; /* connected iface index */118uint32_t conf;119};120121/* This structure is passed to NGM_NETFLOW_SETDLT message */122struct ng_netflow_setdlt {123uint16_t iface; /* which iface dlt change */124uint8_t dlt; /* DLT_XXX from bpf.h */125};126127/* This structure is passed to NGM_NETFLOW_SETIFINDEX */128struct ng_netflow_setifindex {129uint16_t iface; /* which iface index change */130uint16_t index; /* new index */131};132133/* This structure is passed to NGM_NETFLOW_SETTIMEOUTS */134struct ng_netflow_settimeouts {135uint32_t inactive_timeout; /* flow inactive timeout */136uint32_t active_timeout; /* flow active timeout */137};138139#define NG_NETFLOW_CONF_INGRESS 0x01 /* Account on ingress */140#define NG_NETFLOW_CONF_EGRESS 0x02 /* Account on egress */141#define NG_NETFLOW_CONF_ONCE 0x04 /* Add tag to account only once */142#define NG_NETFLOW_CONF_THISONCE 0x08 /* Account once in current node */143#define NG_NETFLOW_CONF_NOSRCLOOKUP 0x10 /* No radix lookup on src */144#define NG_NETFLOW_CONF_NODSTLOOKUP 0x20 /* No radix lookup on dst */145146#define NG_NETFLOW_IS_FRAG 0x01147#define NG_NETFLOW_FLOW_FLAGS (NG_NETFLOW_CONF_NOSRCLOOKUP|\148NG_NETFLOW_CONF_NODSTLOOKUP)149150/* This structure is passed to NGM_NETFLOW_SETCONFIG */151struct ng_netflow_setconfig {152uint16_t iface; /* which iface config change */153uint32_t conf; /* new config */154};155156/* This structure is passed to NGM_NETFLOW_SETTEMPLATE */157struct ng_netflow_settemplate {158uint16_t time; /* max time between announce */159uint16_t packets; /* max packets between announce */160};161162/* This structure is passed to NGM_NETFLOW_SETMTU */163struct ng_netflow_setmtu {164uint16_t mtu; /* MTU for packet */165};166167/* This structure is used in NGM_NETFLOW_SHOW request/response */168struct ngnf_show_header {169u_char version; /* IPv4 or IPv6 */170uint32_t hash_id; /* current hash index */171uint32_t list_id; /* current record number in hash */172uint32_t nentries; /* number of records in response */173};174175/* This structure is used in NGM_NETFLOW_V9INFO message */176struct ng_netflow_v9info {177uint16_t templ_packets; /* v9 template packets */178uint16_t templ_time; /* v9 template time */179uint16_t mtu; /* v9 MTU */180};181182/* XXXGL183* Somewhere flow_rec6 is casted to flow_rec, and flow6_entry_data is184* casted to flow_entry_data. After casting, fle->r.fib is accessed.185* So beginning of these structs up to fib should be kept common.186*/187188/* This is unique data, which identifies flow */189struct flow_rec {190uint16_t flow_type;191uint16_t fib;192struct in_addr r_src;193struct in_addr r_dst;194union {195struct {196uint16_t s_port; /* source TCP/UDP port */197uint16_t d_port; /* destination TCP/UDP port */198} dir;199uint32_t both;200} ports;201union {202struct {203u_char prot; /* IP protocol */204u_char tos; /* IP TOS */205uint16_t i_ifx; /* input interface index */206} i;207uint32_t all;208} misc;209};210211/* This is unique data, which identifies flow */212struct flow6_rec {213uint16_t flow_type;214uint16_t fib;215union {216struct in_addr r_src;217struct in6_addr r_src6;218} src;219union {220struct in_addr r_dst;221struct in6_addr r_dst6;222} dst;223union {224struct {225uint16_t s_port; /* source TCP/UDP port */226uint16_t d_port; /* destination TCP/UDP port */227} dir;228uint32_t both;229} ports;230union {231struct {232u_char prot; /* IP protocol */233u_char tos; /* IP TOS */234uint16_t i_ifx; /* input interface index */235} i;236uint32_t all;237} misc;238};239240#define r_ip_p misc.i.prot241#define r_tos misc.i.tos242#define r_i_ifx misc.i.i_ifx243#define r_misc misc.all244#define r_ports ports.both245#define r_sport ports.dir.s_port246#define r_dport ports.dir.d_port247248/* A flow entry which accumulates statistics */249struct flow_entry_data {250uint16_t version; /* Protocol version */251struct flow_rec r;252struct in_addr next_hop;253uint16_t fle_o_ifx; /* output interface index */254#define fle_i_ifx r.misc.i.i_ifx255uint8_t dst_mask; /* destination route mask bits */256uint8_t src_mask; /* source route mask bits */257u_long packets;258u_long bytes;259long first; /* uptime on first packet */260long last; /* uptime on last packet */261uint16_t tcp_flags; /* cumulative OR */262};263264struct flow6_entry_data {265uint16_t version; /* Protocol version */266struct flow6_rec r;267union {268struct in_addr next_hop;269struct in6_addr next_hop6;270} n;271uint16_t fle_o_ifx; /* output interface index */272#define fle_i_ifx r.misc.i.i_ifx273uint8_t dst_mask; /* destination route mask bits */274uint8_t src_mask; /* source route mask bits */275u_long packets;276u_long bytes;277long first; /* uptime on first packet */278long last; /* uptime on last packet */279uint16_t tcp_flags; /* cumulative OR */280};281282/*283* How many flow records we will transfer at once284* without overflowing socket receive buffer285*/286#define NREC_AT_ONCE 1000287#define NREC6_AT_ONCE (NREC_AT_ONCE * sizeof(struct flow_entry_data) / \288sizeof(struct flow6_entry_data))289#define NGRESP_SIZE (sizeof(struct ngnf_show_header) + (NREC_AT_ONCE * \290sizeof(struct flow_entry_data)))291#define SORCVBUF_SIZE (NGRESP_SIZE + 2 * sizeof(struct ng_mesg))292293/* Everything below is for kernel */294295#ifdef _KERNEL296297struct flow_entry {298TAILQ_ENTRY(flow_entry) fle_hash; /* entries in hash slot */299struct flow_entry_data f;300};301302struct flow6_entry {303TAILQ_ENTRY(flow_entry) fle_hash; /* entries in hash slot */304struct flow6_entry_data f;305};306/* Parsing declarations */307308/* Parse the ifinfo structure */309#define NG_NETFLOW_IFINFO_TYPE { \310{ "packets", &ng_parse_uint32_type },\311{ "data link type", &ng_parse_uint8_type }, \312{ "index", &ng_parse_uint16_type },\313{ "conf", &ng_parse_uint32_type },\314{ NULL } \315}316317/* Parse the setdlt structure */318#define NG_NETFLOW_SETDLT_TYPE { \319{ "iface", &ng_parse_uint16_type }, \320{ "dlt", &ng_parse_uint8_type }, \321{ NULL } \322}323324/* Parse the setifindex structure */325#define NG_NETFLOW_SETIFINDEX_TYPE { \326{ "iface", &ng_parse_uint16_type }, \327{ "index", &ng_parse_uint16_type }, \328{ NULL } \329}330331/* Parse the settimeouts structure */332#define NG_NETFLOW_SETTIMEOUTS_TYPE { \333{ "inactive", &ng_parse_uint32_type }, \334{ "active", &ng_parse_uint32_type }, \335{ NULL } \336}337338/* Parse the setifindex structure */339#define NG_NETFLOW_SETCONFIG_TYPE { \340{ "iface", &ng_parse_uint16_type }, \341{ "conf", &ng_parse_uint32_type }, \342{ NULL } \343}344345/* Parse the settemplate structure */346#define NG_NETFLOW_SETTEMPLATE_TYPE { \347{ "time", &ng_parse_uint16_type }, \348{ "packets", &ng_parse_uint16_type }, \349{ NULL } \350}351352/* Parse the setmtu structure */353#define NG_NETFLOW_SETMTU_TYPE { \354{ "mtu", &ng_parse_uint16_type }, \355{ NULL } \356}357358/* Parse the v9info structure */359#define NG_NETFLOW_V9INFO_TYPE { \360{ "v9 template packets", &ng_parse_uint16_type },\361{ "v9 template time", &ng_parse_uint16_type },\362{ "v9 MTU", &ng_parse_uint16_type },\363{ NULL } \364}365366/* Private hook data */367struct ng_netflow_iface {368hook_p hook; /* NULL when disconnected */369hook_p out; /* NULL when no bypass hook */370struct ng_netflow_ifinfo info;371};372373typedef struct ng_netflow_iface *iface_p;374typedef struct ng_netflow_ifinfo *ifinfo_p;375376struct netflow_export_item {377item_p item;378item_p item9;379struct netflow_v9_packet_opt *item9_opt;380};381382/* Structure contatining fib-specific data */383struct fib_export {384uint32_t fib; /* kernel fib id */385386/* Various data used for export */387struct netflow_export_item exp;388389struct mtx export_mtx; /* exp.item mutex */390struct mtx export9_mtx; /* exp.item9 mutex */391uint32_t flow_seq; /* current V5 flow sequence */392uint32_t flow9_seq; /* current V9 flow sequence */393uint32_t domain_id; /* Observartion domain id */394/* Netflow V9 counters */395uint32_t templ_last_ts; /* unixtime of last template announce */396uint32_t templ_last_pkt; /* packet count on last announce */397uint32_t sent_packets; /* packets sent by exporter; */398399/* Current packet specific options */400struct netflow_v9_packet_opt *export9_opt;401};402403typedef struct fib_export *fib_export_p;404405/* Structure describing our flow engine */406struct netflow {407node_p node; /* link to the node itself */408hook_p export; /* export data goes there */409hook_p export9; /* Netflow V9 export data goes there */410struct callout exp_callout; /* expiry periodic job */411412/*413* Flow entries are allocated in uma(9) zone zone. They are414* indexed by hash hash. Each hash element consist of tailqueue415* head and mutex to protect this element.416*/417#define CACHESIZE (65536*16)418#define CACHELOWAT (CACHESIZE * 3/4)419#define CACHEHIGHWAT (CACHESIZE * 9/10)420uma_zone_t zone;421struct flow_hash_entry *hash;422423/*424* NetFlow data export425*426* export_item is a data item, it has an mbuf with cluster427* attached to it. A thread detaches export_item from priv428* and works with it. If the export is full it is sent, and429* a new one is allocated. Before exiting thread re-attaches430* its current item back to priv. If there is item already,431* current incomplete datagram is sent.432* export_mtx is used for attaching/detaching.433*/434435/* IPv6 support */436#ifdef INET6437uma_zone_t zone6;438struct flow_hash_entry *hash6;439#endif440441/* Statistics. */442counter_u64_t nfinfo_bytes; /* accounted IPv4 bytes */443counter_u64_t nfinfo_packets; /* accounted IPv4 packets */444counter_u64_t nfinfo_bytes6; /* accounted IPv6 bytes */445counter_u64_t nfinfo_packets6; /* accounted IPv6 packets */446counter_u64_t nfinfo_sbytes; /* skipped IPv4 bytes */447counter_u64_t nfinfo_spackets; /* skipped IPv4 packets */448counter_u64_t nfinfo_sbytes6; /* skipped IPv6 bytes */449counter_u64_t nfinfo_spackets6; /* skipped IPv6 packets */450counter_u64_t nfinfo_act_exp; /* active expiries */451counter_u64_t nfinfo_inact_exp; /* inactive expiries */452uint32_t nfinfo_alloc_failed; /* failed allocations */453uint32_t nfinfo_export_failed; /* failed exports */454uint32_t nfinfo_export9_failed; /* failed exports */455uint32_t nfinfo_realloc_mbuf; /* reallocated mbufs */456uint32_t nfinfo_alloc_fibs; /* fibs allocated */457uint32_t nfinfo_inact_t; /* flow inactive timeout */458uint32_t nfinfo_act_t; /* flow active timeout */459460/* Multiple FIB support */461fib_export_p *fib_data; /* vector to per-fib data */462uint16_t maxfibs; /* number of allocated fibs */463464/* Netflow v9 configuration options */465/*466* RFC 3954 clause 7.3467* "Both options MUST be configurable by the user on the Exporter."468*/469uint16_t templ_time; /* time between sending templates */470uint16_t templ_packets; /* packets between sending templates */471#define NETFLOW_V9_MAX_FLOWSETS 2472u_char flowsets_count; /* current flowsets used */473474/* Count of records in each flowset */475u_char flowset_records[NETFLOW_V9_MAX_FLOWSETS - 1];476uint16_t mtu; /* export interface MTU */477478/* Pointers to pre-compiled flowsets */479struct netflow_v9_flowset_header480*v9_flowsets[NETFLOW_V9_MAX_FLOWSETS - 1];481482struct ng_netflow_iface ifaces[NG_NETFLOW_MAXIFACES];483};484485typedef struct netflow *priv_p;486487/* Header of a small list in hash cell */488struct flow_hash_entry {489struct mtx mtx;490TAILQ_HEAD(fhead, flow_entry) head;491};492#define ERROUT(x) { error = (x); goto done; }493494#define MTAG_NETFLOW 1221656444495#define MTAG_NETFLOW_CALLED 0496497#define m_pktlen(m) ((m)->m_pkthdr.len)498#define IP6VERSION 6499500#define priv_to_fib(priv, fib) (priv)->fib_data[(fib)]501502/*503* Cisco uses milliseconds for uptime. Bad idea, since it overflows504* every 48+ days. But we will do same to keep compatibility. This macro505* does overflowable multiplication to 1000.506*/507#define MILLIUPTIME(t) (((t) << 9) + /* 512 */ \508((t) << 8) + /* 256 */ \509((t) << 7) + /* 128 */ \510((t) << 6) + /* 64 */ \511((t) << 5) + /* 32 */ \512((t) << 3)) /* 8 */513514/* Prototypes for netflow.c */515void ng_netflow_cache_init(priv_p);516void ng_netflow_cache_flush(priv_p);517int ng_netflow_fib_init(priv_p priv, int fib);518void ng_netflow_copyinfo(priv_p, struct ng_netflow_info *);519void ng_netflow_copyv9info(priv_p, struct ng_netflow_v9info *);520callout_func_t ng_netflow_expire;521int ng_netflow_flow_add(priv_p, fib_export_p, struct ip *, caddr_t,522uint8_t, uint8_t, unsigned int);523int ng_netflow_flow6_add(priv_p, fib_export_p, struct ip6_hdr *, caddr_t,524uint8_t, uint8_t, unsigned int);525int ng_netflow_flow_show(priv_p, struct ngnf_show_header *req,526struct ngnf_show_header *resp);527void ng_netflow_v9_cache_init(priv_p);528void ng_netflow_v9_cache_flush(priv_p);529item_p get_export9_dgram(priv_p, fib_export_p,530struct netflow_v9_packet_opt **);531void return_export9_dgram(priv_p, fib_export_p, item_p,532struct netflow_v9_packet_opt *, int);533int export9_add(item_p, struct netflow_v9_packet_opt *,534struct flow_entry *);535int export9_send(priv_p, fib_export_p, item_p,536struct netflow_v9_packet_opt *, int);537538#endif /* _KERNEL */539#endif /* _NG_NETFLOW_H_ */540541542