Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 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 | #include <vnet/session/application_interface.h> |
| 17 | #include <vppinfra/lock.h> |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 18 | #include <vnet/tls/tls.h> |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 19 | |
| 20 | static tls_main_t tls_main; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 21 | static tls_engine_vft_t *tls_vfts; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 22 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 23 | #define TLS_INVALID_HANDLE ~0 |
| 24 | #define TLS_IDX_MASK 0x00FFFFFF |
| 25 | #define TLS_ENGINE_TYPE_SHIFT 29 |
| 26 | |
| 27 | void tls_disconnect (u32 ctx_handle, u32 thread_index); |
| 28 | |
| 29 | tls_engine_type_t |
| 30 | tls_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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 40 | |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 41 | int |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 42 | tls_add_vpp_q_evt (svm_fifo_t * f, u8 evt_type) |
| 43 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 44 | if (svm_fifo_set_event (f)) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 45 | session_send_io_evt_to_thread (f, evt_type); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static inline int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 50 | tls_add_app_q_evt (app_worker_t * app, stream_session_t * app_session) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 51 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 52 | return app_worker_send_event (app, app_session, FIFO_EVENT_APP_RX); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | u32 |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 56 | tls_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 | |
| 66 | void |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 67 | tls_listener_ctx_free (tls_ctx_t * ctx) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 68 | { |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 69 | pool_put (tls_main.listener_ctx_pool, ctx); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | tls_ctx_t * |
| 73 | tls_listener_ctx_get (u32 ctx_index) |
| 74 | { |
| 75 | return pool_elt_at_index (tls_main.listener_ctx_pool, ctx_index); |
| 76 | } |
| 77 | |
| 78 | u32 |
| 79 | tls_listener_ctx_index (tls_ctx_t * ctx) |
| 80 | { |
| 81 | return (ctx - tls_main.listener_ctx_pool); |
| 82 | } |
| 83 | |
| 84 | u32 |
| 85 | tls_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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 97 | clib_rwlock_writer_unlock (&tm->half_open_rwlock); |
| 98 | } |
| 99 | else |
| 100 | { |
Ping Yu | e6446a3 | 2018-08-28 18:56:27 -0400 | [diff] [blame] | 101 | /* reader lock assumption: only main thread will call pool_get */ |
| 102 | clib_rwlock_reader_lock (&tm->half_open_rwlock); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 103 | pool_get (tm->half_open_ctx_pool, ctx); |
Ping Yu | e6446a3 | 2018-08-28 18:56:27 -0400 | [diff] [blame] | 104 | clib_rwlock_reader_unlock (&tm->half_open_rwlock); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 105 | } |
Ping Yu | e6446a3 | 2018-08-28 18:56:27 -0400 | [diff] [blame] | 106 | memset (ctx, 0, sizeof (*ctx)); |
| 107 | ctx_index = ctx - tm->half_open_ctx_pool; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 108 | return ctx_index; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | tls_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 | |
| 120 | tls_ctx_t * |
| 121 | tls_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 | |
| 128 | void |
| 129 | tls_ctx_half_open_reader_unlock () |
| 130 | { |
| 131 | clib_rwlock_reader_unlock (&tls_main.half_open_rwlock); |
| 132 | } |
| 133 | |
| 134 | u32 |
| 135 | tls_ctx_half_open_index (tls_ctx_t * ctx) |
| 136 | { |
| 137 | return (ctx - tls_main.half_open_ctx_pool); |
| 138 | } |
| 139 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 140 | void |
| 141 | tls_notify_app_enqueue (tls_ctx_t * ctx, stream_session_t * app_session) |
| 142 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 143 | app_worker_t *app; |
| 144 | app = app_worker_get_if_valid (app_session->app_wrk_index); |
Florin Coras | 5090c57 | 2018-03-18 08:22:17 -0700 | [diff] [blame] | 145 | if (PREDICT_TRUE (app != 0)) |
| 146 | tls_add_app_q_evt (app, app_session); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 150 | tls_notify_app_accept (tls_ctx_t * ctx) |
| 151 | { |
| 152 | stream_session_t *app_listener, *app_session; |
| 153 | segment_manager_t *sm; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 154 | app_worker_t *app_wrk; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 155 | application_t *app; |
| 156 | tls_ctx_t *lctx; |
| 157 | int rv; |
| 158 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 159 | app_wrk = app_worker_get (ctx->parent_app_index); |
| 160 | app = application_get (app_wrk->app_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 161 | lctx = tls_listener_ctx_get (ctx->listener_ctx_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 162 | |
| 163 | app_session = session_alloc (vlib_get_thread_index ()); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 164 | app_session->app_wrk_index = ctx->parent_app_index; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 165 | app_session->connection_index = ctx->tls_ctx_handle; |
Ping Yu | a0c29a9 | 2018-08-16 19:11:05 -0400 | [diff] [blame] | 166 | |
| 167 | app_listener = listen_session_get_from_handle (lctx->app_session_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 168 | app_session->session_type = app_listener->session_type; |
| 169 | app_session->listener_index = app_listener->session_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 170 | sm = app_worker_get_listen_segment_manager (app_wrk, app_listener); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 171 | app_session->t_app_index = tls_main.app_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 172 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 173 | 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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 179 | ctx->app_session_handle = session_handle (app_session); |
| 180 | return app->cb_fns.session_accept_callback (app_session); |
| 181 | } |
| 182 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 183 | int |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 184 | tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 185 | { |
| 186 | int (*cb_fn) (u32, u32, stream_session_t *, u8); |
| 187 | stream_session_t *app_session; |
| 188 | segment_manager_t *sm; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 189 | app_worker_t *app_wrk; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 190 | application_t *app; |
| 191 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 192 | app_wrk = app_worker_get (ctx->parent_app_index); |
| 193 | app = application_get (app_wrk->app_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 194 | cb_fn = app->cb_fns.session_connected_callback; |
| 195 | |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 196 | if (is_failed) |
| 197 | goto failed; |
| 198 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 199 | sm = app_worker_get_connect_segment_manager (app_wrk); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 200 | app_session = session_alloc (vlib_get_thread_index ()); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 201 | app_session->app_wrk_index = ctx->parent_app_index; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 202 | app_session->connection_index = ctx->tls_ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 203 | app_session->session_type = |
| 204 | session_type_from_proto_and_ip (TRANSPORT_PROTO_TLS, ctx->tcp_is_ip4); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 205 | app_session->t_app_index = tls_main.app_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 206 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 207 | if (session_alloc_fifos (sm, app_session)) |
| 208 | goto failed; |
| 209 | |
| 210 | ctx->app_session_handle = session_handle (app_session); |
| 211 | ctx->c_s_index = app_session->session_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 212 | app_session->session_state = SESSION_STATE_READY; |
| 213 | if (cb_fn (ctx->parent_app_index, ctx->parent_app_api_context, |
| 214 | app_session, 0 /* not failed */ )) |
| 215 | { |
| 216 | TLS_DBG (1, "failed to notify app"); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 217 | tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ()); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | |
| 222 | failed: |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 223 | tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ()); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 224 | return cb_fn (ctx->parent_app_index, ctx->parent_app_api_context, 0, |
| 225 | 1 /* failed */ ); |
| 226 | } |
| 227 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 228 | static inline void |
| 229 | tls_ctx_parse_handle (u32 ctx_handle, u32 * ctx_index, u32 * engine_type) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 230 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 231 | *ctx_index = ctx_handle & TLS_IDX_MASK; |
| 232 | *engine_type = ctx_handle >> TLS_ENGINE_TYPE_SHIFT; |
| 233 | } |
| 234 | |
| 235 | static inline tls_engine_type_t |
| 236 | tls_get_engine_type (tls_engine_type_t preferred) |
| 237 | { |
| 238 | if (!tls_vfts[preferred].ctx_alloc) |
| 239 | return tls_get_available_engine (); |
| 240 | return preferred; |
| 241 | } |
| 242 | |
| 243 | static inline u32 |
| 244 | tls_ctx_alloc (tls_engine_type_t engine_type) |
| 245 | { |
| 246 | u32 ctx_index; |
| 247 | ctx_index = tls_vfts[engine_type].ctx_alloc (); |
| 248 | return (((u32) engine_type << TLS_ENGINE_TYPE_SHIFT) | ctx_index); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static inline void |
| 252 | tls_ctx_free (tls_ctx_t * ctx) |
| 253 | { |
| 254 | vec_free (ctx->srv_hostname); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 255 | tls_vfts[ctx->tls_ctx_engine].ctx_free (ctx); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static inline tls_ctx_t * |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 259 | tls_ctx_get (u32 ctx_handle) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 260 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 261 | u32 ctx_index, engine_type; |
| 262 | tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type); |
| 263 | return tls_vfts[engine_type].ctx_get (ctx_index); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static inline tls_ctx_t * |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 267 | tls_ctx_get_w_thread (u32 ctx_handle, u8 thread_index) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 268 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 269 | u32 ctx_index, engine_type; |
| 270 | tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type); |
| 271 | return tls_vfts[engine_type].ctx_get_w_thread (ctx_index, thread_index); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static inline int |
| 275 | tls_ctx_init_server (tls_ctx_t * ctx) |
| 276 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 277 | return tls_vfts[ctx->tls_ctx_engine].ctx_init_server (ctx); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static inline int |
| 281 | tls_ctx_init_client (tls_ctx_t * ctx) |
| 282 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 283 | return tls_vfts[ctx->tls_ctx_engine].ctx_init_client (ctx); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static inline int |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 287 | tls_ctx_write (tls_ctx_t * ctx, stream_session_t * app_session) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 288 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 289 | return tls_vfts[ctx->tls_ctx_engine].ctx_write (ctx, app_session); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static inline int |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 293 | tls_ctx_read (tls_ctx_t * ctx, stream_session_t * tls_session) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 294 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 295 | return tls_vfts[ctx->tls_ctx_engine].ctx_read (ctx, tls_session); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static inline u8 |
| 299 | tls_ctx_handshake_is_over (tls_ctx_t * ctx) |
| 300 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 301 | return tls_vfts[ctx->tls_ctx_engine].ctx_handshake_is_over (ctx); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void |
| 305 | tls_session_reset_callback (stream_session_t * s) |
| 306 | { |
| 307 | clib_warning ("called..."); |
| 308 | } |
| 309 | |
| 310 | int |
| 311 | tls_add_segment_callback (u32 client_index, const ssvm_private_t * fs) |
| 312 | { |
| 313 | /* No-op for builtin */ |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | int |
| 318 | tls_del_segment_callback (u32 client_index, const ssvm_private_t * fs) |
| 319 | { |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | tls_session_disconnect_callback (stream_session_t * tls_session) |
| 325 | { |
| 326 | stream_session_t *app_session; |
| 327 | tls_ctx_t *ctx; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 328 | app_worker_t *app_wrk; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 329 | application_t *app; |
| 330 | |
| 331 | ctx = tls_ctx_get (tls_session->opaque); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 332 | if (!tls_ctx_handshake_is_over (ctx)) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 333 | { |
| 334 | stream_session_disconnect (tls_session); |
| 335 | return; |
| 336 | } |
| 337 | ctx->is_passive_close = 1; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 338 | app_wrk = app_worker_get (ctx->parent_app_index); |
| 339 | app = application_get (app_wrk->app_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 340 | app_session = session_get_from_handle (ctx->app_session_handle); |
| 341 | app->cb_fns.session_disconnect_callback (app_session); |
| 342 | } |
| 343 | |
| 344 | int |
| 345 | tls_session_accept_callback (stream_session_t * tls_session) |
| 346 | { |
| 347 | stream_session_t *tls_listener; |
| 348 | tls_ctx_t *lctx, *ctx; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 349 | u32 ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 350 | |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 351 | tls_listener = listen_session_get (tls_session->listener_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 352 | lctx = tls_listener_ctx_get (tls_listener->opaque); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 353 | |
| 354 | ctx_handle = tls_ctx_alloc (lctx->tls_ctx_engine); |
| 355 | ctx = tls_ctx_get (ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 356 | memcpy (ctx, lctx, sizeof (*lctx)); |
| 357 | ctx->c_thread_index = vlib_get_thread_index (); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 358 | ctx->tls_ctx_handle = ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 359 | tls_session->session_state = SESSION_STATE_READY; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 360 | tls_session->opaque = ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 361 | ctx->tls_session_handle = session_handle (tls_session); |
| 362 | ctx->listener_ctx_index = tls_listener->opaque; |
| 363 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 364 | TLS_DBG (1, "Accept on listener %u new connection [%u]%x", |
| 365 | tls_listener->opaque, vlib_get_thread_index (), ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 366 | |
| 367 | return tls_ctx_init_server (ctx); |
| 368 | } |
| 369 | |
| 370 | int |
| 371 | tls_app_tx_callback (stream_session_t * app_session) |
| 372 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 373 | tls_ctx_t *ctx; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 374 | if (PREDICT_FALSE (app_session->session_state == SESSION_STATE_CLOSED)) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 375 | return 0; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 376 | ctx = tls_ctx_get (app_session->connection_index); |
| 377 | tls_ctx_write (ctx, app_session); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | int |
| 382 | tls_app_rx_callback (stream_session_t * tls_session) |
| 383 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 384 | tls_ctx_t *ctx; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 385 | |
| 386 | ctx = tls_ctx_get (tls_session->opaque); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 387 | tls_ctx_read (ctx, tls_session); |
| 388 | return 0; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | int |
| 392 | tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index, |
| 393 | stream_session_t * tls_session, u8 is_fail) |
| 394 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 395 | tls_ctx_t *ho_ctx, *ctx; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 396 | u32 ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 397 | |
| 398 | ho_ctx = tls_ctx_half_open_get (ho_ctx_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 399 | |
| 400 | if (is_fail) |
Florin Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 401 | { |
Florin Coras | 57791ad | 2018-08-27 10:07:40 -0700 | [diff] [blame] | 402 | int (*cb_fn) (u32, u32, stream_session_t *, u8); |
| 403 | u32 wrk_index, api_context; |
| 404 | app_worker_t *app_wrk; |
| 405 | application_t *app; |
| 406 | |
| 407 | wrk_index = ho_ctx->parent_app_index; |
| 408 | api_context = ho_ctx->c_s_index; |
Florin Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 409 | tls_ctx_half_open_reader_unlock (); |
| 410 | tls_ctx_half_open_free (ho_ctx_index); |
Florin Coras | 57791ad | 2018-08-27 10:07:40 -0700 | [diff] [blame] | 411 | app_wrk = app_worker_get (ho_ctx->parent_app_index); |
| 412 | app = application_get (app_wrk->app_index); |
| 413 | cb_fn = app->cb_fns.session_connected_callback; |
| 414 | return cb_fn (wrk_index, api_context, 0, 1 /* failed */ ); |
Florin Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 415 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 416 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 417 | ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine); |
| 418 | ctx = tls_ctx_get (ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 419 | clib_memcpy (ctx, ho_ctx, sizeof (*ctx)); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 420 | tls_ctx_half_open_reader_unlock (); |
| 421 | tls_ctx_half_open_free (ho_ctx_index); |
| 422 | |
Florin Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 423 | ctx->c_thread_index = vlib_get_thread_index (); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 424 | ctx->tls_ctx_handle = ctx_handle; |
Florin Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 425 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 426 | TLS_DBG (1, "TCP connect for %u returned %u. New connection [%u]%x", |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 427 | ho_ctx_index, is_fail, vlib_get_thread_index (), |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 428 | (ctx) ? ctx_handle : ~0); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 429 | |
| 430 | ctx->tls_session_handle = session_handle (tls_session); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 431 | tls_session->opaque = ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 432 | tls_session->session_state = SESSION_STATE_READY; |
| 433 | |
| 434 | return tls_ctx_init_client (ctx); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* *INDENT-OFF* */ |
| 438 | static session_cb_vft_t tls_app_cb_vft = { |
| 439 | .session_accept_callback = tls_session_accept_callback, |
| 440 | .session_disconnect_callback = tls_session_disconnect_callback, |
| 441 | .session_connected_callback = tls_session_connected_callback, |
| 442 | .session_reset_callback = tls_session_reset_callback, |
| 443 | .add_segment_callback = tls_add_segment_callback, |
| 444 | .del_segment_callback = tls_del_segment_callback, |
| 445 | .builtin_app_rx_callback = tls_app_rx_callback, |
| 446 | .builtin_app_tx_callback = tls_app_tx_callback, |
| 447 | }; |
| 448 | /* *INDENT-ON* */ |
| 449 | |
| 450 | int |
| 451 | tls_connect (transport_endpoint_t * tep) |
| 452 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 453 | vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 454 | session_endpoint_extended_t *sep; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 455 | tls_engine_type_t engine_type; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 456 | tls_main_t *tm = &tls_main; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 457 | app_worker_t *app_wrk; |
| 458 | clib_error_t *error; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 459 | application_t *app; |
| 460 | tls_ctx_t *ctx; |
| 461 | u32 ctx_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 462 | |
| 463 | sep = (session_endpoint_extended_t *) tep; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 464 | app_wrk = app_worker_get (sep->app_wrk_index); |
| 465 | app = application_get (app_wrk->app_index); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 466 | engine_type = tls_get_engine_type (app->tls_engine); |
| 467 | if (engine_type == TLS_ENGINE_NONE) |
| 468 | { |
| 469 | clib_warning ("No tls engine_type available"); |
| 470 | return -1; |
| 471 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 472 | |
| 473 | ctx_index = tls_ctx_half_open_alloc (); |
| 474 | ctx = tls_ctx_half_open_get (ctx_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 475 | ctx->parent_app_index = sep->app_wrk_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 476 | ctx->parent_app_api_context = sep->opaque; |
| 477 | ctx->tcp_is_ip4 = sep->is_ip4; |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 478 | if (sep->hostname) |
| 479 | { |
| 480 | ctx->srv_hostname = format (0, "%v", sep->hostname); |
| 481 | vec_terminate_c_string (ctx->srv_hostname); |
| 482 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 483 | tls_ctx_half_open_reader_unlock (); |
| 484 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 485 | app_worker_alloc_connects_segment_manager (app_wrk); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 486 | ctx->tls_ctx_engine = engine_type; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 487 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 488 | clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_t)); |
| 489 | cargs->sep.transport_proto = TRANSPORT_PROTO_TCP; |
| 490 | cargs->app_index = tm->app_index; |
| 491 | cargs->api_context = ctx_index; |
| 492 | if ((error = vnet_connect (cargs))) |
| 493 | return clib_error_get_code (error); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 494 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 495 | TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | void |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 500 | tls_disconnect (u32 ctx_handle, u32 thread_index) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 501 | { |
| 502 | stream_session_t *tls_session, *app_session; |
| 503 | tls_ctx_t *ctx; |
| 504 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 505 | TLS_DBG (1, "Disconnecting %x", ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 506 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 507 | ctx = tls_ctx_get (ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 508 | tls_session = session_get_from_handle (ctx->tls_session_handle); |
| 509 | stream_session_disconnect (tls_session); |
| 510 | |
| 511 | app_session = session_get_from_handle_if_valid (ctx->app_session_handle); |
| 512 | if (app_session) |
| 513 | { |
| 514 | segment_manager_dealloc_fifos (app_session->svm_segment_index, |
| 515 | app_session->server_rx_fifo, |
| 516 | app_session->server_tx_fifo); |
| 517 | session_free (app_session); |
| 518 | } |
| 519 | tls_ctx_free (ctx); |
| 520 | } |
| 521 | |
| 522 | u32 |
| 523 | tls_start_listen (u32 app_listener_index, transport_endpoint_t * tep) |
| 524 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 525 | app_worker_t *app_wrk; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 526 | tls_main_t *tm = &tls_main; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 527 | session_handle_t tls_handle; |
| 528 | session_endpoint_extended_t *sep; |
| 529 | stream_session_t *tls_listener; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 530 | stream_session_t *app_listener; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 531 | tls_engine_type_t engine_type; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 532 | application_t *app, *tls_app; |
| 533 | tls_ctx_t *lctx; |
| 534 | u32 lctx_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 535 | |
| 536 | sep = (session_endpoint_extended_t *) tep; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 537 | app_wrk = app_worker_get (sep->app_wrk_index); |
| 538 | app = application_get (app_wrk->app_index); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 539 | engine_type = tls_get_engine_type (app->tls_engine); |
| 540 | if (engine_type == TLS_ENGINE_NONE) |
| 541 | { |
| 542 | clib_warning ("No tls engine_type available"); |
| 543 | return -1; |
| 544 | } |
| 545 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 546 | lctx_index = tls_listener_ctx_alloc (); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 547 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 548 | /* TODO hide this by calling vnet_bind() */ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 549 | tls_app = application_get (tm->app_index); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 550 | // tls_app_wrk = application_get_default_worker (tls_app); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 551 | sep->transport_proto = TRANSPORT_PROTO_TCP; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 552 | if (application_start_listen (tls_app, sep, &tls_handle)) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 553 | return ~0; |
| 554 | |
| 555 | tls_listener = listen_session_get_from_handle (tls_handle); |
| 556 | tls_listener->opaque = lctx_index; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 557 | |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 558 | app_listener = listen_session_get (app_listener_index); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 559 | |
| 560 | lctx = tls_listener_ctx_get (lctx_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 561 | lctx->parent_app_index = sep->app_wrk_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 562 | 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 Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 565 | lctx->tls_ctx_engine = engine_type; |
| 566 | |
Ping Yu | decda5b | 2018-08-13 06:20:00 -0400 | [diff] [blame] | 567 | tls_vfts[engine_type].ctx_start_listen (lctx); |
| 568 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 569 | TLS_DBG (1, "Started listening %d, engine type %d", lctx_index, |
| 570 | engine_type); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 571 | return lctx_index; |
| 572 | } |
| 573 | |
| 574 | u32 |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 575 | tls_stop_listen (u32 lctx_index) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 576 | { |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 577 | tls_main_t *tm = &tls_main; |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 578 | tls_ctx_t *lctx; |
Ping Yu | decda5b | 2018-08-13 06:20:00 -0400 | [diff] [blame] | 579 | tls_engine_type_t engine_type; |
| 580 | |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 581 | lctx = tls_listener_ctx_get (lctx_index); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 582 | /* TODO use unbind */ |
| 583 | application_stop_listen (tm->app_index, 0, lctx->tls_session_handle); |
Ping Yu | decda5b | 2018-08-13 06:20:00 -0400 | [diff] [blame] | 584 | engine_type = lctx->tls_ctx_engine; |
| 585 | tls_vfts[engine_type].ctx_stop_listen (lctx); |
| 586 | |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 587 | tls_listener_ctx_free (lctx); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | transport_connection_t * |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 592 | tls_connection_get (u32 ctx_index, u32 thread_index) |
| 593 | { |
| 594 | tls_ctx_t *ctx; |
| 595 | ctx = tls_ctx_get_w_thread (ctx_index, thread_index); |
| 596 | return &ctx->connection; |
| 597 | } |
| 598 | |
| 599 | transport_connection_t * |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 600 | tls_listener_get (u32 listener_index) |
| 601 | { |
| 602 | tls_ctx_t *ctx; |
| 603 | ctx = tls_listener_ctx_get (listener_index); |
| 604 | return &ctx->connection; |
| 605 | } |
| 606 | |
| 607 | u8 * |
| 608 | format_tls_ctx (u8 * s, va_list * args) |
| 609 | { |
| 610 | tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *); |
| 611 | u32 thread_index = va_arg (*args, u32); |
| 612 | u32 child_si, child_ti; |
| 613 | |
| 614 | session_parse_handle (ctx->tls_session_handle, &child_si, &child_ti); |
| 615 | if (thread_index != child_ti) |
| 616 | clib_warning ("app and tls sessions are on different threads!"); |
| 617 | |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 618 | s = format (s, "[#%d][TLS] app %u child %u", child_ti, |
| 619 | ctx->parent_app_index, child_si); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 620 | return s; |
| 621 | } |
| 622 | |
| 623 | u8 * |
| 624 | format_tls_connection (u8 * s, va_list * args) |
| 625 | { |
| 626 | u32 ctx_index = va_arg (*args, u32); |
| 627 | u32 thread_index = va_arg (*args, u32); |
| 628 | u32 verbose = va_arg (*args, u32); |
| 629 | tls_ctx_t *ctx; |
| 630 | |
| 631 | ctx = tls_ctx_get_w_thread (ctx_index, thread_index); |
| 632 | if (!ctx) |
| 633 | return s; |
| 634 | |
| 635 | s = format (s, "%-50U", format_tls_ctx, ctx, thread_index); |
| 636 | if (verbose) |
| 637 | { |
| 638 | s = format (s, "%-15s", "state"); |
| 639 | if (verbose > 1) |
| 640 | s = format (s, "\n"); |
| 641 | } |
| 642 | return s; |
| 643 | } |
| 644 | |
| 645 | u8 * |
| 646 | format_tls_listener (u8 * s, va_list * args) |
| 647 | { |
| 648 | u32 tc_index = va_arg (*args, u32); |
| 649 | tls_ctx_t *ctx = tls_listener_ctx_get (tc_index); |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 650 | u32 listener_index, thread_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 651 | |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 652 | listen_session_parse_handle (ctx->tls_session_handle, &listener_index, |
| 653 | &thread_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 654 | return format (s, "[TLS] listener app %u child %u", ctx->parent_app_index, |
| 655 | listener_index); |
| 656 | } |
| 657 | |
| 658 | u8 * |
| 659 | format_tls_half_open (u8 * s, va_list * args) |
| 660 | { |
| 661 | u32 tc_index = va_arg (*args, u32); |
| 662 | tls_ctx_t *ctx = tls_ctx_half_open_get (tc_index); |
| 663 | s = format (s, "[TLS] half-open app %u", ctx->parent_app_index); |
| 664 | tls_ctx_half_open_reader_unlock (); |
| 665 | return s; |
| 666 | } |
| 667 | |
| 668 | /* *INDENT-OFF* */ |
| 669 | const static transport_proto_vft_t tls_proto = { |
| 670 | .open = tls_connect, |
| 671 | .close = tls_disconnect, |
| 672 | .bind = tls_start_listen, |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 673 | .get_connection = tls_connection_get, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 674 | .get_listener = tls_listener_get, |
| 675 | .unbind = tls_stop_listen, |
| 676 | .tx_type = TRANSPORT_TX_INTERNAL, |
| 677 | .service_type = TRANSPORT_SERVICE_APP, |
| 678 | .format_connection = format_tls_connection, |
| 679 | .format_half_open = format_tls_half_open, |
| 680 | .format_listener = format_tls_listener, |
| 681 | }; |
| 682 | /* *INDENT-ON* */ |
| 683 | |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 684 | void |
| 685 | tls_register_engine (const tls_engine_vft_t * vft, tls_engine_type_t type) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 686 | { |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 687 | vec_validate (tls_vfts, type); |
| 688 | tls_vfts[type] = *vft; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Dave Barach | cc5677b | 2018-03-28 19:08:45 -0400 | [diff] [blame] | 691 | static clib_error_t * |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 692 | tls_init (vlib_main_t * vm) |
| 693 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 694 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 695 | vnet_app_attach_args_t _a, *a = &_a; |
| 696 | u64 options[APP_OPTIONS_N_OPTIONS]; |
| 697 | u32 segment_size = 512 << 20; |
| 698 | tls_main_t *tm = &tls_main; |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 699 | u32 fifo_size = 64 << 10; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 700 | u32 num_threads; |
| 701 | |
| 702 | num_threads = 1 /* main thread */ + vtm->n_threads; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 703 | |
| 704 | memset (a, 0, sizeof (*a)); |
| 705 | memset (options, 0, sizeof (options)); |
| 706 | |
| 707 | a->session_cb_vft = &tls_app_cb_vft; |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 708 | a->api_client_index = APP_INVALID_INDEX; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 709 | a->options = options; |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 710 | a->name = format (0, "tls"); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 711 | a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size; |
| 712 | a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size; |
| 713 | a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size; |
| 714 | a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; |
| 715 | a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 716 | |
| 717 | if (vnet_application_attach (a)) |
| 718 | { |
| 719 | clib_warning ("failed to attach tls app"); |
| 720 | return clib_error_return (0, "failed to attach tls app"); |
| 721 | } |
| 722 | |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 723 | if (!tm->ca_cert_path) |
| 724 | tm->ca_cert_path = TLS_CA_CERT_PATH; |
| 725 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 726 | tm->app_index = a->app_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 727 | clib_rwlock_init (&tm->half_open_rwlock); |
| 728 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 729 | vec_validate (tm->rx_bufs, num_threads - 1); |
| 730 | vec_validate (tm->tx_bufs, num_threads - 1); |
| 731 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 732 | transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto, |
| 733 | FIB_PROTOCOL_IP4, ~0); |
| 734 | transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto, |
| 735 | FIB_PROTOCOL_IP6, ~0); |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 736 | vec_free (a->name); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | VLIB_INIT_FUNCTION (tls_init); |
| 741 | |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 742 | static clib_error_t * |
| 743 | tls_config_fn (vlib_main_t * vm, unformat_input_t * input) |
| 744 | { |
| 745 | tls_main_t *tm = &tls_main; |
| 746 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 747 | { |
| 748 | if (unformat (input, "use-test-cert-in-ca")) |
| 749 | tm->use_test_cert_in_ca = 1; |
| 750 | else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path)) |
| 751 | ; |
| 752 | else |
| 753 | return clib_error_return (0, "unknown input `%U'", |
| 754 | format_unformat_error, input); |
| 755 | } |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | VLIB_EARLY_CONFIG_FUNCTION (tls_config_fn, "tls"); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 760 | |
| 761 | tls_main_t * |
| 762 | vnet_tls_get_main (void) |
| 763 | { |
| 764 | return &tls_main; |
| 765 | } |
| 766 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 767 | /* |
| 768 | * fd.io coding-style-patch-verification: ON |
| 769 | * |
| 770 | * Local Variables: |
| 771 | * eval: (c-set-style "gnu") |
| 772 | * End: |
| 773 | */ |