aboutsummaryrefslogtreecommitdiffstats
path: root/src/dpi.h
blob: cc77e29f725e8e06ea738c426cb11fff47fa1f91 (plain)
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
/*
 *------------------------------------------------------------------
 * Copyright (c) 2018 Intel, Travelping and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#ifndef included_vnet_dpi_h
#define included_vnet_dpi_h

#include <vppinfra/lock.h>
#include <vppinfra/error.h>
#include <vppinfra/hash.h>
#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/ip/ip4_packet.h>
#include <vnet/ip/ip6_packet.h>
#include <vnet/udp/udp.h>
#include <vnet/dpo/dpo.h>
#include <vppinfra/bihash_8_8.h>
#include <vppinfra/bihash_24_8.h>
#include <vppinfra/bihash_48_8.h>

#include <hs/hs.h>
#include <hs/hs_common.h>
#include <hs/hs_compile.h>
#include <hs/hs_runtime.h>
#include "dpi_app_match.h"

extern vlib_node_registration_t dpi4_flow_input_node;
extern vlib_node_registration_t dpi6_flow_input_node;

typedef u8 *regex_t;

typedef struct
{
  u8 *name;
  regex_t *expressions;
  u32 *flags;
  u32 *ids;
  hs_database_t *database;
  hs_scratch_t *scratch;
  u32 ref_cnt;
} dpi_entry_t;

typedef struct
{
  int res;
  u32 id;
} dpi_cb_args_t;

typedef struct
{
  union
  {
    struct
    {
      ip46_address_t src_ip;
      ip46_address_t dst_ip;
      u16 src_port;
      u16 dst_port;
      u8 protocol;
      u32 fib_index;
    };
    u64 key[6];
  };
} dpi_flow_key_t;

typedef clib_bihash_kv_24_8_t dpi4_flow_key_t;
typedef clib_bihash_kv_48_8_t dpi6_flow_key_t;

typedef struct
{
  /* SSL */
  u8 ssl_stage;
  u8 ssl_got_server_cert;
} dpi_flow_tcp_t;

typedef struct
{
  /* TBD */
} dpi_flow_udp_t;

typedef struct segment_
{
  u32 send_sn;
  u8 *data;
  u32 len;
  u32 bi;			/* vlib buffer index */
  struct segment_ *next;
} segment;

typedef struct tcp_stream_
{
  u32 send_sn;			/* expected segment sn */
  u32 ack_sn;			/* acked segment sn */
  segment *seg_queue;		/* store out-of-order segments */
} tcp_stream_t;

typedef struct dpi_flow_info
{
  /* Required for pool_get_aligned  */
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);

  u16 detected_protocol[2];
  u16 protocol_stack_info;
  u16 pkt_num;
  u16 pkt_direct_counter[2];

  hs_stream_t *stream;
  u8 detect_begin;
  u8 detect_done;
  u32 app_id;			/* L7 APP ID */

  u16 guessed_protocol_id;
  u16 guessed_host_protocol_id;

  u8 max_more_pkts_to_check;

  int (*more_pkts_func) (u8 * payload, u32 payload_len,
			 struct dpi_flow_info * flow);

  u16 dst_port;
  u8 l4_protocol;
  union
  {
    dpi_flow_tcp_t tcp;
    dpi_flow_udp_t udp;
  } l4;

  u8 ssl_cert_detected:4;
  u8 ssl_cert_num_checks:4;

  union
  {
    struct
    {
      char server_cert[64];
    } ssl;
    /* TBD: Add more protocols */
  } protos;
} dpi_flow_info_t;

typedef enum
{
  TCP_STATE_SYN = 1,
  TCP_STATE_SYN_ACK = 2,
  TCP_STATE_ACK = 3,
  TCP_STATE_ESTABLISH = 4,
  TCP_STATE_FIN1 = 5,
  TCP_STATE_CLOSE = 6,
} tcp_state_t;

/* tcp packet direction */
#define DIR_C2S 0
#define DIR_S2C 1

/* tcp reassembly side */
#define REASS_C2S 0
#define REASS_S2C 1
#define REASS_BOTH 2

/* Macros to handle sequence numbers */
#define SN_LT(a,b)  ((int)((a) - (b)) <  0)
#define SN_GT(a,b)  ((int)((a) - (b)) >  0)
#define SN_EQ(a,b)  ((int)((a) - (b)) == 0)

typedef struct
{
  /* Required for pool_get_aligned  */
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);

  u32 flow_index;		/* infra flow index */
  u32 next_index;

  u8 check_more_pkts:1;
  u8 pkt_dir:1;
  u8 forward_is_c2s:1;
  u8 consumed:1;
  u8 reass_en:1;
  u8 reass_dir:2;

  dpi_flow_key_t key;
  dpi_flow_info_t *info;

  /* TCP stream reassembly */
  tcp_state_t tcp_state;
  tcp_stream_t c2s;
  tcp_stream_t s2c;
  segment *first_seg;

} dpi_flow_entry_t;

typedef struct
{
  u32 flow_id;
  u8 reass_en;
  u8 reass_dir;
} tcp_reass_args_t;

