forked from vjanelle/nprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.c
1905 lines (1599 loc) · 64.2 KB
/
collect.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
/*
* nProbe - a Netflow v5/v9/IPFIX probe for IPv4/v6
*
* Copyright (C) 2007-14 Luca Deri <deri@ntop.org>
*
* http://www.ntop.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "nprobe.h"
#define DEBUG_FLOWS
//#define CISCO_DEBUG
#define LEN_SMALL_WORK_BUFFER 2048
/* forward */
void* netFlowCollectLoop(void* notUsed);
/* ********************************************************* */
/*
Cisco ASA
http://www.cisco.com/en/US/docs/security/asa/asa81/netflow/netflow.html#wp1028202
*/
/* ********************************************************* */
int createNetFlowListener(u_int16_t collectorInPort) {
int sockopt = 1;
struct sockaddr_in sockInV4;
struct sockaddr_in6 sockInV6;
readWriteGlobals->collectionStats.num_dissected_flow_packets = 0;
readOnlyGlobals.collectorInSocketv4 = readOnlyGlobals.collectorInSocketv6 = readOnlyGlobals.collectorInSctpSocket = -1;
readWriteGlobals->v9_ipfix_templates = NULL;
if(collectorInPort > 0) {
int i;
/*
Check if for some configuration error we'll be sending flows to
ourselves, that creates a waterfall effect
*/
for(i=0; i<readOnlyGlobals.numCollectors; i++) {
if((readOnlyGlobals.netFlowDest[i].u.v4Address.sin_port == htons(collectorInPort))
&& (readOnlyGlobals.netFlowDest[i].u.v4Address.sin_addr.s_addr == inet_addr("127.0.0.1"))) {
traceEvent(TRACE_ERROR, "Bad configuration: flows will be sent to the collection port");
traceEvent(TRACE_ERROR, "causing a waterfall effect: flow collection will be disabled");
readOnlyGlobals.numCollectors = 0;
return(-1);
}
}
errno = 0;
readOnlyGlobals.collectorInSocketv4 = socket(AF_INET, SOCK_DGRAM, 0);
if((readOnlyGlobals.collectorInSocketv4 < 0) || (errno != 0) ) {
traceEvent(TRACE_INFO, "Unable to create a UDPv4 socket - returned %d, error is '%s'(%d)",
readOnlyGlobals.collectorInSocketv4, strerror(errno), errno);
return(-1);
} else
maximize_socket_buffer(readOnlyGlobals.collectorInSocketv4, SO_RCVBUF);
errno = 0;
readOnlyGlobals.collectorInSocketv6 = socket(AF_INET6, SOCK_DGRAM, 0);
if((readOnlyGlobals.collectorInSocketv6 < 0) || (errno != 0) ) {
traceEvent(TRACE_INFO, "Unable to create a UDPv6 socket - returned %d, error is '%s'(%d); IPv6 disabled",
readOnlyGlobals.collectorInSocketv6, strerror(errno), errno);
readOnlyGlobals.collectorInSocketv6 = 0;
} else
maximize_socket_buffer(readOnlyGlobals.collectorInSocketv6, SO_RCVBUF);
#ifdef HAVE_SCTP
errno = 0;
readOnlyGlobals.collectorInSctpSocket = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
if((readOnlyGlobals.collectorInSctpSocket < 0) || (errno != 0)) {
traceEvent(TRACE_INFO, "Unable to create a SCTP socket - returned %d, error is '%s'(%d); SCTP disabled",
readOnlyGlobals.collectorInSocketv4, strerror(errno), errno);
}
#endif
traceEvent(TRACE_INFO, "Created UDP sockets");
#ifdef HAVE_SCTP
if(readOnlyGlobals.collectorInSctpSocket > 0)
traceEvent(TRACE_INFO, "Created a SCTP socket (%d)", readOnlyGlobals.collectorInSctpSocket);
#endif
setsockopt(readOnlyGlobals.collectorInSocketv4, SOL_SOCKET, SO_REUSEADDR, (char *)&sockopt, sizeof(sockopt));
if(readOnlyGlobals.collectorInSocketv6 > 0)
setsockopt(readOnlyGlobals.collectorInSocketv6, SOL_SOCKET, SO_REUSEADDR, (char *)&sockopt, sizeof(sockopt));
sockInV4.sin_family = AF_INET;
sockInV4.sin_port = (int)htons(collectorInPort);
sockInV4.sin_addr.s_addr = INADDR_ANY;
memset(&sockInV6, 0, sizeof(sockInV6));
#ifdef HAVE_SIN6_LEN
#ifndef WIN32
sockInV6.sin6_len = sizeof(struct sockaddr_in6);
#endif
#endif
sockInV6.sin6_family = AF_INET6;
sockInV6.sin6_port = htons(collectorInPort);
sockInV6.sin6_addr = in6addr_any;
if((bind(readOnlyGlobals.collectorInSocketv4, (struct sockaddr *)&sockInV4, sizeof(sockInV4)) < 0)
|| ((readOnlyGlobals.collectorInSocketv6 > 0)
&& (bind(readOnlyGlobals.collectorInSocketv6, (struct sockaddr *)&sockInV6, sizeof(sockInV6)) < 0))
#ifdef HAVE_SCTP
|| ((readOnlyGlobals.collectorInSctpSocket > 0)
&& (bind(readOnlyGlobals.collectorInSctpSocket, (struct sockaddr *)&sockInV4, sizeof(sockInV4)) < 0))
#endif
) {
traceEvent(TRACE_ERROR, "Flow collector UDP port %d already in use ? [%s/%d]",
collectorInPort, strerror(errno), errno);
close(readOnlyGlobals.collectorInSocketv4);
readOnlyGlobals.collectorInSocketv4 = 0;
if(readOnlyGlobals.collectorInSocketv6 > 0)
close(readOnlyGlobals.collectorInSocketv6);
readOnlyGlobals.collectorInSocketv6 = 0;
#ifdef HAVE_SCTP
if(readOnlyGlobals.collectorInSctpSocket) close(readOnlyGlobals.collectorInSctpSocket);
readOnlyGlobals.collectorInSctpSocket = 0;
#endif
exit(0);
}
#ifdef HAVE_SCTP
if(readOnlyGlobals.collectorInSctpSocket > 0) {
if(listen(readOnlyGlobals.collectorInSctpSocket, 100) == -1) {
traceEvent(TRACE_ERROR, "Listen on SCTP socket failed [%s]", strerror(errno));
}
}
#endif
traceEvent(TRACE_NORMAL, "Flow collector listening on port %d (IPv4/v6)", collectorInPort);
for(i=0; i<readOnlyGlobals.numProcessThreads; i++) {
unsigned long id = i;
pthread_create(&readOnlyGlobals.collectThread[i], NULL, netFlowCollectLoop, (void*)id);
}
}
return(0);
}
/* ********************************************************* */
void closeNetFlowListener() {
if(readOnlyGlobals.collectorInSocketv4 != -1) close(readOnlyGlobals.collectorInSocketv4);
if(readOnlyGlobals.collectorInSctpSocket != -1) close(readOnlyGlobals.collectorInSctpSocket);
}
/* ********************************************************* */
static void deEndianRecord(struct generic_netflow_record *record) {
record->last = ntohl(record->last), record->first = ntohl(record->first);
if(record->srcaddr.ipVersion == 4) {
record->srcaddr.ipType.ipv4 = ntohl(record->srcaddr.ipType.ipv4);
record->dstaddr.ipType.ipv4 = ntohl(record->dstaddr.ipType.ipv4);
record->nexthop.ipType.ipv4 = ntohl(record->nexthop.ipType.ipv4);
}
record->sentPkts = ntohl(record->sentPkts), record->rcvdPkts = ntohl(record->rcvdPkts);
record->srcport = ntohs(record->srcport), record->dstport = ntohs(record->dstport);
record->sentOctets = ntohl(record->sentOctets), record->rcvdOctets = ntohl(record->rcvdOctets);
record->input = ntohs(record->input), record->output = ntohs(record->output);
record->src_as = htonl(record->src_as), record->dst_as = htonl(record->dst_as);
record->icmpType = ntohs(record->icmpType);
}
/* *************************** */
static u_int8_t _matchAS(u_int32_t as, ASlist *l) {
while(l != NULL)
if(l->asn == as) return(1); else l = l->next;
return(0);
}
/* *************************** */
static u_int8_t matchAS(struct generic_netflow_record *record, ASlist *l) {
return(_matchAS(record->dst_as, l) || _matchAS(record->src_as, l));
}
/* *************************** */
static u_int8_t _matchPrefix(IpAddress *addr, NetList *l) {
if(addr->ipVersion != 4) return(0); /* IPv4 only so far */
while(l != NULL) {
if(l->net == (addr->ipType.ipv4 & l->mask))
return(1);
else
l = l->next;
}
return(0);
}
/* *************************** */
static u_int8_t matchPrefix(struct generic_netflow_record *record, NetList *l) {
return(_matchPrefix(&record->srcaddr, l) || _matchPrefix(&record->dstaddr, l));
}
/* *************************** */
static void handleGenericFlow(u_int16_t thread_id, u_int32_t netflow_device_ip,
u_int32_t recordActTime, u_int32_t recordSysUpTime,
struct generic_netflow_record *record) {
struct pcap_pkthdr h;
u_int32_t firstSeen, lastSeen;
u_int32_t initTime, subflow_id;
u_int8_t found;
FlowHashBucket *bkt = NULL;
if(readOnlyGlobals.flowCollection.keepProbesUnmerged)
subflow_id = netflow_device_ip;
else
subflow_id = 0;
/* Check if there are some ingress filters applied */
if(readOnlyGlobals.flowCollection.not_as_list
|| readOnlyGlobals.flowCollection.as_list) {
if(readOnlyGlobals.flowCollection.not_as_list
&& matchAS(record, readOnlyGlobals.flowCollection.not_as_list)) {
flow_discard:
//traceEvent(TRACE_INFO, "Discarding ingress flow");
return;
}
if(readOnlyGlobals.flowCollection.as_list
&& (!matchAS(record, readOnlyGlobals.flowCollection.as_list))) {
goto flow_discard;
}
}
if(readOnlyGlobals.flowCollection.not_prefix_list
|| readOnlyGlobals.flowCollection.prefix_list) {
if(readOnlyGlobals.flowCollection.not_prefix_list
&& matchPrefix(record, readOnlyGlobals.flowCollection.not_prefix_list)) {
goto flow_discard;
}
if(readOnlyGlobals.flowCollection.prefix_list
&& (!matchPrefix(record, readOnlyGlobals.flowCollection.prefix_list))) {
goto flow_discard;
}
}
pthread_rwlock_wrlock(&readWriteGlobals->collectorCounterLock);
readWriteGlobals->collectionStats.num_flows_processed++;
pthread_rwlock_unlock(&readWriteGlobals->collectorCounterLock);
if((record->firstEpoch > 0) && (record->lastEpoch > 0)) {
firstSeen = ntohl(record->firstEpoch), lastSeen = ntohl(record->lastEpoch);
if(((firstSeen < 1300618407 /* Dummy date */) || (lastSeen < 1300618407 /* Dummy date */))){
time_t now = time(NULL);
firstSeen = now - (lastSeen - firstSeen);
lastSeen = now;
}
} else {
initTime = recordActTime-(recordSysUpTime/1000);
/* No need to call ntohl() below as this has already been done by deEndianRecord */
firstSeen = (record->first/1000) + initTime;
lastSeen = (record->last/1000) + initTime;
}
/* Sanity check */
if(readOnlyGlobals.initialSniffTime.tv_sec == 0) {
// readOnlyGlobals.initialSniffTime.tv_sec = firstSeen, readOnlyGlobals.initialSniffTime.tv_usec = 0;
readOnlyGlobals.initialSniffTime.tv_sec = time(NULL), readOnlyGlobals.initialSniffTime.tv_usec = 0;
}
#if 0
/*
The check below has been removed as if the router and
the collector PC are not in sync, we'll end up
overwriting all the timestamps
*/
if(firstSeen < readOnlyGlobals.initialSniffTime.tv_sec)
readOnlyGlobals.initialSniffTime.tv_sec = firstSeen, readOnlyGlobals.initialSniffTime.tv_usec = 0;
#endif
memset(&h, 0, sizeof(h));
// h.ts.tv_sec = readWriteGlobals->now;
h.ts.tv_sec = lastSeen;
/* We avoid that probes with bad time spoil our probe */
if(h.ts.tv_sec > readWriteGlobals->now)
h.ts.tv_sec = readWriteGlobals->now;
#if 0
traceEvent(TRACE_INFO,
"Called addPktToHash() [firstSeen=%u][lastSeen=%u][initial=%u]",
firstSeen, lastSeen, readOnlyGlobals.initialSniffTime.tv_sec);
#endif
record->first = record->last = h.ts.tv_sec;
/* In ASA or other units sometimes the number of packets is not emitted and thus we need to guess it */
if((record->sentPkts == 0) && (record->sentOctets > 0))
record->sentPkts = max(1, record->sentOctets / 512); /* We assume packet size ~512 bytes */
if((record->rcvdPkts == 0) && (record->rcvdOctets > 0))
record->rcvdPkts = max(1, record->rcvdOctets / 512); /* We assume packet size ~512 bytes */
if(record->sentPkts && record->sentOctets)
bkt = processFlowPacket(thread_id,
-1 /* Unknown input interface */,
1 /* RX packet */,
subflow_id, record->proto,
0 /* no fragments */,
0 /* unknown ip_offset */,
0 /* no sample */,
record->sentPkts,
record->tos,
record->maxTTL,
record->vlanId,
0, /* tunnel_id */
0, /* gtp offset */
NULL, /* Ethernet */
&record->srcaddr,
record->srcport,
&record->dstaddr,
record->dstport,
0, NULL, 0, NULL, 0, /* Tunnel info */
record->sentOctets,
record->tcp_flags,
0 /* TCP Win */, 0, /* TCP seq num */ 0 /* TCP ack num */,
0 /* TCP MSS */, 0 /* TCP win scale */,
record->icmpType,
0,
0, NULL, /* MPLS */
record->input, record->output,
&h, NULL, 0, 0, 0, /* payload */
firstSeen,
record->src_as, record->dst_as,
record->src_mask, record->dst_mask,
netflow_device_ip, 0,
record->engine_type, record->engine_id,
NULL, NULL, record);
else if(!(record->rcvdPkts && record->rcvdOctets))
traceEvent(TRACE_INFO, "Received flow with invalid count [sentPkts: %u][sentOctets: %u]: discarded [num_flows: %u]",
record->sentPkts, record->sentOctets,
readWriteGlobals->collectionStats.num_dissected_flow_packets);
if(record->rcvdPkts && record->rcvdOctets) {
bkt = processFlowPacket(thread_id,
-1 /* Unknown input interface */,
1 /* RX packet */,
subflow_id, record->proto,
0 /* no fragments */,
0 /* unknown ip_offset */,
0 /* no sample */,
record->rcvdPkts,
record->tos,
0, /* ttl */
record->vlanId,
0, /* tunnel_id */
0, /* gtp offset */
NULL, /* Ethernet */
&record->dstaddr,
record->dstport,
&record->srcaddr,
record->srcport,
0, NULL, 0, NULL, 0, /* Tunnel info */
record->rcvdOctets,
record->tcp_flags,
0 /* TCP Win */, 0, /* TCP seq num */ 0, /* TCP ack num */
0 /* TCP MSS */, 0 /* TCP win scale */,
record->icmpType,
0,
0, NULL, /* MPLS */
record->output, record->input,
&h, NULL, 0, 0, 0, /* payload */
firstSeen,
record->dst_as, record->src_as,
record->dst_mask, record->src_mask,
netflow_device_ip, 0,
record->engine_type, record->engine_id,
NULL, NULL, record);
} else if(record->rcvdPkts || record->rcvdOctets)
traceEvent(TRACE_INFO, "Received flow with invalid count [rcvdPkts: %u][rcvdOctets: %u]: discarded [num_flows: %u]",
record->rcvdPkts, record->rcvdOctets,
readWriteGlobals->collectionStats.num_dissected_flow_packets);
if(bkt && bkt->ext && record->nexthop.ipVersion) {
if(bkt->ext->nextHop.ipVersion == 0) /* Not yet set */
memcpy(&bkt->ext->nextHop, &record->nexthop, sizeof(IpAddress));
}
/*
NOTE
idleThreadTask() must be called after we have manipulated
the bkt pointer as in case --disable-cache is used, it might
have been freed by idleThreadTask() and thus the bkt pointer
would be invalid
*/
idleThreadTask(thread_id, 4);
}
/* ********************************************************* */
static u_int32_t getField3264to32(V9V10TemplateField *field, char *buffer, u_int32_t divide_by) {
u_int32_t val32;
u_int64_t val64;
switch(field->fieldLen) {
case 4:
memcpy(&val32, buffer, 4);
break;
case 8:
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
buffer[0] & 0xFF, buffer[1] & 0xFF, buffer[2] & 0xFF, buffer[3] & 0xFF,
buffer[4] & 0xFF, buffer[5] & 0xFF, buffer[6] & 0xFF, buffer[7] & 0xFF);
memcpy(&val64, buffer, 8);
val64 = _ntohll(val64) / divide_by;
val32 = htonl((u_int32_t)val64); /* Re-endian */
break;
default:
traceEvent(TRACE_WARNING, "Field %d has unknown length %d",
field->fieldId, field->fieldLen);
val32 = 0;
}
return(val32);
}
/* ********************************************************* */
u_int8_t dissectCustomField(struct generic_netflow_record *record,
char *buffer, int bufferLen,
u_int16_t displ, V9V10TemplateField *field,
u_int32_t netflow_device_ip,
u_int32_t *multiplier,
u_int32_t *packet_offset,
u_int8_t *skip_flow) {
u_int32_t fieldId, len;
switch(field->enterpriseId) {
case 3054: /* IXIA */
fieldId = 35822+field->fieldId;
break;
case 35632: /* ntop */
fieldId = NTOP_BASE_ID+field->fieldId;
break;
default:
fieldId = field->fieldId;
break;
}
#if 0
traceEvent(TRACE_NORMAL, "%s() %u->%u [%u]",
__FUNCTION__, field->fieldId, fieldId, field->enterpriseId);
#endif
switch(fieldId) {
case 300:
memcpy(&record->cisco.observationPointId, &buffer[displ], 4);
record->cisco.observationPointId = htonl(record->cisco.observationPointId);
#ifdef CISCO_DEBUG
traceEvent(TRACE_NORMAL, "[300] observationPointId=%d", record->cisco.observationPointId);
#endif
break;
case 302:
memcpy(&record->cisco.selectorId, &buffer[displ], 2);
record->cisco.selectorId = htons(record->cisco.selectorId);
record->cisco.hasSampling++;
if(record->cisco.hasSampling) {
SelectorsList *head = readWriteGlobals->selectors;
#ifdef CISCO_DEBUG
traceEvent(TRACE_NORMAL, "Searching selectorId = %d", record->cisco.selectorId);
#endif
while(head) {
// traceEvent(TRACE_NORMAL, "%d <-> %d", head->selectorId, record->cisco.selectorId);
if((head->selectorId == record->cisco.selectorId)
&& (head->netflow_device_ip == netflow_device_ip)) {
*multiplier = head->samplingPopulation, *packet_offset = head->packet_offset;
#ifdef CISCO_DEBUG
traceEvent(TRACE_NORMAL, "multiplier = %d / packet_offset = %d",
*multiplier, *packet_offset);
#endif
break;
} else {
head = head->next;
}
} /* while */
}
#ifdef CISCO_DEBUG
traceEvent(TRACE_NORMAL, "[302] selectorId=%d [%02X %02X]",
record->cisco.selectorId,
buffer[displ] & 0xFF, buffer[displ+1] & 0xFF);
#endif
break;
case 310:
memcpy(&record->cisco.samplingPopulation, &buffer[displ], 4);
record->cisco.samplingPopulation = htonl(record->cisco.samplingPopulation);
if(record->cisco.samplingPopulation == 0xFFFFFFFF) {
#ifdef CISCO_DEBUG
traceEvent(TRACE_WARNING, "Found samplingPopulation=%04X for selectorId %d: round it to 1",
record->cisco.samplingPopulation, record->cisco.selectorId);
#endif
record->cisco.samplingPopulation = 1;
}
record->cisco.hasSampling++;
#ifdef CISCO_DEBUG
traceEvent(TRACE_NORMAL, "[310] samplingPopulation=%d [%02X %02X %02X %02X]",
record->cisco.samplingPopulation,
buffer[displ] & 0xFF, buffer[displ+1] & 0xFF,
buffer[displ+2] & 0xFF, buffer[displ+3] & 0xFF);
#endif
break;
case 233: /* ASA Firewall Event */
// traceEvent(TRACE_NORMAL, "[233] %u", buffer[displ] & 0xFF);
if((buffer[displ] & 0xFF) != 0x02 /* Flow Deleted */)
*skip_flow = 1; /*
We skip flow creation as many attributes
are missing and use flow deletion that is richer
*/
break;
case 242: /* New FPGA */
case 312: /* Old FPGA */
memcpy(&record->cisco.original_packet_len, &buffer[displ], 2);
record->cisco.original_packet_len = htons(record->cisco.original_packet_len);
#ifdef CISCO_DEBUG
traceEvent(TRACE_NORMAL, "[312] original_packet_len=%d", record->cisco.original_packet_len);
#endif
break;
/* IXIA */
case 35932:
record->ixia.l7_application_id = ntohl(*((u_int32_t*)&buffer[displ]));
break;
case 35933:
len = min(field->fieldLen, sizeof(record->ixia.l7_application_name)-1);
strncpy(record->ixia.l7_application_name, &buffer[displ], len);
record->ixia.l7_application_name[len] = '\0';
break;
case 35942:
len = min(field->fieldLen, sizeof(record->ixia.src_ip_country)-1);
strncpy(record->ixia.src_ip_country, &buffer[displ], len);
record->ixia.src_ip_country[len] = '\0';
break;
case 35947:
len = min(field->fieldLen, sizeof(record->ixia.src_ip_city)-1);
strncpy(record->ixia.src_ip_city, &buffer[displ], len);
record->ixia.src_ip_city[len] = '\0';
break;
case 35962:
len = min(field->fieldLen, sizeof(record->ixia.dst_ip_country)-1);
strncpy(record->ixia.dst_ip_country, &buffer[displ], len);
record->ixia.dst_ip_country[len] = '\0';
break;
case 35967:
len = min(field->fieldLen, sizeof(record->ixia.dst_ip_city)-1);
strncpy(record->ixia.dst_ip_city, &buffer[displ], len);
record->ixia.dst_ip_city[len] = '\0';
break;
case 35983:
len = min(field->fieldLen, sizeof(record->ixia.os_device_name)-1);
strncpy(record->ixia.os_device_name, &buffer[displ], len);
record->ixia.os_device_name[len] = '\0';
break;
case 35998:
record->rcvdOctets = getField3264to32(field, &buffer[displ], 1);
break;
case 35999:
record->rcvdPkts = getField3264to32(field, &buffer[displ], 1);
break;
/* ntop */
case NTOP_BASE_ID+82: /* NW_LATENCY_SEC */
memcpy(&record->ntop.nw_latency_sec, &buffer[displ], 4);
break;
case NTOP_BASE_ID+83: /* NW_LATENCY_USEC */
memcpy(&record->ntop.nw_latency_usec, &buffer[displ], 4);
break;
/* VoIP Extensions */
case NTOP_BASE_ID+130: /* SIP_CALL_ID */
memcpy(&record->ntop.sip_call_id, &buffer[displ], 50);
#ifdef DEBUG_FLOWS
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "SIP: sip_call_id=%s", record->ntop.sip_call_id);
#endif
break;
case NTOP_BASE_ID+131: /* SIP_CALLING_PARTY */
memcpy(&record->ntop.sip_calling_party, &buffer[displ], 50);
#ifdef DEBUG_FLOWS
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "SIP: sip_calling_party=%s", record->ntop.sip_calling_party);
#endif
break;
case NTOP_BASE_ID+132: /* SIP_CALLED_PARTY */
memcpy(&record->ntop.sip_called_party, &buffer[displ], 50);
#ifdef DEBUG_FLOWS
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "SIP: sip_called_party=%s", record->ntop.sip_called_party);
#endif
break;
default:
return(0);
}
return(1);
}
/* ********************************************************* */
void dissectNetFlow(u_int32_t netflow_device_ip,
char *buffer, int bufferLen) {
NetFlow5Record the5Record;
int flowVersion;
u_int32_t recordActTime = 0, recordSysUpTime = 0;
struct generic_netflow_record record;
pthread_rwlock_wrlock(&readWriteGlobals->collectorCounterLock);
readWriteGlobals->collectionStats.num_dissected_flow_packets++;
pthread_rwlock_unlock(&readWriteGlobals->collectorCounterLock);
memcpy(&the5Record, buffer, bufferLen > sizeof(the5Record) ? sizeof(the5Record): bufferLen);
flowVersion = ntohs(the5Record.flowHeader.version);
#ifdef DEBUG_FLOWS
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "NETFLOW: dissectNetFlow(len=%d) [tot flow packets=%u]", bufferLen,
readWriteGlobals->collectionStats.num_dissected_flow_packets);
#endif
#ifdef DEBUG_FLOWS
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "NETFLOW: +++++++ version=%d", flowVersion);
#endif
/*
Convert V7 flows into V5 flows in order to make ntop
able to handle V7 flows.
Courtesy of Bernd Ziller <bziller@ba-stuttgart.de>
*/
if((flowVersion == 1) || (flowVersion == 7)) {
int numFlows, i;
NetFlow1Record the1Record;
NetFlow7Record the7Record;
if(flowVersion == 1) {
memcpy(&the1Record, buffer, bufferLen > sizeof(the1Record) ?
sizeof(the1Record): bufferLen);
numFlows = ntohs(the1Record.flowHeader.count);
if(numFlows > DEFAULT_V1FLOWS_PER_PACKET) numFlows = DEFAULT_V1FLOWS_PER_PACKET;
recordActTime = ntohl(the1Record.flowHeader.unix_secs);
recordSysUpTime = ntohl(the1Record.flowHeader.sysUptime);
} else {
memcpy(&the7Record, buffer, bufferLen > sizeof(the7Record) ?
sizeof(the7Record): bufferLen);
numFlows = ntohs(the7Record.flowHeader.count);
if(numFlows > DEFAULT_V7FLOWS_PER_PACKET) numFlows = DEFAULT_V1FLOWS_PER_PACKET;
recordActTime = ntohl(the7Record.flowHeader.unix_secs);
recordSysUpTime = ntohl(the7Record.flowHeader.sysUptime);
}
#ifdef DEBUG_FLOWS
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "NETFLOW: +++++++ flows=%d", numFlows);
#endif
the5Record.flowHeader.version = htons(5);
the5Record.flowHeader.count = htons(numFlows);
/* rest of flowHeader will not be used */
for(i=0; i<numFlows; i++) {
if(flowVersion == 7) {
the5Record.flowRecord[i].srcaddr = the7Record.flowRecord[i].srcaddr;
the5Record.flowRecord[i].dstaddr = the7Record.flowRecord[i].dstaddr;
the5Record.flowRecord[i].srcport = the7Record.flowRecord[i].srcport;
the5Record.flowRecord[i].dstport = the7Record.flowRecord[i].dstport;
the5Record.flowRecord[i].dPkts = the7Record.flowRecord[i].dPkts;
the5Record.flowRecord[i].dOctets = the7Record.flowRecord[i].dOctets;
the5Record.flowRecord[i].proto = the7Record.flowRecord[i].proto;
the5Record.flowRecord[i].tos = the7Record.flowRecord[i].tos;
the5Record.flowRecord[i].first = the7Record.flowRecord[i].first;
the5Record.flowRecord[i].last = the7Record.flowRecord[i].last;
the5Record.flowRecord[i].tcp_flags = the7Record.flowRecord[i].tcp_flags;
/* rest of flowRecord will not be used */
} else {
/*
Some NetFlow v1 implementations (e.g. Extreme Networks) are
limited and most of the NetFlow fields are empty. In particular
the following fields are empty:
- input
- output
- dOctets
- first
- last
- tos
- tcp_flags
In this case we add a patch for filling some of the fields
in order to let ntop digest this flow.
*/
the5Record.flowRecord[i].srcaddr = the1Record.flowRecord[i].srcaddr;
the5Record.flowRecord[i].dstaddr = the1Record.flowRecord[i].dstaddr;
the5Record.flowRecord[i].srcport = the1Record.flowRecord[i].srcport;
the5Record.flowRecord[i].dstport = the1Record.flowRecord[i].dstport;
the5Record.flowRecord[i].dPkts = the1Record.flowRecord[i].dPkts;
if(ntohl(the1Record.flowRecord[i].dOctets) == 0) {
/* We assume that all packets are 512 bytes long */
u_int32_t tmp = ntohl(the1Record.flowRecord[i].dPkts);
the5Record.flowRecord[i].dOctets = htonl(tmp*512);
} else
the5Record.flowRecord[i].dOctets = the1Record.flowRecord[i].dOctets;
the5Record.flowRecord[i].proto = the1Record.flowRecord[i].proto;
the5Record.flowRecord[i].tos = the1Record.flowRecord[i].tos;
the5Record.flowRecord[i].first = the1Record.flowRecord[i].first;
the5Record.flowRecord[i].last = the1Record.flowRecord[i].last;
/* rest of flowRecord will not be used */
}
}
} /* DON'T ADD a else here ! */
if((flowVersion == 9) || (flowVersion == 10)) {
/* NetFlowV9/IPFIX Record */
u_char foundRecord = 0, done = 0;
u_int16_t numEntries, displ;
V9IpfixSimpleTemplate template;
int i;
u_char handle_ipfix;
u_int32_t observation_domain_id = 0;
u_int8_t engine_type, engine_id;
u_int32_t sourceId;
u_int8_t skip_flow = 0;
if(flowVersion == 9) handle_ipfix = 0; else handle_ipfix = 1;
if(handle_ipfix) {
struct flow_ipfix_hdr* ipfixh = (struct flow_ipfix_hdr*)&the5Record;
observation_domain_id = ntohl(ipfixh->observationDomainId);
engine_type = observation_domain_id >> 8;
engine_id = observation_domain_id & 0xFF;
numEntries = ntohs(the5Record.flowHeader.count), displ = sizeof(V9FlowHeader)-4; // FIX
if(readOnlyGlobals.enable_debug) traceEvent(TRACE_INFO, "IPFIX Length: %d", numEntries);
} else {
V9FlowHeader *v9hdr = (V9FlowHeader*)&the5Record;
observation_domain_id = sourceId = ntohl(v9hdr->sourceId);
engine_type = sourceId >> 8;
engine_id = sourceId & 0xFF;
numEntries = ntohs(the5Record.flowHeader.count), displ = sizeof(V9FlowHeader);
}
recordActTime = ntohl(the5Record.flowHeader.unix_secs);
recordSysUpTime = ntohl(the5Record.flowHeader.sysUptime);
/* NTOHL(recordActTime); NTOHL(recordSysUpTime); */
for(i=0; (!done) && (displ < bufferLen) && (i < numEntries); i++) {
V9V10TemplateField *fields = NULL;
int16_t stillToProcess; /* Do not change to uint: this way I can catch template length issues */
/* 1st byte */
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_INFO, "[displ=%d][%02X %02X %02X]",
displ, buffer[displ] & 0xFF,
buffer[displ+1] & 0xFF,
buffer[displ+2] & 0xFF);
if(buffer[displ] == 0) {
u_int8_t isOptionTemplate = (u_char)buffer[displ+1];
/* Template */
if(handle_ipfix
&& (isOptionTemplate == 2 /* Template Flowset */)) {
/*
IPFIX (isOptionTemplate)
A value of 2 is reserved for the
Template Set. A value of 3 is reserved for the Option Template
Set. All other values from 4 to 255 are reserved for future use.
Values above 255 are used for Data Sets. The Set ID values of 0
and 1 are not used for historical reasons
*/
/* This trick is necessary as only option template flowsets
have to be handled differently from other templates
*/
isOptionTemplate = 0;
}
if(readOnlyGlobals.enable_debug) {
traceEvent(TRACE_INFO, "Found Template [displ=%d][sourceId: %u]", displ, sourceId);
traceEvent(TRACE_INFO, "Found Template Type: %s", isOptionTemplate ? "Option" : "Flow");
}
if(bufferLen > (displ+sizeof(V9TemplateHeader))) {
V9TemplateHeader header;
u_int8_t templateDone = 0;
memcpy(&header, &buffer[displ], sizeof(V9TemplateHeader));
header.templateFlowset = ntohs(header.templateFlowset), header.flowsetLen = ntohs(header.flowsetLen);
stillToProcess = header.flowsetLen - sizeof(V9TemplateHeader);
displ += sizeof(V9TemplateHeader);
while((bufferLen >= (displ+stillToProcess)) && (!templateDone)) {
FlowSetV9Ipfix *cursor = NULL;
u_int16_t len = 0;
int fieldId;
u_char goodTemplate = 0;
u_int accumulatedLen = 0;
memset(&template, 0, sizeof(template));
template.isOptionTemplate = isOptionTemplate, template.netflow_device_ip = netflow_device_ip;
if(isOptionTemplate) {
memcpy(&template.templateId, &buffer[displ], 2);
template.templateId = htons(template.templateId), template.fieldCount = (header.flowsetLen - 14)/4;
if(handle_ipfix) {
u_int16_t tot_field_count, tot_scope_field_count;
displ += 2, stillToProcess -= 2 /*, len += 2 */;
memcpy(&tot_field_count, &buffer[displ], 2); tot_field_count = htons(tot_field_count);
displ += 2, stillToProcess -= 2 /* , len += 2 */;
memcpy(&tot_scope_field_count, &buffer[displ], 2); tot_scope_field_count = htons(tot_scope_field_count);
displ += 2, stillToProcess -= 2 /* , len += 2 */;
template.scopeFieldCount = tot_scope_field_count;
if(tot_field_count >= tot_scope_field_count) {
u_int num = tot_scope_field_count * 4; /* FIX: check PEN here */
u_int field_num = (tot_field_count-tot_scope_field_count) * 4;
u_int delta = num + field_num;
displ += delta, stillToProcess -= delta /* , len += num */;
} else {
traceEvent(TRACE_WARNING,
"It looks looks like the template is broken (tot_field_count=%d, tot_scope_field_count=%d) "
"[num_dissected_flows=%u][templateType=%d]",
tot_field_count, tot_scope_field_count,
readWriteGlobals->collectionStats.num_dissected_flow_packets, isOptionTemplate);
displ += 4 /* , len += 4 */; /* Using default skip */
}
} else {
memcpy(&template.v9ScopeLen, &buffer[displ+8], 2);
template.v9ScopeLen = htons(template.v9ScopeLen);
displ += 10, /* len = 0, */ stillToProcess -= 10;
}
} else {
V9TemplateDef templateDef;
memcpy(&templateDef, &buffer[displ], sizeof(V9TemplateDef));
displ += sizeof(V9TemplateDef), len = 0, stillToProcess -= sizeof(V9TemplateDef);
template.templateId = htons(templateDef.templateId), template.fieldCount = htons(templateDef.fieldCount);
}
if(template.fieldCount > 128) {
traceEvent(TRACE_WARNING, "Too many template fields (%d): skept [pktId: %u]",
template.fieldCount,
readWriteGlobals->collectionStats.num_dissected_flow_packets);
goodTemplate = 0;
} else if(template.fieldCount == 0) {
traceEvent(TRACE_WARNING, "No fields defined on template %d: skept [pktId: %u]",
template.templateId,
readWriteGlobals->collectionStats.num_dissected_flow_packets);
goodTemplate = 0;
} else {
if(handle_ipfix) {
fields = (V9V10TemplateField*)malloc(template.fieldCount * sizeof(V9V10TemplateField));
if(fields == NULL) {
traceEvent(TRACE_WARNING, "Not enough memory");
break;
}
if(((template.fieldCount * 4) + sizeof(FlowSet) + 4 /* templateFlowSet + FlowsetLen */) > header.flowsetLen) {
traceEvent(TRACE_WARNING, "Bad length [expected=%d][real=%d]",
template.fieldCount * 4,
numEntries + sizeof(FlowSet));
} else {
goodTemplate = 1;
if(bufferLen < (displ+stillToProcess)) {
traceEvent(TRACE_INFO, "Broken flow format (bad length) [received: %u][displ: %u][stillToProcess: %u][available: %u]",
bufferLen, displ, stillToProcess, (displ+stillToProcess));
return;
}
/* Check the template before handling it */
for(fieldId=0; fieldId < template.fieldCount; fieldId++) {
u_int32_t enterprise_id = 0;
u_int8_t is_enterprise_specific = (buffer[displ+len] & 0x80) ? 1 : 0;
V9FlowSet *set = (V9FlowSet*)&buffer[displ+len];
len += 4; /* Field Type (2) + Field Length (2) */
if(is_enterprise_specific) {
enterprise_id = ntohl(*(u_int32_t*)&buffer[displ+len]);
len += 4; /* PEN (Private Enterprise Number) */
}
fields[fieldId].fieldId = htons(set->templateId) & 0x7FFF;
fields[fieldId].fieldLen = htons(set->flowsetLen);
fields[fieldId].isPenField = is_enterprise_specific;
fields[fieldId].enterpriseId = enterprise_id;
if(fields[fieldId].fieldLen != (u_int16_t)-1) /* Variable lenght fields */
accumulatedLen += fields[fieldId].fieldLen;
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_NORMAL, "[%d] fieldId=%d/PEN=%d/len=%d [tot=%d]",
1+fieldId, fields[fieldId].fieldId,
is_enterprise_specific, fields[fieldId].fieldLen, len);
}
template.flowsetLen = len;
}
} else {
/* NetFlow */
fields = (V9V10TemplateField*)malloc(template.fieldCount * sizeof(V9V10TemplateField));
if(fields == NULL) {
traceEvent(TRACE_WARNING, "Not enough memory");
break;
}
goodTemplate = 1;
template.flowsetLen = 4 * template.fieldCount;
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_NORMAL, "Template [id=%d] fields: %d", template.templateId, template.fieldCount);
/* Check the template before handling it */
for(fieldId=0;fieldId < template.fieldCount; fieldId++) {
V9FlowSet *set = (V9FlowSet*)&buffer[displ+len];
fields[fieldId].fieldId = htons(set->templateId);
fields[fieldId].fieldLen = htons(set->flowsetLen);
fields[fieldId].isPenField = (fields[fieldId].fieldId >= NTOP_BASE_ID) ? 1 : 0;
fields[fieldId].enterpriseId = 0;
len += 4; /* Field Type (2) + Field Length (2) */
accumulatedLen += fields[fieldId].fieldLen;
if(readOnlyGlobals.enable_debug)
traceEvent(TRACE_NORMAL, "[%d] fieldId=%d (%s)/fieldLen=%d/totLen=%d/templateLen=%d [%02X %02X %02X %02X]",
1+fieldId, fields[fieldId].fieldId,