session: cleanup part 1
[vpp.git] / src / vnet / tls / tls.h
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 #include <vnet/session/application_interface.h>
18 #include <vppinfra/lock.h>
19
20 #ifndef SRC_VNET_TLS_TLS_H_
21 #define SRC_VNET_TLS_TLS_H_
22
23 #define TLS_DEBUG               0
24 #define TLS_DEBUG_LEVEL_CLIENT  0
25 #define TLS_DEBUG_LEVEL_SERVER  0
26
27 #define TLS_CHUNK_SIZE          (1 << 14)
28 #define TLS_CA_CERT_PATH        "/etc/ssl/certs/ca-certificates.crt"
29
30 #if TLS_DEBUG
31 #define TLS_DBG(_lvl, _fmt, _args...)                   \
32   if (_lvl <= TLS_DEBUG)                                \
33     clib_warning (_fmt, ##_args)
34 #else
35 #define TLS_DBG(_lvl, _fmt, _args...)
36 #endif
37
38 /* *INDENT-OFF* */
39 typedef CLIB_PACKED (struct tls_cxt_id_
40 {
41   u32 parent_app_index;
42   union {
43     session_handle_t app_session_handle;
44     u32 parent_app_api_ctx;
45   };
46   session_handle_t tls_session_handle;
47   u32 ssl_ctx;
48   u32 listener_ctx_index;
49   u8 tcp_is_ip4;
50   u8 tls_engine_id;
51 }) tls_ctx_id_t;
52 /* *INDENT-ON* */
53
54 STATIC_ASSERT (sizeof (tls_ctx_id_t) <= 42, "ctx id must be less than 42");
55
56 typedef struct tls_ctx_
57 {
58   union
59   {
60     transport_connection_t connection;
61     tls_ctx_id_t c_tls_ctx_id;
62   };
63 #define parent_app_index c_tls_ctx_id.parent_app_index
64 #define app_session_handle c_tls_ctx_id.app_session_handle
65 #define tls_session_handle c_tls_ctx_id.tls_session_handle
66 #define listener_ctx_index c_tls_ctx_id.listener_ctx_index
67 #define tcp_is_ip4 c_tls_ctx_id.tcp_is_ip4
68 #define tls_ctx_engine c_tls_ctx_id.tls_engine_id
69 #define tls_ssl_ctx c_tls_ctx_id.ssl_ctx
70 #define tls_ctx_handle c_c_index
71   /* Temporary storage for session open opaque. Overwritten once
72    * underlying tcp connection is established */
73 #define parent_app_api_context c_tls_ctx_id.parent_app_api_ctx
74
75   u8 is_passive_close;
76   u8 resume;
77   u8 *srv_hostname;
78 } tls_ctx_t;
79
80 typedef struct tls_main_
81 {
82   u32 app_index;
83   tls_ctx_t *listener_ctx_pool;
84   tls_ctx_t *half_open_ctx_pool;
85   clib_rwlock_t half_open_rwlock;
86   u8 **rx_bufs;
87   u8 **tx_bufs;
88
89   /*
90    * Config
91    */
92   u8 use_test_cert_in_ca;
93   char *ca_cert_path;
94 } tls_main_t;
95
96 typedef struct tls_engine_vft_
97 {
98   u32 (*ctx_alloc) (void);
99   void (*ctx_free) (tls_ctx_t * ctx);
100   tls_ctx_t *(*ctx_get) (u32 ctx_index);
101   tls_ctx_t *(*ctx_get_w_thread) (u32 ctx_index, u8 thread_index);
102   int (*ctx_init_client) (tls_ctx_t * ctx);
103   int (*ctx_init_server) (tls_ctx_t * ctx);
104   int (*ctx_read) (tls_ctx_t * ctx, session_t * tls_session);
105   int (*ctx_write) (tls_ctx_t * ctx, session_t * app_session);
106     u8 (*ctx_handshake_is_over) (tls_ctx_t * ctx);
107   int (*ctx_start_listen) (tls_ctx_t * ctx);
108   int (*ctx_stop_listen) (tls_ctx_t * ctx);
109 } tls_engine_vft_t;
110
111 typedef enum tls_engine_type_
112 {
113   TLS_ENGINE_NONE,
114   TLS_ENGINE_MBEDTLS,
115   TLS_ENGINE_OPENSSL,
116   TLS_N_ENGINES
117 } tls_engine_type_t;
118
119 tls_main_t *vnet_tls_get_main (void);
120 void tls_register_engine (const tls_engine_vft_t * vft,
121                           tls_engine_type_t type);
122 int tls_add_vpp_q_rx_evt (session_t * s);
123 int tls_add_vpp_q_tx_evt (session_t * s);
124 int tls_add_vpp_q_builtin_tx_evt (session_t * s);
125 int tls_add_vpp_q_builtin_rx_evt (session_t * s);
126 int tls_notify_app_accept (tls_ctx_t * ctx);
127 int tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed);
128 void tls_notify_app_enqueue (tls_ctx_t * ctx, session_t * app_session);
129 #endif /* SRC_VNET_TLS_TLS_H_ */
130 /*
131  * fd.io coding-style-patch-verification: ON
132  *
133  * Local Variables:
134  * eval: (c-set-style "gnu")
135  * End:
136  */