typedef struct
{
  u8 is_add;
  u8 is_ipv6;
  ip46_address_t src_ip;
  ip46_address_t dst_ip;
  u16 src_port;
  u16 dst_port;
  u8 protocol;
  u32 fib_index;
} dpi_add_del_flow_args_t;

typedef struct
{
  u32 app_id;
  u32 db_id;
} dpi_adr_t;

typedef struct
{
  /* lookup tunnel by key */
  clib_bihash_24_8_t dpi4_flow_by_key;
  clib_bihash_48_8_t dpi6_flow_by_key;

  /* vector of dpi flow instances */
  dpi_flow_entry_t *dpi_flows;
  u32 flow_id_start;
  dpi_flow_info_t *dpi_infos;
  segment *seg_pool;

  /* Default hyperscan database */
  dpi_entry_t default_db;

  /* graph node state */
  uword *bm_ip4_bypass_enabled_by_sw_if;
  uword *bm_ip6_bypass_enabled_by_sw_if;

  /* API message ID base */
  u16 msg_id_base;

  /* convenience */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;
} dpi_main_t;

extern dpi_main_t dpi_main;

#define foreach_copy_field              \
_(src_ip)                               \
_(dst_ip)                               \
_(src_port)                             \
_(dst_port)                             \
_(protocol)                             \
_(fib_index)


#define dpi_enqueue_tcp_segments(seg,vm,node,next_index,to_next,n_left_to_next,bi0,next0) \
while(seg)        \
  {               \
    bi0 = seg->bi;  \
    to_next[0] = bi0; \
    to_next++;        \
    n_left_to_next--; \
    next0 = flow0->next_index;         \
    vlib_validate_buffer_enqueue_x1 (vm, node, next_index,  \
                     to_next, n_left_to_next,   \
                     bi0, next0);   \
    prev_seg = seg;  \
    seg=seg->next;   \
    pool_put(dm->seg_pool, prev_seg);  \
  }


#define get_u16_t(X,O)  (*(u16 *)(((u8 *)X) + O))
#define DPI_MAX_SSL_REQUEST_SIZE 10000

int dpi_flow_add_del (dpi_add_del_flow_args_t * a, u32 * flow_idp);
int dpi_reverse_flow_add_del (dpi_add_del_flow_args_t * a, u32 flow_id);
int dpi_add_del_rx_flow (u32 hw_if_index, u32 flow_id, int is_add,
			 u32 is_ipv6);
int dpi_tcp_reass (tcp_reass_args_t * a);
void dpi_flow_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable);
void dpi_flow_offload_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable);
int dpi_search_host_protocol (dpi_flow_info_t * flow,
			      char *str_to_match,
			      u32 str_to_match_len,
			      u16 master_protocol_id, u32 * host_protocol_id);
void dpi_search_tcp_ssl (u8 * payload, u32 payload_len,
			 dpi_flow_info_t * flow);

void dpi_detect_application (u8 * payload, u32 payload_len,
			     dpi_flow_info_t * flow);

typedef enum
{
  DPI_PROTOCOL_UNKNOWN = 0,
  DPI_PROTOCOL_SSL = 1,
  DPI_PROTOCOL_SSL_NO_CERT = 2,
  DPI_N_PROTOCOL
} dpi_protocol_id_t;

#define foreach_dpi_input_next        \
_(DROP, "error-drop")                 \
_(IP4_LOOKUP, "ip4-lookup")           \
_(IP6_LOOKUP, "ip6-lookup")

typedef enum
{
#define _(s,n) DPI_INPUT_NEXT_##s,
  foreach_dpi_input_next
#undef _
    DPI_INPUT_N_NEXT,
} dpi_input_next_t;

#endif

#define foreach_dpi_input_error \
 _(NONE, "no error") \
 _(NO_SUCH_FLOW, "flow not existed")

typedef enum
{
#define _(sym,str) DPI_INPUT_ERROR_##sym,
  foreach_dpi_input_error
#undef _
    DPI_INPUT_N_ERROR,
} dpi_input_error_t;

static char *dpi_input_error_strings[] = {
#define _(sym,string) string,
  foreach_dpi_input_error
#undef _
};

#define foreach_dpi_flow_input_next        \
_(DROP, "error-drop")                      \
_(IP4_LOOKUP, "ip4-lookup")                \
_(IP6_LOOKUP, "ip6-lookup")

typedef enum
{
#define _(s,n) DPI_FLOW_NEXT_##s,
  foreach_dpi_flow_input_next
#undef _
    DPI_FLOW_N_NEXT,
} dpi_flow_input_next_t;

#define foreach_dpi_flow_error                    \
  _(NONE, "no error")                           \
  _(IP_CHECKSUM_ERROR, "Rx ip checksum errors")             \
  _(IP_HEADER_ERROR, "Rx ip header errors")             \
  _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors")               \
  _(UDP_LENGTH_ERROR, "Rx udp length errors")

typedef enum
{
#define _(f,s) DPI_FLOW_ERROR_##f,
  foreach_dpi_flow_error
#undef _
    DPI_FLOW_N_ERROR,
} dpi_flow_error_t;

static char *dpi_flow_error_strings[] = {
#define _(n,s) s,
  foreach_dpi_flow_error
#undef _
};

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */