forked from phaag/nfdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_fmt.c
2430 lines (1893 loc) · 92.7 KB
/
output_fmt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009-2024, 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.
*
*/
#include "output_fmt.h"
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include "dns/dns.h"
#include "ifvrf.h"
#include "ja3/ja3.h"
#include "maxmind.h"
#include "nbar.h"
#include "nfdump.h"
#include "nffile.h"
#include "nfxV3.h"
#include "output_util.h"
#include "userio.h"
#include "util.h"
typedef void (*string_function_t)(FILE *, recordHandle_t *);
static struct token_list_s {
string_function_t string_function; // function printing result to stream
char *string_buffer; // buffer for static output string
} *token_list = NULL;
static int max_token_index = 0;
static int token_index = 0;
#define BLOCK_SIZE 32
static char **format_list = NULL; // ordered list of all individual strings formatting the output line
static int max_format_index = 0;
static int do_tag = 0;
static int long_v6 = 0;
static int printPlain = 0;
static double duration = 0;
#define IP_STRING_LEN (INET6_ADDRSTRLEN)
#define STRINGSIZE 10240
static char header_string[STRINGSIZE] = {'\0'};
// tag
static char tag_string[2] = {'\0'};
/* prototypes */
static char *ICMP_Port_decode(EXgenericFlow_t *genericFlow);
static inline uint32_t ApplyV4NetMaskBits(uint32_t ip, uint32_t maskBits);
static inline uint64_t *ApplyV6NetMaskBits(uint64_t *ip, uint32_t maskBits);
static void InitFormatParser(void);
static void AddToken(int index, char *s);
static void String_Version(FILE *stream, recordHandle_t *recordHandle);
static void String_FlowCount(FILE *stream, recordHandle_t *recordHandle);
static void String_FirstSeen(FILE *stream, recordHandle_t *recordHandle);
static void String_LastSeen(FILE *stream, recordHandle_t *recordHandle);
static void String_Received(FILE *stream, recordHandle_t *recordHandle);
static void String_FirstSeenRaw(FILE *stream, recordHandle_t *recordHandle);
static void String_LastSeenRaw(FILE *stream, recordHandle_t *recordHandle);
static void String_ReceivedRaw(FILE *stream, recordHandle_t *recordHandle);
static void String_Duration(FILE *stream, recordHandle_t *recordHandle);
static void String_Duration_Seconds(FILE *stream, recordHandle_t *recordHandle);
static void String_Protocol(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcAddr(FILE *stream, recordHandle_t *recordHandle);
static void String_DstAddr(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcGeoAddr(FILE *stream, recordHandle_t *recordHandle);
static void String_DstGeoAddr(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcAddrPort(FILE *stream, recordHandle_t *recordHandle);
static void String_DstAddrPort(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcAddrGeoPort(FILE *stream, recordHandle_t *recordHandle);
static void String_DstAddrGeoPort(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcNet(FILE *stream, recordHandle_t *recordHandle);
static void String_DstNet(FILE *stream, recordHandle_t *recordHandle);
static void String_NextHop(FILE *stream, recordHandle_t *recordHandle);
static void String_BGPNextHop(FILE *stream, recordHandle_t *recordHandle);
static void String_RouterIP(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcPort(FILE *stream, recordHandle_t *recordHandle);
static void String_DstPort(FILE *stream, recordHandle_t *recordHandle);
static void String_ICMP_code(FILE *stream, recordHandle_t *recordHandle);
static void String_ICMP_type(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcAS(FILE *stream, recordHandle_t *recordHandle);
static void String_DstAS(FILE *stream, recordHandle_t *recordHandle);
static void String_NextAS(FILE *stream, recordHandle_t *recordHandle);
static void String_PrevAS(FILE *stream, recordHandle_t *recordHandle);
static void String_Input(FILE *stream, recordHandle_t *recordHandle);
static void String_InputName(FILE *stream, recordHandle_t *recordHandle);
static void String_Output(FILE *stream, recordHandle_t *recordHandle);
static void String_OutputName(FILE *stream, recordHandle_t *recordHandle);
static void String_InPackets(FILE *stream, recordHandle_t *recordHandle);
static void String_OutPackets(FILE *stream, recordHandle_t *recordHandle);
static void String_InBytes(FILE *stream, recordHandle_t *recordHandle);
static void String_OutBytes(FILE *stream, recordHandle_t *recordHandle);
static void String_Flows(FILE *stream, recordHandle_t *recordHandle);
static void String_Tos(FILE *stream, recordHandle_t *recordHandle);
static void String_Dir(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcTos(FILE *stream, recordHandle_t *recordHandle);
static void String_DstTos(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcMask(FILE *stream, recordHandle_t *recordHandle);
static void String_DstMask(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcVlan(FILE *stream, recordHandle_t *recordHandle);
static void String_DstVlan(FILE *stream, recordHandle_t *recordHandle);
static void String_FwdStatus(FILE *stream, recordHandle_t *recordHandle);
static void String_BiFlowDir(FILE *stream, recordHandle_t *recordHandle);
static void String_FlowEndReason(FILE *stream, recordHandle_t *recordHandle);
static void String_Flags(FILE *stream, recordHandle_t *recordHandle);
static void String_InSrcMac(FILE *stream, recordHandle_t *recordHandle);
static void String_OutDstMac(FILE *stream, recordHandle_t *recordHandle);
static void String_InDstMac(FILE *stream, recordHandle_t *recordHandle);
static void String_OutSrcMac(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_1(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_2(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_3(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_4(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_5(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_6(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_7(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_8(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_9(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLS_10(FILE *stream, recordHandle_t *recordHandle);
static void String_MPLSs(FILE *stream, recordHandle_t *recordHandle);
static void String_Engine(FILE *stream, recordHandle_t *recordHandle);
static void String_Label(FILE *stream, recordHandle_t *recordHandle);
static void String_ClientLatency(FILE *stream, recordHandle_t *recordHandle);
static void String_ServerLatency(FILE *stream, recordHandle_t *recordHandle);
static void String_AppLatency(FILE *stream, recordHandle_t *recordHandle);
static void String_bps(FILE *stream, recordHandle_t *recordHandle);
static void String_pps(FILE *stream, recordHandle_t *recordHandle);
static void String_bpp(FILE *stream, recordHandle_t *recordHandle);
static void String_ExpSysID(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcCountry(FILE *stream, recordHandle_t *recordHandle);
static void String_DstCountry(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcLocation(FILE *stream, recordHandle_t *recordHandle);
static void String_DstLocation(FILE *stream, recordHandle_t *recordHandle);
static void String_SrcASorganisation(FILE *stream, recordHandle_t *recordHandle);
static void String_DstASorganisation(FILE *stream, recordHandle_t *recordHandle);
static void String_inPayload(FILE *stream, recordHandle_t *recordHandle);
static void String_outPayload(FILE *stream, recordHandle_t *recordHandle);
static void String_nbarID(FILE *stream, recordHandle_t *recordHandle);
static void String_nbarName(FILE *stream, recordHandle_t *recordHandle);
static void String_ja3(FILE *stream, recordHandle_t *recordHandle);
static void String_sniName(FILE *stream, recordHandle_t *recordHandle);
static void String_observationDomainID(FILE *stream, recordHandle_t *recordHandle);
static void String_observationPointID(FILE *stream, recordHandle_t *recordHandle);
static void String_ivrf(FILE *stream, recordHandle_t *recordHandle);
static void String_ivrfName(FILE *stream, recordHandle_t *recordHandle);
static void String_evrf(FILE *stream, recordHandle_t *recordHandle);
static void String_evrfName(FILE *stream, recordHandle_t *recordHandle);
static void String_NewLine(FILE *stream, recordHandle_t *recordHandle);
static void String_pfIfName(FILE *stream, recordHandle_t *recordHandle);
static void String_pfAction(FILE *stream, recordHandle_t *recordHandle);
static void String_pfReason(FILE *stream, recordHandle_t *recordHandle);
static void String_pfdir(FILE *stream, recordHandle_t *recordHandle);
static void String_pfrule(FILE *stream, recordHandle_t *recordHandle);
static void String_EventTime(FILE *stream, recordHandle_t *recordHandle);
static void String_nfc(FILE *stream, recordHandle_t *recordHandle);
static void String_evt(FILE *stream, recordHandle_t *recordHandle);
static void String_xevt(FILE *stream, recordHandle_t *recordHandle);
static void String_msecEvent(FILE *stream, recordHandle_t *recordHandle);
static void String_iacl(FILE *stream, recordHandle_t *recordHandle);
static void String_eacl(FILE *stream, recordHandle_t *recordHandle);
static void String_xlateSrcAddr(FILE *stream, recordHandle_t *recordHandle);
static void String_xlateDstAddr(FILE *stream, recordHandle_t *recordHandle);
static void String_xlateSrcPort(FILE *stream, recordHandle_t *recordHandle);
static void String_xlateDstPort(FILE *stream, recordHandle_t *recordHandle);
static void String_xlateSrcAddrPort(FILE *stream, recordHandle_t *recordHandle);
static void String_xlateDstAddrPort(FILE *stream, recordHandle_t *recordHandle);
static void String_userName(FILE *stream, recordHandle_t *recordHandle);
static void String_PortBlockStart(FILE *stream, recordHandle_t *recordHandle);
static void String_PortBlockEnd(FILE *stream, recordHandle_t *recordHandle);
static void String_PortBlockStep(FILE *stream, recordHandle_t *recordHandle);
static void String_PortBlockSize(FILE *stream, recordHandle_t *recordHandle);
static struct format_token_list_s {
char *token; // token
int is_address; // is an IP address
char *header; // header line description
string_function_t string_function; // function generation output string
} format_token_list[] = {
// v3 header info
{"%nfv", 0, "Ver", String_Version}, // netflow version
{"%cnt", 0, "Count", String_FlowCount}, // flow count
{"%eng", 0, " engine", String_Engine}, // Engine Type/ID
{"%exp", 0, "Exp ID", String_ExpSysID}, // Exporter SysID
// EXgenericFlowID
{"%tfs", 0, "Date first seen ", String_FirstSeen}, // Start Time - first seen
{"%ts", 0, "Date first seen ", String_FirstSeen}, // Start Time - first seen
{"%tsr", 0, "First seen raw", String_FirstSeenRaw}, // Start Time - first seen, seconds
{"%te", 0, "Date last seen ", String_LastSeen}, // End Time - last seen
{"%ter", 0, "Last seen raw ", String_LastSeenRaw}, // End Time - first seen, seconds
{"%trr", 0, "Received raw ", String_ReceivedRaw}, // Received Time, seconds
{"%tr", 0, "Date flow received ", String_Received}, // Received Time
{"%td", 0, "Duration ", String_Duration}, // Duration
{"%tds", 0, "Duration ", String_Duration_Seconds}, // Duration always in seconds
{"%pkt", 0, " Packets", String_InPackets}, // Packets - default input - compat
{"%ipkt", 0, " In Pkt", String_InPackets}, // In Packets
{"%byt", 0, " Bytes", String_InBytes}, // Bytes - default input - compat
{"%ibyt", 0, " In Byte", String_InBytes}, // In Bytes
{"%sp", 0, "Src Pt", String_SrcPort}, // Source Port
{"%dp", 0, "Dst Pt", String_DstPort}, // Destination Port
{"%it", 0, "ICMP-T", String_ICMP_type}, // ICMP type
{"%ic", 0, "ICMP-C", String_ICMP_code}, // ICMP code
{"%pr", 0, "Proto", String_Protocol}, // Protocol
{"%flg", 0, " Flags", String_Flags}, // TCP Flags
{"%fwd", 0, "Fwd", String_FwdStatus}, // Forwarding Status
{"%tos", 0, " Tos", String_Tos}, // Tos - compat
{"%stos", 0, "STos", String_SrcTos}, // Tos - Src tos
{"%bps", 0, " bps", String_bps}, // bps - bits per second
{"%pps", 0, " pps", String_pps}, // pps - packets per second
{"%bpp", 0, " Bpp", String_bpp}, // bpp - Bytes per package
// EXipv4FlowID EXipv6FlowID
{"%sa", 1, " Src IP Addr", String_SrcAddr}, // Source Address
{"%da", 1, " Dst IP Addr", String_DstAddr}, // Destination Address
{"%sap", 1, " Src IP Addr:Port ", String_SrcAddrPort}, // Source Address:Port
{"%dap", 1, " Dst IP Addr:Port ", String_DstAddrPort}, // Destination Address:Port
// with maxmind geo info
{"%gsap", 1, " Src IP Addr(..):Port ", String_SrcAddrGeoPort}, // Source Address(geo):Port
{"%gdap", 1, " Dst IP Addr(..):Port ", String_DstAddrGeoPort}, // Destination Address(geo):Port
{"%gsa", 1, " Src IP Addr(..)", String_SrcGeoAddr}, // Source Address
{"%gda", 1, " Dst IP Addr(..)", String_DstGeoAddr}, // Destination Address
// EXflowMiscID
{"%in", 0, " Input", String_Input}, // Input Interface num
{"%out", 0, "Output", String_Output}, // Output Interface num
{"%smk", 0, "SMask", String_SrcMask}, // Src mask
{"%dmk", 0, "DMask", String_DstMask}, // Dst mask
{"%dir", 0, "Dir", String_Dir}, // Direction: ingress, egress
{"%dtos", 0, "DTos", String_DstTos}, // Tos - Dst tos
{"%bfd", 0, "Bfd", String_BiFlowDir}, // BiFlow Direction
{"%end", 0, "End", String_FlowEndReason}, // Flow End Reason
//
{"%sn", 1, " Src Network", String_SrcNet}, // Source Address applied source netmask
{"%dn", 1, " Dst Network", String_DstNet}, // Destination Address applied source netmask
{"%inam", 0, " Input interface name", String_InputName}, // Input Interface name
{"%onam", 0, "Output interface name", String_OutputName}, // Output Interface name
// EXcntFlowID
{"%opkt", 0, " Out Pkt", String_OutPackets}, // Out Packets
{"%obyt", 0, "Out Byte", String_OutBytes}, // In Bytes
{"%fl", 0, "Flows", String_Flows}, // Flows
// EXvLanID
{"%svln", 0, "SVlan", String_SrcVlan}, // Src Vlan
{"%dvln", 0, "DVlan", String_DstVlan}, // Dst Vlan
// EXasRoutingID
{"%sas", 0, "Src AS", String_SrcAS}, // Source AS
{"%das", 0, "Dst AS", String_DstAS}, // Destination AS
// EXbgpNextHopV4ID EXbgpNextHopV6ID
{"%nhb", 1, " BGP next-hop IP", String_BGPNextHop}, // BGP Next-hop IP Address
// EXipNextHopV4ID
{"%nh", 1, " Next-hop IP", String_NextHop}, // Next-hop IP Address
// EXipReceivedV4ID EXipReceivedV6ID
{"%ra", 1, " Router IP", String_RouterIP}, // Router IP Address
// EXmplsLabelID
{"%mpls1", 0, " MPLS lbl 1 ", String_MPLS_1}, // MPLS Label 1
{"%mpls2", 0, " MPLS lbl 2 ", String_MPLS_2}, // MPLS Label 2
{"%mpls3", 0, " MPLS lbl 3 ", String_MPLS_3}, // MPLS Label 3
{"%mpls4", 0, " MPLS lbl 4 ", String_MPLS_4}, // MPLS Label 4
{"%mpls5", 0, " MPLS lbl 5 ", String_MPLS_5}, // MPLS Label 5
{"%mpls6", 0, " MPLS lbl 6 ", String_MPLS_6}, // MPLS Label 6
{"%mpls7", 0, " MPLS lbl 7 ", String_MPLS_7}, // MPLS Label 7
{"%mpls8", 0, " MPLS lbl 8 ", String_MPLS_8}, // MPLS Label 8
{"%mpls9", 0, " MPLS lbl 9 ", String_MPLS_9}, // MPLS Label 9
{"%mpls10", 0, " MPLS lbl 10", String_MPLS_10}, // MPLS Label 10
{"%mpls", 0,
" MPLS labels 1-10 "
" ",
String_MPLSs}, // All MPLS labels
// EXmacAddrID
{"%ismc", 0, " In src MAC Addr", String_InSrcMac}, // Input Src Mac Addr
{"%odmc", 0, " Out dst MAC Addr", String_OutDstMac}, // Output Dst Mac Addr
{"%idmc", 0, " In dst MAC Addr", String_InDstMac}, // Input Dst Mac Addr
{"%osmc", 0, " Out src MAC Addr", String_OutSrcMac}, // Output Src Mac Addr
// EXasAdjacentID
{"%nas", 0, "Next AS", String_NextAS}, // Next AS
{"%pas", 0, "Prev AS", String_PrevAS}, // Previous AS
// EXlatencyID - latency extension for nfpcapd and nprobe
{"%cl", 0, "C Latency", String_ClientLatency}, // client latency
{"%sl", 0, "S latency", String_ServerLatency}, // server latency
{"%al", 0, "A latency", String_AppLatency}, // app latency
// EXsamplerInfoID
// EXnselCommonID & EXnelCommonID
{"%tevt", 0, "Event time ", String_EventTime}, // NSEL Flow start time
{"%msec", 0, " Event Time", String_msecEvent}, // NSEL event time in msec
{"%evt", 0, " Event", String_evt}, // NSEL event
// for v.1.6.10 compatibility, keep NEL specific addr/port format tokens
{"%nevt", 0, " Event", String_evt}, // NAT event
// EXnselCommonID
{"%nfc", 0, " Conn-ID", String_nfc}, // NSEL connection ID
{"%xevt", 0, " XEvent", String_xevt}, // NSEL xevent
// EXnselXlateIPv4ID EXnselXlateIPv6ID
// ASA Firewall
{"%xsa", 0, " X-late Src IP", String_xlateSrcAddr}, // NSEL XLATE src IP
{"%xda", 0, " X-late Dst IP", String_xlateDstAddr}, // NSEL XLATE dst IP
{"%xsap", 1, " X-Src IP Addr:Port ", String_xlateSrcAddrPort}, // NSEL Xlate Source Address:Port
{"%xdap", 1, " X-Src IP Addr:Port ", String_xlateDstAddrPort}, // NSEL Xlate Destination Address:Port
// NAT devices
{"%nsa", 0, " X-late Src IP", String_xlateSrcAddr}, // NAT XLATE src IP
{"%nda", 0, " X-late Dst IP", String_xlateDstAddr}, // NAT XLATE dst IP
{"%nsap", 1, " X-Src IP Addr:Port ", String_xlateSrcAddrPort}, // NAT Xlate Source Address:Port
{"%ndap", 1, " X-Dst IP Addr:Port ", String_xlateDstAddrPort}, // NAT Xlate Destination Address:Port
// EXnselXlatePortID
// ASA Firewall
{"%xsp", 0, "XsPort", String_xlateSrcPort}, // NSEL XLATE src port
{"%xdp", 0, "XdPort", String_xlateDstPort}, // NSEL SLATE dst port
// NAT devices
{"%nsp", 0, "XsPort", String_xlateSrcPort}, // NAT XLATE src port
{"%ndp", 0, "XdPort", String_xlateDstPort}, // NAT SLATE dst port
// EXnselAclID
{"%iacl", 0, "Ingress ACL ", String_iacl}, // NSEL ingress ACL
{"%eacl", 0, "Egress ACL ", String_eacl}, // NSEL egress ACL
// EXnselUserID
{"%uname", 0, "UserName", String_userName}, // NSEL user name
// EXnelXlatePortID - Port block allocation
{"%pbstart", 0, "Pb-Start", String_PortBlockStart}, // Port block start
{"%pbend", 0, "Pb-End", String_PortBlockEnd}, // Port block end
{"%pbstep", 0, "Pb-Step", String_PortBlockStep}, // Port block step
{"%pbsize", 0, "Pb-Size", String_PortBlockSize}, // Port block size
// EXnbarAppID
{"%nbid", 0, "nbar ID", String_nbarID}, // nbar ID
{"%nbnam", 0, "nbar name", String_nbarName}, // nbar Name
// EXinPayloadID
{"%ipl", 0, "Input Payload", String_inPayload}, // in payload
// EXoutPayloadID
{"%opl", 0, "Output Payload", String_outPayload}, // out payload
// EXtunIPv4ID EXtunIPv6ID
// EXobservationID
{"%odid", 0, "obsDomainID", String_observationDomainID}, // observation domainID
{"%opid", 0, " obsPointID", String_observationPointID}, // observation pointID
// EXinmonMetaID
// EXvrfID
{"%vrf", 0, " I-VRF-ID", String_ivrf}, // ingress vrf ID - compatible
{"%ivrf", 0, " I-VRF-ID", String_ivrf}, // ingress vrf ID
{"%ivrfnam", 0, " I-VRF-Name", String_ivrfName}, // ingress vrf name
{"%evrf", 0, " E-VRF-ID", String_evrf}, // egress vrf ID
{"%evrfnam", 0, " E-VRF-Name", String_evrfName}, // egress vrf name
// EXpfinfoID
{"%pfifn", 0, "interface", String_pfIfName}, // pflog ifname
{"%pfact", 0, "action", String_pfAction}, // pflog action
{"%pfrea", 0, "reason", String_pfReason}, // pflog reason
{"%pfdir", 0, "dir", String_pfdir}, // pflog direction
{"%pfrule", 0, "rule", String_pfrule}, // pflog rule
// EXlocal
{"%ja3", 0, " ja3", String_ja3}, // ja3
{"%sni", 0, "sni name", String_sniName}, // TLS sni Name
{"%sc", 0, "SC", String_SrcCountry}, // src IP 2 letter country code
{"%dc", 0, "DC", String_DstCountry}, // dst IP 2 letter country code
{"%sloc", 0, "Src IP location info", String_SrcLocation}, // src IP geo location info
{"%dloc", 0, "Dst IP location info", String_DstLocation}, // dst IP geo location info
{"%sasn", 0, "Src AS organisation", String_SrcASorganisation}, // src IP AS organistaion string
{"%dasn", 0, "Dst AS organisation", String_DstASorganisation}, // dst IP AS organisation string
{"%lbl", 0, " label", String_Label}, // Flow Label
{"%n", 0, "", String_NewLine}, // \n
{NULL, 0, NULL, NULL}};
/* each of the tokens above must not generate output strings larger than this */
#define MAX_STRING_LENGTH 256
/* functions */
void Setv6Mode(int mode) { long_v6 += mode; }
int Getv6Mode(void) { return long_v6; }
static void ListOutputFormats(void) {
printf("Available format elements:");
for (int i = 0; format_token_list[i].token != NULL; i++) {
if ((i & 0xf) == 0) printf("\n");
printf("%s ", format_token_list[i].token);
}
printf("- See also nfdump(1)\n");
} // End of ListOutputFormats
void fmt_record(FILE *stream, recordHandle_t *recordHandle, int tag) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
EXtunIPv4_t *tunIPv4 = (EXtunIPv4_t *)recordHandle->extensionList[EXtunIPv4ID];
EXtunIPv6_t *tunIPv6 = (EXtunIPv6_t *)recordHandle->extensionList[EXtunIPv6ID];
// if this flow is a tunnel, add a flow line with the tunnel IPs
if (genericFlow && (tunIPv4 || tunIPv6)) {
size_t len = V3HeaderRecordSize + EXgenericFlowSize + EXipv6FlowSize;
void *p = malloc(len);
if (!p) {
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
exit(EXIT_FAILURE);
}
AddV3Header(p, v3TunHeader);
PushExtension(v3TunHeader, EXgenericFlow, tunGenericFlow);
memcpy((void *)tunGenericFlow, (void *)genericFlow, sizeof(EXgenericFlow_t));
if (tunIPv4) {
tunGenericFlow->proto = tunIPv4->tunProto;
PushExtension(v3TunHeader, EXipv4Flow, tunIPv4Flow);
tunIPv4Flow->srcAddr = tunIPv4->tunSrcAddr;
tunIPv4Flow->dstAddr = tunIPv4->tunDstAddr;
} else {
tunGenericFlow->proto = tunIPv6->tunProto;
PushExtension(v3TunHeader, EXipv6Flow, tunIPv6Flow);
tunIPv6Flow->srcAddr[0] = tunIPv6->tunSrcAddr[0];
tunIPv6Flow->srcAddr[1] = tunIPv6->tunSrcAddr[1];
tunIPv6Flow->dstAddr[0] = tunIPv6->tunDstAddr[0];
tunIPv6Flow->dstAddr[1] = tunIPv6->tunDstAddr[1];
}
fmt_record(stream, p, tag);
free(p);
}
do_tag = tag;
tag_string[0] = do_tag ? TAG_CHAR : '\0';
tag_string[1] = '\0';
duration = 0;
if (genericFlow && genericFlow->msecLast) {
duration = (genericFlow->msecLast - genericFlow->msecFirst) / 1000.0;
}
for (int i = 0; i < token_index; i++) {
if (token_list[i].string_function) {
token_list[i].string_function(stream, recordHandle);
}
if (token_list[i].string_buffer) {
fprintf(stream, "%s", token_list[i].string_buffer);
}
}
fprintf(stream, "\n");
} // End of fmt_record
void fmt_prolog(void) {
// header
printf("%s\n", header_string);
} // End of fmt_prolog
void fmt_epilog(void) {
// empty
} // End of fmt_epilog
static void InitFormatParser(void) {
max_format_index = max_token_index = BLOCK_SIZE;
format_list = (char **)calloc(1, max_format_index * sizeof(char *));
token_list = (struct token_list_s *)calloc(1, max_token_index * sizeof(struct token_list_s));
if (!format_list || !token_list) {
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
exit(255);
}
} // End of InitFormatParser
void CondenseV6(char *s) {
size_t len = strlen(s);
char *p, *q;
if (len <= 16) return;
// orig: 2001:620:1000:cafe:20e:35ff:fec0:fed5 len = 37
// condensed: 2001:62..e0:fed5
p = s + 7;
*p++ = '.';
*p++ = '.';
q = s + len - 7;
while (*q) {
*p++ = *q++;
}
*p = 0;
} // End of CondenseV6
static inline uint32_t ApplyV4NetMaskBits(uint32_t ip, uint32_t maskBits) {
uint32_t srcMask = 0xffffffff << (32 - maskBits);
return (ip &= srcMask);
} // End of ApplyV4NetMaskBits
static inline uint64_t *ApplyV6NetMaskBits(uint64_t *ip, uint32_t maskBits) {
static uint64_t net[2];
uint64_t mask;
if (maskBits > 64) {
mask = 0xffffffffffffffffLL << (128 - maskBits);
net[0] = ip[0];
net[1] = ip[1] & mask;
} else {
mask = 0xffffffffffffffffLL << (64 - maskBits);
net[0] = ip[0] & mask;
net[1] = 0;
}
return net;
} // End of ApplyV6NetMaskBits
static void AddToken(int index, char *s) {
if (token_index >= max_token_index) { // no slot available - expand table
max_token_index += BLOCK_SIZE;
token_list = (struct token_list_s *)realloc(token_list, max_token_index * sizeof(struct token_list_s));
if (!token_list) {
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
exit(255);
}
}
if (s == NULL) {
token_list[token_index].string_function = format_token_list[index].string_function;
token_list[token_index].string_buffer = s;
} else {
token_list[token_index].string_function = NULL;
token_list[token_index].string_buffer = s;
}
token_index++;
} // End of AddToken
/*
* expand predefined print format into given format, such as -o fmt "%line %ipl"
*/
static char *RecursiveReplace(char *format, printmap_t *printmap) {
int i = 0;
while (printmap[i].printmode) {
char *s, *r;
// check for printmode string
s = strstr(format, printmap[i].printmode);
if (s && printmap[i].Format && s != format) {
int len = strlen(printmap[i].printmode);
if (!isalpha((int)s[len])) {
s--;
if (s[0] == '%') {
int newlen = strlen(format) + strlen(printmap[i].Format);
r = malloc(newlen);
if (!r) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
exit(255);
}
s[0] = '\0';
snprintf(r, newlen, "%s%s%s", format, printmap[i].Format, &(s[len + 1]));
r[newlen - 1] = '\0';
free(format);
format = r;
}
}
}
i++;
}
return format;
} // End of RecursiveReplace
int ParseOutputFormat(char *format, int plain_numbers, printmap_t *printmap) {
char *c, *s, *h;
int i, remaining;
printPlain = plain_numbers;
InitFormatParser();
s = strdup(format);
if (!s) {
LogError("malloc() allocation error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return 0;
}
s = RecursiveReplace(s, printmap);
c = s;
h = header_string;
*h = '\0';
while (*c) {
if (*c == '%') { // it's a token from format_token_list
i = 0;
remaining = strlen(c);
while (format_token_list[i].token) { // sweep through the list
int len = strlen(format_token_list[i].token);
// a token is separated by either a space, another token, or end of string
if (remaining >= len && !isalnum((int)c[len])) {
// separator found a expected position
char p = c[len]; // save separator;
c[len] = '\0';
if (strncmp(format_token_list[i].token, c, len) == 0) { // token found
AddToken(i, NULL);
if (long_v6 && format_token_list[i].is_address)
snprintf(h, STRINGSIZE - 1 - strlen(header_string), "%23s%s", "", format_token_list[i].header);
else
snprintf(h, STRINGSIZE - 1 - strlen(header_string), "%s", format_token_list[i].header);
h += strlen(h);
c[len] = p;
c += len;
break;
} else {
c[len] = p;
}
}
i++;
}
if (format_token_list[i].token == NULL) {
LogError("Output format parse error at: %s", c);
free(s);
ListOutputFormats();
return 0;
}
} else { // it's a static string
/* a static string goes up to next '%' or end of string */
char *p = strchr(c, '%');
char printFormat[32];
if (p) {
// p points to next '%' token
*p = '\0';
AddToken(0, strdup(c));
snprintf(printFormat, 31, "%%%zus", strlen(c));
printFormat[31] = '\0';
snprintf(h, STRINGSIZE - 1 - strlen(header_string), printFormat, "");
h += strlen(h);
*p = '%';
c = p;
} else {
// static string up to end of format string
AddToken(0, strdup(c));
snprintf(printFormat, 31, "%%%zus", strlen(c));
printFormat[31] = '\0';
snprintf(h, STRINGSIZE - 1 - strlen(header_string), printFormat, "");
h += strlen(h);
*c = '\0';
}
}
}
free(s);
return 1;
} // End of ParseOutputFormat
static char *ICMP_Port_decode(EXgenericFlow_t *genericFlow) {
#define ICMPSTRLEN 16
static char icmpString[ICMPSTRLEN];
icmpString[0] = '\0';
if (genericFlow == NULL) return "0";
if (genericFlow->proto == IPPROTO_ICMP || genericFlow->proto == IPPROTO_ICMPV6) { // ICMP
snprintf(icmpString, ICMPSTRLEN - 1, "%u.%u", genericFlow->icmpType, genericFlow->icmpCode);
} else { // dst port
snprintf(icmpString, ICMPSTRLEN - 1, "%u", genericFlow->dstPort);
}
icmpString[ICMPSTRLEN - 1] = '\0';
return icmpString;
} // End of ICMP_Port_decode
/* functions, which create the individual strings for the output line */
static void String_Version(FILE *stream, recordHandle_t *recordHandle) {
recordHeaderV3_t *recordHeaderV3 = recordHandle->recordHeaderV3;
char *type = "UNKN";
uint8_t nfversion = recordHeaderV3->nfversion;
if (TestFlag(recordHeaderV3->flags, V3_FLAG_EVENT)) {
type = "EVT";
fprintf(stream, "%s%u", type, nfversion);
} else {
if (nfversion != 0) {
if (nfversion & 0x80) {
type = "Sv";
} else if (nfversion & 0x40) {
type = "Pv";
} else {
type = "Nv";
}
fprintf(stream, "%s%u", type, nfversion & 0x0F);
} else {
// compat with previous versions
type = "FLO";
fprintf(stream, "%s", type);
}
}
} // End of String_Version
static void String_FlowCount(FILE *stream, recordHandle_t *recordHandle) {
fprintf(stream, "%5u", recordHandle->flowCount);
} // End of String_FlowCount
static void String_FirstSeen(FILE *stream, recordHandle_t *recordHandle) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
uint64_t msecFirst = genericFlow ? genericFlow->msecFirst : 0;
if (msecFirst) {
time_t tt = msecFirst / 1000LL;
struct tm *ts = localtime(&tt);
char s[128];
strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts);
s[127] = '\0';
fprintf(stream, "%s.%03u", s, (unsigned)(msecFirst % 1000LL));
} else {
fprintf(stream, "%s", "0000-00-00 00:00:00.000");
}
} // End of String_FirstSeen
static void String_LastSeen(FILE *stream, recordHandle_t *recordHandle) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
uint64_t msecLast = genericFlow ? genericFlow->msecLast : 0;
if (msecLast) {
time_t tt = msecLast / 1000LL;
struct tm *ts = localtime(&tt);
char s[128];
strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts);
s[127] = '\0';
fprintf(stream, "%s.%03u", s, (unsigned)(msecLast % 1000LL));
} else {
fprintf(stream, "%s", "0000-00-00 00:00:00.000");
}
} // End of String_LastSeen
static void String_Received(FILE *stream, recordHandle_t *recordHandle) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
uint64_t msecReceived = genericFlow ? genericFlow->msecReceived : 0;
if (msecReceived) {
time_t tt = msecReceived / 1000LL;
struct tm *ts = localtime(&tt);
char s[128];
strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts);
s[127] = '\0';
fprintf(stream, "%s.%03llu", s, msecReceived % 1000LL);
} else {
fprintf(stream, "%s", "0000-00-00 00:00:00.000");
}
} // End of String_Received
static void String_ReceivedRaw(FILE *stream, recordHandle_t *recordHandle) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
uint64_t msecReceived = genericFlow ? genericFlow->msecReceived : 0;
fprintf(stream, "%10llu.%03llu", msecReceived / 1000LL, msecReceived % 1000LL);
} // End of String_ReceivedRaw
static void String_FirstSeenRaw(FILE *stream, recordHandle_t *recordHandle) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
uint64_t msecFirst = genericFlow ? genericFlow->msecFirst : 0;
fprintf(stream, "%10llu.%03llu", msecFirst / 1000LL, msecFirst % 1000LL);
} // End of String_FirstSeenRaw
static void String_LastSeenRaw(FILE *stream, recordHandle_t *recordHandle) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
uint64_t msecLast = genericFlow ? genericFlow->msecLast : 0;
fprintf(stream, "%10llu.%03llu", msecLast / 1000LL, msecLast % 1000LL);
} // End of String_LastSeenRaw
static void String_Payload(FILE *stream, uint8_t *payload, EXgenericFlow_t *genericFlow) {
uint32_t payloadLength = 0;
if (payload) {
elementHeader_t *elementHeader = (elementHeader_t *)(payload - sizeof(elementHeader_t));
payloadLength = elementHeader->length - sizeof(elementHeader_t);
} else {
fprintf(stream, "<no payload>");
return;
}
int max = payloadLength > 256 ? 256 : payloadLength;
if (genericFlow && (genericFlow->srcPort == 53 || genericFlow->dstPort == 53)) {
content_decode_dns(stream, genericFlow->proto, payload, payloadLength);
}
int ascii = 1;
for (int i = 0; i < max; i++) {
if ((payload[i] < ' ' || payload[i] > '~') && payload[i] != '\n' && payload[i] != '\r' && payload[i] != 0x09) {
ascii = 0;
break;
}
}
if (ascii) {
fprintf(stream, "%.*s\n", max, payload);
} else {
DumpHex(stream, payload, max);
}
} // End of String_inPayload
static void String_inPayload(FILE *stream, recordHandle_t *recordHandle) {
uint8_t *inPayload = (uint8_t *)recordHandle->extensionList[EXinPayloadID];
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
String_Payload(stream, inPayload, genericFlow);
} // End of String_inPayload
static void String_outPayload(FILE *stream, recordHandle_t *recordHandle) {
uint8_t *outPayload = (uint8_t *)recordHandle->extensionList[EXoutPayloadID];
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
String_Payload(stream, outPayload, genericFlow);
} // End of String_outPayload
static void String_nbarID(FILE *stream, recordHandle_t *recordHandle) {
uint8_t *nbar = (uint8_t *)recordHandle->extensionList[EXnbarAppID];
union {
uint8_t val8[4];
uint32_t val32;
} pen;
if (nbar == NULL) {
fprintf(stream, "0..0..0");
return;
}
uint32_t nbarAppIDlen = ExtensionLength(nbar);