-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowtree.c
1346 lines (1081 loc) · 36.2 KB
/
flowtree.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#include <signal.h>
#include <errno.h>
/* We want to favor the BSD structs over the Linux ones */
#ifndef __USE_BSD
#define __USE_BSD
#endif
#ifndef __FAVOR_BSD
#define __FAVOR_BSD
#endif
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
/* The threading stuff */
#include <pthread.h>
/* The AVL tree */
#include "pavl.h"
/* The listen loop and thread(s) */
int terminate = 0;
/* Network stuff */
#define LISTENADDR "132.239.1.114"
#define LISTENPORT 2055
#define SOCKBUFF 1024 * 1024 /* 1 MB */
#define RECVBUFFSIZE 65536
#define SENDSRC "127.0.0.1"
#define SENDDST "127.0.0.1"
#define SENDPORT 2056
#define SENDBUFFSIZE 65536
int send_fh;
/* ===
* Netflow structs and other values
* http://www.cisco.com/en/US/docs/net_mgmt/netflow_collection_engine/
* 3.6/user/guide/format.html#wp1006108
* ===
*/
/* === Netflow v5 === */
struct netflow_v5 {
uint16_t version;
uint16_t flow_count;
uint32_t uptime;
uint32_t unix_sec;
uint32_t nsec;
uint32_t flow_sequence;
uint8_t engine_type;
uint8_t engine_id;
uint16_t sample_rate;
} __attribute__((__packed__));
struct netflow_v5_record {
in_addr_t src_addr;
in_addr_t dst_addr;
in_addr_t next_hop;
uint16_t src_int;
uint16_t dst_int;
uint32_t num_packets;
uint32_t num_bytes;
uint32_t start_time;
uint32_t end_time;
uint16_t src_port;
uint16_t dst_port;
uint8_t pad1;
uint8_t tcp_flags;
uint8_t protocol;
uint8_t tos;
uint16_t src_as;
uint16_t dst_as;
uint8_t src_mask;
uint8_t dst_mask;
uint16_t pad2;
} __attribute__((__packed__));
/* === Netflow v7 === */
struct netflow_v7 {
uint16_t version;
uint16_t flow_count;
uint32_t uptime;
uint32_t unix_sec;
uint32_t nsec;
uint32_t flow_sequence;
uint32_t reserved;
} __attribute__((__packed__));
struct netflow_v7_record {
in_addr_t src_addr;
in_addr_t dst_addr;
in_addr_t next_hop;
uint16_t src_int;
uint16_t dst_int;
uint32_t num_packets;
uint32_t num_bytes;
uint32_t start_time;
uint32_t end_time;
uint16_t src_port;
uint16_t dst_port;
uint8_t flags1;
uint8_t tcp_flags;
uint8_t protocol;
uint8_t tos;
uint16_t src_as;
uint16_t dst_as;
uint8_t src_mask;
uint8_t dst_mask;
uint16_t flags2;
uint32_t flow_src;
} __attribute__((__packed__));
/* ===
* The unified flow struct that all other formats will be converted to
* ===
*/
struct unified_flow {
in_addr_t flow_src;
time_t recv_time;
uint16_t src_int;
uint16_t dst_int;
struct in_addr src_addr;
struct in_addr dst_addr;
uint8_t protocol;
uint16_t src_port;
uint16_t dst_port;
uint8_t tcp_flags;
uint32_t num_packets;
uint32_t num_bytes;
time_t start_time;
time_t end_time;
};
/* ===
* The flow summary to insert into the flow trees
* ===
*/
struct flow_source_summary {
in_addr_t flow_src;
uint16_t src_int;
uint16_t dst_int;
uint64_t num_packets;
uint64_t num_bytes;
uint64_t num_flows;
struct flow_source_summary *next;
};
struct flow_summary {
time_t time_added;
time_t time_updated;
struct in_addr src_addr;
struct in_addr dst_addr;
uint8_t protocol;
uint16_t src_port;
uint16_t dst_port;
uint8_t tcp_flags;
time_t start_time;
time_t end_time;
uint8_t source_count;
struct flow_source_summary *sources;
};
/* ===
* The exclude list structs and vars
* ===
*/
struct exclude_node {
in_addr_t addr_start;
in_addr_t addr_end;
uint64_t exclude_count;
};
struct pavl_table *exclude_tree;
/* ===
* Function prototypes
* ===
*/
int main(int, char * const []);
void sig_terminate(int);
void packet_callback(const struct sockaddr_in *, const u_char *,
const size_t, const time_t);
void parse_netflow_v5(const struct sockaddr_in *, const u_char *,
const size_t, const time_t);
void parse_netflow_v7(const struct sockaddr_in *, const u_char *,
const size_t, const time_t);
void flow_callback(const struct unified_flow *);
int compare_flows(const void *, const void *, void *);
int compare_excludes(const void *, const void *, void *);
void * copy_flow(const void *, void *);
void add_exclusion(const in_addr_t, const in_addr_t);
int is_excluded(const in_addr_t);
void *thread_flow_janitor(void *);
void free_source_list(struct flow_source_summary *);
void print_flow_json(const struct flow_summary *);
/* ===
* Datastructure stuff
* ===
*/
#define TREES 65536
struct hash_node_tree {
struct pavl_table *tree;
pthread_mutex_t tree_mutex;
};
struct hash_node_tree flow_hash_trees[TREES];
/* === The purge parameters === */
#define MIN_FLOW_AGE 60
#define MAX_FLOW_AGE 300
#define ROL16(x, a) ((((x) << (a)) & 0xFFFF) | (((x) & 0xFFFF) >> (16 - (a))))
#define TREEHASH(f) (((((struct flow_summary *) \
(f))->src_addr.s_addr) & \
0xFFFF) ^ \
(ROL16((((((struct flow_summary *) \
(f))->src_addr.s_addr) & \
0xFFFF0000) >> 16), 7)) ^ \
((((struct flow_summary *) \
(f))->dst_addr.s_addr) & \
0xFFFF) ^ \
(ROL16((((((struct flow_summary *) \
(f))->dst_addr.s_addr) & \
0xFFFF0000) >> 16), 13)) ^ \
(((struct flow_summary *) \
(f))->src_port) ^ \
(ROL16((((struct flow_summary *) \
(f))->dst_port), 3)) ^ \
(((struct flow_summary *)(f))->protocol))
/* ===
* Some stats vars
* ===
*/
uint64_t stat_flow_packets = 0, stat_total_flows = 0, stat_excluded_flows = 0;
uint64_t stat_new_flows = 0, stat_dup_flows = 0, stat_current_flows = 0;
uint64_t stat_proto_flows[256];
pthread_mutex_t stat_current_mutex = PTHREAD_MUTEX_INITIALIZER;
/* ===
* The global time vars
* ===
*/
#define STATS_RATE 60
time_t last_stats_update;
time_t start_time;
int main(int argc, char * const argv[]) {
/* === Signal vars === */
struct sigaction sa_new, sa_old;
sigset_t sigmask, emptysigmask;
/* === Socket vars === */
struct sockaddr_in bind_addrin, peer_addrin, send_addrin;
in_addr_t bind_addr;
in_addr_t send_addr;
int sock_fh;
int setsockbuff = SOCKBUFF, getsockbuff;
socklen_t sockbufflen = sizeof(getsockbuff);
socklen_t peeraddrlen = sizeof(peer_addrin);
/* === Network data vars === */
u_char buffer[RECVBUFFSIZE];
ssize_t msgsize;
fd_set read_fd;
struct timespec sel_timespec;
int select_ret;
time_t recv_time;
/* === Thread vars === */
pthread_t flow_janitor;
int thread_ret;
/* === Misc vars === */
time_t cur_time;
uint32_t time_diff;
int i;
/* Before we start listening we need to setup a signal
* handler so we can cleanly exit */
memset(&sa_new, 0, sizeof(struct sigaction));
sa_new.sa_handler = sig_terminate;
sigaction(SIGTERM, &sa_new, &sa_old);
memset(&sa_new, 0, sizeof(struct sigaction));
sa_new.sa_handler = sig_terminate;
sigaction(SIGINT, &sa_new, &sa_old);
/* Setup the masks for pselect() */
sigemptyset(&emptysigmask);
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGTERM);
sigaddset(&sigmask, SIGINT);
/* Make our listen socket */
if ((sock_fh = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
fprintf(stderr, "Creation of listen socket failed.\n");
return 1;
}
/* Try to set the socket buffer */
if (setsockopt(sock_fh, SOL_SOCKET, SO_RCVBUF,
&setsockbuff, sizeof(setsockbuff)) == -1) {
fprintf(stderr, "Setting listen socket receive buffer failed.\n");
return 1;
}
/* Now find out what our socket buffer really is set to */
if (getsockopt(sock_fh, SOL_SOCKET, SO_RCVBUF,
&getsockbuff, &sockbufflen) == -1) {
fprintf(stderr, "Unable to get listen socket receive buffer.\n");
return 1;
}
else {
fprintf(stderr, "Listen socket receive buffer is %d bytes\n", getsockbuff);
}
/* Make our send socket */
if ((send_fh = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
fprintf(stderr, "Creation of send socket failed.\n");
return 1;
}
/* Try to set the send socket buffer */
if (setsockopt(send_fh, SOL_SOCKET, SO_SNDBUF,
&setsockbuff, sizeof(setsockbuff)) == -1) {
fprintf(stderr, "Setting send socket send buffer failed.\n");
return 1;
}
/* Now find out what our socket buffer really is set to */
if (getsockopt(send_fh, SOL_SOCKET, SO_SNDBUF,
&getsockbuff, &sockbufflen) == -1) {
fprintf(stderr, "Unable to get send socket send buffer.\n");
return 1;
}
else {
fprintf(stderr, "Send socket send buffer is %d bytes\n", getsockbuff);
}
/* Setup the binding struct */
bind_addr = inet_addr(LISTENADDR);
memset(&bind_addrin, 0, sizeof(bind_addrin));
bind_addrin.sin_family = AF_INET;
bind_addrin.sin_port = htons(LISTENPORT);
bind_addrin.sin_addr.s_addr = bind_addr;
/* Do the bind */
if (bind(sock_fh, (const struct sockaddr *)&bind_addrin,
sizeof(bind_addrin)) == -1) {
fprintf(stderr, "Binding to socket failed.\n");
perror("bind");
return 1;
}
/* Setup the send binding struct */
send_addr = inet_addr(SENDSRC);
memset(&send_addrin, 0, sizeof(send_addrin));
send_addrin.sin_family = AF_INET;
send_addrin.sin_port = 0;
send_addrin.sin_addr.s_addr = send_addr;
/* Do the send bind */
if (bind(send_fh, (const struct sockaddr *)&send_addrin,
sizeof(send_addrin)) == -1) {
fprintf(stderr, "Binding to sending socket failed.\n");
perror("bind");
return 1;
}
/* Create the exclude tree */
exclude_tree = pavl_create(compare_excludes, NULL, NULL);
/* Populate the exclusion tree */
add_exclusion(inet_network("132.239.1.114"), inet_network("132.239.1.116"));
add_exclusion(inet_network("132.239.1.199"), inet_network("132.239.1.204"));
add_exclusion(inet_network("44.0.0.0"), inet_network("44.255.255.255"));
/* Create the flow trees */
for (i = 0; i < TREES; i++) {
flow_hash_trees[i].tree = pavl_create(compare_flows, NULL, NULL);
pthread_mutex_init(&(flow_hash_trees[i].tree_mutex), NULL);
}
/* Record what time we started */
start_time = time(NULL);
last_stats_update = start_time;
/* Before listening, start the janitor thread */
thread_ret = pthread_create(&flow_janitor, NULL, thread_flow_janitor, NULL);
/* Testing receive, will do better in final code */
while (terminate == 0) {
/* Check if we need to update the stats */
cur_time = time(NULL);
time_diff = cur_time - last_stats_update;
if (time_diff >= STATS_RATE) {
last_stats_update = cur_time;
time_diff = cur_time - start_time;
if (stat_new_flows == 0) {
fprintf(stderr, "--\n");
fprintf(stderr, "NO FLOWS\n");
}
else {
fprintf(stderr, "--\n");
fprintf(stderr, "flowtree stats:\n");
fprintf(stderr, "===============\n");
fprintf(stderr, "runtime: %d seconds; total packets: %lu; "
"total flows: %lu\n", (int)time_diff,
stat_flow_packets, stat_total_flows);
fprintf(stderr, "packet rate: %.02f pps; "
"flow rate: %.02f fps; new flow rate %.02f fps\n",
(double)stat_flow_packets / (double)time_diff,
(double)(stat_total_flows) / (double)time_diff,
(double)stat_new_flows / (double)time_diff);
fprintf(stderr, "excluded flows: %lu (%.02f%%)\n",
stat_excluded_flows, ((double)stat_excluded_flows /
(double)stat_total_flows) * 100);
/* === *** ACQUIRE STATS LOCK *** === */
pthread_mutex_lock(&stat_current_mutex);
fprintf(stderr, "currently tracking flows: %lu\n", stat_current_flows);
/* === *** UNLOCK STATS LOCK *** === */
pthread_mutex_unlock(&stat_current_mutex);
fprintf(stderr, "total unique flows: %lu (%.02f%%)\n",
stat_new_flows, ((double)stat_new_flows /
(double)(stat_total_flows)) * 100);
fprintf(stderr, "unique tcp flows: %lu (%.02f%%)\n",
stat_proto_flows[6], ((double)stat_proto_flows[6] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique udp flows: %lu (%.02f%%)\n",
stat_proto_flows[17], ((double)stat_proto_flows[17] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique icmp flows: %lu (%.02f%%)\n",
stat_proto_flows[1], ((double)stat_proto_flows[1] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique eth-in-ip flows: %lu (%.02f%%)\n",
stat_proto_flows[97], ((double)stat_proto_flows[97] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique 6in4 flows: %lu (%.02f%%)\n",
stat_proto_flows[41], ((double)stat_proto_flows[41] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique pim flows: %lu (%.02f%%)\n",
stat_proto_flows[103], ((double)stat_proto_flows[103] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique igmp flows: %lu (%.02f%%)\n",
stat_proto_flows[2], ((double)stat_proto_flows[2] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique ip in ip flows: %lu (%.02f%%)\n",
stat_proto_flows[4], ((double)stat_proto_flows[4] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique eigrp flows: %lu (%.02f%%)\n",
stat_proto_flows[88], ((double)stat_proto_flows[88] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique esp flows: %lu (%.02f%%)\n",
stat_proto_flows[50], ((double)stat_proto_flows[50] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique ah flows: %lu (%.02f%%)\n",
stat_proto_flows[51], ((double)stat_proto_flows[51] /
(double)stat_new_flows) * 100);
fprintf(stderr, "unique gre flows: %lu (%.02f%%)\n",
stat_proto_flows[47], ((double)stat_proto_flows[47] /
(double)stat_new_flows) * 100);
}
}
/* prep for the select */
FD_ZERO(&read_fd);
FD_SET(sock_fh, &read_fd);
sel_timespec.tv_sec = 0;
sel_timespec.tv_nsec = 100000000; /* .1 seconds */
/* Block signals */ /* NOT THREAD SAFE */
sigprocmask(SIG_BLOCK, &emptysigmask, NULL);
/* See if we have data */
if ((select_ret = pselect(sock_fh + 1, &read_fd, NULL, NULL,
&sel_timespec, &emptysigmask)) == -1) {
if (errno != EINTR) {
fprintf(stderr, "Call to pselect() failed.\n");
perror("pselect");
return 1;
}
}
/* Nothing became ready */
if (select_ret <= 0) {
continue;
}
/* Select says we have a message, grab it */
if ((msgsize = recvfrom(sock_fh, buffer, RECVBUFFSIZE, 0,
(struct sockaddr *)&peer_addrin,
&peeraddrlen)) == -1) {
fprintf(stderr, "recvfrom() call failed!\n");
perror("recvfrom");
return 1;
}
else {
/*
fprintf(stderr, "Got a packet from %s:%d; size=%d\n",
inet_ntoa(peer_addrin.sin_addr), ntohs(peer_addrin.sin_port),
(int)msgsize);
*/
/* We need to fix the byte order for the peer */
peer_addrin.sin_addr.s_addr = ntohl(peer_addrin.sin_addr.s_addr);
/* Update the counter */
stat_flow_packets += 1;
recv_time = time(NULL);
packet_callback(&peer_addrin, buffer, msgsize, recv_time);
}
}
/* === Stopped listening, must have gotten signal === */
fprintf(stderr, "Waiting for threads to finish before exiting...\n");
pthread_join(flow_janitor, NULL);
close(sock_fh);
return 0;
}
void packet_callback(const struct sockaddr_in *peer, const u_char *flow,
const size_t flow_size, const time_t recv_time) {
/* Check for netflow v5 */
if (flow_size > sizeof(struct netflow_v5)) {
if (ntohs(((struct netflow_v5 *)flow)->version) == 5) {
/* Maybe more checks should be added later... */
parse_netflow_v5(peer, flow, flow_size, recv_time);
return;
}
}
/* Check for netflow v7 */
if (flow_size > sizeof(struct netflow_v7)) {
if (ntohs(((struct netflow_v7 *)flow)->version) == 7) {
/* Maybe more checks should be added later... */
parse_netflow_v7(peer, flow, flow_size, recv_time);
return;
}
}
/* Other version of netflow / sflow / jflow will be handled later */
fprintf(stderr, "Got an uknown flow format\n");
}
void parse_netflow_v5(const struct sockaddr_in *peer, const u_char *flow,
const size_t flow_size, const time_t recv_time) {
struct unified_flow current_flow;
struct netflow_v5_record * record_v5;
/* ===
* Misc vars
* ===
*/
int records = 0;
int i;
/* ===
* Do more sanity checks to make sure we have a netflow v5 record
* ===
*/
if (flow_size < sizeof(struct netflow_v5)) {
fprintf(stderr, "v5 flow not big enough\n");
return;
}
if (ntohs(((struct netflow_v5 *)flow)->version) != 5) {
fprintf(stderr, "not v5\n");
return;
}
records = ntohs(((struct netflow_v5 *)flow)->flow_count);
if (flow_size != sizeof(struct netflow_v5) +
(records * sizeof(struct netflow_v5_record))) {
fprintf(stderr,
"wrong size; flow_count=%d; flow_size=%d; v5=%d, v5r=%d\n",
(int)ntohs(((struct netflow_v5 *)flow)->flow_count),
(int)flow_size, (int)sizeof(struct netflow_v5),
(int)sizeof(struct netflow_v5_record));
return;
}
/*fprintf(stderr, "Got a valid looking netflow v5 packet\n");*/
/* ===
* Looks like valid netflow v5 so parse it
* ===
*/
/* Now loop through the records */
record_v5 = (struct netflow_v5_record *)(flow + sizeof(struct netflow_v5));
for (i = 0; i < records; i++) {
/* Fill in our current flow info */
current_flow.flow_src = peer->sin_addr.s_addr;
current_flow.recv_time = recv_time;
current_flow.src_int = ntohs(record_v5[i].src_int);
current_flow.dst_int = ntohs(record_v5[i].dst_int);
current_flow.src_addr.s_addr = ntohl(record_v5[i].src_addr);
current_flow.dst_addr.s_addr = ntohl(record_v5[i].dst_addr);
current_flow.protocol = record_v5[i].protocol;
current_flow.src_port = ntohs(record_v5[i].src_port);
current_flow.dst_port = ntohs(record_v5[i].dst_port);
current_flow.tcp_flags = record_v5[i].tcp_flags;
current_flow.num_packets = ntohl(record_v5[i].num_packets);
current_flow.num_bytes = ntohl(record_v5[i].num_bytes);
/* Time calculations require a bit of math, namely
* curtime - ((uptime - start) / 1000)
*/
current_flow.start_time = ntohl(((struct netflow_v5 *)flow)->unix_sec) -
(((ntohl(((struct netflow_v5 *)flow)->uptime) - \
ntohl(record_v5[i].start_time)) & 0xFFFFFFFF) / 1000);
current_flow.end_time = ntohl(((struct netflow_v5 *)flow)->unix_sec) -
(((ntohl(((struct netflow_v5 *)flow)->uptime) - \
ntohl(record_v5[i].end_time)) & 0xFFFFFFFF) / 1000);
/* Now handle the current unified flow */
flow_callback(¤t_flow);
}
}
void parse_netflow_v7(const struct sockaddr_in *peer, const u_char *flow,
const size_t flow_size, const time_t recv_time) {
struct unified_flow current_flow;
struct netflow_v7_record * record_v7;
/* ===
* Misc vars
* ===
*/
int records = 0;
int i;
/* ===
* Do more sanity checks to make sure we have a netflow v7 record
* ===
*/
if (flow_size < sizeof(struct netflow_v7)) {
fprintf(stderr, "v7 flow not big enough\n");
return;
}
if (ntohs(((struct netflow_v7 *)flow)->version) != 7) {
fprintf(stderr, "not v7\n");
return;
}
records = ntohs(((struct netflow_v7 *)flow)->flow_count);
if (flow_size != sizeof(struct netflow_v7) +
(records * sizeof(struct netflow_v7_record))) {
fprintf(stderr,
"wrong size; flow_count=%d; flow_size=%d; v7=%d, v7r=%d\n",
(int)ntohs(((struct netflow_v7 *)flow)->flow_count),
(int)flow_size, (int)sizeof(struct netflow_v7),
(int)sizeof(struct netflow_v7_record));
return;
}
/*fprintf(stderr, "Got a valid looking netflow v7 packet\n");*/
/* ===
* Looks like valid netflow v7 so parse it
* ===
*/
/* Now loop through the records */
record_v7 = (struct netflow_v7_record *)(flow + sizeof(struct netflow_v7));
for (i = 0; i < records; i++) {
/* Fill in our current flow info */
current_flow.flow_src = ntohl(record_v7[i].flow_src);
current_flow.recv_time = recv_time;
current_flow.src_int = ntohs(record_v7[i].src_int);
current_flow.dst_int = ntohs(record_v7[i].dst_int);
current_flow.src_addr.s_addr = ntohl(record_v7[i].src_addr);
current_flow.dst_addr.s_addr = ntohl(record_v7[i].dst_addr);
current_flow.protocol = record_v7[i].protocol;
current_flow.src_port = ntohs(record_v7[i].src_port);
current_flow.dst_port = ntohs(record_v7[i].dst_port);
current_flow.tcp_flags = record_v7[i].tcp_flags;
current_flow.num_packets = ntohl(record_v7[i].num_packets);
current_flow.num_bytes = ntohl(record_v7[i].num_bytes);
/* Time calculations require a bit of math, namely
* curtime - ((uptime - start) / 1000)
*/
current_flow.start_time = ntohl(((struct netflow_v7 *)flow)->unix_sec) -
(((ntohl(((struct netflow_v7 *)flow)->uptime) - \
ntohl(record_v7[i].start_time)) & 0xFFFFFFFF) / 1000);
current_flow.end_time = ntohl(((struct netflow_v7 *)flow)->unix_sec) -
(((ntohl(((struct netflow_v7 *)flow)->uptime) - \
ntohl(record_v7[i].end_time)) & 0xFFFFFFFF) / 1000);
/* Now handle the current unified flow */
flow_callback(¤t_flow);
}
}
void flow_callback(const struct unified_flow *current_flow) {
/* ===
* Flow tree and summary vars
* ===
*/
struct flow_summary cur_flow_summary;
struct flow_summary *flow_summary_copy;
struct flow_summary **flow_summary_probe;
struct flow_source_summary *new_flow_source_summary;
struct flow_source_summary **cur_flow_source_summary;
int tree_num;
/* ===
* Misc vars
* ===
*/
struct in_addr temp_inaddr_src, temp_inaddr_dst;
int source_updated;
/* ===
* Update the stats that we got a flow
* ===
*/
stat_total_flows += 1;
temp_inaddr_src.s_addr = htonl(current_flow->src_addr.s_addr);
temp_inaddr_dst.s_addr = htonl(current_flow->dst_addr.s_addr);
/*
fprintf(stderr, "Got proto %d flow from %s:%d",
current_flow->protocol,
inet_ntoa(temp_inaddr_src),
current_flow->src_port);
fprintf(stderr, " to %s:%d (%u to %u)\n",
inet_ntoa(temp_inaddr_dst),
current_flow->dst_port,
(unsigned int)current_flow->start_time,
(unsigned int)current_flow->end_time);
*/
/* ===
* Check to see if this flow is excluded
* ===
*/
if ((is_excluded(current_flow->src_addr.s_addr) == 1) ||
(is_excluded(current_flow->dst_addr.s_addr) == 1)) {
/*
fprintf(stderr, "excluded %s -> ", inet_ntoa(temp_inaddr_src));
fprintf(stderr, "%s\n", inet_ntoa(temp_inaddr_dst));
*/
stat_excluded_flows += 1;
return;
}
/* ===
* Now insert or update the flow in the tree
* ===
*/
/* Setup the current flow summary struct */
cur_flow_summary.time_added = current_flow->recv_time;
cur_flow_summary.time_updated = current_flow->recv_time;
cur_flow_summary.src_addr = current_flow->src_addr;
cur_flow_summary.dst_addr = current_flow->dst_addr;
cur_flow_summary.protocol = current_flow->protocol;
cur_flow_summary.src_port = current_flow->src_port;
cur_flow_summary.dst_port = current_flow->dst_port;
cur_flow_summary.tcp_flags = current_flow->tcp_flags;
cur_flow_summary.start_time = current_flow->start_time;
cur_flow_summary.end_time = current_flow->end_time;
cur_flow_summary.source_count = 0; /* gets updated later */
cur_flow_summary.sources = NULL;
/* Now make an insert-ready copy */
flow_summary_copy = copy_flow(&cur_flow_summary, NULL);
/* Figure out which tree to use */
tree_num = TREEHASH(flow_summary_copy);
/* === *** ACQUIRE TREE LOCK *** === */
pthread_mutex_lock(&(flow_hash_trees[tree_num].tree_mutex));
/* Search and possibly insert this flow */
flow_summary_probe =
(struct flow_summary **)pavl_probe(flow_hash_trees[tree_num].tree,
flow_summary_copy);
/* Figure out what happened */
if (flow_summary_probe == NULL) {
fprintf(stderr, "There was a failure inserting the flow into tree.\n");
/* === *** RELEASE LOCK *** === */
pthread_mutex_unlock(&(flow_hash_trees[tree_num].tree_mutex));
return;
}
/* Now find out if it was already there or we just inserted it */
if (*flow_summary_probe == flow_summary_copy) {
/* well that was easy, nothing fancy to do now */
/* should increment new flow counters */
stat_new_flows++;
/* === *** ACQUIRE STATS LOCK *** === */
pthread_mutex_lock(&stat_current_mutex);
stat_current_flows++;
/* === *** UNLOCK STATS LOCK *** === */
pthread_mutex_unlock(&stat_current_mutex);
stat_proto_flows[(*flow_summary_probe)->protocol] += 1;
}
else {
/* fprintf(stderr, "Flow already in tree; flows=%u\n",
(unsigned int)pavl_count(flow_hash_trees[tree_num].tree));
*/
/* update the stats */
stat_dup_flows++;
/* update some summay stuff about this flow */
(*flow_summary_probe)->tcp_flags |= flow_summary_copy->tcp_flags;
if ((*flow_summary_probe)->start_time > flow_summary_copy->start_time) {
(*flow_summary_probe)->start_time = flow_summary_copy->start_time;
}
if ((*flow_summary_probe)->end_time < flow_summary_copy->end_time) {
(*flow_summary_probe)->end_time = flow_summary_copy->end_time;
}
(*flow_summary_probe)->time_updated = flow_summary_copy->time_updated;
/*
fprintf(stderr, "Sources: %d\n",
(*flow_summary_probe)->source_count);
*/
/* We don't need the copy anymore */
free(flow_summary_copy);
flow_summary_copy = NULL;
}
/*
fprintf(stderr, "Stats: new=%lu, dup=%lu, cur=%lu, tcp=%lu, "
"udp=%lu, icmp=%lu; oth=%lu\n",
stat_new_flows, stat_dup_flows, stat_current_flows,
stat_tcp_flows, stat_udp_flows, stat_icmp_flows,
stat_other_flows);
*/
/* ===
* The flow is now in the tree, we need to update the flow source info
* ===
*/
/* Find the spot to update or where to insert */
source_updated = 0;
cur_flow_source_summary = &((*flow_summary_probe)->sources);
while (*cur_flow_source_summary != NULL) {
if (current_flow->flow_src < (*cur_flow_source_summary)->flow_src) {
/* We are going to need to insert a new flow source here */
break;
}
else if (current_flow->flow_src ==
(*cur_flow_source_summary)->flow_src) {
/* We need to update this flow source */
(*cur_flow_source_summary)->num_packets += current_flow->num_packets;
(*cur_flow_source_summary)->num_bytes += current_flow->num_bytes;
(*cur_flow_source_summary)->num_flows += 1;
source_updated = 1;
break;
}
else {
/* Go on */
cur_flow_source_summary = &((*cur_flow_source_summary)->next);
}
}
/* If we didn't do an update then we need to insert a new flow source */
if (source_updated == 0) {
new_flow_source_summary = malloc(sizeof(struct flow_source_summary));
/* Set the new fields */
new_flow_source_summary->flow_src = current_flow->flow_src;
new_flow_source_summary->src_int = current_flow->src_int;
new_flow_source_summary->dst_int = current_flow->dst_int;
new_flow_source_summary->num_packets = current_flow->num_packets;
new_flow_source_summary->num_bytes = current_flow->num_bytes;
new_flow_source_summary->num_flows = 1;
/* Now insert this into the list */
new_flow_source_summary->next = *cur_flow_source_summary;
*cur_flow_source_summary = new_flow_source_summary;
/* Update the source count for the flow */
(*flow_summary_probe)->source_count += 1;
}
/* === *** RELEASE TREE LOCK *** === */
pthread_mutex_unlock(&(flow_hash_trees[tree_num].tree_mutex));
}
void sig_terminate(int signo) {
/* It is dangerous to do much more than this in a signal handler */
terminate = 1;
}
int compare_flows(const void *a, const void *b, void *param) {
const struct flow_summary *fa = a;
const struct flow_summary *fb = b;
if (fa->protocol > fb->protocol) {
return 1;
}
else if (fa->protocol < fb->protocol) {
return -1;
}
else if (fa->src_addr.s_addr > fb->src_addr.s_addr) {
return 1;
}
else if (fa->src_addr.s_addr < fb->src_addr.s_addr) {
return -1;
}
else if (fa->dst_addr.s_addr > fb->dst_addr.s_addr) {
return 1;
}