nfrb 0.0.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1439 @@
1
+ /*
2
+ * Copyright (c) 2009, Peter Haag
3
+ * Copyright (c) 2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice,
10
+ * this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ * * Neither the name of SWITCH nor the names of its contributors may be
15
+ * used to endorse or promote products derived from this software without
16
+ * specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ * POSSIBILITY OF SUCH DAMAGE.
29
+ *
30
+ * $Author: haag $
31
+ *
32
+ * $Id: nffile.h 40 2009-12-16 10:41:44Z haag $
33
+ *
34
+ * $LastChangedRevision: 40 $
35
+ *
36
+ */
37
+
38
+ #ifndef _NFFILE_H
39
+ #define _NFFILE_H 1
40
+
41
+ #ifdef HAVE_STDDEF_H
42
+ #include <stddef.h>
43
+ #endif
44
+
45
+ #define IDENTLEN 128
46
+ #define IDENTNONE "none"
47
+
48
+ #define NF_EOF 0
49
+ #define NF_ERROR -1
50
+ #define NF_CORRUPT -2
51
+
52
+ #define NF_DUMPFILE "nfcapd.current"
53
+
54
+ /*
55
+ * output buffer max size, before writing data to the file
56
+ * used to cache flows before writing to disk. size: tradeoff between
57
+ * size and time to flush to disk. Do not delay collector with long I/O
58
+ */
59
+ #define WRITE_BUFFSIZE 1048576
60
+
61
+ /*
62
+ * use this buffer size to allocate memory for the output buffer
63
+ * data other than flow records, such as histograms, may be larger than
64
+ * WRITE_BUFFSIZE and have potentially more time to flush to disk
65
+ */
66
+ #define BUFFSIZE (5*WRITE_BUFFSIZE)
67
+
68
+ /* if the output buffer reaches this limit, it gets flushed. This means,
69
+ * that 0.5MB input data may produce max 1MB data in output buffer, otherwise
70
+ * a buffer overflow may occur, and data does not get processed correctly.
71
+ * However, every Process_vx function checks buffer boundaries.
72
+ */
73
+
74
+ /*
75
+ * nfdump binary file layout
76
+ * =========================
77
+ * Each data file starts with a file header, which identifies the file as an nfdump data file.
78
+ * The magic 16bit integer at the beginning of each file must read 0xA50C. This also guarantees
79
+ * that endian dependant files are read correct.
80
+ *
81
+ * Principal layout, recognized as LAYOUT_VERSION_1:
82
+ *
83
+ * +-----------+-------------+-------------+-------------+-----+-------------+
84
+ * |Fileheader | stat record | datablock 1 | datablock 2 | ... | datablock n |
85
+ * +-----------+-------------+-------------+-------------+-----+-------------+
86
+ */
87
+
88
+ typedef struct file_header_s {
89
+ uint16_t magic; // magic to recognize nfdump file type and endian type
90
+ #define MAGIC 0xA50C
91
+
92
+ uint16_t version; // version of binary file layout, incl. magic
93
+ #define LAYOUT_VERSION_1 1
94
+
95
+ uint32_t flags;
96
+ #define NUM_FLAGS 3
97
+ #define FLAG_COMPRESSED 0x1
98
+ #define FLAG_ANONYMIZED 0x2
99
+ #define FLAG_EXTENDED_STATS 0x4
100
+ /*
101
+ 0x1 File is compressed with LZO1X-1 compression
102
+ */
103
+ uint32_t NumBlocks; // number of data blocks in file
104
+ char ident[IDENTLEN]; // string identifier for this file
105
+ } file_header_t;
106
+
107
+ /* FLAG_EXTENDED_STATS bit = 0
108
+ * Compatible with nfdump x.x.x file format: After the file header an
109
+ * inplicit stat record follows, which contains the statistics
110
+ * information about all netflow records in this file.
111
+ */
112
+
113
+ typedef struct stat_record_s {
114
+ // overall stat
115
+ uint64_t numflows;
116
+ uint64_t numbytes;
117
+ uint64_t numpackets;
118
+ // flow stat
119
+ uint64_t numflows_tcp;
120
+ uint64_t numflows_udp;
121
+ uint64_t numflows_icmp;
122
+ uint64_t numflows_other;
123
+ // bytes stat
124
+ uint64_t numbytes_tcp;
125
+ uint64_t numbytes_udp;
126
+ uint64_t numbytes_icmp;
127
+ uint64_t numbytes_other;
128
+ // packet stat
129
+ uint64_t numpackets_tcp;
130
+ uint64_t numpackets_udp;
131
+ uint64_t numpackets_icmp;
132
+ uint64_t numpackets_other;
133
+ // time window
134
+ uint32_t first_seen;
135
+ uint32_t last_seen;
136
+ uint16_t msec_first;
137
+ uint16_t msec_last;
138
+ // other
139
+ uint32_t sequence_failure;
140
+ } stat_record_t;
141
+
142
+ /* FLAG_EXTENDED_STATS bit = 1
143
+ * not yet implemented
144
+ */
145
+
146
+ typedef struct stat_header_s {
147
+ uint16_t type; // stat record type
148
+ // compatible stat type nfdump 1.5.x in new extended stat record type
149
+ #define STD_STAT_TYPE 0
150
+ uint16_t size; // size of the stat record in bytes without this header
151
+ } stat_header_t;
152
+
153
+
154
+ // Netflow v9 field type/values
155
+
156
+
157
+ /*
158
+ *
159
+ * Block type 2:
160
+ * =============
161
+ * Each data block start with a common data block header, which specifies the size, type and the number of records
162
+ * in this data block
163
+ */
164
+
165
+ typedef struct data_block_header_s {
166
+ uint32_t NumRecords; // number of data records in data block
167
+ uint32_t size; // size of this block in bytes without this header
168
+ uint16_t id; // Block ID == DATA_BLOCK_TYPE_2
169
+ uint16_t pad; // unused align 32 bit
170
+ } data_block_header_t;
171
+
172
+ // compat nfdump 1.5.x v1 type
173
+ #define DATA_BLOCK_TYPE_1 1
174
+ #define DATA_BLOCK_TYPE_2 2
175
+
176
+ /*
177
+ *
178
+ * Block type 3
179
+ * ============
180
+ * same block header as type 2. Used for data other than flow data - e.g. histograms. Important difference:
181
+ * included data records have type L_record_header_t headers in order to allow larger data records.
182
+ *
183
+ */
184
+
185
+ #define Large_BLOCK_Type 3
186
+
187
+ typedef struct L_record_header_s {
188
+ // record header
189
+ uint32_t type;
190
+ uint32_t size;
191
+ } L_record_header_t;
192
+
193
+
194
+ /*
195
+ * Generic file handle for reading/writing files
196
+ * if a file is read only writeto and block_header are NULL
197
+ */
198
+ typedef struct nffile_s {
199
+ file_header_t *file_header; // file header
200
+ data_block_header_t *block_header; // buffer
201
+ void *buff_ptr; // pointer into buffer for read/write blocks/records
202
+ stat_record_t *stat_record; // flow stat record
203
+ int _compress; // data compressed flag
204
+ int fd; // file descriptor
205
+ } nffile_t;
206
+
207
+ /*
208
+ * The new block type 2 introduces a changed common record and multiple extension records. This allows a more flexible data
209
+ * storage of netflow v9 records and 3rd party extension to nfdump.
210
+ *
211
+ * A block type 2 may contain different record types, as described below.
212
+ *
213
+ * Record description:
214
+ * -------------------
215
+ * A record always starts with a 16bit record id followed by a 16bit record size. This record size is the full size of this
216
+ * record incl. record type and size fields and all record extensions.
217
+ *
218
+ * Know record types:
219
+ * Type 0: reserved
220
+ * Type 1: Common netflow record incl. all record extensions
221
+ * Type 2: Extension map
222
+ * Type 3: xstat - port histogram record
223
+ * Type 4: xstat - bpp histogram record
224
+ */
225
+
226
+ #define CommonRecordType 1
227
+ #define ExtensionMapType 2
228
+ #define PortHistogramType 3
229
+ #define BppHistogramType 4
230
+
231
+ /*
232
+ * All records are 32bit aligned and layouted in a 64bit array. The numbers placed in () refer to the netflow v9 type id.
233
+ *
234
+ * Record type 1
235
+ * =============
236
+ * The record type 1 describes a netflow data record incl. all optional extensions for this record.
237
+ * A netflow data record requires at least the first 3 extensions 1..3. All other extensions are optional
238
+ * and described in the extensiion map. The common record contains a reference to the extension map which
239
+ * applies for this record.
240
+ *
241
+ * flags:
242
+ * bit 0: 0: IPv4 1: IPv6
243
+ * bit 1: 0: 32bit dPkts 1: 64bit dPkts
244
+ * bit 2: 0: 32bit dOctets 1: 64bit dOctets
245
+ * bit 3: 0: IPv4 next hop 1: IPv6 next hop
246
+ * bit 4: 0: IPv4 BGP next hop 1: BGP IPv6 next hop
247
+ * bit 5: 0: 1:
248
+ * bit 6: 0: 1:
249
+ * bit 7: 0: 1:
250
+ * bit 8: 0: unsampled 1: sampled flow - sampling applied
251
+ *
252
+ * Required extensions: 1,2,3
253
+ * ------------------------------
254
+ * A netflow record consists at least of a common record ( extension 0 ) and 3 required extension:
255
+ *
256
+ * Extension 1: IPv4 or IPv4 src and dst addresses Flags bit 0: 0: IPv4, 1: IPv6
257
+ * Extension 2: 32 or 64 bit packet counter Flags bit 1: 0: 32bit, 1: 64bit
258
+ * Extension 3: 32 or 64 bit byte counter Flags bit 2: 0: 32bit, 1: 64bit
259
+ *
260
+ * Commmon record - extension 0
261
+ * *+---+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
262
+ * | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
263
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
264
+ * | 0 | record type == 1 | size | flags | tag | ext. map |
265
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
266
+ * | 1 | msec_first | msec_last | first (22) |
267
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
268
+ * | 2 | last (21) |fwd_status(89)| tcpflags (6) | proto (4) | src tos (5) |
269
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
270
+ * | 3 | srcport (7) | dstport(11)/ICMP (32) |
271
+ * +----+--------------+--------------+--------------+--------------+
272
+
273
+ *
274
+ */
275
+
276
+ #define COMMON_BLOCK_ID 0
277
+
278
+ typedef struct record_header_s {
279
+ // record header
280
+ uint16_t type;
281
+ uint16_t size;
282
+ } record_header_t;
283
+
284
+ typedef struct common_record_s {
285
+ // record head
286
+ uint16_t type;
287
+ uint16_t size;
288
+
289
+ // record meta data
290
+ uint8_t flags;
291
+ #define FLAG_IPV6_ADDR 1
292
+ #define FLAG_PKG_64 2
293
+ #define FLAG_BYTES_64 4
294
+ #define FLAG_IPV6_NH 8
295
+ #define FLAG_IPV6_NHB 16
296
+ #define FLAG_IPV6_EXP 32
297
+ #define FLAG_SAMPLED 128
298
+
299
+ #define SetFlag(var, flag) (var |= flag)
300
+ #define ClearFlag(var, flag) (var &= ~flag)
301
+ #define TestFlag(var, flag) (var & flag)
302
+
303
+ uint8_t exporter_ref;
304
+ uint16_t ext_map;
305
+
306
+ // netflow common record
307
+ uint16_t msec_first;
308
+ uint16_t msec_last;
309
+ uint32_t first;
310
+ uint32_t last;
311
+
312
+ uint8_t fwd_status;
313
+ uint8_t tcp_flags;
314
+ uint8_t prot;
315
+ uint8_t tos;
316
+ uint16_t srcport;
317
+ uint16_t dstport;
318
+
319
+ // link to extensions
320
+ uint32_t data[1];
321
+ } common_record_t;
322
+ #define COMMON_RECORD_DATA_SIZE (sizeof(common_record_t) - sizeof(uint32_t) )
323
+
324
+ /*
325
+ * Required extensions:
326
+ * --------------------
327
+ * Extension 1:
328
+ * IPv4/v6 address type
329
+ * IP version: IPv4
330
+ * |
331
+ * Flags: xxxx xxx0
332
+ * IPv4:
333
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
334
+ * | 0 | srcip (8) | dstip (12) |
335
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
336
+ *
337
+ * IPv6:
338
+ * IP version: IPv6
339
+ * |
340
+ * Flags: xxxx xxx1
341
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
342
+ * | 0 | srcip (27) |
343
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
344
+ * | 1 | srcip (27) |
345
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
346
+ * | 2 | dstip (28) |
347
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
348
+ * | 3 | dstip (28) |
349
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
350
+ *
351
+ */
352
+
353
+ #define EX_IPv4v6 1
354
+
355
+ typedef struct ipv4_block_s {
356
+ uint32_t srcaddr;
357
+ uint32_t dstaddr;
358
+ uint8_t data[4]; // .. more data below
359
+ } ipv4_block_t;
360
+
361
+ typedef struct ipv6_block_s {
362
+ uint64_t srcaddr[2];
363
+ uint64_t dstaddr[2];
364
+ uint8_t data[4]; // .. more data below
365
+ } ipv6_block_t;
366
+
367
+ // single IP addr for next hop and bgp next hop
368
+ typedef struct ip_addr_s {
369
+ union {
370
+ struct {
371
+ #ifdef WORDS_BIGENDIAN
372
+ uint32_t fill[3];
373
+ uint32_t _v4;
374
+ #else
375
+ uint32_t fill1[2];
376
+ uint32_t _v4;
377
+ uint32_t fill2;
378
+ #endif
379
+ };
380
+ uint64_t _v6[2];
381
+ } ip_union;
382
+ } ip_addr_t;
383
+
384
+ #define v4 ip_union._v4
385
+ #define v6 ip_union._v6
386
+
387
+ /*
388
+ * Extension 2:
389
+ * In packet counter size
390
+ *
391
+ * In packet counter size 4byte
392
+ * |
393
+ * Flags: xxxx xx0x
394
+ * +---++--------------+--------------+--------------+--------------+
395
+ * | 0 | in pkts (2) |
396
+ * +---++--------------+--------------+--------------+--------------+
397
+ *
398
+ * In packet counter size 8byte
399
+ * |
400
+ * Flags: xxxx xx1x
401
+ * +---++--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
402
+ * | 0 | in pkts (2) |
403
+ * +---++--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
404
+ *
405
+ */
406
+
407
+ #define EX_PACKET_4_8 2
408
+
409
+ typedef struct value32_s {
410
+ uint32_t val;
411
+ uint8_t data[4]; // .. more data below
412
+ } value32_t;
413
+
414
+ typedef struct value64_s {
415
+ union val_s {
416
+ uint64_t val64;
417
+ uint32_t val32[2];
418
+ } val;
419
+ uint8_t data[4]; // .. more data below
420
+ } value64_t;
421
+
422
+
423
+ /* Extension 3:
424
+ * in byte counter size
425
+ * In byte counter size 4byte
426
+ * |
427
+ * Flags: xxxx x0xx
428
+ *
429
+ * +---++--------------+--------------+--------------+--------------+
430
+ * | 0 | in bytes (1) |
431
+ * +---++--------------+--------------+--------------+--------------+
432
+ *
433
+ * In byte counter size 8byte
434
+ * |
435
+ * Flags: xxxx x1xx
436
+ * +---++--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
437
+ * | 0 | in bytes (1) |
438
+ * +---++--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
439
+ */
440
+
441
+ #define EX_BYTE_4_8 3
442
+
443
+ /*
444
+ *
445
+ * Optional extension:
446
+ * ===================
447
+ *
448
+ * Interface record
449
+ * ----------------
450
+ * Interface records are optional and accepted as either 2 or 4 bytes numbers
451
+ * Extension 4:
452
+ * +---++--------------+--------------+--------------+--------------+
453
+ * | 0 | input (10) | output (14) |
454
+ * +---++--------------+--------------+--------------+--------------+
455
+ */
456
+ #define EX_IO_SNMP_2 4
457
+ typedef struct tpl_ext_4_s {
458
+ uint16_t input;
459
+ uint16_t output;
460
+ uint8_t data[4]; // points to further data
461
+ } tpl_ext_4_t;
462
+
463
+ /*
464
+ * Extension 5:
465
+ * +---++--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
466
+ * | 0 | input (10) | output (14) |
467
+ * +---++--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
468
+ * Extension 4 and 5 are mutually exclusive in the extension map
469
+ */
470
+ #define EX_IO_SNMP_4 5
471
+ typedef struct tpl_ext_5_s {
472
+ uint32_t input;
473
+ uint32_t output;
474
+ uint8_t data[4]; // points to further data
475
+ } tpl_ext_5_t;
476
+
477
+
478
+ /*
479
+ * AS record
480
+ * ---------
481
+ * AS records are optional and accepted as either 2 or 4 bytes numbers
482
+ * Extension 6:
483
+ * +---++--------------+--------------+--------------+--------------+
484
+ * | 0 | src as (16) | dst as (17) |
485
+ * +---++--------------+--------------+--------------+--------------+
486
+ */
487
+ #define EX_AS_2 6
488
+ typedef struct tpl_ext_6_s {
489
+ uint16_t src_as;
490
+ uint16_t dst_as;
491
+ uint8_t data[4]; // points to further data
492
+ } tpl_ext_6_t;
493
+
494
+ /*
495
+ * Extension 7:
496
+ * +---++--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
497
+ * | 0 | src as (16) | dst as (17) |
498
+ * +---++--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
499
+ * Extension 6 and 7 are mutually exclusive in the extension map
500
+ */
501
+ #define EX_AS_4 7
502
+ typedef struct tpl_ext_7_s {
503
+ uint32_t src_as;
504
+ uint32_t dst_as;
505
+ uint8_t data[4]; // points to further data
506
+ } tpl_ext_7_t;
507
+
508
+
509
+ /*
510
+ * Multiple fields record
511
+ * ----------------------
512
+ * These 4 different fields are grouped together in a 32bit value.
513
+ * Extension 8:
514
+ * +---++--------------+--------------+--------------+--------------+
515
+ * | 3 | dst tos(55) | dir(61) | srcmask(9,29)|dstmask(13,30)|
516
+ * +---++--------------+--------------+--------------+--------------+
517
+ */
518
+ #define EX_MULIPLE 8
519
+ typedef struct tpl_ext_8_s {
520
+ union {
521
+ struct {
522
+ uint8_t dst_tos;
523
+ uint8_t dir;
524
+ uint8_t src_mask;
525
+ uint8_t dst_mask;
526
+ };
527
+ uint32_t any;
528
+ };
529
+ uint8_t data[4]; // points to further data
530
+ } tpl_ext_8_t;
531
+
532
+ /*
533
+ * IP next hop
534
+ * -------------
535
+ * IPv4:
536
+ * Extension 9:
537
+ * IP version: IPv6
538
+ * |
539
+ * Flags: xxxx 0xxx
540
+ * +----+--------------+--------------+--------------+--------------+
541
+ * | 0 | next hop ip (15) |
542
+ * +----+--------------+--------------+--------------+--------------+
543
+ */
544
+ #define EX_NEXT_HOP_v4 9
545
+ typedef struct tpl_ext_9_s {
546
+ uint32_t nexthop;
547
+ uint8_t data[4]; // points to further data
548
+ } tpl_ext_9_t;
549
+
550
+ /*
551
+ * IPv6:
552
+ * Extension 10:
553
+ * IP version: IPv6
554
+ * |
555
+ * Flags: xxxx 1xxx
556
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
557
+ * | 0 | next hop ip (62) |
558
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
559
+ * | 1 | next hop ip (62) |
560
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
561
+ * Extension 9 and 10 are mutually exclusive in the extension map
562
+ */
563
+ #define EX_NEXT_HOP_v6 10
564
+ typedef struct tpl_ext_10_s {
565
+ uint64_t nexthop[2];
566
+ uint8_t data[4]; // points to further data
567
+ } tpl_ext_10_t;
568
+
569
+
570
+ /*
571
+ * BGP next hop IP
572
+ * ------------------
573
+ * IPv4:
574
+ * Extension 11:
575
+ * IP version: IPv6
576
+ * |
577
+ * Flags: xxx0 xxxx
578
+ * +----+--------------+--------------+--------------+--------------+
579
+ * | 0 | bgp next ip (18) |
580
+ * +----+--------------+--------------+--------------+--------------+
581
+ */
582
+ #define EX_NEXT_HOP_BGP_v4 11
583
+ typedef struct tpl_ext_11_s {
584
+ uint32_t bgp_nexthop;
585
+ uint8_t data[4]; // points to further data
586
+ } tpl_ext_11_t;
587
+
588
+ /*
589
+ * IPv6:
590
+ * Extension 12:
591
+ * IP version: IPv6
592
+ * |
593
+ * Flags: xxx1 xxxx
594
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
595
+ * | 0 | bgp next ip (63) |
596
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
597
+ * | 1 | bgp next ip (63) |
598
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
599
+ */
600
+ #define EX_NEXT_HOP_BGP_v6 12
601
+ typedef struct tpl_ext_12_s {
602
+ uint64_t bgp_nexthop[2];
603
+ uint8_t data[4]; // points to further data
604
+ } tpl_ext_12_t;
605
+
606
+
607
+ /*
608
+ * VLAN record
609
+ * -----------
610
+ * Extension 13:
611
+ * +----+--------------+--------------+--------------+--------------+
612
+ * | 0 | src vlan(58) | dst vlan (59) |
613
+ * +----+--------------+--------------+--------------+--------------+
614
+ */
615
+ #define EX_VLAN 13
616
+ typedef struct tpl_ext_13_s {
617
+ uint16_t src_vlan;
618
+ uint16_t dst_vlan;
619
+ uint8_t data[4]; // points to further data
620
+ } tpl_ext_13_t;
621
+
622
+
623
+ /*
624
+ * Out packet counter size
625
+ * ------------------------
626
+ * 2 byte
627
+ * Extension 14:
628
+ * +----+--------------+--------------+--------------+--------------+
629
+ * | 0 | out pkts (24) |
630
+ * +----+--------------+--------------+--------------+--------------+
631
+ */
632
+ #define EX_OUT_PKG_4 14
633
+ typedef struct tpl_ext_14_s {
634
+ uint32_t out_pkts;
635
+ uint8_t data[4]; // points to further data
636
+ } tpl_ext_14_t;
637
+
638
+ /*
639
+ * 4 byte
640
+ * Extension 15:
641
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
642
+ * | 0 | out pkts (24) |
643
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
644
+ * Extension 14 and 15 are mutually exclusive in the extension map
645
+ */
646
+ #define EX_OUT_PKG_8 15
647
+ typedef struct tpl_ext_15_s {
648
+ union {
649
+ uint64_t out_pkts;
650
+ uint32_t v[2]; // for strict alignment use 2x32bits
651
+ };
652
+ uint8_t data[4]; // points to further data
653
+ } tpl_ext_15_t;
654
+
655
+
656
+ /*
657
+ * Out byte counter size
658
+ * ---------------------
659
+ * 4 byte
660
+ * Extension 16:
661
+ * +----+--------------+--------------+--------------+--------------+
662
+ * | 0 | out bytes (23) |
663
+ * +----+--------------+--------------+--------------+--------------+
664
+ */
665
+ #define EX_OUT_BYTES_4 16
666
+ typedef struct tpl_ext_16_s {
667
+ uint32_t out_bytes;
668
+ uint8_t data[4]; // points to further data
669
+ } tpl_ext_16_t;
670
+
671
+
672
+ /* 8 byte
673
+ * Extension 17:
674
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
675
+ * | 0 | out bytes (23) |
676
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
677
+ * Extension 16 and 17 are mutually exclusive in the extension map
678
+ */
679
+ #define EX_OUT_BYTES_8 17
680
+ typedef struct tpl_ext_17_s {
681
+ union {
682
+ uint64_t out_bytes;
683
+ uint32_t v[2]; // potential 32bit alignment
684
+ };
685
+ uint8_t data[4]; // points to further data
686
+ } tpl_ext_17_t;
687
+
688
+ /*
689
+ * Aggr flows
690
+ * ----------
691
+ * 4 byte
692
+ * Extension 18:
693
+ * +----+--------------+--------------+--------------+--------------+
694
+ * | 0 | aggr flows (3) |
695
+ * +----+--------------+--------------+--------------+--------------+
696
+ */
697
+ #define EX_AGGR_FLOWS_4 18
698
+ typedef struct tpl_ext_18_s {
699
+ uint32_t aggr_flows;
700
+ uint8_t data[4]; // points to further data
701
+ } tpl_ext_18_t;
702
+
703
+
704
+ /* 8 byte
705
+ * Extension 19:
706
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
707
+ * | 0 | aggr flows (3) |
708
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
709
+ * Extension 18 and 19 are mutually exclusive in the extension map
710
+ */
711
+ #define EX_AGGR_FLOWS_8 19
712
+ typedef struct tpl_ext_19_s {
713
+ union {
714
+ uint64_t aggr_flows;
715
+ uint32_t v[2]; // 32bit alignment
716
+ };
717
+ uint8_t data[4]; // points to further data
718
+ } tpl_ext_19_t;
719
+
720
+ /* 16 byte
721
+ * Extension 20:
722
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
723
+ * | 0 | 0 | in src mac (56) |
724
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
725
+ * | 1 | 0 | out dst mac (57) |
726
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
727
+ */
728
+ #define EX_MAC_1 20
729
+ typedef struct tpl_ext_20_s {
730
+ union {
731
+ uint64_t in_src_mac;
732
+ uint32_t v1[2];
733
+ };
734
+ union {
735
+ uint64_t out_dst_mac;
736
+ uint32_t v2[2];
737
+ };
738
+ uint8_t data[4]; // points to further data
739
+ } tpl_ext_20_t;
740
+
741
+ /* 16 byte
742
+ * Extension 21:
743
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
744
+ * | 0 | 0 | in dst mac (80) |
745
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
746
+ * | 1 | 0 | out src mac (81) |
747
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
748
+ */
749
+ #define EX_MAC_2 21
750
+ typedef struct tpl_ext_21_s {
751
+ union {
752
+ uint64_t in_dst_mac;
753
+ uint32_t v1[2];
754
+ };
755
+ union {
756
+ uint64_t out_src_mac;
757
+ uint32_t v2[2];
758
+ };
759
+ uint8_t data[4]; // points to further data
760
+ } tpl_ext_21_t;
761
+
762
+ /* 40 byte
763
+ * Extension 22:
764
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
765
+ * | 0 | 0 | MPLS_LABEL_2 (71) | 0 | MPLS_LABEL_1 (70) |
766
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
767
+ * | 1 | 0 | MPLS_LABEL_4 (73) | 0 | MPLS_LABEL_3 (72) |
768
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
769
+ * | 2 | 0 | MPLS_LABEL_6 (75) | 0 | MPLS_LABEL_5 (74) |
770
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
771
+ * | 3 | 0 | MPLS_LABEL_8 (77) | 0 | MPLS_LABEL_7 (76) |
772
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
773
+ * | 3 | 0 | MPLS_LABEL_10 (79) | 0 | MPLS_LABEL_9 (78) |
774
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
775
+ */
776
+ #define EX_MPLS 22
777
+ typedef struct tpl_ext_22_s {
778
+ uint32_t mpls_label[10];
779
+ uint8_t data[4]; // points to further data
780
+ } tpl_ext_22_t;
781
+
782
+ /*
783
+ * Sending router IP
784
+ * -----------------
785
+ * IPv4:
786
+ * Extension 23:
787
+ * IP version: IPv6
788
+ * |
789
+ * Flags: xx0x xxxx
790
+ * +----+--------------+--------------+--------------+--------------+
791
+ * | 0 | router ipv4 () |
792
+ * +----+--------------+--------------+--------------+--------------+
793
+ */
794
+ #define EX_ROUTER_IP_v4 23
795
+ typedef struct tpl_ext_23_s {
796
+ uint32_t router_ip;
797
+ uint8_t data[4]; // points to further data
798
+ } tpl_ext_23_t;
799
+
800
+ /*
801
+ * IPv6:
802
+ * Extension 24:
803
+ * IP version: IPv6
804
+ * |
805
+ * Flags: xx1x xxxx
806
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
807
+ * | 0 | router ip v6 () |
808
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
809
+ * | 1 | router ip v6 () |
810
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
811
+ * Extension 23 and 24 are mutually exclusive in the extension map
812
+ */
813
+ #define EX_ROUTER_IP_v6 24
814
+ typedef struct tpl_ext_24_s {
815
+ uint64_t router_ip[2];
816
+ uint8_t data[4]; // points to further data
817
+ } tpl_ext_24_t;
818
+
819
+ /*
820
+ * router source ID
821
+ * ----------------
822
+ * For v5 netflow, it's engine type/engine ID
823
+ * for v9 it's the source_id
824
+ * Extension 25:
825
+ * +----+--------------+--------------+--------------+--------------+
826
+ * | 0 | fill |engine tpe(38)|engine ID(39) |
827
+ * +----+--------------+--------------+--------------+--------------+
828
+ */
829
+ #define EX_ROUTER_ID 25
830
+ typedef struct tpl_ext_25_s {
831
+ uint16_t fill;
832
+ uint8_t engine_type;
833
+ uint8_t engine_id;
834
+ uint8_t data[4]; // points to further data
835
+ } tpl_ext_25_t;
836
+
837
+
838
+ /*
839
+ *
840
+ *
841
+ * Extension map:
842
+ * =============
843
+ * The extension map replaces the individual flags in v1 layout. With many possible extensions and combination of extensions
844
+ * an extension map is more efficient and flexible while reading and decoding the record.
845
+ * In current version of nfdump, up to 65535 individual extension maps are supported, which is considered to be enough.
846
+ *
847
+ * For each available extension record, the ids are recorded in the extension map in the order they appear.
848
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
849
+ * | - | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
850
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
851
+ * | 0 | record type == 2 | size | map id | extension size |
852
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
853
+ * | 0 | extension id 1 | extension id 2 | extension id 3 | extension id 4 |
854
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
855
+ * ...
856
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
857
+ * | 0 | extension id n | extension id n+1 | extension id n+2 | extension id n+3 |
858
+ * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
859
+ * ...
860
+ * +----+--------------+--------------+--------------+--------------+
861
+ * | 0 | 0 | opt. 32bit alignment: 0 |
862
+ * +----+--------------+--------------+--------------+--------------+
863
+ */
864
+
865
+ /* extension IDs above are 16 bit integers. So themax number of available extensions is */
866
+ #define MAX_EXTENSIONS 65536
867
+
868
+ typedef struct extension_map_s {
869
+ // record head
870
+ uint16_t type; // is ExtensionMapType
871
+ uint16_t size; // size of full map incl. header
872
+
873
+ // map data
874
+ #define INIT_ID 0xFFFF
875
+ uint16_t map_id; // identifies this map
876
+ uint16_t extension_size; // size of all extensions
877
+ uint16_t ex_id[1]; // extension id array
878
+ } extension_map_t;
879
+
880
+
881
+ // see nfx.c - extension_descriptor
882
+ #define DefaultExtensions "1,2"
883
+
884
+ // typedef struct master_record_s master_record_t;
885
+
886
+ /* the master record contains all possible records unpacked */
887
+ typedef struct master_record_s {
888
+ // common information from all netflow versions
889
+ // // interpreted as uint64_t[]
890
+ // #ifdef WORDS_BIGENDIAN
891
+
892
+ uint16_t type; // index 0 0xffff 0000 0000 0000
893
+ uint16_t size; // index 0 0x0000'ffff'0000 0000
894
+ uint8_t flags; // index 0 0x0000'0000'ff00'0000
895
+ uint8_t exporter_ref; // index 0 0x0000'0000'00ff'0000
896
+ uint16_t ext_map; // index 0 0x0000'0000'0000'ffff
897
+ #ifdef WORDS_BIGENDIAN
898
+ # define OffsetRecordFlags 0
899
+ # define MaskRecordFlags 0x00000000ff000000LL
900
+ # define ShiftRecordFlags 24
901
+ #else
902
+ # define OffsetRecordFlags 0
903
+ # define MaskRecordFlags 0x000000ff00000000LL
904
+ # define ShiftRecordFlags 32
905
+ #endif
906
+
907
+ //
908
+ uint16_t msec_first; // index 1 0xffff'0000'0000'0000
909
+ uint16_t msec_last; // index 1 0x0000'ffff'0000'0000
910
+
911
+ // 12 bytes offset in master record to first
912
+ #define BYTE_OFFSET_first 12
913
+
914
+ uint32_t first; // index 1 0x0000'0000'ffff'ffff
915
+
916
+ //
917
+ uint32_t last; // index 2 0xffff'ffff'0000'0000
918
+ uint8_t fwd_status; // index 2 0x0000'0000'ff00'0000
919
+ uint8_t tcp_flags; // index 2 0x0000'0000'00ff'0000
920
+ uint8_t prot; // index 2 0x0000'0000'0000'ff00
921
+ uint8_t tos; // index 2 0x0000'0000'0000'00ff
922
+ #ifdef WORDS_BIGENDIAN
923
+ # define OffsetStatus 2
924
+ # define MaskStatus 0x00000000ff000000LL
925
+ # define ShiftStatus 24
926
+
927
+ # define OffsetFlags 2
928
+ # define MaskFlags 0x0000000000ff0000LL
929
+ #define ShiftFlags 16
930
+
931
+ # define OffsetProto 2
932
+ # define MaskProto 0x000000000000ff00LL
933
+ # define ShiftProto 8
934
+
935
+ # define OffsetTos 2
936
+ # define MaskTos 0x00000000000000ffLL
937
+ # define ShiftTos 0
938
+
939
+ #else
940
+ # define OffsetStatus 2
941
+ # define MaskStatus 0x000000ff00000000LL
942
+ # define ShiftStatus 32
943
+
944
+ # define OffsetFlags 2
945
+ # define MaskFlags 0x0000ff0000000000LL
946
+ # define ShiftFlags 40
947
+
948
+ # define OffsetProto 2
949
+ # define MaskProto 0x00ff000000000000LL
950
+ # define ShiftProto 48
951
+
952
+ # define OffsetTos 2
953
+ # define MaskTos 0xff00000000000000LL
954
+ # define ShiftTos 56
955
+ #endif
956
+
957
+ // extension 8
958
+ uint16_t srcport; // index 3 0xffff'0000'0000'0000
959
+ uint16_t dstport; // index 3 0x0000'ffff'0000'0000
960
+ union {
961
+ struct {
962
+ uint8_t dst_tos; // index 3 0x0000'0000'ff00'0000
963
+ uint8_t dir; // index 3 0x0000'0000'00ff'0000
964
+ uint8_t src_mask; // index 3 0x0000'0000'0000'ff00
965
+ uint8_t dst_mask; // index 3 0x0000'0000'0000'00ff
966
+ };
967
+ uint32_t any;
968
+ };
969
+ #ifdef WORDS_BIGENDIAN
970
+ # define OffsetPort 3
971
+ # define MaskSrcPort 0xffff000000000000LL
972
+ # define ShiftSrcPort 48
973
+
974
+ # define MaskDstPort 0x0000ffff00000000LL
975
+ # define ShiftDstPort 32
976
+
977
+ # define MaskICMPtype 0x0000ff0000000000LL
978
+ # define ShiftICMPtype 40
979
+ # define MaskICMPcode 0x000000ff00000000LL
980
+ # define ShiftICMPcode 32
981
+
982
+ # define OffsetDstTos 3
983
+ # define MaskDstTos 0x00000000ff000000LL
984
+ # define ShiftDstTos 24
985
+
986
+ # define OffsetDir 3
987
+ # define MaskDir 0x0000000000ff0000LL
988
+ # define ShiftDir 16
989
+
990
+ # define OffsetMask 3
991
+ # define MaskSrcMask 0x000000000000ff00LL
992
+ # define ShiftSrcMask 8
993
+
994
+ # define MaskDstMask 0x00000000000000ffLL
995
+ # define ShiftDstMask 0
996
+
997
+ #else
998
+ # define OffsetPort 3
999
+ # define MaskSrcPort 0x000000000000ffffLL
1000
+ # define ShiftSrcPort 0
1001
+
1002
+ # define MaskDstPort 0x00000000ffff0000LL
1003
+ # define ShiftDstPort 16
1004
+
1005
+ # define MaskICMPtype 0x00000000ff000000LL
1006
+ # define ShiftICMPtype 24
1007
+ # define MaskICMPcode 0x0000000000ff0000LL
1008
+ # define ShiftICMPcode 16
1009
+
1010
+ # define OffsetDstTos 3
1011
+ # define MaskDstTos 0x000000ff00000000LL
1012
+ # define ShiftDstTos 32
1013
+
1014
+ # define OffsetDir 3
1015
+ # define MaskDir 0x0000ff0000000000LL
1016
+ # define ShiftDir 40
1017
+
1018
+ # define OffsetMask 3
1019
+ # define MaskSrcMask 0x00ff000000000000LL
1020
+ # define ShiftSrcMask 48
1021
+
1022
+ # define MaskDstMask 0xff00000000000000LL
1023
+ # define ShiftDstMask 56
1024
+ #endif
1025
+
1026
+ // extension 4 / 5
1027
+ uint32_t input; // index 4 0xffff'ffff'0000'0000
1028
+ uint32_t output; // index 4 0x0000'0000'ffff'ffff
1029
+ #ifdef WORDS_BIGENDIAN
1030
+ # define OffsetInOut 4
1031
+ # define MaskInput 0xffffffff00000000LL
1032
+ # define ShiftInput 32
1033
+ # define MaskOutput 0x00000000ffffffffLL
1034
+ # define ShiftOutput 0
1035
+
1036
+ #else
1037
+ # define OffsetInOut 4
1038
+ # define MaskInput 0x00000000ffffffffLL
1039
+ # define ShiftInput 0
1040
+ # define MaskOutput 0xffffffff00000000LL
1041
+ # define ShiftOutput 32
1042
+ #endif
1043
+
1044
+ // extension 6 / 7
1045
+ uint32_t srcas; // index 5 0xffff'ffff'0000'0000
1046
+ uint32_t dstas; // index 5 0x0000'0000'ffff'ffff
1047
+ #ifdef WORDS_BIGENDIAN
1048
+ # define OffsetAS 5
1049
+ # define MaskSrcAS 0xffffffff00000000LL
1050
+ # define ShiftSrcAS 32
1051
+ # define MaskDstAS 0x00000000ffffffffLL
1052
+ # define ShiftDstAS 0
1053
+
1054
+ #else
1055
+ # define OffsetAS 5
1056
+ # define MaskSrcAS 0x00000000ffffffffLL
1057
+ # define ShiftSrcAS 0
1058
+ # define MaskDstAS 0xffffffff00000000LL
1059
+ # define ShiftDstAS 32
1060
+ #endif
1061
+
1062
+
1063
+ // IP address block
1064
+ union {
1065
+ struct _ipv4_s {
1066
+ #ifdef WORDS_BIGENDIAN
1067
+ uint32_t fill1[3]; // <empty> index 6 0xffff'ffff'ffff'ffff
1068
+ // <empty> index 7 0xffff'ffff'0000'0000
1069
+ uint32_t srcaddr; // srcaddr index 7 0x0000'0000'ffff'ffff
1070
+ uint32_t fill2[3]; // <empty> index 8 0xffff'ffff'ffff'ffff
1071
+ // <empty> index 9 0xffff'ffff'0000'0000
1072
+ uint32_t dstaddr; // dstaddr index 9 0x0000'0000'ffff'ffff
1073
+ #else
1074
+ uint32_t fill1[2]; // <empty> index 6 0xffff'ffff'ffff'ffff
1075
+ uint32_t srcaddr; // srcaddr index 7 0xffff'ffff'0000'0000
1076
+ uint32_t fill2; // <empty> index 7 0x0000'0000'ffff'ffff
1077
+ uint32_t fill3[2]; // <empty> index 8 0xffff'ffff'ffff'ffff
1078
+ uint32_t dstaddr; // dstaddr index 9 0xffff'ffff'0000'0000
1079
+ uint32_t fill4; // <empty> index 9 0xffff'ffff'0000'0000
1080
+ #endif
1081
+ } _v4;
1082
+ struct _ipv6_s {
1083
+ uint64_t srcaddr[2]; // srcaddr[0-1] index 6 0xffff'ffff'ffff'ffff
1084
+ // srcaddr[2-3] index 7 0xffff'ffff'ffff'ffff
1085
+ uint64_t dstaddr[2]; // dstaddr[0-1] index 8 0xffff'ffff'ffff'ffff
1086
+ // dstaddr[2-3] index 9 0xffff'ffff'ffff'ffff
1087
+ } _v6;
1088
+ } ip_union;
1089
+
1090
+ #ifdef WORDS_BIGENDIAN
1091
+ # define OffsetSrcIPv4 7
1092
+ # define MaskSrcIPv4 0x00000000ffffffffLL
1093
+ # define ShiftSrcIPv4 0
1094
+
1095
+ # define OffsetDstIPv4 9
1096
+ # define MaskDstIPv4 0x00000000ffffffffLL
1097
+ # define ShiftDstIPv4 0
1098
+
1099
+ # define OffsetSrcIPv6a 6
1100
+ # define OffsetSrcIPv6b 7
1101
+ # define OffsetDstIPv6a 8
1102
+ # define OffsetDstIPv6b 9
1103
+ # define MaskIPv6 0xffffffffffffffffLL
1104
+ # define ShiftIPv6 0
1105
+
1106
+ #else
1107
+ # define OffsetSrcIPv4 6
1108
+ # define MaskSrcIPv4 0xffffffff00000000LL
1109
+ # define ShiftSrcIPv4 32
1110
+
1111
+ # define OffsetDstIPv4 8
1112
+ # define MaskDstIPv4 0xffffffff00000000LL
1113
+ # define ShiftDstIPv4 32
1114
+
1115
+ # define OffsetSrcIPv6a 6
1116
+ # define OffsetSrcIPv6b 7
1117
+ # define OffsetDstIPv6a 8
1118
+ # define OffsetDstIPv6b 9
1119
+ # define MaskIPv6 0xffffffffffffffffLL
1120
+ # define ShiftIPv6 0
1121
+ #endif
1122
+
1123
+
1124
+ // counter block - expanded to 8 bytes
1125
+ uint64_t dPkts; // index 10 0xffff'ffff'ffff'ffff
1126
+ # define OffsetPackets 10
1127
+ # define MaskPackets 0xffffffffffffffffLL
1128
+ # define ShiftPackets 0
1129
+
1130
+ uint64_t dOctets; // index 11 0xffff'ffff'ffff'ffff
1131
+ # define OffsetBytes 11
1132
+ # define MaskBytes 0xffffffffffffffffLL
1133
+ # define ShiftBytes 0
1134
+
1135
+ // extension 9 / 10
1136
+ ip_addr_t ip_nexthop; // ipv4 index 13 0x0000'0000'ffff'ffff
1137
+ // ipv6 index 12 0xffff'ffff'ffff'ffff
1138
+ // ipv6 index 13 0xffff'ffff'ffff'ffff
1139
+
1140
+ #ifdef WORDS_BIGENDIAN
1141
+ # define OffsetNexthopv4 13
1142
+ # define MaskNexthopv4 0x00000000ffffffffLL
1143
+ # define ShiftNexthopv4 0
1144
+
1145
+ # define OffsetNexthopv6a 12
1146
+ # define OffsetNexthopv6b 13
1147
+ // MaskIPv6 and ShiftIPv6 already defined
1148
+
1149
+ #else
1150
+ # define OffsetNexthopv4 13
1151
+ # define MaskNexthopv4 0xffffffff00000000LL
1152
+ # define ShiftNexthopv4 0
1153
+
1154
+ # define OffsetNexthopv6a 12
1155
+ # define OffsetNexthopv6b 13
1156
+ #endif
1157
+
1158
+ // extension 11 / 12
1159
+ ip_addr_t bgp_nexthop; // ipv4 index 15 0x0000'0000'ffff'ffff
1160
+ // ipv6 index 14 0xffff'ffff'ffff'ffff
1161
+ // ipv6 index 15 0xffff'ffff'ffff'ffff
1162
+
1163
+ #ifdef WORDS_BIGENDIAN
1164
+ # define OffsetBGPNexthopv4 15
1165
+ # define MaskBGPNexthopv4 0x00000000ffffffffLL
1166
+ # define ShiftBGPNexthopv4 0
1167
+
1168
+ # define OffsetBGPNexthopv6a 14
1169
+ # define OffsetBGPNexthopv6b 15
1170
+ // MaskIPv6 and ShiftIPv6 already defined
1171
+
1172
+ #else
1173
+ # define OffsetBGPNexthopv4 15
1174
+ # define MaskBGPNexthopv4 0xffffffff00000000LL
1175
+ # define ShiftBGPNexthopv4 0
1176
+
1177
+ # define OffsetBGPNexthopv6a 14
1178
+ # define OffsetBGPNexthopv6b 15
1179
+ #endif
1180
+
1181
+ // extension 13
1182
+ uint16_t src_vlan; // index 16 0xffff'0000'0000'0000
1183
+ uint16_t dst_vlan; // index 16 0x0000'ffff'0000'0000
1184
+ uint32_t fill1; // align 64bit word
1185
+
1186
+ #ifdef WORDS_BIGENDIAN
1187
+ # define OffsetVlan 16
1188
+ # define MaskSrcVlan 0xffff000000000000LL
1189
+ # define ShiftSrcVlan 48
1190
+
1191
+ # define MaskDstVlan 0x0000ffff00000000LL
1192
+ # define ShiftDstVlan 32
1193
+
1194
+ #else
1195
+ # define OffsetVlan 16
1196
+ # define MaskSrcVlan 0x000000000000ffffLL
1197
+ # define ShiftSrcVlan 0
1198
+
1199
+ # define MaskDstVlan 0x00000000ffff0000LL
1200
+ # define ShiftDstVlan 16
1201
+ #endif
1202
+
1203
+ // extension 14 / 15
1204
+ uint64_t out_pkts; // index 17 0xffff'ffff'ffff'ffff
1205
+ # define OffsetOutPackets 17
1206
+ // MaskPackets and ShiftPackets already defined
1207
+
1208
+ // extension 16 / 17
1209
+ uint64_t out_bytes; // index 18 0xffff'ffff'ffff'ffff
1210
+ # define OffsetOutBytes 18
1211
+
1212
+ // extension 18 / 19
1213
+ uint64_t aggr_flows; // index 19 0xffff'ffff'ffff'ffff
1214
+ # define OffsetAggrFlows 19
1215
+ # define MaskFlows 0xffffffffffffffffLL
1216
+
1217
+ // extension 20
1218
+ uint64_t in_src_mac; // index 20 0xffff'ffff'ffff'ffff
1219
+ # define OffsetInSrcMAC 20
1220
+ # define MaskMac 0xffffffffffffffffLL
1221
+
1222
+ // extension 20
1223
+ uint64_t out_dst_mac; // index 21 0xffff'ffff'ffff'ffff
1224
+ # define OffsetOutDstMAC 21
1225
+
1226
+ // extension 21
1227
+ uint64_t in_dst_mac; // index 22 0xffff'ffff'ffff'ffff
1228
+ # define OffsetInDstMAC 22
1229
+
1230
+ // extension 21
1231
+ uint64_t out_src_mac; // index 23 0xffff'ffff'ffff'ffff
1232
+ # define OffsetOutSrcMAC 23
1233
+
1234
+ // extension 22
1235
+ uint32_t mpls_label[10];
1236
+ # define OffsetMPLS12 24
1237
+ # define OffsetMPLS34 25
1238
+ # define OffsetMPLS56 26
1239
+ # define OffsetMPLS78 27
1240
+ # define OffsetMPLS910 28
1241
+
1242
+ #ifdef WORDS_BIGENDIAN
1243
+ # define MaskMPLSlabelOdd 0x00fffff000000000LL
1244
+ # define ShiftMPLSlabelOdd 36
1245
+ # define MaskMPLSexpOdd 0x0000000e00000000LL
1246
+ # define ShiftMPLSexpOdd 33
1247
+
1248
+ # define MaskMPLSlabelEven 0x0000000000fffff0LL
1249
+ # define ShiftMPLSlabelEven 4
1250
+ # define MaskMPLSexpEven 0x000000000000000eLL
1251
+ # define ShiftMPLSexpEven 1
1252
+ #else
1253
+ # define MaskMPLSlabelOdd 0x000000000000fff0LL
1254
+ # define ShiftMPLSlabelOdd 4
1255
+ # define MaskMPLSexpOdd 0x000000000000000eLL
1256
+ # define ShiftMPLSexpOdd 1
1257
+
1258
+ # define MaskMPLSlabelEven 0x00fffff000000000LL
1259
+ # define ShiftMPLSlabelEven 36
1260
+ # define MaskMPLSexpEven 0x0000000e00000000LL
1261
+ # define ShiftMPLSexpEven 33
1262
+
1263
+ #endif
1264
+
1265
+ // extension 23 / 24
1266
+ ip_addr_t ip_router; // ipv4 index 30 0x0000'0000'ffff'ffff
1267
+ // ipv6 index 29 0xffff'ffff'ffff'ffff
1268
+ // ipv6 index 30 0xffff'ffff'ffff'ffff
1269
+
1270
+ #ifdef WORDS_BIGENDIAN
1271
+ # define OffsetRouterv4 30
1272
+ # define MaskRouterv4 0x00000000ffffffffLL
1273
+ # define ShiftRouterv4 0
1274
+
1275
+ # define OffsetRouterv6a 29
1276
+ # define OffsetRouterv6b 30
1277
+ // MaskIPv6 and ShiftIPv6 already defined
1278
+
1279
+ #else
1280
+ # define OffsetRouterv4 30
1281
+ # define MaskRouterv4 0xffffffff00000000LL
1282
+ # define ShiftRouterv4 0
1283
+
1284
+ # define OffsetRouterv6a 29
1285
+ # define OffsetRouterv6b 30
1286
+ #endif
1287
+
1288
+ // extension 25
1289
+ uint16_t fill; // fill index 31 0xffff'0000'0000'0000
1290
+ uint8_t engine_type; // type index 31 0x0000'ff00'0000'0000
1291
+ uint8_t engine_id; // ID index 31 0x0000'00ff'0000'0000
1292
+
1293
+ # define OffsetRouterID 31
1294
+ #ifdef WORDS_BIGENDIAN
1295
+ # define MaskEngineType 0x0000FF0000000000LL
1296
+ # define ShiftEngineType 40
1297
+ # define MaskEngineID 0x000000FF00000000LL
1298
+ # define ShiftEngineID 32
1299
+
1300
+ #else
1301
+ # define MaskEngineType 0x0000000000FF0000LL
1302
+ # define ShiftEngineType 16
1303
+ # define MaskEngineID 0x00000000FF000000LL
1304
+ # define ShiftEngineID 24
1305
+ #endif
1306
+
1307
+ /* possible user extensions may fit here
1308
+ * - Put each extension into its own #ifdef
1309
+ * - Define the base offset for the user extension as reference to the first object
1310
+ * - Refer to this base offset for each of the values in the master record for the extension
1311
+ * - make sure the extension is 64bit aligned
1312
+ * - The user extension must be independant of the number of user extensions already defined
1313
+ * - the extension map must be updated accordingly
1314
+ */
1315
+
1316
+ #ifdef USER_EXTENSION_1
1317
+ uint64_t u64_1;
1318
+ # define Offset_BASE_U1 offsetof(master_record_t, u64_1)
1319
+ # define OffsetUser1_u64 Offset_BASE_U1
1320
+
1321
+ uint32_t u32_1;
1322
+ uint32_t u32_2;
1323
+ # define OffsetUser1_u32_1 Offset_BASE_U1 + 8
1324
+ # define MaskUser1_u32_1 0xffffffff00000000LL
1325
+ # define MaskUser1_u32_2 0x00000000ffffffffLL
1326
+
1327
+ #endif
1328
+
1329
+ // last entry in master record
1330
+ extension_map_t *map_ref;
1331
+ } master_record_t;
1332
+
1333
+ #define AnyMask 0xffffffffffffffffLL
1334
+
1335
+
1336
+ // convenience type conversion record
1337
+ typedef struct type_mask_s {
1338
+ union {
1339
+ uint8_t val8[8];
1340
+ uint16_t val16[4];
1341
+ uint32_t val32[2];
1342
+ uint64_t val64;
1343
+ } val;
1344
+ } type_mask_t;
1345
+
1346
+ /*
1347
+ * offset translation table
1348
+ * In netflow v9 values may have a different length, and may or may not be present.
1349
+ * The commmon information ( see data_block_record_t ) is expected to be present
1350
+ * unconditionally, and has a fixed size. IP addrs as well as counters for packets and
1351
+ * bytes are expexted to exist as well, but may be variable in size. Further information
1352
+ * may or may not be present, according the flags. See flags
1353
+ * To cope with this situation, the offset translation table gives the offset into an
1354
+ * uint32_t array at which offset the requested value start.
1355
+ *
1356
+ * index:
1357
+ * 0: dstip
1358
+ * for IPv4 netflow v5/v7 10
1359
+ * 1: dPkts
1360
+ * for IPv4 netflow v5/v7 11
1361
+ * 2: dOctets
1362
+ * for IPv4 netflow v5/v7 12
1363
+ */
1364
+
1365
+
1366
+ #ifdef COMPAT15
1367
+ /*
1368
+ * Data block type 1 compatibility
1369
+ */
1370
+
1371
+ typedef struct common_record_v1_s {
1372
+ // the head of each data record
1373
+ uint32_t flags;
1374
+ uint16_t size;
1375
+ uint16_t exporter_ref;
1376
+ uint16_t msec_first;
1377
+ uint16_t msec_last;
1378
+ uint32_t first;
1379
+ uint32_t last;
1380
+
1381
+ uint8_t dir;
1382
+ uint8_t tcp_flags;
1383
+ uint8_t prot;
1384
+ uint8_t tos;
1385
+ uint16_t input;
1386
+ uint16_t output;
1387
+ uint16_t srcport;
1388
+ uint16_t dstport;
1389
+ uint16_t srcas;
1390
+ uint16_t dstas;
1391
+ uint8_t data[4]; // .. more data below
1392
+ } common_record_v1_t;
1393
+
1394
+ #endif
1395
+
1396
+ // a few handy shortcuts
1397
+ #define FILE_IS_COMPRESSED(n) ((n)->file_header->flags & FLAG_COMPRESSED)
1398
+ #define HAS_EXTENDED_STAT(n) ((n)->file_header->flags & FLAG_EXTENDED_STATS)
1399
+ #define IP_ANONYMIZED(n) ((n)->file_header->flags & FLAG_ANONYMIZED)
1400
+
1401
+ void SumStatRecords(stat_record_t *s1, stat_record_t *s2);
1402
+
1403
+ nffile_t *OpenFile(char *filename, nffile_t *nffile);
1404
+
1405
+ nffile_t *OpenNewFile(char *filename, nffile_t *nffile, int compressed, int anonymized, char *ident);
1406
+
1407
+ nffile_t *AppendFile(char *filename);
1408
+
1409
+ int ChangeIdent(char *filename, char *Ident);
1410
+
1411
+ void PrintStat(stat_record_t *s);
1412
+
1413
+ void QueryFile(char *filename);
1414
+
1415
+ stat_record_t *GetStatRecord(char *filename, stat_record_t *stat_record);
1416
+
1417
+ nffile_t *DisposeFile(nffile_t *nffile);
1418
+
1419
+ void CloseFile(nffile_t *nffile);
1420
+
1421
+ int CloseUpdateFile(nffile_t *nffile, char *ident);
1422
+
1423
+ int ReadBlock(nffile_t *nffile);
1424
+
1425
+ int WriteBlock(nffile_t *nffile);
1426
+
1427
+ int WriteExtraBlock(nffile_t *nffile, data_block_header_t *block_header);
1428
+
1429
+ void UnCompressFile(char * filename);
1430
+
1431
+ void ExpandRecord_v1(common_record_t *input_record,master_record_t *output_record );
1432
+
1433
+
1434
+ #ifdef COMPAT15
1435
+ void Convert_v1_to_v2(void *mem);
1436
+ #endif
1437
+
1438
+ #endif //_NFFILE_H
1439
+