Skip to content

Commit 5cd6645

Browse files
committed
Fix ja3 pre-processing in filter
1 parent e3f4436 commit 5cd6645

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/libnfdump/filter/filter.c

+11-13
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,14 @@ static void *ja3_preproc(uint32_t length, data_t data, recordHandle_t *handle) {
318318
// return ja3 string if it already exists
319319
if (handle->extensionList[JA3index]) return handle->extensionList[JA3index];
320320

321-
ssl_t *ssl = ssl_preproc(length, data, handle);
321+
ssl_t *ssl = handle->extensionList[SSLindex];
322+
if (ssl == NULL) ssl = ssl_preproc(length, data, handle);
322323
if (!ssl) return NULL;
323324

324-
return ja3Process(ssl, NULL);
325+
handle->extensionList[SSLindex] = (void *)ssl;
326+
handle->extensionList[JA3index] = ja3Process(ssl, NULL);
327+
328+
return handle->extensionList[JA3index];
325329

326330
} // End of ja3_preproc
327331

@@ -336,17 +340,10 @@ static void *ja4_preproc(uint32_t length, data_t data, recordHandle_t *handle) {
336340

337341
ssl_t *ssl = ssl_preproc(length, data, handle);
338342
if (ssl == NULL || ssl->type != CLIENTssl) return NULL;
339-
ja4_t *ja4 = malloc(sizeof(ja4_t) + SIZEja4String + 1);
340-
if (!ja4) {
341-
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
342-
return NULL;
343-
}
344-
ja4 = ja4Process(ssl, genericFlow->proto);
345-
if (ja4) {
346-
handle->extensionList[JA4index] = (void *)ja4;
347-
return (void *)ja4;
348-
}
349-
return NULL;
343+
344+
handle->extensionList[JA4index] = (void *)ja4Process(ssl, genericFlow->proto);
345+
return handle->extensionList[JA4index];
346+
350347
} // End of ja4_preproc
351348

