blob: d5dbf2e16d1ad237eabe7633b911129f9fa7213c [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 Coras21795132018-09-09 09:40:51 -070052 return app_worker_lock_and_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);
Florin Coras371ca502018-02-21 12:07:41 -080097 clib_rwlock_writer_unlock (&tm->half_open_rwlock);
98 }
99 else
100 {
Ping Yue6446a32018-08-28 18:56:27 -0400101 /* reader lock assumption: only main thread will call pool_get */
102 clib_rwlock_reader_lock (&tm->half_open_rwlock);
Florin Coras371ca502018-02-21 12:07:41 -0800103 pool_get (tm->half_open_ctx_pool, ctx);
Ping Yue6446a32018-08-28 18:56:27 -0400104 clib_rwlock_reader_unlock (&tm->half_open_rwlock);
Florin Coras371ca502018-02-21 12:07:41 -0800105 }
Ping Yue6446a32018-08-28 18:56:27 -0400106 memset (ctx, 0, sizeof (*ctx));
107 ctx_index = ctx - tm->half_open_ctx_pool;
Florin Coras371ca502018-02-21 12:07:41 -0800108 return ctx_index;
109}
110
111void
112tls_ctx_half_open_free (u32 ho_index)
113{
114 tls_main_t *tm = &tls_main;
115 clib_rwlock_writer_lock (&tm->half_open_rwlock);
116 pool_put_index (tls_main.half_open_ctx_pool, ho_index);
117 clib_rwlock_writer_unlock (&tm->half_open_rwlock);
118}
119
120tls_ctx_t *
121tls_ctx_half_open_get (u32 ctx_index)
122{
123 tls_main_t *tm = &tls_main;
124 clib_rwlock_reader_lock (&tm->half_open_rwlock);
125 return pool_elt_at_index (tm->half_open_ctx_pool, ctx_index);
126}
127
128void
129tls_ctx_half_open_reader_unlock ()
130{
131 clib_rwlock_reader_unlock (&tls_main.half_open_rwlock);
132}
133
134u32
135tls_ctx_half_open_index (tls_ctx_t * ctx)
136{
137 return (ctx - tls_main.half_open_ctx_pool);
138}
139
Florin Coras58d36f02018-03-09 13:05:53 -0800140void
141tls_notify_app_enqueue (tls_ctx_t * ctx, stream_session_t * app_session)
142{
Florin Coras15531972018-08-12 23:50:53 -0700143 app_worker_t *app;
144 app = app_worker_get_if_valid (app_session->app_wrk_index);
Florin Coras5090c572018-03-18 08:22:17 -0700145 if (PREDICT_TRUE (app != 0))
146 tls_add_app_q_evt (app, app_session);
Florin Coras58d36f02018-03-09 13:05:53 -0800147}
148
149int
Florin Coras371ca502018-02-21 12:07:41 -0800150tls_notify_app_accept (tls_ctx_t * ctx)
151{
152 stream_session_t *app_listener, *app_session;
153 segment_manager_t *sm;
Florin Coras15531972018-08-12 23:50:53 -0700154 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800155 application_t *app;
156 tls_ctx_t *lctx;
157 int rv;
158
Florin Coras15531972018-08-12 23:50:53 -0700159 app_wrk = app_worker_get (ctx->parent_app_index);
160 app = application_get (app_wrk->app_index);
Florin Coras371ca502018-02-21 12:07:41 -0800161 lctx = tls_listener_ctx_get (ctx->listener_ctx_index);
Florin Coras371ca502018-02-21 12:07:41 -0800162
163 app_session = session_alloc (vlib_get_thread_index ());
Florin Coras15531972018-08-12 23:50:53 -0700164 app_session->app_wrk_index = ctx->parent_app_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800165 app_session->connection_index = ctx->tls_ctx_handle;
Ping Yua0c29a92018-08-16 19:11:05 -0400166
167 app_listener = listen_session_get_from_handle (lctx->app_session_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800168 app_session->session_type = app_listener->session_type;
169 app_session->listener_index = app_listener->session_index;
Florin Coras15531972018-08-12 23:50:53 -0700170 sm = app_worker_get_listen_segment_manager (app_wrk, app_listener);
Florin Corasab2f6db2018-08-31 14:31:41 -0700171 app_session->t_app_index = tls_main.app_index;
Florin Coras15531972018-08-12 23:50:53 -0700172
Florin Coras371ca502018-02-21 12:07:41 -0800173 if ((rv = session_alloc_fifos (sm, app_session)))
174 {
175 TLS_DBG (1, "failed to allocate fifos");
176 return rv;
177 }
178 ctx->c_s_index = app_session->session_index;
Florin Coras371ca502018-02-21 12:07:41 -0800179 ctx->app_session_handle = session_handle (app_session);
Florin Corasb5e94e32018-09-14 14:46:39 -0700180 session_lookup_add_connection (&ctx->connection,
181 session_handle (app_session));
Florin Coras371ca502018-02-21 12:07:41 -0800182 return app->cb_fns.session_accept_callback (app_session);
183}
184
Florin Coras58d36f02018-03-09 13:05:53 -0800185int
Florin Coras8f89dd02018-03-05 16:53:07 -0800186tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed)
Florin Coras371ca502018-02-21 12:07:41 -0800187{
188 int (*cb_fn) (u32, u32, stream_session_t *, u8);
189 stream_session_t *app_session;
190 segment_manager_t *sm;
Florin Coras15531972018-08-12 23:50:53 -0700191 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800192 application_t *app;
193
Florin Coras15531972018-08-12 23:50:53 -0700194 app_wrk = app_worker_get (ctx->parent_app_index);
195 app = application_get (app_wrk->app_index);
Florin Coras371ca502018-02-21 12:07:41 -0800196 cb_fn = app->cb_fns.session_connected_callback;
197
Florin Coras8f89dd02018-03-05 16:53:07 -0800198 if (is_failed)
199 goto failed;
200
Florin Coras15531972018-08-12 23:50:53 -0700201 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Coras371ca502018-02-21 12:07:41 -0800202 app_session = session_alloc (vlib_get_thread_index ());
Florin Coras15531972018-08-12 23:50:53 -0700203 app_session->app_wrk_index = ctx->parent_app_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800204 app_session->connection_index = ctx->tls_ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800205 app_session->session_type =
206 session_type_from_proto_and_ip (TRANSPORT_PROTO_TLS, ctx->tcp_is_ip4);
Florin Corasab2f6db2018-08-31 14:31:41 -0700207 app_session->t_app_index = tls_main.app_index;
Florin Coras15531972018-08-12 23:50:53 -0700208
Florin Coras371ca502018-02-21 12:07:41 -0800209 if (session_alloc_fifos (sm, app_session))
210 goto failed;
211
212 ctx->app_session_handle = session_handle (app_session);
213 ctx->c_s_index = app_session->session_index;
Florin Coras371ca502018-02-21 12:07:41 -0800214 app_session->session_state = SESSION_STATE_READY;
215 if (cb_fn (ctx->parent_app_index, ctx->parent_app_api_context,
216 app_session, 0 /* not failed */ ))
217 {
218 TLS_DBG (1, "failed to notify app");
Florin Coras58d36f02018-03-09 13:05:53 -0800219 tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
Florin Coras371ca502018-02-21 12:07:41 -0800220 }
221
Florin Corasb5e94e32018-09-14 14:46:39 -0700222 session_lookup_add_connection (&ctx->connection,
223 session_handle (app_session));
224
Florin Coras371ca502018-02-21 12:07:41 -0800225 return 0;
226
227failed:
Florin Coras58d36f02018-03-09 13:05:53 -0800228 tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
Florin Coras371ca502018-02-21 12:07:41 -0800229 return cb_fn (ctx->parent_app_index, ctx->parent_app_api_context, 0,
230 1 /* failed */ );
231}
232
Florin Coras58d36f02018-03-09 13:05:53 -0800233static inline void
234tls_ctx_parse_handle (u32 ctx_handle, u32 * ctx_index, u32 * engine_type)
Florin Corasd77eee62018-03-07 08:49:27 -0800235{
Florin Coras58d36f02018-03-09 13:05:53 -0800236 *ctx_index = ctx_handle & TLS_IDX_MASK;
237 *engine_type = ctx_handle >> TLS_ENGINE_TYPE_SHIFT;
238}
239
240static inline tls_engine_type_t
241tls_get_engine_type (tls_engine_type_t preferred)
242{
243 if (!tls_vfts[preferred].ctx_alloc)
244 return tls_get_available_engine ();
245 return preferred;
246}
247
248static inline u32
249tls_ctx_alloc (tls_engine_type_t engine_type)
250{
251 u32 ctx_index;
252 ctx_index = tls_vfts[engine_type].ctx_alloc ();
253 return (((u32) engine_type << TLS_ENGINE_TYPE_SHIFT) | ctx_index);
Florin Corasd77eee62018-03-07 08:49:27 -0800254}
255
256static inline void
257tls_ctx_free (tls_ctx_t * ctx)
258{
259 vec_free (ctx->srv_hostname);
Florin Coras58d36f02018-03-09 13:05:53 -0800260 tls_vfts[ctx->tls_ctx_engine].ctx_free (ctx);
Florin Corasd77eee62018-03-07 08:49:27 -0800261}
262
263static inline tls_ctx_t *
Florin Coras58d36f02018-03-09 13:05:53 -0800264tls_ctx_get (u32 ctx_handle)
Florin Corasd77eee62018-03-07 08:49:27 -0800265{
Florin Coras58d36f02018-03-09 13:05:53 -0800266 u32 ctx_index, engine_type;
267 tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type);
268 return tls_vfts[engine_type].ctx_get (ctx_index);
Florin Corasd77eee62018-03-07 08:49:27 -0800269}
270
271static inline tls_ctx_t *
Florin Coras58d36f02018-03-09 13:05:53 -0800272tls_ctx_get_w_thread (u32 ctx_handle, u8 thread_index)
Florin Corasd77eee62018-03-07 08:49:27 -0800273{
Florin Coras58d36f02018-03-09 13:05:53 -0800274 u32 ctx_index, engine_type;
275 tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type);
276 return tls_vfts[engine_type].ctx_get_w_thread (ctx_index, thread_index);
Florin Corasd77eee62018-03-07 08:49:27 -0800277}
278
279static inline int
280tls_ctx_init_server (tls_ctx_t * ctx)
281{
Florin Coras58d36f02018-03-09 13:05:53 -0800282 return tls_vfts[ctx->tls_ctx_engine].ctx_init_server (ctx);
Florin Corasd77eee62018-03-07 08:49:27 -0800283}
284
285static inline int
286tls_ctx_init_client (tls_ctx_t * ctx)
287{
Florin Coras58d36f02018-03-09 13:05:53 -0800288 return tls_vfts[ctx->tls_ctx_engine].ctx_init_client (ctx);
Florin Corasd77eee62018-03-07 08:49:27 -0800289}
290
291static inline int
Florin Coras58d36f02018-03-09 13:05:53 -0800292tls_ctx_write (tls_ctx_t * ctx, stream_session_t * app_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_write (ctx, app_session);
Florin Corasd77eee62018-03-07 08:49:27 -0800295}
296
297static inline int
Florin Coras58d36f02018-03-09 13:05:53 -0800298tls_ctx_read (tls_ctx_t * ctx, stream_session_t * tls_session)
Florin Corasd77eee62018-03-07 08:49:27 -0800299{
Florin Coras58d36f02018-03-09 13:05:53 -0800300 return tls_vfts[ctx->tls_ctx_engine].ctx_read (ctx, tls_session);
Florin Corasd77eee62018-03-07 08:49:27 -0800301}
302
303static inline u8
304tls_ctx_handshake_is_over (tls_ctx_t * ctx)
305{
Florin Coras58d36f02018-03-09 13:05:53 -0800306 return tls_vfts[ctx->tls_ctx_engine].ctx_handshake_is_over (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800307}
308
309void
310tls_session_reset_callback (stream_session_t * s)
311{
312 clib_warning ("called...");
313}
314
315int
316tls_add_segment_callback (u32 client_index, const ssvm_private_t * fs)
317{
318 /* No-op for builtin */
319 return 0;
320}
321
322int
323tls_del_segment_callback (u32 client_index, const ssvm_private_t * fs)
324{
325 return 0;
326}
327
328void
329tls_session_disconnect_callback (stream_session_t * tls_session)
330{
331 stream_session_t *app_session;
332 tls_ctx_t *ctx;
Florin Coras15531972018-08-12 23:50:53 -0700333 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800334 application_t *app;
335
336 ctx = tls_ctx_get (tls_session->opaque);
Florin Corasd77eee62018-03-07 08:49:27 -0800337 if (!tls_ctx_handshake_is_over (ctx))
Florin Coras371ca502018-02-21 12:07:41 -0800338 {
339 stream_session_disconnect (tls_session);
340 return;
341 }
342 ctx->is_passive_close = 1;
Florin Coras15531972018-08-12 23:50:53 -0700343 app_wrk = app_worker_get (ctx->parent_app_index);
344 app = application_get (app_wrk->app_index);
Florin Coras371ca502018-02-21 12:07:41 -0800345 app_session = session_get_from_handle (ctx->app_session_handle);
346 app->cb_fns.session_disconnect_callback (app_session);
347}
348
349int
350tls_session_accept_callback (stream_session_t * tls_session)
351{
352 stream_session_t *tls_listener;
353 tls_ctx_t *lctx, *ctx;
Florin Coras58d36f02018-03-09 13:05:53 -0800354 u32 ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800355
Florin Coras5c9083d2018-04-13 06:39:07 -0700356 tls_listener = listen_session_get (tls_session->listener_index);
Florin Coras371ca502018-02-21 12:07:41 -0800357 lctx = tls_listener_ctx_get (tls_listener->opaque);
Florin Coras58d36f02018-03-09 13:05:53 -0800358
359 ctx_handle = tls_ctx_alloc (lctx->tls_ctx_engine);
360 ctx = tls_ctx_get (ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800361 memcpy (ctx, lctx, sizeof (*lctx));
362 ctx->c_thread_index = vlib_get_thread_index ();
Florin Coras58d36f02018-03-09 13:05:53 -0800363 ctx->tls_ctx_handle = ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800364 tls_session->session_state = SESSION_STATE_READY;
Florin Coras58d36f02018-03-09 13:05:53 -0800365 tls_session->opaque = ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800366 ctx->tls_session_handle = session_handle (tls_session);
367 ctx->listener_ctx_index = tls_listener->opaque;
368
Florin Coras58d36f02018-03-09 13:05:53 -0800369 TLS_DBG (1, "Accept on listener %u new connection [%u]%x",
370 tls_listener->opaque, vlib_get_thread_index (), ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800371
372 return tls_ctx_init_server (ctx);
373}
374
375int
376tls_app_tx_callback (stream_session_t * app_session)
377{
Florin Coras371ca502018-02-21 12:07:41 -0800378 tls_ctx_t *ctx;
Florin Coras58d36f02018-03-09 13:05:53 -0800379 if (PREDICT_FALSE (app_session->session_state == SESSION_STATE_CLOSED))
Florin Coras371ca502018-02-21 12:07:41 -0800380 return 0;
Florin Coras58d36f02018-03-09 13:05:53 -0800381 ctx = tls_ctx_get (app_session->connection_index);
382 tls_ctx_write (ctx, app_session);
Florin Coras371ca502018-02-21 12:07:41 -0800383 return 0;
384}
385
386int
387tls_app_rx_callback (stream_session_t * tls_session)
388{
Florin Coras371ca502018-02-21 12:07:41 -0800389 tls_ctx_t *ctx;
Florin Coras371ca502018-02-21 12:07:41 -0800390
391 ctx = tls_ctx_get (tls_session->opaque);
Florin Coras58d36f02018-03-09 13:05:53 -0800392 tls_ctx_read (ctx, tls_session);
393 return 0;
Florin Coras371ca502018-02-21 12:07:41 -0800394}
395
396int
397tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index,
398 stream_session_t * tls_session, u8 is_fail)
399{
Florin Coras371ca502018-02-21 12:07:41 -0800400 tls_ctx_t *ho_ctx, *ctx;
Florin Coras58d36f02018-03-09 13:05:53 -0800401 u32 ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800402
403 ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
Florin Coras371ca502018-02-21 12:07:41 -0800404
405 if (is_fail)
Florin Corasdcde9272018-03-02 09:23:42 -0800406 {
Florin Coras57791ad2018-08-27 10:07:40 -0700407 int (*cb_fn) (u32, u32, stream_session_t *, u8);
408 u32 wrk_index, api_context;
409 app_worker_t *app_wrk;
410 application_t *app;
411
412 wrk_index = ho_ctx->parent_app_index;
413 api_context = ho_ctx->c_s_index;
Florin Corasdcde9272018-03-02 09:23:42 -0800414 tls_ctx_half_open_reader_unlock ();
415 tls_ctx_half_open_free (ho_ctx_index);
Florin Coras57791ad2018-08-27 10:07:40 -0700416 app_wrk = app_worker_get (ho_ctx->parent_app_index);
417 app = application_get (app_wrk->app_index);
418 cb_fn = app->cb_fns.session_connected_callback;
419 return cb_fn (wrk_index, api_context, 0, 1 /* failed */ );
Florin Corasdcde9272018-03-02 09:23:42 -0800420 }
Florin Coras371ca502018-02-21 12:07:41 -0800421
Florin Coras58d36f02018-03-09 13:05:53 -0800422 ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine);
423 ctx = tls_ctx_get (ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800424 clib_memcpy (ctx, ho_ctx, sizeof (*ctx));
Florin Coras371ca502018-02-21 12:07:41 -0800425 tls_ctx_half_open_reader_unlock ();
426 tls_ctx_half_open_free (ho_ctx_index);
427
Florin Corasdcde9272018-03-02 09:23:42 -0800428 ctx->c_thread_index = vlib_get_thread_index ();
Florin Coras58d36f02018-03-09 13:05:53 -0800429 ctx->tls_ctx_handle = ctx_handle;
Florin Corasdcde9272018-03-02 09:23:42 -0800430
Florin Coras58d36f02018-03-09 13:05:53 -0800431 TLS_DBG (1, "TCP connect for %u returned %u. New connection [%u]%x",
Florin Coras371ca502018-02-21 12:07:41 -0800432 ho_ctx_index, is_fail, vlib_get_thread_index (),
Florin Coras58d36f02018-03-09 13:05:53 -0800433 (ctx) ? ctx_handle : ~0);
Florin Coras371ca502018-02-21 12:07:41 -0800434
435 ctx->tls_session_handle = session_handle (tls_session);
Florin Coras58d36f02018-03-09 13:05:53 -0800436 tls_session->opaque = ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800437 tls_session->session_state = SESSION_STATE_READY;
438
439 return tls_ctx_init_client (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800440}
441
442/* *INDENT-OFF* */
443static session_cb_vft_t tls_app_cb_vft = {
444 .session_accept_callback = tls_session_accept_callback,
445 .session_disconnect_callback = tls_session_disconnect_callback,
446 .session_connected_callback = tls_session_connected_callback,
447 .session_reset_callback = tls_session_reset_callback,
448 .add_segment_callback = tls_add_segment_callback,
449 .del_segment_callback = tls_del_segment_callback,
450 .builtin_app_rx_callback = tls_app_rx_callback,
451 .builtin_app_tx_callback = tls_app_tx_callback,
452};
453/* *INDENT-ON* */
454
455int
456tls_connect (transport_endpoint_t * tep)
457{
Florin Coras15531972018-08-12 23:50:53 -0700458 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
Florin Coras371ca502018-02-21 12:07:41 -0800459 session_endpoint_extended_t *sep;
Florin Coras58d36f02018-03-09 13:05:53 -0800460 tls_engine_type_t engine_type;
Florin Coras371ca502018-02-21 12:07:41 -0800461 tls_main_t *tm = &tls_main;
Florin Coras15531972018-08-12 23:50:53 -0700462 app_worker_t *app_wrk;
463 clib_error_t *error;
Florin Coras371ca502018-02-21 12:07:41 -0800464 application_t *app;
465 tls_ctx_t *ctx;
466 u32 ctx_index;
Florin Coras371ca502018-02-21 12:07:41 -0800467
468 sep = (session_endpoint_extended_t *) tep;
Florin Coras15531972018-08-12 23:50:53 -0700469 app_wrk = app_worker_get (sep->app_wrk_index);
470 app = application_get (app_wrk->app_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800471 engine_type = tls_get_engine_type (app->tls_engine);
472 if (engine_type == TLS_ENGINE_NONE)
473 {
474 clib_warning ("No tls engine_type available");
475 return -1;
476 }
Florin Coras371ca502018-02-21 12:07:41 -0800477
478 ctx_index = tls_ctx_half_open_alloc ();
479 ctx = tls_ctx_half_open_get (ctx_index);
Florin Coras15531972018-08-12 23:50:53 -0700480 ctx->parent_app_index = sep->app_wrk_index;
Florin Coras371ca502018-02-21 12:07:41 -0800481 ctx->parent_app_api_context = sep->opaque;
482 ctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras8f89dd02018-03-05 16:53:07 -0800483 if (sep->hostname)
484 {
485 ctx->srv_hostname = format (0, "%v", sep->hostname);
486 vec_terminate_c_string (ctx->srv_hostname);
487 }
Florin Coras371ca502018-02-21 12:07:41 -0800488 tls_ctx_half_open_reader_unlock ();
489
Florin Coras15531972018-08-12 23:50:53 -0700490 app_worker_alloc_connects_segment_manager (app_wrk);
Florin Coras58d36f02018-03-09 13:05:53 -0800491 ctx->tls_ctx_engine = engine_type;
Florin Coras371ca502018-02-21 12:07:41 -0800492
Florin Coras15531972018-08-12 23:50:53 -0700493 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_t));
494 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
495 cargs->app_index = tm->app_index;
496 cargs->api_context = ctx_index;
497 if ((error = vnet_connect (cargs)))
498 return clib_error_get_code (error);
Florin Coras371ca502018-02-21 12:07:41 -0800499
Florin Coras58d36f02018-03-09 13:05:53 -0800500 TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
Florin Coras371ca502018-02-21 12:07:41 -0800501 return 0;
502}
503
504void
Florin Coras58d36f02018-03-09 13:05:53 -0800505tls_disconnect (u32 ctx_handle, u32 thread_index)
Florin Coras371ca502018-02-21 12:07:41 -0800506{
Florin Coras371ca502018-02-21 12:07:41 -0800507 tls_ctx_t *ctx;
508
Florin Coras58d36f02018-03-09 13:05:53 -0800509 TLS_DBG (1, "Disconnecting %x", ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800510
Florin Coras58d36f02018-03-09 13:05:53 -0800511 ctx = tls_ctx_get (ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800512
Florin Corasb5e94e32018-09-14 14:46:39 -0700513 vnet_disconnect_args_t a = {
514 .handle = ctx->tls_session_handle,
515 .app_index = tls_main.app_index,
516 };
517
518 if (vnet_disconnect_session (&a))
519 clib_warning ("disconnect returned");
520
521 stream_session_delete_notify (&ctx->connection);
Florin Coras371ca502018-02-21 12:07:41 -0800522 tls_ctx_free (ctx);
523}
524
525u32
526tls_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
527{
Florin Coras74cac882018-09-07 09:13:15 -0700528 vnet_bind_args_t _bargs, *args = &_bargs;
Florin Corasab2f6db2018-08-31 14:31:41 -0700529 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800530 tls_main_t *tm = &tls_main;
Florin Coras371ca502018-02-21 12:07:41 -0800531 session_handle_t tls_handle;
532 session_endpoint_extended_t *sep;
533 stream_session_t *tls_listener;
Florin Coras371ca502018-02-21 12:07:41 -0800534 stream_session_t *app_listener;
Florin Coras58d36f02018-03-09 13:05:53 -0800535 tls_engine_type_t engine_type;
Florin Coras74cac882018-09-07 09:13:15 -0700536 application_t *app;
Florin Coras15531972018-08-12 23:50:53 -0700537 tls_ctx_t *lctx;
538 u32 lctx_index;
Florin Coras371ca502018-02-21 12:07:41 -0800539
540 sep = (session_endpoint_extended_t *) tep;
Florin Coras15531972018-08-12 23:50:53 -0700541 app_wrk = app_worker_get (sep->app_wrk_index);
542 app = application_get (app_wrk->app_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800543 engine_type = tls_get_engine_type (app->tls_engine);
544 if (engine_type == TLS_ENGINE_NONE)
545 {
546 clib_warning ("No tls engine_type available");
547 return -1;
548 }
549
Florin Coras371ca502018-02-21 12:07:41 -0800550 sep->transport_proto = TRANSPORT_PROTO_TCP;
Florin Coras74cac882018-09-07 09:13:15 -0700551 memset (args, 0, sizeof (*args));
552 args->app_index = tm->app_index;
553 args->sep_ext = *sep;
554 if (vnet_bind (args))
555 return -1;
Florin Coras371ca502018-02-21 12:07:41 -0800556
Florin Coras74cac882018-09-07 09:13:15 -0700557 tls_handle = args->handle;
558 lctx_index = tls_listener_ctx_alloc ();
Florin Coras371ca502018-02-21 12:07:41 -0800559 tls_listener = listen_session_get_from_handle (tls_handle);
560 tls_listener->opaque = lctx_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800561
Florin Coras5c9083d2018-04-13 06:39:07 -0700562 app_listener = listen_session_get (app_listener_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800563
564 lctx = tls_listener_ctx_get (lctx_index);
Florin Coras15531972018-08-12 23:50:53 -0700565 lctx->parent_app_index = sep->app_wrk_index;
Florin Coras371ca502018-02-21 12:07:41 -0800566 lctx->tls_session_handle = tls_handle;
567 lctx->app_session_handle = listen_session_get_handle (app_listener);
568 lctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras58d36f02018-03-09 13:05:53 -0800569 lctx->tls_ctx_engine = engine_type;
570
Ping Yudecda5b2018-08-13 06:20:00 -0400571 tls_vfts[engine_type].ctx_start_listen (lctx);
572
Florin Coras58d36f02018-03-09 13:05:53 -0800573 TLS_DBG (1, "Started listening %d, engine type %d", lctx_index,
574 engine_type);
Florin Coras371ca502018-02-21 12:07:41 -0800575 return lctx_index;
576}
577
578u32
Florin Corase3e2f072018-03-04 07:24:30 -0800579tls_stop_listen (u32 lctx_index)
Florin Coras371ca502018-02-21 12:07:41 -0800580{
Ping Yudecda5b2018-08-13 06:20:00 -0400581 tls_engine_type_t engine_type;
Florin Corasb5e94e32018-09-14 14:46:39 -0700582 tls_ctx_t *lctx;
Ping Yudecda5b2018-08-13 06:20:00 -0400583
Florin Corase3e2f072018-03-04 07:24:30 -0800584 lctx = tls_listener_ctx_get (lctx_index);
Florin Corasb5e94e32018-09-14 14:46:39 -0700585 vnet_unbind_args_t a = {
586 .handle = lctx->tls_session_handle,
587 .app_index = tls_main.app_index,
588 .wrk_map_index = 0 /* default wrk */
589 };
590 if (vnet_unbind (&a))
591 clib_warning ("unbind returned");
592
Ping Yudecda5b2018-08-13 06:20:00 -0400593 engine_type = lctx->tls_ctx_engine;
594 tls_vfts[engine_type].ctx_stop_listen (lctx);
595
Florin Corase3e2f072018-03-04 07:24:30 -0800596 tls_listener_ctx_free (lctx);
Florin Coras371ca502018-02-21 12:07:41 -0800597 return 0;
598}
599
600transport_connection_t *
Florin Corase3e2f072018-03-04 07:24:30 -0800601tls_connection_get (u32 ctx_index, u32 thread_index)
602{
603 tls_ctx_t *ctx;
604 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
605 return &ctx->connection;
606}
607
608transport_connection_t *
Florin Coras371ca502018-02-21 12:07:41 -0800609tls_listener_get (u32 listener_index)
610{
611 tls_ctx_t *ctx;
612 ctx = tls_listener_ctx_get (listener_index);
613 return &ctx->connection;
614}
615
616u8 *
617format_tls_ctx (u8 * s, va_list * args)
618{
619 tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *);
620 u32 thread_index = va_arg (*args, u32);
621 u32 child_si, child_ti;
622
623 session_parse_handle (ctx->tls_session_handle, &child_si, &child_ti);
624 if (thread_index != child_ti)
625 clib_warning ("app and tls sessions are on different threads!");
626
Florin Corase3e2f072018-03-04 07:24:30 -0800627 s = format (s, "[#%d][TLS] app %u child %u", child_ti,
628 ctx->parent_app_index, child_si);
Florin Coras371ca502018-02-21 12:07:41 -0800629 return s;
630}
631
632u8 *
633format_tls_connection (u8 * s, va_list * args)
634{
635 u32 ctx_index = va_arg (*args, u32);
636 u32 thread_index = va_arg (*args, u32);
637 u32 verbose = va_arg (*args, u32);
638 tls_ctx_t *ctx;
639
640 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
641 if (!ctx)
642 return s;
643
644 s = format (s, "%-50U", format_tls_ctx, ctx, thread_index);
645 if (verbose)
646 {
647 s = format (s, "%-15s", "state");
648 if (verbose > 1)
649 s = format (s, "\n");
650 }
651 return s;
652}
653
654u8 *
655format_tls_listener (u8 * s, va_list * args)
656{
657 u32 tc_index = va_arg (*args, u32);
658 tls_ctx_t *ctx = tls_listener_ctx_get (tc_index);
Florin Coras5c9083d2018-04-13 06:39:07 -0700659 u32 listener_index, thread_index;
Florin Coras371ca502018-02-21 12:07:41 -0800660
Florin Coras5c9083d2018-04-13 06:39:07 -0700661 listen_session_parse_handle (ctx->tls_session_handle, &listener_index,
662 &thread_index);
Florin Coras371ca502018-02-21 12:07:41 -0800663 return format (s, "[TLS] listener app %u child %u", ctx->parent_app_index,
664 listener_index);
665}
666
667u8 *
668format_tls_half_open (u8 * s, va_list * args)
669{
670 u32 tc_index = va_arg (*args, u32);
671 tls_ctx_t *ctx = tls_ctx_half_open_get (tc_index);
672 s = format (s, "[TLS] half-open app %u", ctx->parent_app_index);
673 tls_ctx_half_open_reader_unlock ();
674 return s;
675}
676
677/* *INDENT-OFF* */
678const static transport_proto_vft_t tls_proto = {
679 .open = tls_connect,
680 .close = tls_disconnect,
681 .bind = tls_start_listen,
Florin Corase3e2f072018-03-04 07:24:30 -0800682 .get_connection = tls_connection_get,
Florin Coras371ca502018-02-21 12:07:41 -0800683 .get_listener = tls_listener_get,
684 .unbind = tls_stop_listen,
685 .tx_type = TRANSPORT_TX_INTERNAL,
686 .service_type = TRANSPORT_SERVICE_APP,
687 .format_connection = format_tls_connection,
688 .format_half_open = format_tls_half_open,
689 .format_listener = format_tls_listener,
690};
691/* *INDENT-ON* */
692
Florin Corasd77eee62018-03-07 08:49:27 -0800693void
694tls_register_engine (const tls_engine_vft_t * vft, tls_engine_type_t type)
Florin Coras371ca502018-02-21 12:07:41 -0800695{
Florin Corasd77eee62018-03-07 08:49:27 -0800696 vec_validate (tls_vfts, type);
697 tls_vfts[type] = *vft;
Florin Coras371ca502018-02-21 12:07:41 -0800698}
699
Dave Barachcc5677b2018-03-28 19:08:45 -0400700static clib_error_t *
Florin Coras371ca502018-02-21 12:07:41 -0800701tls_init (vlib_main_t * vm)
702{
Florin Coras58d36f02018-03-09 13:05:53 -0800703 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -0800704 vnet_app_attach_args_t _a, *a = &_a;
705 u64 options[APP_OPTIONS_N_OPTIONS];
706 u32 segment_size = 512 << 20;
707 tls_main_t *tm = &tls_main;
Florin Corasd77eee62018-03-07 08:49:27 -0800708 u32 fifo_size = 64 << 10;
Florin Coras58d36f02018-03-09 13:05:53 -0800709 u32 num_threads;
710
711 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras371ca502018-02-21 12:07:41 -0800712
713 memset (a, 0, sizeof (*a));
714 memset (options, 0, sizeof (options));
715
716 a->session_cb_vft = &tls_app_cb_vft;
Florin Coras0bee9ce2018-03-22 21:24:31 -0700717 a->api_client_index = APP_INVALID_INDEX;
Florin Coras371ca502018-02-21 12:07:41 -0800718 a->options = options;
Florin Coras0bee9ce2018-03-22 21:24:31 -0700719 a->name = format (0, "tls");
Florin Coras371ca502018-02-21 12:07:41 -0800720 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
721 a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
722 a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
723 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
724 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
Florin Corasda3eec12018-09-07 13:29:17 -0700725 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
Florin Coras371ca502018-02-21 12:07:41 -0800726
727 if (vnet_application_attach (a))
728 {
729 clib_warning ("failed to attach tls app");
730 return clib_error_return (0, "failed to attach tls app");
731 }
732
Florin Corasd77eee62018-03-07 08:49:27 -0800733 if (!tm->ca_cert_path)
734 tm->ca_cert_path = TLS_CA_CERT_PATH;
735
Florin Coras371ca502018-02-21 12:07:41 -0800736 tm->app_index = a->app_index;
Florin Coras371ca502018-02-21 12:07:41 -0800737 clib_rwlock_init (&tm->half_open_rwlock);
738
Florin Coras58d36f02018-03-09 13:05:53 -0800739 vec_validate (tm->rx_bufs, num_threads - 1);
740 vec_validate (tm->tx_bufs, num_threads - 1);
741
Florin Coras371ca502018-02-21 12:07:41 -0800742 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
743 FIB_PROTOCOL_IP4, ~0);
744 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
745 FIB_PROTOCOL_IP6, ~0);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700746 vec_free (a->name);
Florin Coras371ca502018-02-21 12:07:41 -0800747 return 0;
748}
749
750VLIB_INIT_FUNCTION (tls_init);
751
Florin Coras8f89dd02018-03-05 16:53:07 -0800752static clib_error_t *
753tls_config_fn (vlib_main_t * vm, unformat_input_t * input)
754{
755 tls_main_t *tm = &tls_main;
756 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
757 {
758 if (unformat (input, "use-test-cert-in-ca"))
759 tm->use_test_cert_in_ca = 1;
760 else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path))
761 ;
762 else
763 return clib_error_return (0, "unknown input `%U'",
764 format_unformat_error, input);
765 }
766 return 0;
767}
768
769VLIB_EARLY_CONFIG_FUNCTION (tls_config_fn, "tls");
Florin Corasd77eee62018-03-07 08:49:27 -0800770
771tls_main_t *
772vnet_tls_get_main (void)
773{
774 return &tls_main;
775}
776
Florin Coras371ca502018-02-21 12:07:41 -0800777/*
778 * fd.io coding-style-patch-verification: ON
779 *
780 * Local Variables:
781 * eval: (c-set-style "gnu")
782 * End:
783 */