Coverage Report

Created: 2024-01-13 07:07

/src/ndpi/example/reader_util.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * ndpi_util.h
3
 *
4
 * Copyright (C) 2011-22 - ntop.org
5
 *
6
 * nDPI is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * nDPI is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 */
20
21
/**
22
 * This module contains routines to help setup a simple nDPI program.
23
 *
24
 * If you concern about performance or have to integrate nDPI in your
25
 * application, you could need to reimplement them yourself.
26
 *
27
 * WARNING: this API is just a demo od nDPI usage: Use it at your own risk!
28
 */
29
#ifndef __NDPI_UTIL_H__
30
#define __NDPI_UTIL_H__
31
32
#include "../src/lib/third_party/include/uthash.h"
33
#include <pcap.h>
34
#include "ndpi_includes.h"
35
#include "ndpi_classify.h"
36
#include "ndpi_typedefs.h"
37
38
#ifdef USE_DPDK
39
#include <rte_eal.h>
40
#include <rte_ether.h>
41
#include <rte_ethdev.h>
42
#include <rte_cycles.h>
43
#include <rte_lcore.h>
44
#include <rte_mbuf.h>
45
46
#define RX_RING_SIZE     128
47
#define TX_RING_SIZE     512
48
#define NUM_MBUFS       8191
49
#define MBUF_CACHE_SIZE  250
50
#define BURST_SIZE        32
51
#define PREFETCH_OFFSET    3
52
53
extern int dpdk_port_init(int port, struct rte_mempool *mbuf_pool);
54
extern int dpdk_port_deinit(int port);
55
#endif
56
57
895k
#define PLEN_MAX         1504
58
890k
#define PLEN_BIN_LEN     32
59
360k
#define PLEN_NUM_BINS    48 /* 47*32 = 1504 */
60
937k
#define MAX_NUM_BIN_PKTS 256
61
62
/* ETTA Spec defiintions for feature readiness */
63
#define ETTA_MIN_PACKETS 10
64
199M
#define ETTA_MIN_OCTETS 4000
65
/** maximum line length */
66
#define LINEMAX 512
67
#define MAX_BYTE_COUNT_ARRAY_LENGTH 256
68
#define MAX_NUM_PKTS               10
69
70
#define MAX_NUM_READER_THREADS     16
71
#define IDLE_SCAN_PERIOD           10 /* msec (use TICK_RESOLUTION = 1000) */
72
#define MAX_IDLE_TIME           30000
73
#define IDLE_SCAN_BUDGET         1024
74
#define NUM_ROOTS                 512
75
#define MAX_EXTRA_PACKETS_TO_CHECK  7
76
#define MAX_NDPI_FLOWS      200000000
77
3.31M
#define TICK_RESOLUTION          1000
78
#define MAX_NUM_IP_ADDRESS          5  /* len of ip address array */
79
#define UPDATED_TREE                1
80
#define AGGRESSIVE_PERCENT      95.00
81
#define DIR_SRC                    10
82
#define DIR_DST                    20
83
#define PORT_ARRAY_SIZE            20
84
#define HOST_ARRAY_SIZE            20
85
#define FLOWS_PACKETS_THRESHOLD   0.9
86
#define FLOWS_PERCENT_THRESHOLD   1.0
87
#define FLOWS_PERCENT_THRESHOLD_2 0.2
88
#define FLOWS_THRESHOLD          1000
89
#define PKTS_PERCENT_THRESHOLD    0.1
90
#define MAX_TABLE_SIZE_1         4096
91
#define MAX_TABLE_SIZE_2         8192
92
#define INIT_VAL                   -1
93
#define SERIALIZATION_BUFSIZ     (8192 * 2)
94
95
96
#ifdef __cplusplus
97
extern "C" {
98
#endif
99
100
// inner hash table (ja3 -> security state)
101
typedef struct ndpi_ja3_info {
102
  char * ja3;
103
  ndpi_cipher_weakness unsafe_cipher;
104
  UT_hash_handle hh;
105
} ndpi_ja3_info;
106
107
// external hash table (host ip -> <ip string, hash table ja3c, hash table ja3s>)
108
// used to aggregate ja3 fingerprints by hosts
109
typedef struct ndpi_host_ja3_fingerprints {
110
  u_int32_t ip;
111
  char *ip_string;
112
  char *dns_name;
113
  ndpi_ja3_info *host_client_info_hasht;
114
  ndpi_ja3_info *host_server_info_hasht;
115
116
  UT_hash_handle hh;
117
} ndpi_host_ja3_fingerprints;
118
119
120
//inner hash table
121
typedef struct ndpi_ip_dns{
122
  u_int32_t ip;
123
  char *ip_string;
124
  char *dns_name; //server name if any;
125
  UT_hash_handle hh;
126
} ndpi_ip_dns;
127
128
//hash table ja3 -> <host, ip, security>, used to aggregate host by ja3 fingerprints
129
typedef struct ndpi_ja3_fingerprints_host{
130
  char *ja3; //key
131
  ndpi_cipher_weakness unsafe_cipher;
132
  ndpi_ip_dns *ipToDNS_ht;
133
  UT_hash_handle hh;
134
} ndpi_ja3_fingerprints_host;
135
136
struct flow_metrics {
137
  float entropy, average, stddev;
138
};
139
140
struct ndpi_entropy {
141
  // Entropy fields
142
  u_int16_t src2dst_pkt_len[MAX_NUM_PKTS];                     /*!< array of packet appdata lengths */
143
  pkt_timeval src2dst_pkt_time[MAX_NUM_PKTS];               /*!< array of arrival times          */
144
  u_int16_t dst2src_pkt_len[MAX_NUM_PKTS];                     /*!< array of packet appdata lengths */
145
  pkt_timeval dst2src_pkt_time[MAX_NUM_PKTS];               /*!< array of arrival times          */
146
  pkt_timeval src2dst_start;                                /*!< first packet arrival time       */
147
  pkt_timeval dst2src_start;                                /*!< first packet arrival time       */
148
  u_int32_t src2dst_opackets;                                  /*!< non-zero packet counts          */
149
  u_int32_t dst2src_opackets;                                  /*!< non-zero packet counts          */
150
  u_int16_t src2dst_pkt_count;                                 /*!< packet counts                   */
151
  u_int16_t dst2src_pkt_count;                                 /*!< packet counts                   */
152
  u_int32_t src2dst_l4_bytes;                                  /*!< packet counts                   */
153
  u_int32_t dst2src_l4_bytes;                                  /*!< packet counts                   */
154
  u_int32_t src2dst_byte_count[MAX_BYTE_COUNT_ARRAY_LENGTH];   /*!< number of occurences of each byte   */
155
  u_int32_t dst2src_byte_count[MAX_BYTE_COUNT_ARRAY_LENGTH];   /*!< number of occurences of each byte   */
156
  u_int32_t src2dst_num_bytes;
157
  u_int32_t dst2src_num_bytes;
158
  double src2dst_bd_mean;
159
  double src2dst_bd_variance;
160
  double dst2src_bd_mean;
161
  double dst2src_bd_variance;
162
  float score;
163
};
164
165
enum info_type {
166
    INFO_INVALID = 0,
167
    INFO_GENERIC,
168
    INFO_KERBEROS,
169
    INFO_SOFTETHER,
170
    INFO_TIVOCONNECT,
171
    INFO_FTP_IMAP_POP_SMTP,
172
    INFO_NATPMP,
173
};
174
175
// flow tracking
176
typedef struct ndpi_flow_info {
177
  u_int32_t flow_id;
178
  u_int32_t hashval;
179
  u_int32_t src_ip; /* network order */
180
  u_int32_t dst_ip; /* network order */
181
  struct ndpi_in6_addr src_ip6; /* network order */
182
  struct ndpi_in6_addr dst_ip6; /* network order */
183
  u_int16_t src_port; /* network order */
184
  u_int16_t dst_port; /* network order */
185
  u_int8_t detection_completed, protocol, bidirectional, check_extra_packets;
186
  u_int16_t vlan_id;
187
  ndpi_packet_tunnel tunnel_type;
188
  struct ndpi_flow_struct *ndpi_flow;
189
  char src_name[INET6_ADDRSTRLEN], dst_name[INET6_ADDRSTRLEN];
190
  u_int8_t ip_version;
191
  u_int32_t cwr_count, src2dst_cwr_count, dst2src_cwr_count;
192
  u_int32_t ece_count, src2dst_ece_count, dst2src_ece_count;
193
  u_int32_t urg_count, src2dst_urg_count, dst2src_urg_count;
194
  u_int32_t ack_count, src2dst_ack_count, dst2src_ack_count;
195
  u_int32_t psh_count, src2dst_psh_count, dst2src_psh_count;
196
  u_int32_t syn_count, src2dst_syn_count, dst2src_syn_count;
197
  u_int32_t fin_count, src2dst_fin_count, dst2src_fin_count;
198
  u_int32_t rst_count, src2dst_rst_count, dst2src_rst_count;
199
  u_int32_t c_to_s_init_win, s_to_c_init_win;
200
  u_int64_t first_seen_ms, last_seen_ms;
201
  u_int64_t src2dst_bytes, dst2src_bytes;
202
  u_int64_t src2dst_goodput_bytes, dst2src_goodput_bytes;
203
  u_int32_t src2dst_packets, dst2src_packets;
204
  u_int32_t has_human_readeable_strings;
205
  char human_readeable_string_buffer[32];
206
  char *risk_str;
207
208
  // result only, not used for flow identification
209
  ndpi_protocol detected_protocol;
210
  ndpi_confidence_t confidence;
211
  u_int16_t num_dissector_calls;
212
  u_int16_t dpi_packets;
213
214
  // Flow data analysis
215
  pkt_timeval src2dst_last_pkt_time, dst2src_last_pkt_time, flow_last_pkt_time;
216
  struct ndpi_analyze_struct *iat_c_to_s, *iat_s_to_c, *iat_flow,
217
    *pktlen_c_to_s, *pktlen_s_to_c;
218
219
  enum info_type info_type;
220
221
  union {
222
    char info[256];
223
    
224
    struct {
225
      unsigned char auth_failed;
226
      char username[127];
227
      char password[128];
228
    } ftp_imap_pop_smtp;
229
    
230
    struct {
231
      char domain[85];
232
      char hostname[85];
233
      char username[86];
234
    } kerberos;
235
    
236
    struct {
237
      char ip[16];
238
      char port[6];
239
      char hostname[48];
240
      char fqdn[48];
241
    } softether;
242
    
243
    struct {
244
      char identity_uuid[36];
245
      char machine[48];
246
      char platform[32];
247
      char services[48];
248
    } tivoconnect;
249
    
250
    struct  {
251
      uint16_t result_code;
252
      uint16_t internal_port;
253
      uint16_t external_port;
254
      char ip[16];
255
    } natpmp;
256
  };
257
258
  ndpi_serializer ndpi_flow_serializer;
259
260
  char host_server_name[80]; /* Hostname/SNI */
261
  char *bittorent_hash;
262
  char *dhcp_fingerprint;
263
  char *dhcp_class_ident;
264
  ndpi_risk risk;
265
266
  struct {
267
    char currency[16];
268
  } mining;
269
  
270
  struct {
271
    u_int16_t ssl_version;
272
    char server_info[64],
273
      client_hassh[33], server_hassh[33], *server_names,
274
      *advertised_alpns, *negotiated_alpn, *tls_supported_versions,
275
      *tls_issuerDN, *tls_subjectDN,
276
      ja3_client[33], ja3_server[33], ja4_client[37],
277
      sha1_cert_fingerprint[20];
278
    u_int8_t sha1_cert_fingerprint_set;
279
    struct tls_heuristics browser_heuristics;
280
    
281
    struct {
282
      u_int16_t cipher_suite;
283
      char *esni;
284
    } encrypted_sni;    
285
286
    struct {
287
      u_int16_t version;
288
    } encrypted_ch;
289
290
    time_t notBefore, notAfter;
291
    u_int16_t server_cipher;
292
    ndpi_cipher_weakness client_unsafe_cipher, server_unsafe_cipher;
293
294
    u_int32_t quic_version;
295
  } ssh_tls;
296
297
  struct {
298
    char url[256], request_content_type[64], content_type[64], user_agent[256], server[128], nat_ip[32], filename[256];
299
    u_int response_status_code;
300
  } http;
301
302
  struct {
303
    char *username, *password;
304
  } telnet;
305
306
  struct {
307
    char geolocation_iata_code[4];
308
  } dns;
309
310
  ndpi_multimedia_flow_type multimedia_flow_type;
311
  
312
  void *src_id, *dst_id;
313
314
  struct ndpi_entropy *entropy;
315
  struct ndpi_entropy *last_entropy;
316
317
  /* Payload lenght bins */
318
#ifdef DIRECTION_BINS
319
  struct ndpi_bin payload_len_bin_src2dst, payload_len_bin_dst2src;
320
#else
321
  struct ndpi_bin payload_len_bin;
322
#endif
323
324
  /* Flow payload */
325
  u_int16_t flow_payload_len;
326
  char *flow_payload;  
327
} ndpi_flow_info_t;
328
329
330
// flow statistics info
331
typedef struct ndpi_stats {
332
  u_int32_t guessed_flow_protocols;
333
  u_int64_t raw_packet_count;
334
  u_int64_t ip_packet_count;
335
  u_int64_t total_wire_bytes, total_ip_bytes, total_discarded_bytes;
336
  u_int64_t protocol_counter[NDPI_MAX_SUPPORTED_PROTOCOLS + NDPI_MAX_NUM_CUSTOM_PROTOCOLS + 1];
337
  u_int64_t protocol_counter_bytes[NDPI_MAX_SUPPORTED_PROTOCOLS + NDPI_MAX_NUM_CUSTOM_PROTOCOLS + 1];
338
  u_int32_t protocol_flows[NDPI_MAX_SUPPORTED_PROTOCOLS + NDPI_MAX_NUM_CUSTOM_PROTOCOLS + 1];
339
  u_int32_t ndpi_flow_count;
340
  u_int32_t flow_count[3];
341
  u_int64_t tcp_count, udp_count;
342
  u_int64_t mpls_count, pppoe_count, vlan_count, fragmented_count;
343
  u_int64_t packet_len[6];
344
  u_int16_t max_packet_len;
345
  u_int64_t dpi_packet_count[3];
346
  u_int64_t flow_confidence[NDPI_CONFIDENCE_MAX];
347
  u_int64_t num_dissector_calls;
348
349
  struct ndpi_lru_cache_stats lru_stats[NDPI_LRUCACHE_MAX];
350
  struct ndpi_automa_stats automa_stats[NDPI_AUTOMA_MAX];
351
  struct ndpi_patricia_tree_stats patricia_stats[NDPI_PTREE_MAX];
352
} ndpi_stats_t;
353
354
355
// flow preferences
356
typedef struct ndpi_workflow_prefs {
357
  u_int8_t decode_tunnels;
358
  u_int8_t quiet_mode;
359
  u_int8_t ignore_vlanid;
360
  u_int32_t num_roots;
361
  u_int32_t max_ndpi_flows;
362
} ndpi_workflow_prefs_t;
363
364
struct ndpi_workflow;
365
366
/** workflow, flow, user data */
367
typedef void (*ndpi_workflow_callback_ptr) (struct ndpi_workflow *, struct ndpi_flow_info *, void *);
368
369
370
// workflow main structure
371
typedef struct ndpi_workflow {
372
  u_int64_t last_time;
373
374
  struct ndpi_workflow_prefs prefs;
375
  struct ndpi_stats stats;
376
377
  ndpi_workflow_callback_ptr flow_callback;
378
  void * flow_callback_userdata;
379
380
  /* outside referencies */
381
  pcap_t *pcap_handle;
382
383
  /* allocated by prefs */
384
  void **ndpi_flows_root;
385
  struct ndpi_detection_module_struct *ndpi_struct;
386
  u_int32_t num_allocated_flows;
387
388
  /* CSV,TLV,JSON serialization interface */
389
  ndpi_serialization_format ndpi_serialization_format;
390
} ndpi_workflow_t;
391
392
393
/* TODO: remove wrappers parameters and use ndpi global, when their initialization will be fixed... */
394
struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, pcap_t * pcap_handle, int do_init_flows_root, ndpi_serialization_format serialization_format);
395
396
397
/* workflow main free function */
398
void ndpi_workflow_free(struct ndpi_workflow * workflow);
399
400
401
/** Free flow_info ndpi support structures but not the flow_info itself
402
 *
403
 *  TODO remove! Half freeing things is bad!
404
 */
