/* * Copyright (c) 2017, Peter Haag * Copyright (c) 2014, Peter Haag * Copyright (c) 2009, Peter Haag * Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the author nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Author: haag $ * * $Id: nfdump.c 69 2010-09-09 07:17:43Z haag $ * * $LastChangedRevision: 69 $ * * */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_STDINT_H #include #endif #include "nffile.h" #include "nfx.h" #include "nfnet.h" #include "bookkeeper.h" #include "collector.h" #include "exporter.h" #include "nf_common.h" #include "netflow_v5_v7.h" #include "netflow_v9.h" #include "rbtree.h" #include "nftree.h" #include "nfprof.h" #include "nfdump.h" #include "nflowcache.h" #include "nfstat.h" #include "nfexport.h" #include "ipconv.h" #include "util.h" #include "flist.h" #ifndef DEVEL # define dbg_printf(...) /* printf(__VA_ARGS__) */ #else # define dbg_printf(...) printf(__VA_ARGS__) #endif /* hash parameters */ #define NumPrealloc 128000 #define AGGR_SIZE 7 /* Global Variables */ FilterEngine_data_t *Engine; extern char *FilterFilename; extern uint32_t loopcnt; #ifdef COMPAT15 extern extension_descriptor_t extension_descriptor[]; #endif /* Local Variables */ const char *nfdump_version = VERSION; static uint64_t total_bytes; static uint32_t total_flows; static uint32_t skipped_blocks; static uint32_t is_anonymized; static time_t t_first_flow, t_last_flow; static char Ident[IDENTLEN]; int hash_hit = 0; int hash_miss = 0; int hash_skip = 0; extension_map_list_t *extension_map_list; extern generic_exporter_t **exporter_list; /* * Output Formats: * User defined output formats can be compiled into nfdump, for easy access * The format has the same syntax as describe in nfdump(1) -o fmt: * * A format description consists of a single line containing arbitrary strings * and format specifier as described below: * * %ts // Start Time - first seen * %te // End Time - last seen * %td // Duration * %pr // Protocol * %sa // Source Address * %da // Destination Address * %sap // Source Address:Port * %dap // Destination Address:Port * %sp // Source Port * %dp // Destination Port * %nh // Next-hop IP Address * %nhb // BGP Next-hop IP Address * %sas // Source AS * %das // Destination AS * %in // Input Interface num * %out // Output Interface num * %pkt // Packets - default input * %ipkt // Input Packets * %opkt // Output Packets * %byt // Bytes - default input * %ibyt // Input Bytes * %obyt // Output Bytes * %fl // Flows * %flg // TCP Flags * %tos // Tos - Default src * %stos // Src Tos * %dtos // Dst Tos * %dir // Direction: ingress, egress * %smk // Src mask * %dmk // Dst mask * %fwd // Forwarding Status * %svln // Src Vlan * %dvln // Dst Vlan * %ismc // Input Src Mac Addr * %odmc // Output Dst Mac Addr * %idmc // Output Src Mac Addr * %osmc // Input Dst Mac Addr * %mpls1 // MPLS label 1 * %mpls2 // MPLS label 2 * %mpls3 // MPLS label 3 * %mpls4 // MPLS label 4 * %mpls5 // MPLS label 5 * %mpls6 // MPLS label 6 * %mpls7 // MPLS label 7 * %mpls8 // MPLS label 8 * %mpls9 // MPLS label 9 * %mpls10 // MPLS label 10 * * %bps // bps - bits per second * %pps // pps - packets per second * %bpp // bps - Bytes per package * * The nfdump standard output formats line, long and extended are defined as follows: */ #define FORMAT_line "%ts %td %pr %sap -> %dap %pkt %byt %fl" #define FORMAT_long "%ts %td %pr %sap -> %dap %flg %tos %pkt %byt %fl" #define FORMAT_extended "%ts %td %pr %sap -> %dap %flg %tos %pkt %byt %pps %bps %bpp %fl" #define FORMAT_biline "%ts %td %pr %sap <-> %dap %opkt %ipkt %obyt %ibyt %fl" #define FORMAT_bilong "%ts %td %pr %sap <-> %dap %flg %tos %opkt %ipkt %obyt %ibyt %fl" #define FORMAT_nsel "%ts %evt %xevt %pr %sap -> %dap %xsap -> %xdap %ibyt %obyt" #define FORMAT_nel "%ts %nevt %pr %sap -> %dap %nsap -> %ndap" #ifdef NSEL # define DefaultMode "nsel" #else # define DefaultMode "line" #endif /* The appropriate header line is compiled automatically. * * For each defined output format a v6 long format automatically exists as well e.g. * line -> line6, long -> long6, extended -> extended6 * v6 long formats need more space to print IP addresses, as IPv6 addresses are printed in full length, * where as in standard output format IPv6 addresses are condensed for better readability. * * Define your own output format and compile it into nfdumnp: * 1. Define your output format string. * 2. Test the format using standard syntax -o "fmt:" * 3. Create a #define statement for your output format, similar than the standard output formats above. * 4. Add another line into the printmap[] struct below BEFORE the last NULL line for you format: * { "formatname", format_special, FORMAT_definition, NULL }, * The first parameter is the name of your format as recognized on the command line as -o * The second parameter is always 'format_special' - the printing function. * The third parameter is your format definition as defined in #define. * The forth parameter is always NULL for user defined formats. * 5. Recompile nfdump */ // Assign print functions for all output options -o // Teminated with a NULL record printmap_t printmap[] = { { "raw", format_file_block_record, NULL }, { "line", format_special, FORMAT_line }, { "long", format_special, FORMAT_long }, { "extended", format_special, FORMAT_extended }, { "biline", format_special, FORMAT_biline }, { "bilong", format_special, FORMAT_bilong }, { "pipe", flow_record_to_pipe, NULL }, { "csv", flow_record_to_csv, NULL }, { "null", flow_record_to_null, NULL }, #ifdef NSEL { "nsel", format_special, FORMAT_nsel }, { "nel", format_special, FORMAT_nel }, #endif // add your formats here // This is always the last line { NULL, NULL, NULL } }; // For automatic output format generation in case of custom aggregation #define AggrPrependFmt "%ts %td " #define AggrAppendFmt "%pkt %byt %bps %bpp %fl" // compare at most 16 chars #define MAXMODELEN 16 /* Function Prototypes */ static void usage(char *name); static void PrintSummary(stat_record_t *stat_record, int plain_numbers, int csv_output); static stat_record_t process_data(char *wfile, int element_stat, int flow_stat, int sort_flows, printer_t print_header, printer_t print_record, time_t twin_start, time_t twin_end, uint64_t limitflows, int tag, int compress); /* Functions */ #include "nfdump_inline.c" #include "nffile_inline.c" static void usage(char *name) { printf("usage %s [options] [\"filter\"]\n" "-h\t\tthis text you see right here\n" "-V\t\tPrint version and exit.\n" "-a\t\tAggregate netflow data.\n" "-A [/net]\tHow to aggregate: ',' sep list of tags see nfdump(1)\n" "\t\tor subnet aggregation: srcip4/24, srcip6/64.\n" "-b\t\tAggregate netflow records as bidirectional flows.\n" "-B\t\tAggregate netflow records as bidirectional flows - Guess direction.\n" "-r \tread input from file\n" "-w \twrite output to file\n" "-f\t\tread netflow filter from file\n" "-n\t\tDefine number of top N for stat or sorted output.\n" "-c\t\tLimit number of records to read from source(es)\n" "-D \tUse nameserver for host lookup.\n" "-N\t\tPrint plain numbers\n" "-s [/]\tGenerate statistics for any valid record element.\n" "\t\tand ordered by : packets, bytes, flows, bps pps and bpp.\n" "-q\t\tQuiet: Do not print the header and bottom stat lines.\n" "-i \tChange Ident to in file given by -r.\n" "-J \tModify file compression: 0: uncompressed - 1: LZO - 2: BZ2 - 3: LZ4 compressed.\n" "-z\t\tLZO compress flows in output file. Used in combination with -w.\n" "-y\t\tLZ4 compress flows in output file. Used in combination with -w.\n" "-j\t\tBZ2 compress flows in output file. Used in combination with -w.\n" "-l \tSet limit on packets for line and packed output format.\n" "\t\tkey: 32 character string or 64 digit hex string starting with 0x.\n" "-L \tSet limit on bytes for line and packed output format.\n" "-I \t\tPrint netflow summary statistics info from file, specified by -r.\n" "-M \tRead input from multiple directories.\n" "\t\t/dir/dir1:dir2:dir3 Read the same files from '/dir/dir1' '/dir/dir2' and '/dir/dir3'.\n" "\t\trequests either -r filename or -R firstfile:lastfile without pathnames\n" "-m\t\tdeprecated\n" "-O Sort order for aggregated flows - tstart, tend, flows, packets bps pps bbp etc.\n" "-R \tRead input from sequence of files.\n" "\t\t/any/dir Read all files in that directory.\n" "\t\t/dir/file Read all files beginning with 'file'.\n" "\t\t/dir/file1:file2: Read all files from 'file1' to file2.\n" "-o \tUse to print out netflow records:\n" "\t\t raw Raw record dump.\n" "\t\t line Standard output line format.\n" "\t\t long Standard output line format with additional fields.\n" "\t\t extended Even more information.\n" "\t\t csv ',' separated, machine parseable output format.\n" "\t\t pipe '|' separated legacy machine parseable output format.\n" "\t\t\tmode may be extended by '6' for full IPv6 listing. e.g.long6, extended6.\n" "-E \tPrint exporter ans sampling info for collected flows.\n" "-v \tverify netflow data file. Print version and blocks.\n" "-x \tverify extension records in netflow data file.\n" "-X\t\tDump Filtertable and exit (debug option).\n" "-Z\t\tCheck filter syntax and exit.\n" "-t