-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathpacket.c
2070 lines (1767 loc) · 77.6 KB
/
packet.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
/* packet.c -- Functions for acquiring data
*
* Copyright 2012-2017 AOL Inc. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "arkime.h"
#include "patricia.h"
#include <inttypes.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <errno.h>
#include "pcap.h"
#include "arkimeconfig.h"
//#define DEBUG_PACKET
/******************************************************************************/
extern ArkimeConfig_t config;
ArkimePcapFileHdr_t pcapFileHeader;
uint64_t totalPackets;
LOCAL uint64_t totalBytes[ARKIME_MAX_PACKET_THREADS];
LOCAL uint64_t initialDropped = 0;
struct timeval initialPacket; // Don't make LOCAL for now because of netflow plugin
extern void *esServer;
extern uint32_t pluginsCbs;
uint64_t writtenBytes;
uint64_t unwrittenBytes;
int mac1Field;
int mac2Field;
int vlanField;
int vniField;
LOCAL int oui1Field;
LOCAL int oui2Field;
LOCAL int outermac1Field;
LOCAL int outermac2Field;
LOCAL int outeroui1Field;
LOCAL int outeroui2Field;
LOCAL int outerip1Field;
LOCAL int outerip2Field;
LOCAL int dscpField[2];
LOCAL int ttlField[2];
LOCAL uint64_t droppedFrags;
time_t currentTime[ARKIME_MAX_PACKET_THREADS];
time_t lastPacketSecs[ARKIME_MAX_PACKET_THREADS];
LOCAL int inProgress[ARKIME_MAX_PACKET_THREADS];
LOCAL patricia_tree_t *ipTree4 = 0;
LOCAL patricia_tree_t *ipTree6 = 0;
LOCAL patricia_tree_t *newipTree4 = 0;
LOCAL patricia_tree_t *newipTree6 = 0;
extern ArkimeFieldOps_t readerFieldOps[256];
extern ArkimeSchemeAction_t *schemeActions[256];
LOCAL ArkimePacketEnqueue_cb udpPortCbs[0x10000];
LOCAL ArkimePacketEnqueue_cb ethernetCbs[0x10000];
LOCAL ArkimePacketEnqueue_cb ipCbs[ARKIME_IPPROTO_MAX];
int tcpMProtocol;
int udpMProtocol;
LOCAL int mProtocolCnt;
ArkimeProtocol_t mProtocols[0x100];
/******************************************************************************/
uint64_t packetStats[ARKIME_PACKET_MAX];
/******************************************************************************/
LOCAL ArkimePacketHead_t packetQ[ARKIME_MAX_PACKET_THREADS];
LOCAL uint32_t overloadDrops[ARKIME_MAX_PACKET_THREADS];
LOCAL uint32_t overloadDropTimes[ARKIME_MAX_PACKET_THREADS];
LOCAL ARKIME_LOCK_DEFINE(frags);
LOCAL ArkimePacketRC arkime_packet_ip4(ArkimePacketBatch_t *batch, ArkimePacket_t *const packet, const uint8_t *data, int len);
LOCAL ArkimePacketRC arkime_packet_ip6(ArkimePacketBatch_t *batch, ArkimePacket_t *const packet, const uint8_t *data, int len);
LOCAL ArkimePacketRC arkime_packet_frame_relay(ArkimePacketBatch_t *batch, ArkimePacket_t *const packet, const uint8_t *data, int len);
LOCAL ArkimePacketRC arkime_packet_ether(ArkimePacketBatch_t *batch, ArkimePacket_t *const packet, const uint8_t *data, int len);
typedef struct arkimefrags_t {
struct arkimefrags_t *fragh_next, *fragh_prev;
struct arkimefrags_t *fragl_next, *fragl_prev;
uint32_t fragh_bucket;
uint32_t fragh_hash;
ArkimePacketHead_t packets;
char key[10];
uint32_t secs;
char haveNoFlags;
} ArkimeFrags_t;
typedef struct {
struct arkimefrags_t *fragh_next, *fragh_prev;
struct arkimefrags_t *fragl_next, *fragl_prev;
uint32_t fragh_count;
uint32_t fragl_count;
} ArkimeFragsHead_t;
typedef HASH_VAR(h_, ArkimeFragsHash_t, ArkimeFragsHead_t, 199337);
LOCAL ArkimeFragsHash_t fragsHash;
LOCAL ArkimeFragsHead_t fragsList;
// These are in network byte order
LOCAL ArkimeDropHashGroup_t packetDrop4;
LOCAL ArkimeDropHashGroup_t packetDrop6;
LOCAL ArkimeDropHashGroup_t packetDrop4S;
LOCAL ArkimeDropHashGroup_t packetDrop6S;
#ifndef IPPROTO_IPV4
#define IPPROTO_IPV4 4
#endif
/******************************************************************************/
void arkime_packet_free(ArkimePacket_t *packet)
{
if (packet->copied) {
free(packet->pkt);
}
packet->pkt = 0;
ARKIME_TYPE_FREE(ArkimePacket_t, packet);
}
/******************************************************************************/
void arkime_packet_process_data(ArkimeSession_t *session, const uint8_t *data, int len, int which)
{
int i;
for (i = 0; i < session->parserNum; i++) {
if (session->parserInfo[i].parserFunc) {
int consumed = session->parserInfo[i].parserFunc(session, session->parserInfo[i].uw, data, len, which);
if (consumed) {
if (consumed == ARKIME_PARSER_UNREGISTER) {
if (session->parserInfo[i].parserFreeFunc) {
session->parserInfo[i].parserFreeFunc(session, session->parserInfo[i].uw);
}
memset(&session->parserInfo[i], 0, sizeof(session->parserInfo[i]));
continue;
}
session->consumed[which] += consumed;
}
if (consumed >= len)
break;
}
}
}
/******************************************************************************/
void arkime_packet_thread_wake(int thread)
{
ARKIME_LOCK(packetQ[thread].lock);
ARKIME_COND_SIGNAL(packetQ[thread].lock);
ARKIME_UNLOCK(packetQ[thread].lock);
}
/******************************************************************************/
/* Only called on main thread, we busy block until all packet threads are empty.
* Should only be used by tests and at end
*/
void arkime_packet_flush()
{
int flushed = 0;
int t;
while (!flushed) {
flushed = !arkime_session_cmd_outstanding();
for (t = 0; t < config.packetThreads; t++) {
ARKIME_LOCK(packetQ[t].lock);
if (DLL_COUNT(packet_, &packetQ[t]) > 0) {
flushed = 0;
}
ARKIME_UNLOCK(packetQ[t].lock);
usleep(10000);
}
}
}
/******************************************************************************/
SUPPRESS_ALIGNMENT
LOCAL void arkime_packet_process(ArkimePacket_t *packet, int thread)
{
#ifdef DEBUG_PACKET
LOG("Processing %p %d", packet, packet->pktlen);
#endif
lastPacketSecs[thread] = packet->ts.tv_sec;
arkime_pq_run(thread, 10);
ArkimeSession_t *session;
struct ip *ip4 = (struct ip *)(packet->pkt + packet->ipOffset);
const struct ip6_hdr *ip6 = (struct ip6_hdr *)(packet->pkt + packet->ipOffset);
uint8_t sessionId[ARKIME_SESSIONID_LEN];
mProtocols[packet->mProtocol].createSessionId(sessionId, packet);
// Try at most 2 times
int isNew;
for (int i = 0; i < 2; i++) {
session = arkime_session_find_or_create(packet->mProtocol, packet->hash, sessionId, &isNew);
if (isNew) {
session->saveTime = packet->ts.tv_sec + config.tcpSaveTimeout;
session->firstPacket = packet->ts;
session->thread = thread;
if (packet->ipProtocol) {
session->ipProtocol = packet->ipProtocol;
if (ip4->ip_v == 4) {
((uint32_t *)session->addr1.s6_addr)[2] = htonl(0xffff);
((uint32_t *)session->addr1.s6_addr)[3] = ip4->ip_src.s_addr;
((uint32_t *)session->addr2.s6_addr)[2] = htonl(0xffff);
((uint32_t *)session->addr2.s6_addr)[3] = ip4->ip_dst.s_addr;
session->ip_tos = ip4->ip_tos;
} else {
session->addr1 = ip6->ip6_src;
session->addr2 = ip6->ip6_dst;
session->ip_tos = 0;
}
}
}
int rc = mProtocols[packet->mProtocol].preProcess(session, packet, isNew);
// Close out the old session and create a new one
if (rc == 1) {
void arkime_session_save(ArkimeSession_t *session);
arkime_session_save(session);
continue;
}
break;
}
if (session->stopSPI) {
arkime_packet_free(packet);
return;
}
if (isNew) {
arkime_parsers_initial_tag(session);
if (readerFieldOps[packet->readerPos].num)
arkime_field_ops_run(session, &readerFieldOps[packet->readerPos]);
if (schemeActions[packet->readerPos] && schemeActions[packet->readerPos]->ops.num) {
arkime_field_ops_run(session, &schemeActions[packet->readerPos]->ops);
}
if (pluginsCbs & ARKIME_PLUGIN_NEW)
arkime_plugins_cb_new(session);
arkime_rules_session_create(session);
}
/* Check if the stop saving bpf filters match */
if (session->packets[packet->direction] == 0 && session->stopSaving == 0xffff) {
arkime_rules_run_session_setup(session, packet);
}
session->packets[packet->direction]++;
session->bytes[packet->direction] += packet->pktlen;
session->lastPacket = packet->ts;
uint32_t packets = session->packets[0] + session->packets[1];
if (packets <= session->stopSaving) {
arkime_writer_write(session, packet);
// If writerFilePos is 0, then the writer couldn't save the packet
if (packet->writerFilePos == 0) {
if (!session->diskOverload) {
arkime_session_add_tag(session, "pcap-disk-overload");
session->diskOverload = 1;
}
ARKIME_THREAD_INCR_NUM(unwrittenBytes, packet->pktlen);
} else {
ARKIME_THREAD_INCR_NUM(writtenBytes, packet->pktlen);
// If the last fileNum used in the session isn't the same as the
// lastest packets fileNum then we need to add to the filePos and
// fileNum arrays.
uint16_t len;
if (session->lastFileNum != packet->writerFileNum) {
session->lastFileNum = packet->writerFileNum;
g_array_append_val(session->fileNumArray, packet->writerFileNum);
int64_t pos = -1LL * packet->writerFileNum;
g_array_append_val(session->filePosArray, pos);
if (config.enablePacketLen) {
len = 0;
g_array_append_val(session->fileLenArray, len);
}
}
g_array_append_val(session->filePosArray, packet->writerFilePos);
if (config.enablePacketLen) {
len = 16 + packet->pktlen;
g_array_append_val(session->fileLenArray, len);
}
}
if (packets >= config.maxPackets || session->midSave) {
arkime_session_mid_save(session, packet->ts.tv_sec);
}
} else {
// If we hit stopSaving for this session and try and save 1 more packet then
// add truncated-pcap tag to the session
if (packets - 1 == session->stopSaving) {
arkime_session_set_stop_saving(session);
}
ARKIME_THREAD_INCR_NUM(unwrittenBytes, packet->pktlen);
}
// Check the first 10 packets for dscp, vlans, tunnels, and macs
if (session->packets[packet->direction] <= 10) {
const uint8_t *pcapData = packet->pkt;
if (packet->ipProtocol) {
int tc = ip4->ip_v == 4 ? ip4->ip_tos >> 2 : ip6->ip6_vfc & 0xf;
if (tc != 0) {
arkime_field_int_add(dscpField[packet->direction], session, tc);
}
int ttl = ip4->ip_v == 4 ? ip4->ip_ttl : ip6->ip6_hops;
arkime_field_int_add(ttlField[packet->direction], session, ttl);
}
if (pcapFileHeader.dlt == DLT_EN10MB) {
if (packet->direction == 1) {
arkime_field_macoui_add(session, mac1Field, oui1Field, packet->pkt + packet->etherOffset);
arkime_field_macoui_add(session, mac2Field, oui2Field, packet->pkt + packet->etherOffset + 6);
} else {
arkime_field_macoui_add(session, mac1Field, oui1Field, packet->pkt + packet->etherOffset + 6);
arkime_field_macoui_add(session, mac2Field, oui2Field, packet->pkt + packet->etherOffset);
}
int n = 12;
while ((pcapData[n] == 0x81 && pcapData[n + 1] == 0x00) || (pcapData[n] == 0x88 && pcapData[n + 1] == 0xa8)) {
uint16_t vlan = ((uint16_t)(pcapData[n + 2] << 8 | pcapData[n + 3])) & 0xfff;
arkime_field_int_add(vlanField, session, vlan);
n += 4;
}
}
if (packet->vlan)
arkime_field_int_add(vlanField, session, packet->vlan);
if (packet->vni)
arkime_field_int_add(vniField, session, packet->vni);
if (packet->etherOffset != 0 && packet->outerEtherOffset != packet->etherOffset) {
arkime_field_macoui_add(session, outermac1Field, outeroui1Field, packet->pkt + packet->outerEtherOffset);
arkime_field_macoui_add(session, outermac2Field, outeroui2Field, packet->pkt + packet->outerEtherOffset + 6);
}
if (packet->outerIpOffset != 0 && packet->outerIpOffset != packet->ipOffset) {
if (packet->outerv6 == 0) {
ip4 = (struct ip *) (packet->pkt + packet->outerIpOffset);
arkime_field_ip4_add(outerip1Field, session, ip4->ip_src.s_addr);
arkime_field_ip4_add(outerip2Field, session, ip4->ip_dst.s_addr);
} else {
ip6 = (struct ip6_hdr *) (packet->pkt + packet->outerIpOffset);
arkime_field_ip6_add(outerip1Field, session, ip6->ip6_src.s6_addr);
arkime_field_ip6_add(outerip2Field, session, ip6->ip6_dst.s6_addr);
}
}
if (packet->tunnel & ARKIME_PACKET_TUNNEL_GRE) {
arkime_session_add_protocol(session, "gre");
}
if (packet->tunnel & ARKIME_PACKET_TUNNEL_PPPOE) {
arkime_session_add_protocol(session, "pppoe");
}
if (packet->tunnel & ARKIME_PACKET_TUNNEL_PPP) {
arkime_session_add_protocol(session, "ppp");
}
if (packet->tunnel & ARKIME_PACKET_TUNNEL_MPLS) {
arkime_session_add_protocol(session, "mpls");
}
if (packet->tunnel & ARKIME_PACKET_TUNNEL_GTP) {
arkime_session_add_protocol(session, "gtp");
}
if (packet->tunnel & ARKIME_PACKET_TUNNEL_VXLAN) {
arkime_session_add_protocol(session, "vxlan");
}
if (packet->tunnel & ARKIME_PACKET_TUNNEL_VXLAN_GPE) {
arkime_session_add_protocol(session, "vxlan-gpe");
}
if (packet->tunnel & ARKIME_PACKET_TUNNEL_GENEVE) {
arkime_session_add_protocol(session, "geneve");
}
}
if (mProtocols[packet->mProtocol].process) {
// If there is a process callback, call and determine if we free the packet.
if (mProtocols[packet->mProtocol].process(session, packet))
arkime_packet_free(packet);
} else {
// No process callback, always free
arkime_packet_free(packet);
}
}
/******************************************************************************/
#ifndef FUZZLOCH
LOCAL void *arkime_packet_thread(void *threadp)
{
int thread = (long)threadp;
const uint32_t maxPackets75 = config.maxPackets * 0.75;
uint32_t skipCount = 0;
while (1) {
ArkimePacket_t *packet;
ARKIME_LOCK(packetQ[thread].lock);
inProgress[thread] = 0;
if (DLL_COUNT(packet_, &packetQ[thread]) == 0) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME_COARSE, &ts);
currentTime[thread] = ts.tv_sec;
ts.tv_sec++;
ARKIME_COND_TIMEDWAIT(packetQ[thread].lock, ts);
/* If we are in live capture mode and we haven't received any packets for 10 seconds we set current time to 10
* seconds in the past so arkime_session_process_commands will clean things up. 10 seconds is arbitrary but
* we want to make sure we don't set the time ahead of any packets that are currently being read off the wire
*/
if (!config.pcapReadOffline && DLL_COUNT(packet_, &packetQ[thread]) == 0 && ts.tv_sec - 10 > lastPacketSecs[thread]) {
lastPacketSecs[thread] = ts.tv_sec - 10;
}
}
inProgress[thread] = 1;
DLL_POP_HEAD(packet_, &packetQ[thread], packet);
ARKIME_UNLOCK(packetQ[thread].lock);
// Only process commands if the packetQ is less then 75% full or every 8 packets
if (likely(DLL_COUNT(packet_, &packetQ[thread]) < maxPackets75) || (skipCount & 0x7) == 0) {
arkime_session_process_commands(thread);
if (!packet)
continue;
} else {
skipCount++;
}
arkime_packet_process(packet, thread);
}
return NULL;
}
#endif
/******************************************************************************/
static FILE *unknownPacketFile[3];
LOCAL void arkime_packet_save_unknown_packet(int type, ArkimePacket_t *const packet)
{
static ARKIME_LOCK_DEFINE(lock);
struct arkime_pcap_sf_pkthdr hdr;
hdr.ts.tv_sec = packet->ts.tv_sec;
hdr.ts.tv_usec = packet->ts.tv_usec;
hdr.caplen = packet->pktlen;
hdr.pktlen = packet->pktlen;
ARKIME_LOCK(lock);
if (!unknownPacketFile[type]) {
char str[PATH_MAX];
static const char *names[] = {"unknown.ether", "unknown.ip", "corrupt"};
snprintf(str, sizeof(str), "%s/%s.%d.pcap", config.pcapDir[0], names[type], getpid());
unknownPacketFile[type] = fopen(str, "w");
// TODO-- should we also add logic to pick right pcapDir when there are multiple?
if (unknownPacketFile[type] == NULL) {
LOGEXIT("ERROR - Unable to open pcap file %s to store unknown type %s. Error %s", str, names[type], strerror (errno));
ARKIME_UNLOCK(lock);
return;
}
fwrite(&pcapFileHeader, 24, 1, unknownPacketFile[type]);
}
fwrite(&hdr, 16, 1, unknownPacketFile[type]);
fwrite(packet->pkt, packet->pktlen, 1, unknownPacketFile[type]);
ARKIME_UNLOCK(lock);
}
/******************************************************************************/
void arkime_packet_frags_free(ArkimeFrags_t *const frags)
{
ArkimePacket_t *packet;
while (DLL_POP_HEAD(packet_, &frags->packets, packet)) {
arkime_packet_free(packet);
}
HASH_REMOVE(fragh_, fragsHash, frags);
DLL_REMOVE(fragl_, &fragsList, frags);
ARKIME_TYPE_FREE(ArkimeFrags_t, frags);
}
/******************************************************************************/
SUPPRESS_ALIGNMENT
LOCAL gboolean arkime_packet_frags_process(ArkimePacket_t *const packet)
{
ArkimePacket_t *fpacket;
ArkimeFrags_t *frags;
char key[10];
struct ip *const ip4 = (struct ip *)(packet->pkt + packet->ipOffset);
memcpy(key, &ip4->ip_src.s_addr, 4);
memcpy(key + 4, &ip4->ip_dst.s_addr, 4);
memcpy(key + 8, &ip4->ip_id, 2);
HASH_FIND(fragh_, fragsHash, key, frags);
if (!frags) {
frags = ARKIME_TYPE_ALLOC0(ArkimeFrags_t);
memcpy(frags->key, key, 10);
frags->secs = packet->ts.tv_sec;
HASH_ADD(fragh_, fragsHash, key, frags);
DLL_PUSH_TAIL(fragl_, &fragsList, frags);
DLL_INIT(packet_, &frags->packets);
DLL_PUSH_TAIL(packet_, &frags->packets, packet);
if (DLL_COUNT(fragl_, &fragsList) > config.maxFrags) {
droppedFrags++;
arkime_packet_frags_free(DLL_PEEK_HEAD(fragl_, &fragsList));
}
return FALSE;
} else {
DLL_MOVE_TAIL(fragl_, &fragsList, frags);
}
uint16_t ip_off = ntohs(ip4->ip_off);
uint16_t ip_flags = ip_off & ~IP_OFFMASK;
ip_off &= IP_OFFMASK;
// we might be done once we receive the packets with no flags
if (ip_flags == 0) {
frags->haveNoFlags = 1;
}
// Insert this packet in correct location sorted by offset
DLL_FOREACH_REVERSE(packet_, &frags->packets, fpacket) {
struct ip *fip4 = (struct ip *)(fpacket->pkt + fpacket->ipOffset);
uint16_t fip_off = ntohs(fip4->ip_off) & IP_OFFMASK;
if (ip_off >= fip_off) {
DLL_ADD_AFTER(packet_, &frags->packets, fpacket, packet);
break;
}
}
if ((void * )fpacket == (void * )&frags->packets) {
DLL_PUSH_HEAD(packet_, &frags->packets, packet);
}
if (DLL_COUNT(packet_, &frags->packets) > 50) {
droppedFrags++;
arkime_packet_frags_free(frags);
return FALSE;
}
// Don't bother checking until we get a packet with no flags
if (!frags->haveNoFlags) {
return FALSE;
}
int off = 0;
struct ip *fip4;
int payloadLen = 0;
DLL_FOREACH(packet_, &frags->packets, fpacket) {
fip4 = (struct ip *)(fpacket->pkt + fpacket->ipOffset);
uint16_t fip_off = ntohs(fip4->ip_off) & IP_OFFMASK;
if (fip_off != off)
break;
off += fpacket->payloadLen / 8;
payloadLen = MAX(payloadLen, fip_off * 8 + fpacket->payloadLen);
}
// We have a hole
if ((void * )fpacket != (void * )&frags->packets) {
return FALSE;
}
// Packet is too large, hacker
if (payloadLen + packet->payloadOffset >= ARKIME_PACKET_MAX_LEN) {
droppedFrags++;
arkime_packet_frags_free(frags);
return FALSE;
}
// Now alloc the full packet
packet->pktlen = packet->payloadOffset + payloadLen;
uint8_t *pkt = malloc(packet->pktlen);
// Copy packet header
memcpy(pkt, packet->pkt, packet->payloadOffset);
// Fix header of new packet
fip4 = (struct ip *)(pkt + packet->ipOffset);
fip4->ip_len = htons(payloadLen + 4 * ip4->ip_hl);
fip4->ip_off = 0;
// Copy payload
DLL_FOREACH(packet_, &frags->packets, fpacket) {
fip4 = (struct ip *)(fpacket->pkt + fpacket->ipOffset);
uint16_t fip_off = ntohs(fip4->ip_off) & IP_OFFMASK;
if (packet->payloadOffset + (fip_off * 8) + fpacket->payloadLen <= packet->pktlen)
memcpy(pkt + packet->payloadOffset + (fip_off * 8), fpacket->pkt + fpacket->payloadOffset, fpacket->payloadLen);
else
LOG("WARNING - Not enough room for frag %d > %d", packet->payloadOffset + (fip_off * 8) + fpacket->payloadLen, packet->pktlen);
}
// Set all the vars in the current packet to new defraged packet
if (packet->copied)
free(packet->pkt);
packet->pkt = pkt;
packet->copied = 1;
packet->wasfrag = 1;
packet->payloadLen = payloadLen;
DLL_REMOVE(packet_, &frags->packets, packet); // Remove from list so we don't get freed in frags_free
arkime_packet_frags_free(frags);
return TRUE;
}
/******************************************************************************/
LOCAL void arkime_packet_frags4(ArkimePacketBatch_t *batch, ArkimePacket_t *const packet)
{
ArkimeFrags_t *frags;
// ALW - Should change frags_process to make the copy when needed
if (!packet->copied) {
uint8_t *pkt = malloc(packet->pktlen);
memcpy(pkt, packet->pkt, packet->pktlen);
packet->pkt = pkt;
packet->copied = 1;
}
ARKIME_LOCK(frags);
// Remove expired entries
while ((frags = DLL_PEEK_HEAD(fragl_, &fragsList)) && (frags->secs + config.fragsTimeout < packet->ts.tv_sec)) {
droppedFrags++;
arkime_packet_frags_free(frags);
}
gboolean process = arkime_packet_frags_process(packet);
ARKIME_UNLOCK(frags);
if (process)
arkime_packet_batch(batch, packet);
}
/******************************************************************************/
int arkime_packet_frags_size()
{
return DLL_COUNT(fragl_, &fragsList);
}
/******************************************************************************/
int arkime_packet_frags_outstanding()
{
return 0;
}
/******************************************************************************/
LOCAL void arkime_packet_log(SessionTypes ses)
{
ArkimeReaderStats_t stats;
if (arkime_reader_stats(&stats)) {
stats.dropped = 0;
stats.total = totalPackets;
}
uint32_t wql = arkime_writer_queue_length();
LOG("packets: %" PRIu64 " current sessions: %u/%u oldest: %d - recv: %" PRIu64 " drop: %" PRIu64 " (%0.2f) queue: %d disk: %d packet: %d close: %d ns: %d frags: %d/%d pstats: %" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 "/%" PRIu64 " ver: %s",
totalPackets,
arkime_session_watch_count(ses),
arkime_session_monitoring(),
arkime_session_idle_seconds(ses),
stats.total,
stats.dropped - initialDropped,
(stats.total ? (stats.dropped - initialDropped) * (double)100.0 / stats.total : 0),
arkime_http_queue_length(esServer),
wql,
arkime_packet_outstanding(),
arkime_session_close_outstanding(),
arkime_session_need_save_outstanding(),
arkime_packet_frags_outstanding(),
arkime_packet_frags_size(),
packetStats[ARKIME_PACKET_DO_PROCESS],
packetStats[ARKIME_PACKET_IP_DROPPED],
packetStats[ARKIME_PACKET_OVERLOAD_DROPPED],
packetStats[ARKIME_PACKET_CORRUPT],
packetStats[ARKIME_PACKET_UNKNOWN],
packetStats[ARKIME_PACKET_IPPORT_DROPPED],
packetStats[ARKIME_PACKET_DUPLICATE_DROPPED],
PACKAGE_VERSION
);
if (config.debug > 0) {
arkime_rules_stats();
}
}
/******************************************************************************/
LOCAL void arkime_packet_cmd_stats(int UNUSED(argc), char **UNUSED(argv), gpointer cc)
{
char output[20000];
BSB bsb;
BSB_INIT(bsb, output, sizeof(output));
ArkimeReaderStats_t stats;
if (arkime_reader_stats(&stats)) {
stats.dropped = 0;
stats.total = totalPackets;
}
uint32_t wql = arkime_writer_queue_length();
BSB_EXPORT_sprintf(bsb,
"Arkime Version: %s\n"
"Packets Processed: %" PRIu64 "\n"
"Packets Received: %" PRIu64 "\n"
"Packets Dropped: %" PRIu64 " (%0.2f)\n"
"\n"
"Current Sessions: %u\n"
"Current TCP Sessions: %u\n"
"Current UDP Sessions: %u\n"
"Current ICMP Sessions: %u\n"
"Oldest TCP Session: %d\n"
"Oldest UDP Session: %d\n"
"Oldest ICMP Session: %d\n"
"\n"
"ES Queue: %d\n"
"DIsk Queue: %d\n"
"Packet Queue: %d\n"
"Close Queue: %d\n"
"Need Saving Queue: %d\n"
"Frags: %d/%d\n"
"\n"
"Packets Processed: %" PRIu64 "\n"
"Packets IP Dropped: %" PRIu64 "\n"
"Packets Overload Dropped: %" PRIu64 "\n"
"Packets Corrupt: %" PRIu64 "\n"
"Packets Unknown: %" PRIu64 "\n"
"Packets IPPort Dropped: %" PRIu64 "\n"
"Packets Duplicate Dropped: %" PRIu64 "\n",
PACKAGE_VERSION,
totalPackets,
stats.total,
stats.dropped - initialDropped,
(stats.total ? (stats.dropped - initialDropped) * (double)100.0 / stats.total : 0),
arkime_session_monitoring(),
arkime_session_watch_count(SESSION_TCP),
arkime_session_watch_count(SESSION_UDP),
arkime_session_watch_count(SESSION_ICMP),
arkime_session_idle_seconds(SESSION_TCP),
arkime_session_idle_seconds(SESSION_UDP),
arkime_session_idle_seconds(SESSION_ICMP),
arkime_http_queue_length(esServer),
wql,
arkime_packet_outstanding(),
arkime_session_close_outstanding(),
arkime_session_need_save_outstanding(),
arkime_packet_frags_outstanding(),
arkime_packet_frags_size(),
packetStats[ARKIME_PACKET_DO_PROCESS],
packetStats[ARKIME_PACKET_IP_DROPPED],
packetStats[ARKIME_PACKET_OVERLOAD_DROPPED],
packetStats[ARKIME_PACKET_CORRUPT],
packetStats[ARKIME_PACKET_UNKNOWN],
packetStats[ARKIME_PACKET_IPPORT_DROPPED],
packetStats[ARKIME_PACKET_DUPLICATE_DROPPED]
);
arkime_command_respond(cc, output, BSB_LENGTH(bsb));
}
/******************************************************************************/
SUPPRESS_ALIGNMENT
LOCAL ArkimePacketRC arkime_packet_ip4(ArkimePacketBatch_t *batch, ArkimePacket_t *const packet, const uint8_t *data, int len)
{
struct ip *ip4 = (struct ip *)data;
const struct tcphdr *tcphdr = 0;
const struct udphdr *udphdr = 0;
uint8_t sessionId[ARKIME_SESSIONID_LEN];
#ifdef DEBUG_PACKET
LOG("enter %p %p %d", packet, data, len);
#endif
if (len < (int)sizeof(struct ip)) {
#ifdef DEBUG_PACKET
LOG("BAD PACKET: too small for header %p %d", packet, len);
#endif
return ARKIME_PACKET_CORRUPT;
}
if (ip4->ip_v != 4) {
#ifdef DEBUG_PACKET
LOG("BAD PACKET: ip4->ip_v4 %d != 4", ip4->ip_v);
#endif
return ARKIME_PACKET_CORRUPT;
}
int ip_len = ntohs(ip4->ip_len);
if (len < ip_len) {
#ifdef DEBUG_PACKET
LOG("BAD PACKET: incomplete %p %d %d", packet, len, ip_len);
#endif
return ARKIME_PACKET_CORRUPT;
}
int ip_hdr_len = 4 * ip4->ip_hl;
if (ip_hdr_len < 4 * 5 || len < ip_hdr_len || ip_len < ip_hdr_len) {
#ifdef DEBUG_PACKET
LOG("BAD PACKET: too small for header and options %p %d %d", packet, len, ip_hdr_len);
#endif
return ARKIME_PACKET_CORRUPT;
}
if (ipTree4) {
const patricia_node_t *node;
if ((node = patricia_search_best3 (ipTree4, (u_char * )&ip4->ip_src, 32)) && node->data == NULL)
return ARKIME_PACKET_IP_DROPPED;
if ((node = patricia_search_best3 (ipTree4, (u_char * )&ip4->ip_dst, 32)) && node->data == NULL)
return ARKIME_PACKET_IP_DROPPED;
}
if ((uint8_t *)data - packet->pkt >= 2048)
return ARKIME_PACKET_CORRUPT;
packet->outerv6 = packet->v6; // v6 will get reset
packet->v6 = 0;
packet->outerIpOffset = packet->ipOffset; // ipOffset will get reset
packet->ipOffset = (uint8_t *)data - packet->pkt;
packet->payloadOffset = packet->ipOffset + ip_hdr_len;
packet->payloadLen = ip_len - ip_hdr_len;
uint16_t ip_off = ntohs(ip4->ip_off);
uint16_t ip_flags = ip_off & ~IP_OFFMASK;
ip_off &= IP_OFFMASK;
if ((ip_flags & IP_MF) || ip_off > 0) {
arkime_packet_frags4(batch, packet);
return ARKIME_PACKET_DONT_PROCESS_OR_FREE;
}
packet->mProtocol = 0;
packet->ipProtocol = ip4->ip_p;
switch (ip4->ip_p) {
case IPPROTO_IPV4:
return arkime_packet_ip4(batch, packet, data + ip_hdr_len, len - ip_hdr_len);
break;
case IPPROTO_TCP:
if (len < ip_hdr_len + (int)sizeof(struct tcphdr)) {
#ifdef DEBUG_PACKET
LOG("BAD PACKET: too small for tcp hdr %p %d", packet, len);
#endif
return ARKIME_PACKET_CORRUPT;
}
tcphdr = (struct tcphdr *)((char *)ip4 + ip_hdr_len);
if (packetDrop4.drops[tcphdr->th_sport] &&
arkime_drophash_should_drop(&packetDrop4, tcphdr->th_sport, &ip4->ip_src.s_addr, packet->ts.tv_sec)) {
return ARKIME_PACKET_IPPORT_DROPPED;
}
if (packetDrop4.drops[tcphdr->th_dport] &&
arkime_drophash_should_drop(&packetDrop4, tcphdr->th_dport, &ip4->ip_dst.s_addr, packet->ts.tv_sec)) {
return ARKIME_PACKET_IPPORT_DROPPED;
}
if (config.enablePacketDedup && arkime_dedup_should_drop(packet, ip_hdr_len + sizeof(struct tcphdr)))
return ARKIME_PACKET_DUPLICATE_DROPPED;
arkime_session_id(sessionId, ip4->ip_src.s_addr, tcphdr->th_sport,
ip4->ip_dst.s_addr, tcphdr->th_dport, packet->vlan, packet->vni);
packet->mProtocol = tcpMProtocol;
const int dropPort = ((uint32_t)tcphdr->th_dport * (uint32_t)tcphdr->th_sport) & 0xffff;
if (packetDrop4S.drops[dropPort] &&
arkime_drophash_should_drop(&packetDrop4, dropPort, sessionId + 1, packet->ts.tv_sec)) {
return ARKIME_PACKET_IPPORT_DROPPED;
}
break;
case IPPROTO_UDP:
if (len < ip_hdr_len + (int)sizeof(struct udphdr)) {
#ifdef DEBUG_PACKET
LOG("BAD PACKET: too small for udp header %p %d", packet, len);
#endif
return ARKIME_PACKET_CORRUPT;
}
udphdr = (struct udphdr *)((char *)ip4 + ip_hdr_len);
if (len > ip_hdr_len + (int)sizeof(struct udphdr) + 8 && udpPortCbs[udphdr->uh_dport]) {
int rc = udpPortCbs[udphdr->uh_dport](batch, packet, (uint8_t *)ip4 + ip_hdr_len + sizeof(struct udphdr *), len - ip_hdr_len - sizeof(struct udphdr *));
if (rc != ARKIME_PACKET_UNKNOWN)
return rc;
// Reset state on UNKNOWN
packet->v6 = 0;
packet->ipOffset = (uint8_t *)data - packet->pkt;
packet->payloadOffset = packet->ipOffset + ip_hdr_len;
packet->payloadLen = ip_len - ip_hdr_len;
}
if (config.enablePacketDedup && arkime_dedup_should_drop(packet, ip_hdr_len + sizeof(struct udphdr)))
return ARKIME_PACKET_DUPLICATE_DROPPED;
arkime_session_id(sessionId, ip4->ip_src.s_addr, udphdr->uh_sport,
ip4->ip_dst.s_addr, udphdr->uh_dport, packet->vlan, packet->vni);
packet->mProtocol = udpMProtocol;
break;
case IPPROTO_IPV6:
return arkime_packet_ip6(batch, packet, data + ip_hdr_len, len - ip_hdr_len);
default:
return arkime_packet_run_ip_cb(batch, packet, data + ip_hdr_len, len - ip_hdr_len, ip4->ip_p, "IP4");
}
packet->hash = arkime_session_hash(sessionId);
return ARKIME_PACKET_DO_PROCESS;
}
/******************************************************************************/
SUPPRESS_ALIGNMENT
LOCAL ArkimePacketRC arkime_packet_ip6(ArkimePacketBatch_t *batch, ArkimePacket_t *const packet, const uint8_t *data, int len)
{
const struct ip6_hdr *ip6 = (struct ip6_hdr *)data;
const struct tcphdr *tcphdr = 0;
const struct udphdr *udphdr = 0;
uint8_t sessionId[ARKIME_SESSIONID_LEN];
#ifdef DEBUG_PACKET
LOG("enter %p %p %d", packet, data, len);
#endif
if (len < (int)sizeof(struct ip6_hdr)) {
return ARKIME_PACKET_CORRUPT;
}
int ip_len = ntohs(ip6->ip6_plen);
if (len < ip_len) {
return ARKIME_PACKET_CORRUPT;
}
// Corrupt ip6 header
if ((ip6->ip6_vfc & 0xf0) != 0x60) {
return ARKIME_PACKET_CORRUPT;
}
if (ipTree6) {
const patricia_node_t *node;
if ((node = patricia_search_best3 (ipTree6, (u_char * )&ip6->ip6_src, 128)) && node->data == NULL)
return ARKIME_PACKET_IP_DROPPED;
if ((node = patricia_search_best3 (ipTree6, (u_char * )&ip6->ip6_dst, 128)) && node->data == NULL)
return ARKIME_PACKET_IP_DROPPED;
}
int ip_hdr_len = sizeof(struct ip6_hdr);
packet->outerv6 = packet->v6; // v6 will get reset
packet->v6 = 1;
packet->outerIpOffset = packet->ipOffset; // ipOffset will get reset
packet->ipOffset = (uint8_t *)data - packet->pkt;
packet->payloadOffset = packet->ipOffset + ip_hdr_len;
if (ip_len + (int)sizeof(struct ip6_hdr) < ip_hdr_len) {
#ifdef DEBUG_PACKET
LOG ("ERROR - %d + %ld < %d", ip_len, (long)sizeof(struct ip6_hdr), ip_hdr_len);
#endif
return ARKIME_PACKET_CORRUPT;
}
packet->payloadLen = ip_len + sizeof(struct ip6_hdr) - ip_hdr_len;