-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexport.c
2561 lines (2147 loc) · 86.6 KB
/
export.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) 2016 Eneo Tecnologia S.L.
Author: Eugenio Perez <eupm90@gmail.com>
Based on Luca Deri nprobe 6.22 collector
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "export.h"
#include "util.h"
#include "rb_mac.h"
#include "rb_sensor.h"
#ifdef HAVE_UDNS
#include "rb_dns_cache.h"
#endif
#include <librd/rdfile.h>
#define NETFLOW_DIRECTION_INGRESS 0
#define NETFLOW_DIRECTION_EGRESS 1
typedef size_t (*entity_fn)(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flowCache);
#define assert_multi(...) do {size_t assert_i; \
for(assert_i=0; \
assert_i<sizeof((const void *[]){__VA_ARGS__})/sizeof(const void *);\
++assert_i) {assert(((const void *[]){__VA_ARGS__})[assert_i]);}}while(0)
/// Get rid of unused parameters
static void unused_params0(const void *p,...) {(void)p;}
#define unused_params(p...) unused_params0(&p)
typedef enum {
DIRECTION_UNSET, ///< Unknown traffic direction
DIRECTION_UPSTREAM, ///< Traffic goes from LAN to WAN
DIRECTION_DOWNSTREAM, ///< Traffic goes from WAN to LAN
DIRECTION_INTERNAL, ///< Traffic is home_net internal
} direction_t;
struct flowCache *new_flowCache(){
return calloc(1,sizeof(struct flowCache));
}
void associateSensor(struct flowCache *flowCache, struct sensor *sensor){
assert_multi(flowCache, sensor);
flowCache->sensor = sensor;
}
void free_flowCache(struct flowCache *cache){
free(cache);
}
static int ip_direction(int known_src,int known_dst) {
if(!known_src && known_dst) {
return DIRECTION_DOWNSTREAM;
} else if(known_src && !known_dst) {
return DIRECTION_UPSTREAM;
} else if(known_src && known_dst) {
return DIRECTION_INTERNAL;
}
// No "external"
return DIRECTION_UNSET;
}
/*
Try to guess direction based on source and destination address
return: true if guessed/already setted. false if couldn't set
*/
bool guessDirection(struct flowCache *cache) {
assert(cache);
static const char zeros[sizeof(cache->address.src)] = {0};
if (0 == memcmp(cache->address.src, zeros, sizeof(cache->address.src)) ||
0 == memcmp(cache->address.dst, zeros, sizeof(cache->address.dst))) {
/* Can't guess direction */
return false;
}
const int src_ip_in_home_net = NULL!=network_ip(cache->observation_id,
cache->address.src);
const int dst_ip_in_home_net = NULL!=network_ip(cache->observation_id,
cache->address.dst);
const int ip_guessed_direction = ip_direction(src_ip_in_home_net,dst_ip_in_home_net);
if (ip_guessed_direction != DIRECTION_UNSET) {
cache->macs.direction = ip_guessed_direction;
return true;
}
return true;
}
#if 0
/* Just for templating */
/*
* Function: C++ version 0.4 char* style "itoa", Written by Lukás Chmela. (Modified)
*
* Purpose: Fast itoa conversion. snprintf is slow.
*
* Arguments: value => Number.
* result => Where to save result
* base => Number base.
*
* Return: result
* TODO: Return writed buffer lenght.
*
*/
static char* _itoa(uint64_t value, char* result, int base, size_t bufsize) {
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char *ptr = result+bufsize;
uint64_t tmp_value;
*--ptr = '\0';
do {
tmp_value = value;
value /= base;
*--ptr = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
if (tmp_value < 0) *--ptr = '-';
return ptr;
}
#endif
static char* _itoa10(int64_t value, char* result, size_t bufsize) {
assert(result);
char *ptr = result+bufsize;
int64_t tmp_value;
*--ptr = '\0';
do {
tmp_value = value;
value /= 10;
*--ptr = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * 10)];
} while ( value );
if (tmp_value < 0) *--ptr = '-';
return ptr;
}
static size_t printbuf_memappend_fast_n10(struct printbuf *kafka_line_buffer,const uint64_t value){
assert(kafka_line_buffer);
static const size_t bufsize = 64;
char buf[bufsize];
const char *buf_start = _itoa10(value,buf,bufsize);
const size_t number_strlen = buf+bufsize-buf_start-1;
printbuf_memappend_fast(kafka_line_buffer,buf_start,number_strlen);
return number_strlen;
}
#define get_mac(buffer) net2number(buffer,6);
size_t print_string(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flowCache){
const char *buffer = vbuffer;
assert_multi(kafka_line_buffer, vbuffer);
unused_params(flowCache);
const size_t bef_len = kafka_line_buffer->bpos;
printbuf_memappend_fast(kafka_line_buffer, buffer, real_field_len);
return kafka_line_buffer->bpos - bef_len;
}
#define SAVE_FLOWCACHE_NUMBER_PARAMETER(kafka_line_buffer, buffer, \
real_field_len, flowCache, parameter) do { \
assert_multi(buffer, flowCache); unused_params(kafka_line_buffer); \
flowCache->parameter = net2number( \
(const uint8_t *)buffer, real_field_len); \
return 0; } while(0)
size_t save_first_second(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
SAVE_FLOWCACHE_NUMBER_PARAMETER(kafka_line_buffer, buffer,
real_field_len, flowCache, time.first_timestamp_s);
}
size_t save_last_second(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
SAVE_FLOWCACHE_NUMBER_PARAMETER(kafka_line_buffer, buffer,
real_field_len, flowCache, time.last_timestamp_s);
}
/** Auxiliar function for first/last switched
* @param dst Destination to save buffer
* @param buffer Uptime buffer
* @param real_field_len Buffer length
*/
static void save_x_switched(uint64_t *dst, const void *vbuffer,
const size_t real_field_len) {
const uint8_t *buffer = vbuffer;
assert_multi(dst, buffer);
// uptime switched in miliseconds
*dst = net2number(buffer, real_field_len)/1000;
}
size_t save_first_switched(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
unused_params(kafka_line_buffer);
save_x_switched(&flowCache->time.first_switched_uptime_s,
buffer, real_field_len);
return 0;
}
size_t save_last_switched(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
unused_params(kafka_line_buffer);
save_x_switched(&flowCache->time.last_switched_uptime_s,
buffer, real_field_len);
return 0;
}
size_t save_flow_bytes(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
SAVE_FLOWCACHE_NUMBER_PARAMETER(kafka_line_buffer, buffer, real_field_len,
flowCache, bytes);
}
size_t save_flow_pkts(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
SAVE_FLOWCACHE_NUMBER_PARAMETER(kafka_line_buffer, buffer, real_field_len,
flowCache, packets);
}
/** Calls cb assuming that buffers brings the number in mili(units)
* @param cb Callback to call with number(buffer)/1000
* @param kafka_line_buffer Kafka line buffer to use with cb
* @param buffer Buffer that contains the mili-value
* @param real_field_len Length of buffer
* @param flowCache Flow cache to call with number(buffer)/1000
* @return Callback return
*/
static size_t callback_mili_buffer(entity_fn cb,
struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flowCache) {
const uint8_t *buffer = vbuffer;
assert_multi(buffer);
const uint64_t number = net2number(buffer, real_field_len);
const uint64_t be_number = htonll(number);
return cb(kafka_line_buffer, &be_number, sizeof(be_number), flowCache);
}
size_t save_first_msecond(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
return callback_mili_buffer(save_first_second, kafka_line_buffer,
buffer, real_field_len, flowCache);
}
size_t save_last_msecond(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
return callback_mili_buffer(save_last_second, kafka_line_buffer,
buffer, real_field_len, flowCache);
}
size_t print_tcp_flags(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
// Avoid warning storm
static bool warned = false;
const size_t start_bpos = kafka_line_buffer->bpos;
char tcp_flags_str[8];
size_t i;
assert_multi(kafka_line_buffer, buffer);
unused_params(flowCache);
if (unlikely(real_field_len != 1 && real_field_len != 2)) {
if (unlikely(ATOMIC_TEST_AND_SET(&warned))) {
traceEvent(TRACE_ERROR, "TCP flags length %zu not in (1,2)",
real_field_len);
}
return 0;
}
const uint8_t tcp_flags = 1==real_field_len ? ((const uint8_t *)buffer)[0] :
((const uint8_t *)buffer)[1];
if (0 == tcp_flags) {
// Not interesting
return 0;
}
for (i=0; i<RD_ARRAYSIZE(tcp_flags_str); ++i) {
// 0x80, CWR Congestion Window Reduced
// 0x40, ECE ECN Echo
// 0x20, URG Urgent Pointer
// 0x10, ACK Acknowledgment
// 0x08, PSH Push Function
// 0x04, RST Reset the connection
// 0x02, SYN Synchronize sequence numbers
// 0x01, FIN No more data from sender'
static const char flag_id_char[] = "CEUAPRSF";
tcp_flags_str[i] = (tcp_flags & 1<<(7-i)) ? flag_id_char[i] : '.';
}
printbuf_memappend_fast(kafka_line_buffer, tcp_flags_str,
sizeof(tcp_flags_str));
return kafka_line_buffer->bpos - start_bpos;
}
size_t print_number(struct printbuf *kafka_line_buffer, const void *vbuffer,
const size_t real_field_len, struct flowCache *flowCache) {
const uint8_t *buffer = vbuffer;
assert_multi(kafka_line_buffer, buffer);
unused_params(flowCache);
const uint64_t number = net2number(buffer, real_field_len);
return printbuf_memappend_fast_n10(kafka_line_buffer,number);
}
size_t print_netflow_type(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flowCache) {
static const char *type_msg = "netflowv";
const uint8_t *buffer = vbuffer;
assert_multi(kafka_line_buffer, buffer);
unused_params(flowCache);
const size_t start_bpos = kafka_line_buffer->bpos;
const uint64_t type = net2number(buffer, real_field_len);
printbuf_memappend_fast(kafka_line_buffer,type_msg,strlen(type_msg));
printbuf_memappend_fast_n10(kafka_line_buffer,type);
return kafka_line_buffer->bpos - start_bpos;
}
/**
* Append string in print buffer and return increased length
* @param kafka_line_buffer Line buffer to append into
* @param str String to append
* @return Increased length
*/
static size_t printbuf_memappend_fast_string(struct printbuf *kafka_line_buffer,const char *str){
assert_multi(kafka_line_buffer, str);
const size_t len = strlen(str);
printbuf_memappend_fast(kafka_line_buffer,str,len);
return len;
}
size_t print_flow_end_reason(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flowCache) {
const char *buffer = vbuffer;
static const char *reasons[] = {
[1] = "idle timeout",
[2] = "active timeout",
[3] = "end of flow",
[4] = "forced end",
[5] = "lack of resources",
};
const uint8_t reason = buffer[0];
assert_multi(kafka_line_buffer, buffer);
unused_params(real_field_len, flowCache);
assert(real_field_len > 0);
if (likely(reason > 0 && reason < sizeof(reasons)/sizeof(reasons[0]))) {
return printbuf_memappend_fast_string(kafka_line_buffer, reasons[reason]);
} else {
traceEvent(TRACE_WARNING,"UNKNOWN flow end reason %d",buffer[0]);
return 0;
}
}
size_t print_biflow_direction(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flowCache) {
static const char *directions[] = {
[0] = "arbitrary",
[1] = "initiator",
[2] = "reverse initiator",
[3] = "perimeter",
};
const uint8_t *buffer = vbuffer;
const uint8_t direction = buffer[0];
assert(real_field_len > 0);
unused_params(real_field_len, flowCache);
assert_multi(kafka_line_buffer, buffer);
if (likely(direction < RD_ARRAYSIZE(directions))) {
return printbuf_memappend_fast_string(kafka_line_buffer,
directions[direction]);
} else {
traceEvent(TRACE_WARNING,"UNKNOWN buflow direction: %d",buffer[0]);
return 0;
}
}
static size_t print_netflow_direction(struct printbuf *kafka_line_buffer,
const uint8_t direction) {
return printbuf_memappend_fast_string(kafka_line_buffer,
(direction == NETFLOW_DIRECTION_INGRESS) ? "ingress" :
(direction == NETFLOW_DIRECTION_EGRESS) ? "egress" :
"");
}
size_t print_flow_cache_direction(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flow_cache) {
assert_multi(kafka_line_buffer, flow_cache);
unused_params(buffer, real_field_len);
const char *to_print = "";
switch (flow_cache->macs.direction) {
case DIRECTION_UPSTREAM:
to_print = "upstream";
break;
case DIRECTION_DOWNSTREAM:
to_print = "downstream";
break;
case DIRECTION_INTERNAL:
to_print = "internal";
break;
default:
break;
}
return printbuf_memappend_fast_string(kafka_line_buffer, to_print);
}
size_t process_direction(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flow_cache) {
const uint8_t *buffer = vbuffer;
assert_multi(buffer);
unused_params(real_field_len, kafka_line_buffer);
const uint64_t direction = net2number(buffer, real_field_len);
if (unlikely(direction > 1)) {
traceEvent(TRACE_ERROR, "Unknown netflow direction %"PRIu64, direction);
return 0;
}
if (readOnlyGlobals.normalize_directions) {
uint64_t netflow_direction = direction;
if (is_exporter_in_wan_side(flow_cache->observation_id)) {
netflow_direction = !netflow_direction;
}
flow_cache->macs.direction =
(netflow_direction == NETFLOW_DIRECTION_INGRESS) ? DIRECTION_UPSTREAM :
DIRECTION_DOWNSTREAM;
return 0; /* nothing printed */
} else {
return print_netflow_direction(kafka_line_buffer, direction);
}
}
/**
* Save MAC address in given buffer
* @param dst_buffer Destination buffer (have to be sizeof()>6)
* @param src_buffer_mac_name MAC name description for errors
* @param kafka_line_buffer unused
* @param vbuffer Buffer where MAC address is
* @param real_field_len Length of buffer (needs to be 6)
* @param flowCache unused
*/
static void save_mac(uint8_t *dst_buffer, const char *src_buffer_mac_name,
const void *vbuffer, const size_t real_field_len) {
const uint8_t *buffer = vbuffer;
assert_multi(buffer);
if (likely(real_field_len == 6)) {
memcpy(dst_buffer, buffer, 6);
} else {
traceEvent(TRACE_WARNING, "%s length != 6.", src_buffer_mac_name);
}
}
/**
* Process a given MAC
* @param dst_buffer Buffer to save if we are going to process it
* later
* @param src_buffer_mac_name MAC name for debug purposes
* @param kafka_line_buffer Buffer to print MAC
* @param vbuffer MAC buffer
* @param real_field_len MAC length
* @return [description]
*/
static size_t process_mac0(uint8_t *dst_buffer, const char *src_buffer_mac_name,
struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len) {
if (readOnlyGlobals.normalize_directions) {
save_mac(dst_buffer, src_buffer_mac_name, vbuffer, real_field_len);
return 0;
} else {
return print_mac(kafka_line_buffer, vbuffer, real_field_len, NULL);
}
}
size_t process_src_mac(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flow_cache) {
unused_params(flow_cache);
return process_mac0(flow_cache->macs.src_mac, "Source mac",
kafka_line_buffer, buffer, real_field_len);
}
size_t process_post_src_mac(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flow_cache) {
unused_params(flow_cache);
return process_mac0(flow_cache->macs.post_src_mac, "PST Source mac",
kafka_line_buffer, buffer, real_field_len);
}
size_t process_dst_mac(struct printbuf *kafka_line_buffer, const void *buffer,
const size_t real_field_len, struct flowCache *flow_cache) {
unused_params(flow_cache);
return process_mac0(flow_cache->macs.dst_mac, "DST mac",
kafka_line_buffer, buffer, real_field_len);
}
size_t process_post_dst_mac(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flow_cache) {
unused_params(flow_cache);
return process_mac0(flow_cache->macs.post_dst_mac, "POST DST mac",
kafka_line_buffer, buffer, real_field_len);
}
static size_t print_ssid_name0(struct printbuf *kafka_line_buffer,const void *buffer,const uint16_t real_field_len){
const size_t len = strnlen(buffer,real_field_len);
printbuf_memappend_fast(kafka_line_buffer,buffer,len);
return len;
}
size_t print_ssid_name(struct printbuf *kafka_line_buffer,
const void *vbuffer,const size_t real_field_len,
struct flowCache *flowCache) {
const uint8_t *buffer = vbuffer;
assert_multi(kafka_line_buffer, buffer);
unused_params(flowCache);
return print_ssid_name0(kafka_line_buffer, buffer, real_field_len);
}
/** Transforms ipv4 buffer in ipv6
@param vbuffer Buffer of ipv6
@param ipv6 Buffer to sabe IP
*/
static void ipv4buf_to_6(uint8_t ipv6[16],const void *vbuffer){
const uint8_t *buffer = vbuffer;
assert_multi(ipv6, buffer);
int i;
for (i = 0; i < 10; i++)
ipv6[i] = 0;
ipv6[10] = 0xFF;
ipv6[11] = 0xFF;
ipv6[12] = buffer[0];
ipv6[13] = buffer[1];
ipv6[14] = buffer[2];
ipv6[15] = buffer[3];
}
/** Prints net information
@param kafka_line_buffer Buffer to print net information
@param vbuffer Buffer where net is
@param real_field_len Length of buffer
@param flowCache Flow cache information
@param sensor_list_search_cb Callback to search in sensor information
@param global_net_list_cb Callback to manage global nets list.
@return Printed length
*/
static size_t print_net0(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flowCache,
const char *(*sensor_list_search_cb)(observation_id_t *,const uint8_t[16]),
const char *(*global_net_list_cb)(const IPNameAssoc *)) {
const uint8_t *buffer = vbuffer;
assert_multi(kafka_line_buffer, buffer, flowCache);
if (unlikely(16 != real_field_len)) {
traceEvent(TRACE_ERROR, "IPv6 net length %zu != 16", real_field_len);
return 0;
}
/* First try: Has the observation id a home net that contains this ip? */
const char *sensor_home_net = sensor_list_search_cb(flowCache->observation_id,
buffer);
if(sensor_home_net){
return printbuf_memappend_fast_string(kafka_line_buffer, sensor_home_net);
}
/* Second try: General nets ip list */
const IPNameAssoc *ip_name_as = ipInList(buffer,
readOnlyGlobals.rb_databases.nets_name_as_list);
if (ip_name_as) {
const char *to_print = global_net_list_cb(ip_name_as);
return printbuf_memappend_fast_string(kafka_line_buffer, to_print);
} else {
/* Nothing more to do, sorry */
return 0;
}
}
static const char *global_net_list_number(const IPNameAssoc *assoc) {
return assoc->number;
}
static const char *global_net_list_name(const IPNameAssoc *assoc) {
return assoc->name;
}
static size_t print_net_v6_0(struct printbuf *kafka_line_buffer,
const void *vbuffer,const size_t real_field_len,
struct flowCache *flowCache) {
return print_net0(kafka_line_buffer, vbuffer, real_field_len, flowCache,
network_ip, global_net_list_number);
}
static size_t print_net_name_v6_0(struct printbuf *kafka_line_buffer,
const void *vbuffer,const size_t real_field_len,
struct flowCache *flowCache) {
return print_net0(kafka_line_buffer, vbuffer, real_field_len, flowCache,
network_name, global_net_list_name);
}
size_t print_net_v6(struct printbuf *kafka_line_buffer,
const void *buffer,const size_t real_field_len,
struct flowCache *flow_cache) {
return (!readOnlyGlobals.normalize_directions) ?
print_net_v6_0(kafka_line_buffer, buffer, real_field_len, flow_cache) : 0;
}
size_t print_net_name_v6(struct printbuf *kafka_line_buffer,
const void *buffer,const size_t real_field_len,
struct flowCache *flow_cache) {
return (!readOnlyGlobals.normalize_directions) ?
print_net_name_v6_0(kafka_line_buffer, buffer, real_field_len, flow_cache) :
0;
}
/** Decorate a function making an ipv6 function be called over an ipv4 buffer
* @param kafka_line_buffer Kafka line buffer to print
* @param vbuffer Buffer
* @param real_field_len IPv4 length length
* @param flowCache Flow cache
* @param cb Function to call with IPv6 buffer
* @return Printed length
*/
static size_t ipv4_to_6_decorator(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flowCache,
size_t (cb)(struct printbuf *, const void *, const size_t,
struct flowCache *)) {
const uint8_t *buffer = vbuffer;
assert(buffer);
uint8_t ipv6[16];
if (unlikely(4 != real_field_len)) {
traceEvent(TRACE_WARNING, "Net length %zu != 4", real_field_len);
return 0;
}
ipv4buf_to_6(ipv6, buffer);
return cb(kafka_line_buffer, ipv6, sizeof(ipv6), flowCache);
}
size_t print_net(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
return ipv4_to_6_decorator(kafka_line_buffer, buffer, real_field_len,
flowCache, print_net_v6);
}
size_t print_net_name(struct printbuf *kafka_line_buffer,
const void *buffer,const size_t real_field_len,
struct flowCache *flowCache) {
return ipv4_to_6_decorator(kafka_line_buffer, buffer, real_field_len,
flowCache, print_net_name_v6);
}
static size_t print_ipv4_addr0(struct printbuf *kafka_line_buffer,
const uint32_t ipv4) {
assert(kafka_line_buffer);
static const size_t bufsize = sizeof("255.255.255.255")+1;
char buf[bufsize];
const char *ip_as_text = _intoaV4(ipv4,buf,bufsize);
const size_t ip_as_text_size = buf+bufsize-ip_as_text-1;
printbuf_memappend_fast(kafka_line_buffer,ip_as_text,ip_as_text_size);
return ip_as_text_size;
}
static size_t print_ipv6_addr0(struct printbuf *kafka_line_buffer,
const void *vbuffer) {
size_t i=0;
const uint8_t *buffer = vbuffer;
for (i=0;i<8;++i) {
printbuf_memappend_fast_n16(kafka_line_buffer,buffer[2*i]);
printbuf_memappend_fast_n16(kafka_line_buffer,buffer[2*i+1]);
if(i<7)
printbuf_memappend_fast(kafka_line_buffer,":",1);
}
return strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
}
static size_t print_ipv4_addr(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
const struct flowCache *flow_cache) {
const char *buffer = vbuffer;
(void)flow_cache;
assert_multi(kafka_line_buffer, buffer);
if (unlikely(4 != real_field_len)) {
traceEvent(TRACE_ERROR, "IPv4 real field len %zu != 4", real_field_len);
return 0;
}
const uint32_t ipv4 = net2number(buffer, 4);
return print_ipv4_addr0(kafka_line_buffer, ipv4);
}
static void flow_cache_save_ipv4(void *dst_buf, const void *vbuffer,
const size_t real_field_len) {
const uint8_t *buffer = vbuffer;
if (unlikely(real_field_len != 4)) {
traceEvent(TRACE_ERROR, "ipv4 length != 4");
return;
}
ipv4buf_to_6(dst_buf, buffer);
}
static size_t process_ipv4_addr0(void *dst_buf,
struct printbuf *kafka_line_buffer,
const void *buffer,
const size_t real_field_len,
struct flowCache *flowCache) {
if (readOnlyGlobals.normalize_directions) {
flow_cache_save_ipv4(dst_buf, buffer, real_field_len);
return 0;
} else {
return print_ipv4_addr(kafka_line_buffer, buffer, real_field_len,
flowCache);
}
}
size_t print_ipv4_src_addr(struct printbuf *kafka_line_buffer,
const void *buffer,const size_t real_field_len,
struct flowCache *flow_cache) {
return process_ipv4_addr0(flow_cache->address.src, kafka_line_buffer, buffer,
real_field_len, flow_cache);
}
size_t print_ipv4_dst_addr(struct printbuf *kafka_line_buffer,
const void *buffer,const size_t real_field_len,
struct flowCache *flow_cache) {
return process_ipv4_addr0(flow_cache->address.dst, kafka_line_buffer, buffer,
real_field_len, flow_cache);
}
static bool is_ipv4_mapped(const void *ipv6) {
static const uint8_t ipv4_mapped[12] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF};
return 0 == memcmp(ipv6, ipv4_mapped, 12);
}
static size_t print_flow_cache_address(struct printbuf *kafka_line_buffer,
struct flowCache *flow_cache,
const uint8_t *(*get_addr_cb)(const struct flowCache *flowCache),
size_t (*print_addr_cb)(struct printbuf *kafka_line_buffer,
const void *addr, struct flowCache *flow_cache)) {
assert(kafka_line_buffer);
assert(flow_cache);
const uint8_t *client_ip = get_addr_cb(flow_cache);
if (NULL == client_ip) {
return 0;
}
return print_addr_cb(kafka_line_buffer, client_ip, flow_cache);
}
static size_t print_flow_cache_addr(struct printbuf *kafka_line_buffer,
const void *vip_addr, struct flowCache *flow_cache) {
(void)flow_cache;
const uint8_t *ip_addr = vip_addr;
return is_ipv4_mapped(ip_addr) ?
print_ipv4_addr0(kafka_line_buffer,
ip_addr[15] + (ip_addr[14] << 8) +
(ip_addr[13] << 16) + (ip_addr[12] << 24))
: print_ipv6_addr0(kafka_line_buffer, ip_addr);
}
static size_t print_flow_cache_net0(struct printbuf *kafka_line_buffer,
const void *vip_addr, struct flowCache *flow_cache,
size_t (*print_net_cb)(struct printbuf *kafka_line_buffer,
const void *vip_addr, const size_t len,
struct flowCache *flow_cache)) {
static const size_t addr_len = 16;
return print_net_cb(kafka_line_buffer, vip_addr, addr_len, flow_cache);
}
static size_t print_flow_cache_net(struct printbuf *kafka_line_buffer,
const void *ip_addr, struct flowCache *flow_cache) {
return print_flow_cache_net0(kafka_line_buffer, ip_addr, flow_cache,
print_net_v6_0);
}
static size_t print_flow_cache_net_name(struct printbuf *kafka_line_buffer,
const void *ip_addr, struct flowCache *flow_cache) {
return print_flow_cache_net0(kafka_line_buffer, ip_addr, flow_cache,
print_net_name_v6_0);
}
size_t print_sta_ipv4_address(struct printbuf *kafka_line_buffer,
const void *vbuffer, const size_t real_field_len,
struct flowCache *flow_cache) {
const char *buffer = vbuffer;
if (unlikely(real_field_len != 4)) {
traceEvent(TRACE_ERROR, "Bad client ip addr received");
return 0;
}
/* Save client address for DNS query */
flow_cache->address.client[10] = flow_cache->address.client[11] = 0xff;
memcpy(&flow_cache->address.client[12], buffer, 4);
/* print */
const uint32_t addr = net2number(buffer, real_field_len);
return print_ipv4_addr0(kafka_line_buffer, addr);
}
size_t print_lan_addr(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flow_cache) {
unused_params(buffer, real_field_len);
if (!readOnlyGlobals.normalize_directions) {
return 0;
}
return print_flow_cache_address(kafka_line_buffer, flow_cache,
get_direction_based_client_ip, print_flow_cache_addr);
}
size_t print_lan_addr_net(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
unused_params(buffer, real_field_len);
assert_multi(kafka_line_buffer, flowCache);
return print_flow_cache_address(kafka_line_buffer, flowCache,
get_direction_based_client_ip, print_flow_cache_net);
}
size_t print_wan_addr_net(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
unused_params(buffer, real_field_len);
assert_multi(kafka_line_buffer, flowCache);
return print_flow_cache_address(kafka_line_buffer, flowCache,
get_direction_based_target_ip, print_flow_cache_net);
}
size_t print_lan_addr_net_name(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
unused_params(buffer, real_field_len);
assert_multi(kafka_line_buffer, flowCache);
return print_flow_cache_address(kafka_line_buffer, flowCache,
get_direction_based_client_ip, print_flow_cache_net_name);
}
size_t print_wan_addr_net_name(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
unused_params(buffer, real_field_len);
assert_multi(kafka_line_buffer, flowCache);
return print_flow_cache_address(kafka_line_buffer, flowCache,
get_direction_based_target_ip, print_flow_cache_net_name);
}
size_t print_wan_addr(struct printbuf *kafka_line_buffer,
const void *buffer, const size_t real_field_len,
struct flowCache *flowCache) {
unused_params(buffer, real_field_len);
return print_flow_cache_address(kafka_line_buffer, flowCache,
get_direction_based_target_ip, print_flow_cache_addr);
}
static size_t print_mac0(struct printbuf *kafka_line_buffer,
const void *vbuffer) {
const uint8_t *buffer = vbuffer;
assert_multi(kafka_line_buffer, buffer);
int i;
for(i=0;i<6;++i){
printbuf_memappend_fast_n16(kafka_line_buffer,buffer[i]);
if(i<5)
printbuf_memappend_fast(kafka_line_buffer,":",1);
}
return strlen("ff:ff:ff:ff:ff:ff");
}
size_t print_mac(struct printbuf *kafka_line_buffer,
const void *vbuffer,const size_t real_field_len,
struct flowCache *flowCache) {
const char *buffer = vbuffer;
assert_multi(kafka_line_buffer, buffer);
unused_params(flowCache);
if (unlikely(real_field_len!=6)) {
traceEvent(TRACE_ERROR,"Mac with len %zu != 6", real_field_len);
return 0;
}
return print_mac0(kafka_line_buffer, buffer);
}
static size_t print_mac_vendor0(struct printbuf *kafka_line_buffer,const void *buffer){
const uint64_t mac = get_mac(buffer);
if(mac){
const char *vendor = NULL;
pthread_rwlock_rdlock(&readOnlyGlobals.rb_databases.mutex);
if(readOnlyGlobals.rb_databases.mac_vendor_database)
vendor = rb_find_mac_vendor(mac,readOnlyGlobals.rb_databases.mac_vendor_database);
pthread_rwlock_unlock(&readOnlyGlobals.rb_databases.mutex);
if(vendor){
const size_t vendor_len = strlen(vendor);
printbuf_memappend_fast(kafka_line_buffer,vendor,vendor_len);
return vendor_len;
}
}
return 0;
}
static bool empty_ipv6_addr(const uint8_t *addr) {
static const uint8_t ipv6[16];
return !memcmp(addr, ipv6, sizeof(ipv6));
}
#define GET_CLIENT_ENDPOINT(t_direction, t_src, t_dst) ({ \
const typeof(t_src) src = (t_src), dst = (t_dst); \
/* Default exporter position is LAN side */ \
const uint8_t direction = (t_direction); \
(direction == DIRECTION_DOWNSTREAM) ? dst : src;})
static const uint8_t *get_direction_based_client_mac(struct flowCache *flowCache){
assert(flowCache);
const uint8_t *src_mac = flowCache->macs.src_mac;
const uint8_t *dst_mac = is_span_observation_id(flowCache->observation_id) ?
flowCache->macs.dst_mac : flowCache->macs.post_dst_mac;
return GET_CLIENT_ENDPOINT(flowCache->macs.direction,
src_mac, dst_mac);
}
static uint64_t get_direction_based_client_port(
const struct flowCache *flow_cache) {
assert(flow_cache);