352349
static void *as_preproc(uint32_t length, data_t data, recordHandle_t *handle) {
@@ -856,6 +853,7 @@ void *CompileFilter(char *FilterSyntax) {
856853
LogError("Memory allocation error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
857854
exit(255);
858855
}
856+
859857
*engine = (FilterEngine_t){
860858
.label = NULL,
861859
.StartNode = StartNode,

src/nfdump/nflowcache.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,10 @@ static inline void flowHash_resize(flowHash_t *flowHash) {
453453
* 1 - value was inserted.
454454
* returns the index into the stat record array of new or existing value
455455
*/
456-
static inline int flowHash_add(flowHash_t *flowHash, const hashValue_t *value, int *insert) {
456+
static inline int flowHash_add(flowHash_t *flowHash, const hashValue_t value, int *insert) {
457457
if (flowHash->count == flowHash->load_factor) flowHash_resize(flowHash);
458458

459-
uint32_t hash = value->hash;
459+
uint32_t hash = value.hash;
460460
// cell address
461461
uint32_t cell = ___fib_hash(hash, flowHash->shift);
462462

@@ -465,7 +465,7 @@ static inline int flowHash_add(flowHash_t *flowHash, const hashValue_t *value, i
465465
if (is_free(flowHash->flags, cell)) {
466466
int index = flowHash->count++;
467467
flowHash->flags[cell] = flag;
468-
flowHash->cells[cell] = *value;
468+
flowHash->cells[cell] = value;
469469
flowHash->cells[cell].index = index;
470470
*insert = 1;
471471
return index;
@@ -481,13 +481,13 @@ static inline int flowHash_add(flowHash_t *flowHash, const hashValue_t *value, i
481481
// free cell found
482482
int index = flowHash->count++;
483483
flowHash->flags[cell] = flag;
484-
flowHash->cells[cell] = *value;
484+
flowHash->cells[cell] = value;
485485
flowHash->cells[cell].index = index;
486486
*insert = 1;
487487
return index;
488488
} else {
489489
// cell with matching flag
490-
if (valCompare(flowHash->cells[cell], *value)) {
490+
if (valCompare(flowHash->cells[cell], value)) {
491491
// existing value found
492492
*insert = 0;
493493
return flowHash->cells[cell].index;
@@ -505,8 +505,8 @@ static inline int flowHash_add(flowHash_t *flowHash, const hashValue_t *value, i
505505
* index into the stat record array if found
506506
* -1 if value does not exists
507507
*/
508-
static inline int flowHash_get(flowHash_t *flowHash, hashValue_t *value) {
509-
uint32_t hash = value->hash;
508+
static inline int flowHash_get(flowHash_t *flowHash, const hashValue_t value) {
509+
uint32_t hash = value.hash;
510510
// cell address
511511
uint32_t cell = ___fib_hash(hash, flowHash->shift);
512512

@@ -521,7 +521,7 @@ static inline int flowHash_get(flowHash_t *flowHash, hashValue_t *value) {
521521
if (++cell == flowHash->capacity) cell = 0;
522522

523523
if (is_free(flowHash->flags, cell)) return -1;
524-
if (valCompare(flowHash->cells[cell], *value)) return flowHash->cells[cell].index;
524+
if (valCompare(flowHash->cells[cell], value)) return flowHash->cells[cell].index;
525525

526526
// collision - flag matches but compare does not - loop
527527
if (++cell == flowHash->capacity) cell = 0;
@@ -1309,7 +1309,7 @@ static void AddBidirFlow(recordHandle_t *recordHandle) {
13091309
// generate 32bit hash from hash value
13101310
hashValue.hash = SuperFastHash(*keymem, keyLen);
13111311

1312-
int index = flowHash_get(flowHash, &hashValue);
1312+
int index = flowHash_get(flowHash, hashValue);
13131313
if (index >= 0) {
13141314
// flow record found - update all fields
13151315
flowHash->records[index].inBytes += inBytes;
@@ -1329,7 +1329,7 @@ static void AddBidirFlow(recordHandle_t *recordHandle) {
13291329
} else if (genericFlow->proto != IPPROTO_TCP && genericFlow->proto != IPPROTO_UDP) {
13301330
// no flow record found and no TCP/UDP bidir flows. Insert flow record into hash
13311331
int insert;
1332-
index = flowHash_add(flowHash, &hashValue, &insert);
1332+
index = flowHash_add(flowHash, hashValue, &insert);
13331333
flowHash->records[index].inBytes = inBytes;
13341334
flowHash->records[index].inPackets = inPackets;
13351335
flowHash->records[index].outBytes = outBytes;
@@ -1359,7 +1359,7 @@ static void AddBidirFlow(recordHandle_t *recordHandle) {
13591359
New_HashKey(*keymem, recordHandle, 1);
13601360
hashValue.hash = SuperFastHash(*keymem, keyLen);
13611361

1362-
index = flowHash_get(flowHash, &hashValue);
1362+
index = flowHash_get(flowHash, hashValue);
13631363
if (index >= 0) {
13641364
// we found a corresponding reverse flow - so update all fields in reverse direction
13651365
flowHash->records[index].outBytes += inBytes;
@@ -1383,7 +1383,7 @@ static void AddBidirFlow(recordHandle_t *recordHandle) {
13831383
hashValue.hash = SuperFastHash(*keymem, keyLen);
13841384

13851385
int insert;
1386-
index = flowHash_add(flowHash, &hashValue, &insert);
1386+
index = flowHash_add(flowHash, hashValue, &insert);
13871387
flowHash->records[index].inBytes = inBytes;
13881388
flowHash->records[index].inPackets = inPackets;
13891389
flowHash->records[index].outBytes = outBytes;
@@ -1467,7 +1467,7 @@ void AddFlowCache(recordHandle_t *recordHandle) {
14671467
hashValue.hash = SuperFastHash(*keymem, keyLen);
14681468

14691469
int insert;
1470-
int index = flowHash_add(flowHash, &hashValue, &insert);
1470+
int index = flowHash_add(flowHash, hashValue, &insert);
14711471
if (insert == 0) {
14721472
// flow record found - update all fields
14731473
flowHash->records[index].inBytes += inBytes;

0 commit comments

Comments
 (0)