/
sflowtool.c
6690 lines (6006 loc) · 220 KB
/
sflowtool.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) 2002-2019 InMon Corp. Licensed under the terms of the InMon sFlow licence: */
/* http://www.inmon.com/technology/sflowlicense.txt */
#if defined(__cplusplus)
extern "C" {
#endif
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <setjmp.h>
#include <ctype.h>
#include <search.h>
#include <stdint.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <byteswap.h>
#include <getopt.h>
#include "sflow.h" /* sFlow v5 */
#include "sflow_v2v4.h" /* sFlow v2/4 */
#include "assert.h"
#include "sflow_xdr.h" /* sFlow encode */
#define SPOOFSOURCE 1
#define YES 1
#define NO 0
/* define my own IP header struct - to ease portability */
struct myiphdr
{
uint8_t version_and_headerLen;
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
};
/* ip6 header if no option headers */
struct myip6hdr {
uint8_t version_and_priority;
uint8_t priority_and_label1;
uint8_t label2;
uint8_t label3;
uint16_t payloadLength;
uint8_t nextHeader;
uint8_t ttl;
struct in6_addr saddr;
struct in6_addr daddr;
};
/* same for tcp */
struct mytcphdr
{
uint16_t th_sport; /* source port */
uint16_t th_dport; /* destination port */
uint32_t th_seq; /* sequence number */
uint32_t th_ack; /* acknowledgement number */
uint8_t th_off_and_unused;
uint8_t th_flags;
uint16_t th_win; /* window */
uint16_t th_sum; /* checksum */
uint16_t th_urp; /* urgent pointer */
};
/* and UDP */
struct myudphdr {
uint16_t uh_sport; /* source port */
uint16_t uh_dport; /* destination port */
uint16_t uh_ulen; /* udp length */
uint16_t uh_sum; /* udp checksum */
};
/* and ICMP */
struct myicmphdr
{
uint8_t type; /* message type */
uint8_t code; /* type sub-code */
/* ignore the rest */
};
#ifdef SPOOFSOURCE
#define SPOOFSOURCE_SENDPACKET_SIZE 2000
struct mySendPacket {
struct myiphdr ip;
struct myudphdr udp;
uint8_t data[SPOOFSOURCE_SENDPACKET_SIZE];
};
#endif
/* tcpdump file format */
struct pcap_file_header {
uint32_t magic;
uint16_t version_major;
uint16_t version_minor;
uint32_t thiszone; /* gmt to local correction */
uint32_t sigfigs; /* accuracy of timestamps */
uint32_t snaplen; /* max length saved portion of each pkt */
uint32_t linktype; /* data link type (DLT_*) */
};
struct pcap_pkthdr {
uint32_t ts_sec; /* time stamp - used to be struct timeval, but time_t can be 64 bits now */
uint32_t ts_usec;
uint32_t caplen; /* length of portion present */
uint32_t len; /* length this packet (off wire) */
/* some systems expect to see more information here. For example,
* on some versions of RedHat Linux, there are three extra fields:
* int index;
* unsigned short protocol;
* unsigned char pkt_type;
*/
};
typedef struct _SFForwardingTarget {
struct _SFForwardingTarget *nxt;
struct sockaddr_in addr;
int sock;
} SFForwardingTarget;
typedef struct _SFForwardingTarget6 {
struct _SFForwardingTarget6 *nxt;
struct sockaddr_in6 addr;
int sock;
} SFForwardingTarget6;
typedef union _SFSockAddr {
struct sockaddr_in sa4;
struct sockaddr_in6 sa6;
} SFSockAddr;
typedef enum { SFLFMT_FULL=0,
SFLFMT_PCAP,
SFLFMT_PCAP_DISCARD,
SFLFMT_LINE,
SFLFMT_LINE_CUSTOM,
SFLFMT_NETFLOW,
SFLFMT_FWD,
SFLFMT_CLF,
SFLFMT_SCRIPT,
SFLFMT_JSON,
SFLFMT_SFLOW
} EnumSFLFormat;
#define SA_MAX_PCAP_PKT 65536
#define SA_MAX_SFLOW_PKT_SIZ 65536
#define SA_MAX_FIELDNAME_LEN 64
#define SA_MAX_FIELD_PREFIX 4
#define MAX_STRBUF_LEN 2048
typedef struct {
int cap;
int len;
char str[MAX_STRBUF_LEN];
} SFStr;
typedef enum { SFSCOPE_NONE, SFSCOPE_DATAGRAM, SFSCOPE_SAMPLE } EnumSFScope;
typedef struct _SFFieldList {
int n;
char **fields;
SFStr *values;
/* dynamic info */
char *fieldScope;
int sampleFields;
} SFFieldList;
typedef struct _SFConfig {
/* sflow(R) options */
uint16_t sFlowInputPort;
/* netflow(TM) options */
uint16_t netFlowOutputPort;
SFLAddress netFlowOutputIP;
SFSockAddr netFlowOutputSA;
int netFlowOutputSocket;
uint16_t netFlowPeerAS;
int disableNetFlowScale;
uint16_t netFlowVersion;
/* tcpdump options */
char *readPcapFileName;
FILE *readPcapFile;
struct pcap_file_header readPcapHdr;
int pcapSwap;
/* sFlow-from-pcap generator */
uint32_t pcapSamplingN;
SFDDgram *sFlowDatagram;
uint64_t pcap_uS;
double playback;
uint32_t output_sample_pool;
uint32_t output_sample_seqNo;
int output_sample_skip;
EnumSFLFormat outputFormat;
/* JSON */
int jsonIndent;
int jsonStart;
int jsonListStart;
int outputDepth;
SFFieldList outputFieldList;
EnumSFScope currentFieldScope;
#ifdef SPOOFSOURCE
int spoofSource;
uint16_t ipid;
struct mySendPacket sendPkt;
uint32_t packetLen;
#endif
SFForwardingTarget *forwardingTargets;
SFForwardingTarget6 *forwardingTargets6;
/* vlan filtering */
int gotVlanFilter;
#define FILTER_MAX_VLAN 4096
uint8_t vlanFilter[FILTER_MAX_VLAN + 1];
/* content stripping */
int removeContent;
/* options to restrict IP socket / bind */
int listen4;
int listen6;
int listenControlled;
/* general options */
int keepGoing;
int allowDNS;
} SFConfig;
/* make the options structure global to the program */
static SFConfig sfConfig;
/* define a separate global we can use to construct the common-log-file format */
typedef struct _SFCommonLogFormat {
#define SFLFMT_CLF_MAX_LINE 2000
#define SFLFMT_CLF_MAX_CLIENT_LEN 64
int valid;
char client[SFLFMT_CLF_MAX_CLIENT_LEN];
char http_log[SFLFMT_CLF_MAX_LINE];
} SFCommonLogFormat;
static SFCommonLogFormat sfCLF;
static const char *SFHTTP_method_names[] = { "-", "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT" };
typedef struct _SFSample {
/* the raw pdu */
uint8_t *rawSample;
uint32_t rawSampleLen;
uint8_t *endp;
time_t pcapTimestamp;
time_t readTimestamp;
/* decode cursor */
uint32_t *datap;
/* datagram fields */
SFLAddress sourceIP;
SFLAddress agent_addr;
uint32_t agentSubId;
uint32_t datagramVersion;
uint32_t sysUpTime;
uint32_t sequenceNo;
/* per-element fields */
struct {
uint32_t prefixCount;
char *fieldPrefix[SA_MAX_FIELD_PREFIX];
uint32_t sampleType;
uint32_t elementType;
uint32_t ds_class;
uint32_t ds_index;
/* generic interface counter sample */
SFLIf_counters ifCounters;
/* data-source stream info */
uint32_t samplesGenerated;
uint32_t meanSkipCount;
uint32_t samplePool;
uint32_t dropEvents;
/* the sampled header */
uint32_t sampledPacketSize;
uint32_t packet_data_tag;
uint32_t headerProtocol;
uint8_t *header;
uint32_t headerLen;
uint32_t stripped;
/* header decode */
int gotIPV4;
int gotIPV4Struct;
int offsetToIPV4;
int gotIPV6;
int gotIPV6Struct;
int offsetToIPV6;
int offsetToPayload;
SFLAddress ipsrc;
SFLAddress ipdst;
uint32_t dcd_ipProtocol;
uint32_t dcd_ipTos;
uint32_t dcd_ipTTL;
uint32_t dcd_sport;
uint32_t dcd_dport;
uint32_t dcd_tcpFlags;
uint32_t dcd_flowLabel;
uint32_t ip_fragmentOffset;
uint32_t udp_pduLen;
/* ports */
uint32_t inputPortFormat;
uint32_t outputPortFormat;
uint32_t inputPort;
uint32_t outputPort;
/* ethernet */
uint32_t eth_type;
uint32_t eth_len;
uint8_t eth_src[8];
uint8_t eth_dst[8];
/* vlan */
uint32_t in_vlan;
uint32_t in_priority;
uint32_t internalPriority;
uint32_t out_vlan;
uint32_t out_priority;
int vlanFilterReject;
/* extended data fields */
uint32_t num_extended;
uint32_t extended_data_tag;
#define SASAMPLE_EXTENDED_DATA_SWITCH 1
#define SASAMPLE_EXTENDED_DATA_ROUTER 4
#define SASAMPLE_EXTENDED_DATA_GATEWAY 8
#define SASAMPLE_EXTENDED_DATA_USER 16
#define SASAMPLE_EXTENDED_DATA_URL 32
#define SASAMPLE_EXTENDED_DATA_MPLS 64
#define SASAMPLE_EXTENDED_DATA_NAT 128
#define SASAMPLE_EXTENDED_DATA_MPLS_TUNNEL 256
#define SASAMPLE_EXTENDED_DATA_MPLS_VC 512
#define SASAMPLE_EXTENDED_DATA_MPLS_FTN 1024
#define SASAMPLE_EXTENDED_DATA_MPLS_LDP_FEC 2048
#define SASAMPLE_EXTENDED_DATA_VLAN_TUNNEL 4096
#define SASAMPLE_EXTENDED_DATA_NAT_PORT 8192
/* IP forwarding info */
SFLAddress nextHop;
uint32_t srcMask;
uint32_t dstMask;
/* BGP info */
SFLAddress bgp_nextHop;
uint32_t my_as;
uint32_t src_as;
uint32_t src_peer_as;
uint32_t dst_as_path_len;
uint32_t *dst_as_path;
/* note: version 4 dst as path segments just get printed, not stored here, however
* the dst_peer and dst_as are filled in, since those are used for netflow encoding
*/
uint32_t dst_peer_as;
uint32_t dst_as;
uint32_t communities_len;
uint32_t *communities;
uint32_t localpref;
/* mpls */
SFLAddress mpls_nextHop;
/* nat */
SFLAddress nat_src;
SFLAddress nat_dst;
/* counter blocks */
uint32_t statsSamplingInterval;
uint32_t counterBlockVersion;
} s;
/* exception handler context */
jmp_buf env;
#define ERROUT stderr
#ifdef DEBUG
# define SFABORT(s, r) abort()
# undef ERROUT
# define ERROUT stdout
#else
# define SFABORT(s, r) longjmp((s)->env, (r))
#endif
#define SF_ABORT_EOS 1
#define SF_ABORT_DECODE_ERROR 2
#define SF_ABORT_LENGTH_ERROR 3
} SFSample;
/* Cisco netflow version 5 record format */
typedef struct _NFFlow5 {
uint32_t srcIP;
uint32_t dstIP;
uint32_t nextHop;
uint16_t if_in;
uint16_t if_out;
uint32_t frames;
uint32_t bytes;
uint32_t firstTime;
uint32_t lastTime;
uint16_t srcPort;
uint16_t dstPort;
uint8_t pad1;
uint8_t tcpFlags;
uint8_t ipProto;
uint8_t ipTos;
uint16_t srcAS;
uint16_t dstAS;
uint8_t srcMask; /* No. bits */
uint8_t dstMask; /* No. bits */
uint16_t pad2;
} NFFlow5;
typedef struct _NFFlowHdr5 {
uint16_t version;
uint16_t count;
uint32_t sysUpTime;
uint32_t unixSeconds;
uint32_t unixNanoSeconds;
uint32_t flowSequence;
uint8_t engineType;
uint8_t engineId;
uint16_t sampling_interval;
} NFFlowHdr5;
typedef struct _NFFlowPkt5 {
NFFlowHdr5 hdr;
NFFlow5 flow; /* normally an array, but here we always send just 1 at a time */
} NFFlowPkt5;
/* Cisco NetFlow version 9 format */
/* NetFlow v9/ipfix element ids */
#define ID_SRC_IP 8
#define ID_DST_IP 12
#define ID_NEXT_HOP 15
#define ID_IF_IN 10
#define ID_IF_OUT 14
#define ID_PACKETS 2
#define ID_BYTES 1
#define ID_FIRST_SWITCHED 22
#define ID_LAST_SWITCHED 21
#define ID_SRC_PORT 7
#define ID_DST_PORT 11
#define ID_TCP_FLAGS 6
#define ID_PROTOCOL 4
#define ID_TOS 5
#define ID_SRC_AS 16
#define ID_DST_AS 17
#define ID_SRC_MASK 9
#define ID_DST_MASK 13
#define ID_SAMPLING_INTERVAL 34
#define ID_SRC_IP6 27
#define ID_DST_IP6 28
#define ID_NEXT_HOP_IP6 62
#define ID_FLOW_LABEL_IP6 31
/* NetFlow v9/ipfix element sizes */
#define SZ_SRC_IP 4
#define SZ_DST_IP 4
#define SZ_NEXT_HOP 4
#define SZ_IF_IN 4
#define SZ_IF_OUT 4
#define SZ_PACKETS 4
#define SZ_BYTES 4
#define SZ_FIRST_SWITCHED 4
#define SZ_LAST_SWITCHED 4
#define SZ_SRC_PORT 2
#define SZ_DST_PORT 2
#define SZ_TCP_FLAGS 1
#define SZ_PROTOCOL 1
#define SZ_TOS 1
#define SZ_SRC_AS 4
#define SZ_DST_AS 4
#define SZ_SRC_MASK 1
#define SZ_DST_MASK 1
#define SZ_SAMPLING_INTERVAL 4
#define SZ_SRC_IP6 16
#define SZ_DST_IP6 16
#define SZ_NEXT_HOP_IP6 16
#define SZ_FLOW_LABEL_IP6 4
/* NetFlow v9/ipfix element type */
typedef struct _NFField9 {
uint16_t id;
uint16_t sz;
} __attribute__ ((packed)) NFField9;
/* NetFlow v9/ipfix (id, sz) pairs for each element */
static const NFField9 nfField9[] = {
{ ID_SRC_IP, SZ_SRC_IP },
{ ID_DST_IP, SZ_DST_IP },
{ ID_NEXT_HOP, SZ_NEXT_HOP },
{ ID_IF_IN, SZ_IF_IN },
{ ID_IF_OUT, SZ_IF_OUT },
{ ID_PACKETS, SZ_PACKETS },
{ ID_BYTES, SZ_BYTES },
{ ID_FIRST_SWITCHED, SZ_FIRST_SWITCHED },
{ ID_LAST_SWITCHED, SZ_LAST_SWITCHED },
{ ID_SRC_PORT, SZ_SRC_PORT },
{ ID_DST_PORT, SZ_DST_PORT },
{ ID_TCP_FLAGS, SZ_TCP_FLAGS },
{ ID_PROTOCOL, SZ_PROTOCOL },
{ ID_TOS, SZ_TOS },
{ ID_SRC_AS, SZ_SRC_AS },
{ ID_DST_AS, SZ_DST_AS },
{ ID_SRC_MASK, SZ_SRC_MASK },
{ ID_DST_MASK, SZ_DST_MASK },
{ ID_SAMPLING_INTERVAL, SZ_SAMPLING_INTERVAL }
};
static const NFField9 nfField9_v6[] = {
{ ID_SRC_IP6, SZ_SRC_IP6 },
{ ID_DST_IP6, SZ_DST_IP6 },
{ ID_NEXT_HOP_IP6, SZ_NEXT_HOP_IP6 },
{ ID_IF_IN, SZ_IF_IN },
{ ID_IF_OUT, SZ_IF_OUT },
{ ID_PACKETS, SZ_PACKETS },
{ ID_BYTES, SZ_BYTES },
{ ID_FIRST_SWITCHED, SZ_FIRST_SWITCHED },
{ ID_LAST_SWITCHED, SZ_LAST_SWITCHED },
{ ID_FLOW_LABEL_IP6, SZ_FLOW_LABEL_IP6 },
{ ID_SRC_PORT, SZ_SRC_PORT },
{ ID_DST_PORT, SZ_DST_PORT },
{ ID_TCP_FLAGS, SZ_TCP_FLAGS },
{ ID_PROTOCOL, SZ_PROTOCOL },
{ ID_TOS, SZ_TOS },
{ ID_SRC_AS, SZ_SRC_AS },
{ ID_DST_AS, SZ_DST_AS },
{ ID_SRC_MASK, SZ_SRC_MASK },
{ ID_DST_MASK, SZ_DST_MASK },
{ ID_SAMPLING_INTERVAL, SZ_SAMPLING_INTERVAL }
};
/* The NetFlow v9 flow will be shaped similarly to v5,
* but we move sampling interval from the v5 header into
* the flow dataset and expand the interface field widths. */
typedef struct _NFFlow9 {
uint32_t srcIP;
uint32_t dstIP;
uint32_t nextHop;
uint32_t if_in;
uint32_t if_out;
uint32_t packets;
uint32_t bytes;
uint32_t firstTime;
uint32_t lastTime;
uint16_t srcPort;
uint16_t dstPort;
uint8_t tcpFlags;
uint8_t ipProto;
uint8_t ipTos;
uint32_t srcAS;
uint32_t dstAS;
uint8_t srcMask;
uint8_t dstMask;
uint32_t samplingInterval;
#define NFFLOW9_NUM_ELEMENTS 19
} __attribute__ ((packed)) NFFlow9;
typedef struct _NFFlow9_v6 {
uint32_t srcIP6[4];
uint32_t dstIP6[4];
uint32_t nextHopIP6[4];
uint32_t if_in;
uint32_t if_out;
uint32_t packets;
uint32_t bytes;
uint32_t firstTime;
uint32_t lastTime;
uint32_t flowLabel;
uint16_t srcPort;
uint16_t dstPort;
uint8_t tcpFlags;
uint8_t ipProto;
uint8_t ipTos;
uint32_t srcAS;
uint32_t dstAS;
uint8_t srcMask;
uint8_t dstMask;
uint32_t samplingInterval;
#define NFFLOW9_V6_NUM_ELEMENTS 20
} __attribute__ ((packed)) NFFlow9_v6;
/* NetFlow v9 template flowset */
typedef struct _NFTemplateFlowSet9 {
uint16_t setId;
uint16_t length;
uint16_t templateId;
#define NFTEMPLATE_IPV4 256
#define NFTEMPLATE_IPV6 257
uint16_t fieldCount;
NFField9 field[NFFLOW9_NUM_ELEMENTS];
} __attribute__ ((packed)) NFTemplateFlowSet9;
typedef struct _NFTemplateFlowSet9_v6 {
uint16_t setId;
uint16_t length;
uint16_t templateId;
uint16_t fieldCount;
NFField9 field[NFFLOW9_V6_NUM_ELEMENTS];
} __attribute__ ((packed)) NFTemplateFlowSet9_v6;
/* NetFlow v9 data flowset */
typedef struct _NFDataFlowSet9 {
uint16_t templateId;
uint16_t length;
NFFlow9 flow;
} __attribute__ ((packed)) NFDataFlowSet9;
typedef struct _NFDataFlowSet9_v6 {
uint16_t templateId;
uint16_t length;
NFFlow9_v6 flow;
} __attribute__ ((packed)) NFDataFlowSet9_v6;
/* NetFlow v9 flow packet header */
typedef struct _NFFlowHeader9 {
uint16_t version;
uint16_t count;
uint32_t sysUpTime;
uint32_t unixSeconds;
uint32_t flowSequence;
uint32_t sourceId;
} __attribute__ ((packed)) NFFlowHeader9;
/* NetFlow v9 flow packet */
typedef struct _NFFlowPkt9 {
NFFlowHeader9 hdr;
NFTemplateFlowSet9 tmpl;
NFDataFlowSet9 data;
} __attribute__ ((packed)) NFFlowPkt9;
typedef struct _NFFlowPkt9_v6 {
NFFlowHeader9 hdr;
NFTemplateFlowSet9_v6 tmpl;
NFDataFlowSet9_v6 data;
} __attribute__ ((packed)) NFFlowPkt9_v6;
/* NetFLow packet can be either v5 or v9 or v9v6 */
typedef struct _NFFlowPkt {
union {
NFFlowPkt5 v5;
NFFlowPkt9 v9;
NFFlowPkt9_v6 v9v6;
};
} __attribute__ ((packed)) NFFlowPkt;
/* NetFlow functions to send datagrams */
static void sendNetFlowV5Datagram(SFSample *sample);
static void sendNetFlowV9Datagram(SFSample *sample);
static void sendNetFlowV9V6Datagram(SFSample *sample);
static void (*sendNetFlowDatagram)(SFSample *sample) = sendNetFlowV5Datagram;
static void (*sendNetFlowDatagram_v6)(SFSample *sample) = NULL;
static void readFlowSample_header(SFSample *sample);
static void readFlowSample(SFSample *sample, int expanded);
/*_________________---------------------------__________________
_________________ heap allocation __________________
-----------------___________________________------------------
*/
void *my_calloc(size_t bytes) {
void *mem = calloc(1, bytes);
if(mem == NULL) {
fprintf(ERROUT, "calloc(%"PRIu64") failed: %s\n", (uint64_t)bytes, strerror(errno));
exit(-1);
}
return mem;
}
void my_free(void *ptr) {
if(ptr) {
free(ptr);
}
}
void *my_cb_alloc(void *magic, size_t bytes) {
return my_calloc(bytes);
}
void my_cb_free(void *magic, void *ptr) {
my_free(ptr);
}
/*_________________------------------------__________________
_________________ time __________________
-----------------________________________------------------
*/
void clockMono(struct timespec *ts) {
clockid_t monoClock = CLOCK_MONOTONIC;
#ifdef CLOCK_MONOTONIC_COARSE
// more efficient if supported, since we only need mS accuracy
monoClock = CLOCK_MONOTONIC_COARSE;
#endif
if(clock_gettime(monoClock, ts) == -1) {
fprintf(ERROUT, "clock_gettime() failed: %s", strerror(errno));
exit(-51);
}
}
uint64_t now_mS(void *magic) {
struct timespec ts;
clockMono(&ts);
uint64_t mS = ts.tv_sec;
mS *= 1000;
mS += (ts.tv_nsec >> 20); // approximation of / 1e6
return mS;
}
/*_________________---------------------------__________________
_________________ sfl_random __________________
-----------------___________________________------------------
Gerhard's generator
*/
static uint32_t SFLRandom = 1;
uint32_t sfl_random(uint32_t lim) {
SFLRandom = ((SFLRandom * 32719) + 3) % 32749;
return ((SFLRandom % lim) + 1);
}
void sfl_random_init(uint32_t seed) {
SFLRandom = seed;
}
/*_________________---------------------------__________________
_________________ string buffer __________________
-----------------___________________________------------------
use string buffer scratchpad to avoid snprintf() idiosyncracies
*/
static void SFStr_init(SFStr *sb) {
sb->cap = MAX_STRBUF_LEN;
sb->len = 0;
sb->str[0] = '\0';
}
static char *SFStr_str(SFStr *sb) {
return sb->str;
}
static int SFStr_len(SFStr *sb) {
return sb->len;
}
static int SFStr_append(SFStr *sb, char *str) {
if(str == NULL)
return YES;
int slen = strlen(str);
int copylen = strlen(str);
if((sb->len + copylen) >= sb->cap)
copylen = sb->cap - sb->len - 1;
if(copylen > 0) {
memcpy(sb->str + sb->len, str, copylen);
sb->len += copylen;
sb->str[sb->len] = '\0';
}
return (copylen == slen);
}
/* hex printing tends to be one of the performance bottlenecks,
so take the trouble to optimize it just a little */
static u_int8_t HexLookupL[513]= {
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f"
"505152535455565758595a5b5c5d5e5f"
"606162636465666768696a6b6c6d6e6f"
"707172737475767778797a7b7c7d7e7f"
"808182838485868788898a8b8c8d8e8f"
"909192939495969798999a9b9c9d9e9f"
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
};
static uint8_t HexLookupU[513]= {
"000102030405060708090A0B0C0D0E0F"
"101112131415161718191A1B1C1D1E1F"
"202122232425262728292A2B2C2D2E2F"
"303132333435363738393A3B3C3D3E3F"
"404142434445464748494A4B4C4D4E4F"
"505152535455565758595A5B5C5D5E5F"
"606162636465666768696A6B6C6D6E6F"
"707172737475767778797A7B7C7D7E7F"
"808182838485868788898A8B8C8D8E8F"
"909192939495969798999A9B9C9D9E9F"
"A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
"B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
"D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
"E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"
};
static int SFStr_append_hex(SFStr *sb, u_char *hex, int nbytes, int prefix, int upper, char sep) {
if(prefix) {
if((sb->cap - sb->len) < 3)
return NO;
sb->str[sb->len++] = '0';
sb->str[sb->len++] = 'x';
}
int hexPerByte = 2;
if(sep) hexPerByte++;
int roomForBytes = (sb->cap - sb->len - 1) / hexPerByte;
if(nbytes > roomForBytes)
nbytes = roomForBytes;
uint16_t *lookup = (uint16_t *)(upper ? HexLookupU : HexLookupL);
for(int ii = 0; ii < nbytes; ii++) {
if(sep && ii) sb->str[sb->len++] = sep;
uint16_t word = ntohs(lookup[hex[ii]]);
sb->str[sb->len++] = word >> 8;
sb->str[sb->len++] = word & 0xFF;
}
sb->str[sb->len] = '\0';
return (nbytes < roomForBytes);
}
static int SFStr_append_array32(SFStr *sb, uint32_t *array32, int n, int net_byte_order, char sep) {
for(int i = 0; i < n; i++) {
char ibuf[64];
uint32_t val32 = array32[i];
if(net_byte_order)
val32 = ntohl(val32);
snprintf(ibuf, 64, "%u", val32);
int ilen = strlen(ibuf);
if((sb->len + 1 + ilen) >= sb->cap)
return NO;
if(i > 0)
sb->str[sb->len++] = sep;
memcpy(sb->str + sb->len, ibuf, ilen);
sb->len += ilen;
sb->str[sb->len] = '\0';
}
return YES;
}
static int SFStr_append_U32(SFStr *sb, char *fmt, uint32_t val32) {
char ibuf[200];
snprintf(ibuf, 64, fmt, val32);
return SFStr_append(sb, ibuf);
}
static int SFStr_append_U64(SFStr *sb, char *fmt, uint64_t val64) {
char ibuf[200];
snprintf(ibuf, 200, fmt, val64);
return SFStr_append(sb, ibuf);
return YES;
}
static int SFStr_append_double(SFStr *sb, char *fmt, double vald) {
char ibuf[200];
snprintf(ibuf, 200, fmt, vald);
return SFStr_append(sb, ibuf);
return YES;
}
static int SFStr_append_mac(SFStr *sb, uint8_t *mac) {
return SFStr_append_hex(sb, mac, 6, NO, NO, 0);
}
static int SFStr_append_ip(SFStr *sb, uint8_t *ip) {
uint32_t array32[4];
for(int i = 0; i < 4; i++)
array32[i] = ip[i];
return SFStr_append_array32(sb, array32, 4, NO, '.');
}
static int SFStr_append_ip6(SFStr *sb, uint8_t *ip6) {
for(int i = 0; i < 16; i += 2) {
if(i > 0
&& SFStr_append(sb, ":") == NO)
return NO;
if(SFStr_append_hex(sb, (ip6+i), 2, NO, NO, 0) == NO)
return NO;
}
return YES;
}
static int SFStr_append_address(SFStr *sb, SFLAddress *address) {
if(address->type == SFLADDRESSTYPE_IP_V4)
return SFStr_append_ip(sb, (uint8_t *)&address->address.ip_v4.addr);
if(address->type == SFLADDRESSTYPE_IP_V6)
return SFStr_append_ip6(sb, address->address.ip_v6.addr);
return SFStr_append(sb, "-");
}
static int SFStr_append_UUID(SFStr *sb, uint8_t *uuid) {
SFStr_append_hex(sb, uuid, 4, NO, NO, 0);
SFStr_append(sb, "-");
SFStr_append_hex(sb, uuid+4, 2, NO, NO, 0);
SFStr_append(sb, "-");
SFStr_append_hex(sb, uuid+6, 2, NO, NO, 0);
SFStr_append(sb, "-");
SFStr_append_hex(sb, uuid+8, 2, NO, NO, 0);
SFStr_append(sb, "-");
return SFStr_append_hex(sb, uuid+10, 6, NO, NO, 0);
}
static int SFStr_append_tag(SFStr *sb, uint32_t tag) {
uint32_t parts[2];
parts[0] = (tag >> 12);
parts[1] = (tag & 0x00000FFF);
return SFStr_append_array32(sb, parts, 2, NO, ':');
}
static int SFStr_append_timestamp(SFStr *sb, time_t ts) {
char tstr[200];
/* ISO8601 compatible localtime */
strftime(tstr, 200, "%Y-%m-%dT%H:%M:%S%z", localtime(&ts));
return SFStr_append(sb, tstr);
}
static int SFStr_append_dataSource(SFStr *sb, uint32_t ds_class, uint32_t ds_index) {
char buf[200];
snprintf(buf, 200, "%u:%u", ds_class, ds_index);
return SFStr_append(sb, buf);
}
static int SFStr_copy(SFStr *sb, char *to, int capacity) {
int max = capacity - 1;
int bytes = max > sb->len ? sb->len : max;
memcpy(to, sb->str, bytes);
to[bytes] = '\0';
return bytes;
}
/*_________________---------------------------__________________
_________________ print functions __________________
-----------------___________________________------------------
*/
static char *printAddress(SFLAddress *address, SFStr *sb) {
SFStr_init(sb);
SFStr_append_address(sb, address);
return SFStr_str(sb);
}
static char *printMAC(uint8_t *mac, SFStr *sb) {