405
void ndpi_free_flow_info_half(struct ndpi_flow_info *flow);
406
407
408
/* Process a packet and update the workflow  */
409
struct ndpi_proto ndpi_workflow_process_packet(struct ndpi_workflow * workflow,
410
                 const struct pcap_pkthdr *header,
411
                 const u_char *packet,
412
                 ndpi_risk *flow_risk);
413
414
415
/* Flow callback for completed flows, before the flow memory will be freed. */
416
0
static inline void ndpi_workflow_set_flow_callback(struct ndpi_workflow * workflow, ndpi_workflow_callback_ptr callback, void * userdata) {
417
0
  workflow->flow_callback = callback;
418
0
  workflow->flow_callback_userdata = userdata;
419
0
}
Unexecuted instantiation: fuzz_readerutils_parseprotolist.cpp:ndpi_workflow_set_flow_callback(ndpi_workflow*, void (*)(ndpi_workflow*, ndpi_flow_info*, void*), void*)
Unexecuted instantiation: reader_util.c:ndpi_workflow_set_flow_callback
Unexecuted instantiation: fuzz_ndpi_reader.c:ndpi_workflow_set_flow_callback
Unexecuted instantiation: fuzz_readerutils_workflow.cpp:ndpi_workflow_set_flow_callback(ndpi_workflow*, void (*)(ndpi_workflow*, ndpi_flow_info*, void*), void*)
420
421
int ndpi_is_datalink_supported(int datalink_type);
422
423
/* compare two nodes in workflow */
424
int ndpi_workflow_node_cmp(const void *a, const void *b);
425
void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_flow_info *flow);
426
void ndpi_flow_info_free_data(struct ndpi_flow_info *flow);
427
void ndpi_flow_info_freer(void *node);
428
const char* print_cipher_id(u_int32_t cipher);
429
int parse_proto_name_list(char *str, NDPI_PROTOCOL_BITMASK *bitmask, int inverted_logic);
430
431
extern int nDPI_LogLevel;
432
433
#if defined(NDPI_ENABLE_DEBUG_MESSAGES) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
434
#define LOG(log_level, args...)     \
435
  {           \
436
    if(log_level <= nDPI_LogLevel)    \
437
      printf(args);       \
438
  }
439
#else
440
9.14M
#define LOG(...) {}
441
#endif
442
443
#ifndef LINKTYPE_LINUX_SLL2
444
44.9k
#define LINKTYPE_LINUX_SLL2 276
445
#endif
446
447
#ifdef __cplusplus
448
}
449
#endif
450
451
#endif