blob: b576c00d4e9752c463c2a195363ce66e606080e4 [file] [log] [blame]
Florin Coras371ca502018-02-21 12:07:41 -08001/*
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#include <vnet/session/application_interface.h>
17#include <vppinfra/lock.h>
Florin Corasd77eee62018-03-07 08:49:27 -080018#include <vnet/tls/tls.h>
Florin Coras371ca502018-02-21 12:07:41 -080019
20static tls_main_t tls_main;
Florin Corasd77eee62018-03-07 08:49:27 -080021static tls_engine_vft_t *tls_vfts;
Florin Coras371ca502018-02-21 12:07:41 -080022
Florin Coras58d36f02018-03-09 13:05:53 -080023#define TLS_INVALID_HANDLE ~0
24#define TLS_IDX_MASK 0x00FFFFFF
25#define TLS_ENGINE_TYPE_SHIFT 29
26
27void tls_disconnect (u32 ctx_handle, u32 thread_index);
28
29tls_engine_type_t
30tls_get_available_engine (void)
31{
32 int i;
33 for (i = 0; i < vec_len (tls_vfts); i++)
34 {
35 if (tls_vfts[i].ctx_alloc)
36 return i;
37 }
38 return TLS_ENGINE_NONE;
39}
Florin Coras371ca502018-02-21 12:07:41 -080040
Florin Corasd77eee62018-03-07 08:49:27 -080041int
Florin Coras371ca502018-02-21 12:07:41 -080042tls_add_vpp_q_evt (svm_fifo_t * f, u8 evt_type)
43{
Florin Coras371ca502018-02-21 12:07:41 -080044 if (svm_fifo_set_event (f))
Florin Coras3c2fed52018-07-04 04:15:05 -070045 session_send_io_evt_to_thread (f, evt_type);
Florin Coras371ca502018-02-21 12:07:41 -080046 return 0;
47}
48
49static inline int
Florin Coras15531972018-08-12 23:50:53 -070050tls_add_app_q_evt (app_worker_t * app, stream_session_t * app_session)
Florin Coras371ca502018-02-21 12:07:41 -080051{
Florin Coras3c2fed52018-07-04 04:15:05 -070052 return application_send_event (app, app_session, FIFO_EVENT_APP_RX);
Florin Coras371ca502018-02-21 12:07:41 -080053}
54
55u32
Florin Coras371ca502018-02-21 12:07:41 -080056tls_listener_ctx_alloc (void)
57{
58 tls_main_t *tm = &tls_main;
59 tls_ctx_t *ctx;
60
61 pool_get (tm->listener_ctx_pool, ctx);
62 memset (ctx, 0, sizeof (*ctx));
63 return ctx - tm->listener_ctx_pool;
64}
65
66void
Florin Corase3e2f072018-03-04 07:24:30 -080067tls_listener_ctx_free (tls_ctx_t * ctx)
Florin Coras371ca502018-02-21 12:07:41 -080068{
Florin Corase3e2f072018-03-04 07:24:30 -080069 pool_put (tls_main.listener_ctx_pool, ctx);
Florin Coras371ca502018-02-21 12:07:41 -080070}
71
72tls_ctx_t *
73tls_listener_ctx_get (u32 ctx_index)
74{
75 return pool_elt_at_index (tls_main.listener_ctx_pool, ctx_index);
76}
77
78u32
79tls_listener_ctx_index (tls_ctx_t * ctx)
80{
81 return (ctx - tls_main.listener_ctx_pool);
82}
83
84u32
85tls_ctx_half_open_alloc (void)
86{
87 tls_main_t *tm = &tls_main;
88 u8 will_expand = 0;
89 tls_ctx_t *ctx;
90 u32 ctx_index;
91
92 pool_get_aligned_will_expand (tm->half_open_ctx_pool, will_expand, 0);
93 if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
94 {
95 clib_rwlock_writer_lock (&tm->half_open_rwlock);
96 pool_get (tm->half_open_ctx_pool, ctx);
97 memset (ctx, 0, sizeof (*ctx));
98 ctx_index = ctx - tm->half_open_ctx_pool;
99 clib_rwlock_writer_unlock (&tm->half_open_rwlock);
100 }
101 else
102 {
103 pool_get (tm->half_open_ctx_pool, ctx);
104 memset (ctx, 0, sizeof (*ctx));
105 ctx_index = ctx - tm->half_open_ctx_pool;
106 }
107 return ctx_index;
108}
109
110void
111tls_ctx_half_open_free (u32 ho_index)
112{
113 tls_main_t *tm = &tls_main;
114 clib_rwlock_writer_lock (&tm->half_open_rwlock);
115 pool_put_index (tls_main.half_open_ctx_pool, ho_index);
116 clib_rwlock_writer_unlock (&tm->half_open_rwlock);
117}
118
119tls_ctx_t *
120tls_ctx_half_open_get (u32 ctx_index)
121{
122 tls_main_t *tm = &tls_main;
123 clib_rwlock_reader_lock (&tm->half_open_rwlock);
124 return pool_elt_at_index (tm->half_open_ctx_pool, ctx_index);
125}
126
127void
128tls_ctx_half_open_reader_unlock ()
129{
130 clib_rwlock_reader_unlock (&tls_main.half_open_rwlock);
131}
132
133u32
134tls_ctx_half_open_index (tls_ctx_t * ctx)
135{
136 return (ctx - tls_main.half_open_ctx_pool);
137}
138
Florin Coras58d36f02018-03-09 13:05:53 -0800139void
140tls_notify_app_enqueue (tls_ctx_t * ctx, stream_session_t * app_session)
141{
Florin Coras15531972018-08-12 23:50:53 -0700142 app_worker_t *app;
143 app = app_worker_get_if_valid (app_session->app_wrk_index);
Florin Coras5090c572018-03-18 08:22:17 -0700144 if (PREDICT_TRUE (app != 0))
145 tls_add_app_q_evt (app, app_session);
Florin Coras58d36f02018-03-09 13:05:53 -0800146}
147
148int
Florin Coras371ca502018-02-21 12:07:41 -0800149tls_notify_app_accept (tls_ctx_t * ctx)
150{
151 stream_session_t *app_listener, *app_session;
152 segment_manager_t *sm;
Florin Coras15531972018-08-12 23:50:53 -0700153 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800154 application_t *app;
155 tls_ctx_t *lctx;
156 int rv;
157
Florin Coras15531972018-08-12 23:50:53 -0700158 app_wrk = app_worker_get (ctx->parent_app_index);
159 app = application_get (app_wrk->app_index);
Florin Coras371ca502018-02-21 12:07:41 -0800160 lctx = tls_listener_ctx_get (ctx->listener_ctx_index);
Florin Coras371ca502018-02-21 12:07:41 -0800161
162 app_session = session_alloc (vlib_get_thread_index ());
Florin Coras15531972018-08-12 23:50:53 -0700163 app_session->app_wrk_index = ctx->parent_app_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800164 app_session->connection_index = ctx->tls_ctx_handle;
Ping Yua0c29a92018-08-16 19:11:05 -0400165
166 app_listener = listen_session_get_from_handle (lctx->app_session_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800167 app_session->session_type = app_listener->session_type;
168 app_session->listener_index = app_listener->session_index;
Florin Coras15531972018-08-12 23:50:53 -0700169 sm = app_worker_get_listen_segment_manager (app_wrk, app_listener);
170 app_session->opaque = tls_main.app_index;
171
Florin Coras371ca502018-02-21 12:07:41 -0800172 if ((rv = session_alloc_fifos (sm, app_session)))
173 {
174 TLS_DBG (1, "failed to allocate fifos");
175 return rv;
176 }
177 ctx->c_s_index = app_session->session_index;
Florin Coras371ca502018-02-21 12:07:41 -0800178 ctx->app_session_handle = session_handle (app_session);
179 return app->cb_fns.session_accept_callback (app_session);
180}
181
Florin Coras58d36f02018-03-09 13:05:53 -0800182int
Florin Coras8f89dd02018-03-05 16:53:07 -0800183tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed)
Florin Coras371ca502018-02-21 12:07:41 -0800184{
185 int (*cb_fn) (u32, u32, stream_session_t *, u8);
186 stream_session_t *app_session;
187 segment_manager_t *sm;
Florin Coras15531972018-08-12 23:50:53 -0700188 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800189 application_t *app;
190
Florin Coras15531972018-08-12 23:50:53 -0700191 app_wrk = app_worker_get (ctx->parent_app_index);
192 app = application_get (app_wrk->app_index);
Florin Coras371ca502018-02-21 12:07:41 -0800193 cb_fn = app->cb_fns.session_connected_callback;
194
Florin Coras8f89dd02018-03-05 16:53:07 -0800195 if (is_failed)
196 goto failed;
197
Florin Coras15531972018-08-12 23:50:53 -0700198 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Coras371ca502018-02-21 12:07:41 -0800199 app_session = session_alloc (vlib_get_thread_index ());
Florin Coras15531972018-08-12 23:50:53 -0700200 app_session->app_wrk_index = ctx->parent_app_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800201 app_session->connection_index = ctx->tls_ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800202 app_session->session_type =
203 session_type_from_proto_and_ip (TRANSPORT_PROTO_TLS, ctx->tcp_is_ip4);
Florin Coras15531972018-08-12 23:50:53 -0700204 app_session->opaque = tls_main.app_index;
205
Florin Coras371ca502018-02-21 12:07:41 -0800206 if (session_alloc_fifos (sm, app_session))
207 goto failed;
208
209 ctx->app_session_handle = session_handle (app_session);
210 ctx->c_s_index = app_session->session_index;
Florin Coras371ca502018-02-21 12:07:41 -0800211 app_session->session_state = SESSION_STATE_READY;
212 if (cb_fn (ctx->parent_app_index, ctx->parent_app_api_context,
213 app_session, 0 /* not failed */ ))
214 {
215 TLS_DBG (1, "failed to notify app");
Florin Coras58d36f02018-03-09 13:05:53 -0800216 tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
Florin Coras371ca502018-02-21 12:07:41 -0800217 }
218
219 return 0;
220
221failed:
Florin Coras58d36f02018-03-09 13:05:53 -0800222 tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
Florin Coras371ca502018-02-21 12:07:41 -0800223 return cb_fn (ctx->parent_app_index, ctx->parent_app_api_context, 0,
224 1 /* failed */ );
225}
226
Florin Coras58d36f02018-03-09 13:05:53 -0800227static inline void
228tls_ctx_parse_handle (u32 ctx_handle, u32 * ctx_index, u32 * engine_type)
Florin Corasd77eee62018-03-07 08:49:27 -0800229{
Florin Coras58d36f02018-03-09 13:05:53 -0800230 *ctx_index = ctx_handle & TLS_IDX_MASK;
231 *engine_type = ctx_handle >> TLS_ENGINE_TYPE_SHIFT;
232}
233
234static inline tls_engine_type_t
235tls_get_engine_type (tls_engine_type_t preferred)
236{
237 if (!tls_vfts[preferred].ctx_alloc)
238 return tls_get_available_engine ();
239 return preferred;
240}
241
242static inline u32
243tls_ctx_alloc (tls_engine_type_t engine_type)
244{
245 u32 ctx_index;
246 ctx_index = tls_vfts[engine_type].ctx_alloc ();
247 return (((u32) engine_type << TLS_ENGINE_TYPE_SHIFT) | ctx_index);
Florin Corasd77eee62018-03-07 08:49:27 -0800248}
249
250static inline void
251tls_ctx_free (tls_ctx_t * ctx)
252{
253 vec_free (ctx->srv_hostname);
Florin Coras58d36f02018-03-09 13:05:53 -0800254 tls_vfts[ctx->tls_ctx_engine].ctx_free (ctx);
Florin Corasd77eee62018-03-07 08:49:27 -0800255}
256
257static inline tls_ctx_t *
Florin Coras58d36f02018-03-09 13:05:53 -0800258tls_ctx_get (u32 ctx_handle)
Florin Corasd77eee62018-03-07 08:49:27 -0800259{
Florin Coras58d36f02018-03-09 13:05:53 -0800260 u32 ctx_index, engine_type;
261 tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type);
262 return tls_vfts[engine_type].ctx_get (ctx_index);
Florin Corasd77eee62018-03-07 08:49:27 -0800263}
264
265static inline tls_ctx_t *
Florin Coras58d36f02018-03-09 13:05:53 -0800266tls_ctx_get_w_thread (u32 ctx_handle, u8 thread_index)
Florin Corasd77eee62018-03-07 08:49:27 -0800267{
Florin Coras58d36f02018-03-09 13:05:53 -0800268 u32 ctx_index, engine_type;
269 tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type);
270 return tls_vfts[engine_type].ctx_get_w_thread (ctx_index, thread_index);
Florin Corasd77eee62018-03-07 08:49:27 -0800271}
272
273static inline int
274tls_ctx_init_server (tls_ctx_t * ctx)
275{
Florin Coras58d36f02018-03-09 13:05:53 -0800276 return tls_vfts[ctx->tls_ctx_engine].ctx_init_server (ctx);
Florin Corasd77eee62018-03-07 08:49:27 -0800277}
278
279static inline int
280tls_ctx_init_client (tls_ctx_t * ctx)
281{
Florin Coras58d36f02018-03-09 13:05:53 -0800282 return tls_vfts[ctx->tls_ctx_engine].ctx_init_client (ctx);
Florin Corasd77eee62018-03-07 08:49:27 -0800283}
284
285static inline int
Florin Coras58d36f02018-03-09 13:05:53 -0800286tls_ctx_write (tls_ctx_t * ctx, stream_session_t * app_session)
Florin Corasd77eee62018-03-07 08:49:27 -0800287{
Florin Coras58d36f02018-03-09 13:05:53 -0800288 return tls_vfts[ctx->tls_ctx_engine].ctx_write (ctx, app_session);
Florin Corasd77eee62018-03-07 08:49:27 -0800289}
290
291static inline int
Florin Coras58d36f02018-03-09 13:05:53 -0800292tls_ctx_read (tls_ctx_t * ctx, stream_session_t * tls_session)
Florin Corasd77eee62018-03-07 08:49:27 -0800293{
Florin Coras58d36f02018-03-09 13:05:53 -0800294 return tls_vfts[ctx->tls_ctx_engine].ctx_read (ctx, tls_session);
Florin Corasd77eee62018-03-07 08:49:27 -0800295}
296
297static inline u8
298tls_ctx_handshake_is_over (tls_ctx_t * ctx)
299{
Florin Coras58d36f02018-03-09 13:05:53 -0800300 return tls_vfts[ctx->tls_ctx_engine].ctx_handshake_is_over (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800301}
302
303void
304tls_session_reset_callback (stream_session_t * s)
305{
306 clib_warning ("called...");
307}
308
309int
310tls_add_segment_callback (u32 client_index, const ssvm_private_t * fs)
311{
312 /* No-op for builtin */
313 return 0;
314}
315
316int
317tls_del_segment_callback (u32 client_index, const ssvm_private_t * fs)
318{
319 return 0;
320}
321
322void
323tls_session_disconnect_callback (stream_session_t * tls_session)
324{
325 stream_session_t *app_session;
326 tls_ctx_t *ctx;
Florin Coras15531972018-08-12 23:50:53 -0700327 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800328 application_t *app;
329
330 ctx = tls_ctx_get (tls_session->opaque);
Florin Corasd77eee62018-03-07 08:49:27 -0800331 if (!tls_ctx_handshake_is_over (ctx))
Florin Coras371ca502018-02-21 12:07:41 -0800332 {
333 stream_session_disconnect (tls_session);
334 return;
335 }
336 ctx->is_passive_close = 1;
Florin Coras15531972018-08-12 23:50:53 -0700337 app_wrk = app_worker_get (ctx->parent_app_index);
338 app = application_get (app_wrk->app_index);
Florin Coras371ca502018-02-21 12:07:41 -0800339 app_session = session_get_from_handle (ctx->app_session_handle);
340 app->cb_fns.session_disconnect_callback (app_session);
341}
342
343int
344tls_session_accept_callback (stream_session_t * tls_session)
345{
346 stream_session_t *tls_listener;
347 tls_ctx_t *lctx, *ctx;
Florin Coras58d36f02018-03-09 13:05:53 -0800348 u32 ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800349
Florin Coras5c9083d2018-04-13 06:39:07 -0700350 tls_listener = listen_session_get (tls_session->listener_index);
Florin Coras371ca502018-02-21 12:07:41 -0800351 lctx = tls_listener_ctx_get (tls_listener->opaque);
Florin Coras58d36f02018-03-09 13:05:53 -0800352
353 ctx_handle = tls_ctx_alloc (lctx->tls_ctx_engine);
354 ctx = tls_ctx_get (ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800355 memcpy (ctx, lctx, sizeof (*lctx));
356 ctx->c_thread_index = vlib_get_thread_index ();
Florin Coras58d36f02018-03-09 13:05:53 -0800357 ctx->tls_ctx_handle = ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800358 tls_session->session_state = SESSION_STATE_READY;
Florin Coras58d36f02018-03-09 13:05:53 -0800359 tls_session->opaque = ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800360 ctx->tls_session_handle = session_handle (tls_session);
361 ctx->listener_ctx_index = tls_listener->opaque;
362
Florin Coras58d36f02018-03-09 13:05:53 -0800363 TLS_DBG (1, "Accept on listener %u new connection [%u]%x",
364 tls_listener->opaque, vlib_get_thread_index (), ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800365
366 return tls_ctx_init_server (ctx);
367}
368
369int
370tls_app_tx_callback (stream_session_t * app_session)
371{
Florin Coras371ca502018-02-21 12:07:41 -0800372 tls_ctx_t *ctx;
Florin Coras58d36f02018-03-09 13:05:53 -0800373 if (PREDICT_FALSE (app_session->session_state == SESSION_STATE_CLOSED))
Florin Coras371ca502018-02-21 12:07:41 -0800374 return 0;
Florin Coras58d36f02018-03-09 13:05:53 -0800375 ctx = tls_ctx_get (app_session->connection_index);
376 tls_ctx_write (ctx, app_session);
Florin Coras371ca502018-02-21 12:07:41 -0800377 return 0;
378}
379
380int
381tls_app_rx_callback (stream_session_t * tls_session)
382{
Florin Coras371ca502018-02-21 12:07:41 -0800383 tls_ctx_t *ctx;
Florin Coras371ca502018-02-21 12:07:41 -0800384
385 ctx = tls_ctx_get (tls_session->opaque);
Florin Coras58d36f02018-03-09 13:05:53 -0800386 tls_ctx_read (ctx, tls_session);
387 return 0;
Florin Coras371ca502018-02-21 12:07:41 -0800388}
389
390int
391tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index,
392 stream_session_t * tls_session, u8 is_fail)
393{
Florin Coras371ca502018-02-21 12:07:41 -0800394 tls_ctx_t *ho_ctx, *ctx;
Florin Coras58d36f02018-03-09 13:05:53 -0800395 u32 ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800396
397 ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
Florin Coras371ca502018-02-21 12:07:41 -0800398
399 if (is_fail)
Florin Corasdcde9272018-03-02 09:23:42 -0800400 {
Florin Coras57791ad2018-08-27 10:07:40 -0700401 int (*cb_fn) (u32, u32, stream_session_t *, u8);
402 u32 wrk_index, api_context;
403 app_worker_t *app_wrk;
404 application_t *app;
405
406 wrk_index = ho_ctx->parent_app_index;
407 api_context = ho_ctx->c_s_index;
Florin Corasdcde9272018-03-02 09:23:42 -0800408 tls_ctx_half_open_reader_unlock ();
409 tls_ctx_half_open_free (ho_ctx_index);
Florin Coras57791ad2018-08-27 10:07:40 -0700410 app_wrk = app_worker_get (ho_ctx->parent_app_index);
411 app = application_get (app_wrk->app_index);
412 cb_fn = app->cb_fns.session_connected_callback;
413 return cb_fn (wrk_index, api_context, 0, 1 /* failed */ );
Florin Corasdcde9272018-03-02 09:23:42 -0800414 }
Florin Coras371ca502018-02-21 12:07:41 -0800415
Florin Coras58d36f02018-03-09 13:05:53 -0800416 ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine);
417 ctx = tls_ctx_get (ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800418 clib_memcpy (ctx, ho_ctx, sizeof (*ctx));
Florin Coras371ca502018-02-21 12:07:41 -0800419 tls_ctx_half_open_reader_unlock ();
420 tls_ctx_half_open_free (ho_ctx_index);
421
Florin Corasdcde9272018-03-02 09:23:42 -0800422 ctx->c_thread_index = vlib_get_thread_index ();
Florin Coras58d36f02018-03-09 13:05:53 -0800423 ctx->tls_ctx_handle = ctx_handle;
Florin Corasdcde9272018-03-02 09:23:42 -0800424
Florin Coras58d36f02018-03-09 13:05:53 -0800425 TLS_DBG (1, "TCP connect for %u returned %u. New connection [%u]%x",
Florin Coras371ca502018-02-21 12:07:41 -0800426 ho_ctx_index, is_fail, vlib_get_thread_index (),
Florin Coras58d36f02018-03-09 13:05:53 -0800427 (ctx) ? ctx_handle : ~0);
Florin Coras371ca502018-02-21 12:07:41 -0800428
429 ctx->tls_session_handle = session_handle (tls_session);
Florin Coras58d36f02018-03-09 13:05:53 -0800430 tls_session->opaque = ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800431 tls_session->session_state = SESSION_STATE_READY;
432
433 return tls_ctx_init_client (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800434}
435
436/* *INDENT-OFF* */
437static session_cb_vft_t tls_app_cb_vft = {
438 .session_accept_callback = tls_session_accept_callback,
439 .session_disconnect_callback = tls_session_disconnect_callback,
440 .session_connected_callback = tls_session_connected_callback,
441 .session_reset_callback = tls_session_reset_callback,
442 .add_segment_callback = tls_add_segment_callback,
443 .del_segment_callback = tls_del_segment_callback,
444 .builtin_app_rx_callback = tls_app_rx_callback,
445 .builtin_app_tx_callback = tls_app_tx_callback,
446};
447/* *INDENT-ON* */
448
449int
450tls_connect (transport_endpoint_t * tep)
451{
Florin Coras15531972018-08-12 23:50:53 -0700452 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
Florin Coras371ca502018-02-21 12:07:41 -0800453 session_endpoint_extended_t *sep;
Florin Coras58d36f02018-03-09 13:05:53 -0800454 tls_engine_type_t engine_type;
Florin Coras371ca502018-02-21 12:07:41 -0800455 tls_main_t *tm = &tls_main;
Florin Coras15531972018-08-12 23:50:53 -0700456 app_worker_t *app_wrk;
457 clib_error_t *error;
Florin Coras371ca502018-02-21 12:07:41 -0800458 application_t *app;
459 tls_ctx_t *ctx;
460 u32 ctx_index;
Florin Coras371ca502018-02-21 12:07:41 -0800461
462 sep = (session_endpoint_extended_t *) tep;
Florin Coras15531972018-08-12 23:50:53 -0700463 app_wrk = app_worker_get (sep->app_wrk_index);
464 app = application_get (app_wrk->app_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800465 engine_type = tls_get_engine_type (app->tls_engine);
466 if (engine_type == TLS_ENGINE_NONE)
467 {
468 clib_warning ("No tls engine_type available");
469 return -1;
470 }
Florin Coras371ca502018-02-21 12:07:41 -0800471
472 ctx_index = tls_ctx_half_open_alloc ();
473 ctx = tls_ctx_half_open_get (ctx_index);
Florin Coras15531972018-08-12 23:50:53 -0700474 ctx->parent_app_index = sep->app_wrk_index;
Florin Coras371ca502018-02-21 12:07:41 -0800475 ctx->parent_app_api_context = sep->opaque;
476 ctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras8f89dd02018-03-05 16:53:07 -0800477 if (sep->hostname)
478 {
479 ctx->srv_hostname = format (0, "%v", sep->hostname);
480 vec_terminate_c_string (ctx->srv_hostname);
481 }
Florin Coras371ca502018-02-21 12:07:41 -0800482 tls_ctx_half_open_reader_unlock ();
483
Florin Coras15531972018-08-12 23:50:53 -0700484 app_worker_alloc_connects_segment_manager (app_wrk);
Florin Coras58d36f02018-03-09 13:05:53 -0800485 ctx->tls_ctx_engine = engine_type;
Florin Coras371ca502018-02-21 12:07:41 -0800486
Florin Coras15531972018-08-12 23:50:53 -0700487 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_t));
488 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
489 cargs->app_index = tm->app_index;
490 cargs->api_context = ctx_index;
491 if ((error = vnet_connect (cargs)))
492 return clib_error_get_code (error);
Florin Coras371ca502018-02-21 12:07:41 -0800493
Florin Coras58d36f02018-03-09 13:05:53 -0800494 TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
Florin Coras371ca502018-02-21 12:07:41 -0800495 return 0;
496}
497
498void
Florin Coras58d36f02018-03-09 13:05:53 -0800499tls_disconnect (u32 ctx_handle, u32 thread_index)
Florin Coras371ca502018-02-21 12:07:41 -0800500{
501 stream_session_t *tls_session, *app_session;
502 tls_ctx_t *ctx;
503
Florin Coras58d36f02018-03-09 13:05:53 -0800504 TLS_DBG (1, "Disconnecting %x", ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800505
Florin Coras58d36f02018-03-09 13:05:53 -0800506 ctx = tls_ctx_get (ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800507 tls_session = session_get_from_handle (ctx->tls_session_handle);
508 stream_session_disconnect (tls_session);
509
510 app_session = session_get_from_handle_if_valid (ctx->app_session_handle);
511 if (app_session)
512 {
513 segment_manager_dealloc_fifos (app_session->svm_segment_index,
514 app_session->server_rx_fifo,
515 app_session->server_tx_fifo);
516 session_free (app_session);
517 }
518 tls_ctx_free (ctx);
519}
520
521u32
522tls_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
523{
Florin Coras15531972018-08-12 23:50:53 -0700524 app_worker_t *tls_app_wrk, *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800525 tls_main_t *tm = &tls_main;
Florin Coras371ca502018-02-21 12:07:41 -0800526 session_handle_t tls_handle;
527 session_endpoint_extended_t *sep;
528 stream_session_t *tls_listener;
Florin Coras371ca502018-02-21 12:07:41 -0800529 stream_session_t *app_listener;
Florin Coras58d36f02018-03-09 13:05:53 -0800530 tls_engine_type_t engine_type;
Florin Coras15531972018-08-12 23:50:53 -0700531 application_t *app, *tls_app;
532 tls_ctx_t *lctx;
533 u32 lctx_index;
Florin Coras371ca502018-02-21 12:07:41 -0800534
535 sep = (session_endpoint_extended_t *) tep;
Florin Coras15531972018-08-12 23:50:53 -0700536 app_wrk = app_worker_get (sep->app_wrk_index);
537 app = application_get (app_wrk->app_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800538 engine_type = tls_get_engine_type (app->tls_engine);
539 if (engine_type == TLS_ENGINE_NONE)
540 {
541 clib_warning ("No tls engine_type available");
542 return -1;
543 }
544
Florin Coras371ca502018-02-21 12:07:41 -0800545 lctx_index = tls_listener_ctx_alloc ();
Florin Coras371ca502018-02-21 12:07:41 -0800546
Florin Coras15531972018-08-12 23:50:53 -0700547 /* TODO hide this by calling vnet_bind() */
Florin Coras371ca502018-02-21 12:07:41 -0800548 tls_app = application_get (tm->app_index);
Florin Coras15531972018-08-12 23:50:53 -0700549 tls_app_wrk = application_get_default_worker (tls_app);
Florin Coras371ca502018-02-21 12:07:41 -0800550 sep->transport_proto = TRANSPORT_PROTO_TCP;
Florin Coras15531972018-08-12 23:50:53 -0700551 if (app_worker_start_listen (tls_app_wrk, (session_endpoint_t *) sep,
552 &tls_handle))
Florin Coras371ca502018-02-21 12:07:41 -0800553 return ~0;
554
555 tls_listener = listen_session_get_from_handle (tls_handle);
556 tls_listener->opaque = lctx_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800557
Florin Coras5c9083d2018-04-13 06:39:07 -0700558 app_listener = listen_session_get (app_listener_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800559
560 lctx = tls_listener_ctx_get (lctx_index);
Florin Coras15531972018-08-12 23:50:53 -0700561 lctx->parent_app_index = sep->app_wrk_index;
Florin Coras371ca502018-02-21 12:07:41 -0800562 lctx->tls_session_handle = tls_handle;
563 lctx->app_session_handle = listen_session_get_handle (app_listener);
564 lctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras58d36f02018-03-09 13:05:53 -0800565 lctx->tls_ctx_engine = engine_type;
566
Ping Yudecda5b2018-08-13 06:20:00 -0400567 tls_vfts[engine_type].ctx_start_listen (lctx);
568
Florin Coras58d36f02018-03-09 13:05:53 -0800569 TLS_DBG (1, "Started listening %d, engine type %d", lctx_index,
570 engine_type);
Florin Coras371ca502018-02-21 12:07:41 -0800571 return lctx_index;
572}
573
574u32
Florin Corase3e2f072018-03-04 07:24:30 -0800575tls_stop_listen (u32 lctx_index)
Florin Coras371ca502018-02-21 12:07:41 -0800576{
Florin Corase3e2f072018-03-04 07:24:30 -0800577 tls_main_t *tm = &tls_main;
Florin Corase3e2f072018-03-04 07:24:30 -0800578 tls_ctx_t *lctx;
Ping Yudecda5b2018-08-13 06:20:00 -0400579 tls_engine_type_t engine_type;
580
Florin Corase3e2f072018-03-04 07:24:30 -0800581 lctx = tls_listener_ctx_get (lctx_index);
Florin Coras15531972018-08-12 23:50:53 -0700582 app_worker_stop_listen (lctx->tls_session_handle, tm->app_index);
Ping Yudecda5b2018-08-13 06:20:00 -0400583 engine_type = lctx->tls_ctx_engine;
584 tls_vfts[engine_type].ctx_stop_listen (lctx);
585
Florin Corase3e2f072018-03-04 07:24:30 -0800586 tls_listener_ctx_free (lctx);
Florin Coras371ca502018-02-21 12:07:41 -0800587 return 0;
588}
589
590transport_connection_t *
Florin Corase3e2f072018-03-04 07:24:30 -0800591tls_connection_get (u32 ctx_index, u32 thread_index)
592{
593 tls_ctx_t *ctx;
594 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
595 return &ctx->connection;
596}
597
598transport_connection_t *
Florin Coras371ca502018-02-21 12:07:41 -0800599tls_listener_get (u32 listener_index)
600{
601 tls_ctx_t *ctx;
602 ctx = tls_listener_ctx_get (listener_index);
603 return &ctx->connection;
604}
605
606u8 *
607format_tls_ctx (u8 * s, va_list * args)
608{
609 tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *);
610 u32 thread_index = va_arg (*args, u32);
611 u32 child_si, child_ti;
612
613 session_parse_handle (ctx->tls_session_handle, &child_si, &child_ti);
614 if (thread_index != child_ti)
615 clib_warning ("app and tls sessions are on different threads!");
616
Florin Corase3e2f072018-03-04 07:24:30 -0800617 s = format (s, "[#%d][TLS] app %u child %u", child_ti,
618 ctx->parent_app_index, child_si);
Florin Coras371ca502018-02-21 12:07:41 -0800619 return s;
620}
621
622u8 *
623format_tls_connection (u8 * s, va_list * args)
624{
625 u32 ctx_index = va_arg (*args, u32);
626 u32 thread_index = va_arg (*args, u32);
627 u32 verbose = va_arg (*args, u32);
628 tls_ctx_t *ctx;
629
630 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
631 if (!ctx)
632 return s;
633
634 s = format (s, "%-50U", format_tls_ctx, ctx, thread_index);
635 if (verbose)
636 {
637 s = format (s, "%-15s", "state");
638 if (verbose > 1)
639 s = format (s, "\n");
640 }
641 return s;
642}
643
644u8 *
645format_tls_listener (u8 * s, va_list * args)
646{
647 u32 tc_index = va_arg (*args, u32);
648 tls_ctx_t *ctx = tls_listener_ctx_get (tc_index);
Florin Coras5c9083d2018-04-13 06:39:07 -0700649 u32 listener_index, thread_index;
Florin Coras371ca502018-02-21 12:07:41 -0800650
Florin Coras5c9083d2018-04-13 06:39:07 -0700651 listen_session_parse_handle (ctx->tls_session_handle, &listener_index,
652 &thread_index);
Florin Coras371ca502018-02-21 12:07:41 -0800653 return format (s, "[TLS] listener app %u child %u", ctx->parent_app_index,
654 listener_index);
655}
656
657u8 *
658format_tls_half_open (u8 * s, va_list * args)
659{
660 u32 tc_index = va_arg (*args, u32);
661 tls_ctx_t *ctx = tls_ctx_half_open_get (tc_index);
662 s = format (s, "[TLS] half-open app %u", ctx->parent_app_index);
663 tls_ctx_half_open_reader_unlock ();
664 return s;
665}
666
667/* *INDENT-OFF* */
668const static transport_proto_vft_t tls_proto = {
669 .open = tls_connect,
670 .close = tls_disconnect,
671 .bind = tls_start_listen,
Florin Corase3e2f072018-03-04 07:24:30 -0800672 .get_connection = tls_connection_get,
Florin Coras371ca502018-02-21 12:07:41 -0800673 .get_listener = tls_listener_get,
674 .unbind = tls_stop_listen,
675 .tx_type = TRANSPORT_TX_INTERNAL,
676 .service_type = TRANSPORT_SERVICE_APP,
677 .format_connection = format_tls_connection,
678 .format_half_open = format_tls_half_open,
679 .format_listener = format_tls_listener,
680};
681/* *INDENT-ON* */
682
Florin Corasd77eee62018-03-07 08:49:27 -0800683void
684tls_register_engine (const tls_engine_vft_t * vft, tls_engine_type_t type)
Florin Coras371ca502018-02-21 12:07:41 -0800685{
Florin Corasd77eee62018-03-07 08:49:27 -0800686 vec_validate (tls_vfts, type);
687 tls_vfts[type] = *vft;
Florin Coras371ca502018-02-21 12:07:41 -0800688}
689
Dave Barachcc5677b2018-03-28 19:08:45 -0400690static clib_error_t *
Florin Coras371ca502018-02-21 12:07:41 -0800691tls_init (vlib_main_t * vm)
692{
Florin Coras58d36f02018-03-09 13:05:53 -0800693 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -0800694 vnet_app_attach_args_t _a, *a = &_a;
695 u64 options[APP_OPTIONS_N_OPTIONS];
696 u32 segment_size = 512 << 20;
697 tls_main_t *tm = &tls_main;
Florin Corasd77eee62018-03-07 08:49:27 -0800698 u32 fifo_size = 64 << 10;
Florin Coras58d36f02018-03-09 13:05:53 -0800699 u32 num_threads;
700
701 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras371ca502018-02-21 12:07:41 -0800702
703 memset (a, 0, sizeof (*a));
704 memset (options, 0, sizeof (options));
705
706 a->session_cb_vft = &tls_app_cb_vft;
Florin Coras0bee9ce2018-03-22 21:24:31 -0700707 a->api_client_index = APP_INVALID_INDEX;
Florin Coras371ca502018-02-21 12:07:41 -0800708 a->options = options;
Florin Coras0bee9ce2018-03-22 21:24:31 -0700709 a->name = format (0, "tls");
Florin Coras371ca502018-02-21 12:07:41 -0800710 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
711 a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
712 a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
713 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
714 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
715
716 if (vnet_application_attach (a))
717 {
718 clib_warning ("failed to attach tls app");
719 return clib_error_return (0, "failed to attach tls app");
720 }
721
Florin Corasd77eee62018-03-07 08:49:27 -0800722 if (!tm->ca_cert_path)
723 tm->ca_cert_path = TLS_CA_CERT_PATH;
724
Florin Coras371ca502018-02-21 12:07:41 -0800725 tm->app_index = a->app_index;
Florin Coras371ca502018-02-21 12:07:41 -0800726 clib_rwlock_init (&tm->half_open_rwlock);
727
Florin Coras58d36f02018-03-09 13:05:53 -0800728 vec_validate (tm->rx_bufs, num_threads - 1);
729 vec_validate (tm->tx_bufs, num_threads - 1);
730
Florin Coras371ca502018-02-21 12:07:41 -0800731 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
732 FIB_PROTOCOL_IP4, ~0);
733 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
734 FIB_PROTOCOL_IP6, ~0);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700735 vec_free (a->name);
Florin Coras371ca502018-02-21 12:07:41 -0800736 return 0;
737}
738
739VLIB_INIT_FUNCTION (tls_init);
740
Florin Coras8f89dd02018-03-05 16:53:07 -0800741static clib_error_t *
742tls_config_fn (vlib_main_t * vm, unformat_input_t * input)
743{
744 tls_main_t *tm = &tls_main;
745 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
746 {
747 if (unformat (input, "use-test-cert-in-ca"))
748 tm->use_test_cert_in_ca = 1;
749 else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path))
750 ;
751 else
752 return clib_error_return (0, "unknown input `%U'",
753 format_unformat_error, input);
754 }
755 return 0;
756}
757
758VLIB_EARLY_CONFIG_FUNCTION (tls_config_fn, "tls");
Florin Corasd77eee62018-03-07 08:49:27 -0800759
760tls_main_t *
761vnet_tls_get_main (void)
762{
763 return &tls_main;
764}
765
Florin Coras371ca502018-02-21 12:07:41 -0800766/*
767 * fd.io coding-style-patch-verification: ON
768 *
769 * Local Variables:
770 * eval: (c-set-style "gnu")
771 * End:
772 */