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 | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 52 | return application_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); |
| 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 | |
| 110 | void |
| 111 | tls_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 | |
| 119 | tls_ctx_t * |
| 120 | tls_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 | |
| 127 | void |
| 128 | tls_ctx_half_open_reader_unlock () |
| 129 | { |
| 130 | clib_rwlock_reader_unlock (&tls_main.half_open_rwlock); |
| 131 | } |
| 132 | |
| 133 | u32 |
| 134 | tls_ctx_half_open_index (tls_ctx_t * ctx) |
| 135 | { |
| 136 | return (ctx - tls_main.half_open_ctx_pool); |
| 137 | } |
| 138 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 139 | void |
| 140 | tls_notify_app_enqueue (tls_ctx_t * ctx, stream_session_t * app_session) |
| 141 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 142 | app_worker_t *app; |
| 143 | app = app_worker_get_if_valid (app_session->app_wrk_index); |
Florin Coras | 5090c57 | 2018-03-18 08:22:17 -0700 | [diff] [blame] | 144 | if (PREDICT_TRUE (app != 0)) |
| 145 | tls_add_app_q_evt (app, app_session); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 149 | tls_notify_app_accept (tls_ctx_t * ctx) |
| 150 | { |
| 151 | stream_session_t *app_listener, *app_session; |
| 152 | segment_manager_t *sm; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 153 | app_worker_t *app_wrk; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 154 | application_t *app; |
| 155 | tls_ctx_t *lctx; |
| 156 | int rv; |
| 157 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 158 | app_wrk = app_worker_get (ctx->parent_app_index); |
| 159 | app = application_get (app_wrk->app_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 160 | lctx = tls_listener_ctx_get (ctx->listener_ctx_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 161 | |
| 162 | app_session = session_alloc (vlib_get_thread_index ()); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 163 | app_session->app_wrk_index = ctx->parent_app_index; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 164 | app_session->connection_index = ctx->tls_ctx_handle; |
Ping Yu | a0c29a9 | 2018-08-16 19:11:05 -0400 | [diff] [blame] | 165 | |
| 166 | app_listener = listen_session_get_from_handle (lctx->app_session_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 167 | app_session->session_type = app_listener->session_type; |
| 168 | app_session->listener_index = app_listener->session_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 169 | sm = app_worker_get_listen_segment_manager (app_wrk, app_listener); |
| 170 | app_session->opaque = tls_main.app_index; |
| 171 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 172 | 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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 178 | ctx->app_session_handle = session_handle (app_session); |
| 179 | return app->cb_fns.session_accept_callback (app_session); |
| 180 | } |
| 181 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 182 | int |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 183 | tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 184 | { |
| 185 | int (*cb_fn) (u32, u32, stream_session_t *, u8); |
| 186 | stream_session_t *app_session; |
| 187 | segment_manager_t *sm; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 188 | app_worker_t *app_wrk; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 189 | application_t *app; |
| 190 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 191 | app_wrk = app_worker_get (ctx->parent_app_index); |
| 192 | app = application_get (app_wrk->app_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 193 | cb_fn = app->cb_fns.session_connected_callback; |
| 194 | |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 195 | if (is_failed) |
| 196 | goto failed; |
| 197 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 198 | sm = app_worker_get_connect_segment_manager (app_wrk); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 199 | app_session = session_alloc (vlib_get_thread_index ()); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 200 | app_session->app_wrk_index = ctx->parent_app_index; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 201 | app_session->connection_index = ctx->tls_ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 202 | app_session->session_type = |
| 203 | session_type_from_proto_and_ip (TRANSPORT_PROTO_TLS, ctx->tcp_is_ip4); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 204 | app_session->opaque = tls_main.app_index; |
| 205 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 206 | 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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 211 | 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 Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 216 | tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ()); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | |
| 221 | failed: |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 222 | tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ()); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 223 | return cb_fn (ctx->parent_app_index, ctx->parent_app_api_context, 0, |
| 224 | 1 /* failed */ ); |
| 225 | } |
| 226 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 227 | static inline void |
| 228 | 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] | 229 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 230 | *ctx_index = ctx_handle & TLS_IDX_MASK; |
| 231 | *engine_type = ctx_handle >> TLS_ENGINE_TYPE_SHIFT; |
| 232 | } |
| 233 | |
| 234 | static inline tls_engine_type_t |
| 235 | tls_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 | |
| 242 | static inline u32 |
| 243 | tls_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 Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static inline void |
| 251 | tls_ctx_free (tls_ctx_t * ctx) |
| 252 | { |
| 253 | vec_free (ctx->srv_hostname); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 254 | tls_vfts[ctx->tls_ctx_engine].ctx_free (ctx); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static inline tls_ctx_t * |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 258 | tls_ctx_get (u32 ctx_handle) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 259 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 260 | 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 Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static inline tls_ctx_t * |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 266 | tls_ctx_get_w_thread (u32 ctx_handle, u8 thread_index) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 267 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 268 | 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 Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static inline int |
| 274 | tls_ctx_init_server (tls_ctx_t * ctx) |
| 275 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 276 | return tls_vfts[ctx->tls_ctx_engine].ctx_init_server (ctx); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static inline int |
| 280 | tls_ctx_init_client (tls_ctx_t * ctx) |
| 281 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 282 | return tls_vfts[ctx->tls_ctx_engine].ctx_init_client (ctx); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static inline int |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 286 | tls_ctx_write (tls_ctx_t * ctx, stream_session_t * app_session) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 287 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 288 | return tls_vfts[ctx->tls_ctx_engine].ctx_write (ctx, app_session); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static inline int |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 292 | tls_ctx_read (tls_ctx_t * ctx, stream_session_t * tls_session) |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 293 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 294 | return tls_vfts[ctx->tls_ctx_engine].ctx_read (ctx, tls_session); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static inline u8 |
| 298 | tls_ctx_handshake_is_over (tls_ctx_t * ctx) |
| 299 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 300 | return tls_vfts[ctx->tls_ctx_engine].ctx_handshake_is_over (ctx); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void |
| 304 | tls_session_reset_callback (stream_session_t * s) |
| 305 | { |
| 306 | clib_warning ("called..."); |
| 307 | } |
| 308 | |
| 309 | int |
| 310 | tls_add_segment_callback (u32 client_index, const ssvm_private_t * fs) |
| 311 | { |
| 312 | /* No-op for builtin */ |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | int |
| 317 | tls_del_segment_callback (u32 client_index, const ssvm_private_t * fs) |
| 318 | { |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | void |
| 323 | tls_session_disconnect_callback (stream_session_t * tls_session) |
| 324 | { |
| 325 | stream_session_t *app_session; |
| 326 | tls_ctx_t *ctx; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 327 | app_worker_t *app_wrk; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 328 | application_t *app; |
| 329 | |
| 330 | ctx = tls_ctx_get (tls_session->opaque); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 331 | if (!tls_ctx_handshake_is_over (ctx)) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 332 | { |
| 333 | stream_session_disconnect (tls_session); |
| 334 | return; |
| 335 | } |
| 336 | ctx->is_passive_close = 1; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 337 | app_wrk = app_worker_get (ctx->parent_app_index); |
| 338 | app = application_get (app_wrk->app_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 339 | app_session = session_get_from_handle (ctx->app_session_handle); |
| 340 | app->cb_fns.session_disconnect_callback (app_session); |
| 341 | } |
| 342 | |
| 343 | int |
| 344 | tls_session_accept_callback (stream_session_t * tls_session) |
| 345 | { |
| 346 | stream_session_t *tls_listener; |
| 347 | tls_ctx_t *lctx, *ctx; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 348 | u32 ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 349 | |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 350 | tls_listener = listen_session_get (tls_session->listener_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 351 | lctx = tls_listener_ctx_get (tls_listener->opaque); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 352 | |
| 353 | ctx_handle = tls_ctx_alloc (lctx->tls_ctx_engine); |
| 354 | ctx = tls_ctx_get (ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 355 | memcpy (ctx, lctx, sizeof (*lctx)); |
| 356 | ctx->c_thread_index = vlib_get_thread_index (); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 357 | ctx->tls_ctx_handle = ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 358 | tls_session->session_state = SESSION_STATE_READY; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 359 | tls_session->opaque = ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 360 | ctx->tls_session_handle = session_handle (tls_session); |
| 361 | ctx->listener_ctx_index = tls_listener->opaque; |
| 362 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 363 | TLS_DBG (1, "Accept on listener %u new connection [%u]%x", |
| 364 | tls_listener->opaque, vlib_get_thread_index (), ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 365 | |
| 366 | return tls_ctx_init_server (ctx); |
| 367 | } |
| 368 | |
| 369 | int |
| 370 | tls_app_tx_callback (stream_session_t * app_session) |
| 371 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 372 | tls_ctx_t *ctx; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 373 | if (PREDICT_FALSE (app_session->session_state == SESSION_STATE_CLOSED)) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 374 | return 0; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 375 | ctx = tls_ctx_get (app_session->connection_index); |
| 376 | tls_ctx_write (ctx, app_session); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | int |
| 381 | tls_app_rx_callback (stream_session_t * tls_session) |
| 382 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 383 | tls_ctx_t *ctx; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 384 | |
| 385 | ctx = tls_ctx_get (tls_session->opaque); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 386 | tls_ctx_read (ctx, tls_session); |
| 387 | return 0; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | int |
| 391 | tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index, |
| 392 | stream_session_t * tls_session, u8 is_fail) |
| 393 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 394 | tls_ctx_t *ho_ctx, *ctx; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 395 | u32 ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 396 | |
| 397 | ho_ctx = tls_ctx_half_open_get (ho_ctx_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 398 | |
| 399 | if (is_fail) |
Florin Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 400 | { |
Florin Coras | 57791ad | 2018-08-27 10:07:40 -0700 | [diff] [blame] | 401 | 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 Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 408 | tls_ctx_half_open_reader_unlock (); |
| 409 | tls_ctx_half_open_free (ho_ctx_index); |
Florin Coras | 57791ad | 2018-08-27 10:07:40 -0700 | [diff] [blame] | 410 | 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 Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 414 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 415 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 416 | ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine); |
| 417 | ctx = tls_ctx_get (ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 418 | clib_memcpy (ctx, ho_ctx, sizeof (*ctx)); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 419 | tls_ctx_half_open_reader_unlock (); |
| 420 | tls_ctx_half_open_free (ho_ctx_index); |
| 421 | |
Florin Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 422 | ctx->c_thread_index = vlib_get_thread_index (); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 423 | ctx->tls_ctx_handle = ctx_handle; |
Florin Coras | dcde927 | 2018-03-02 09:23:42 -0800 | [diff] [blame] | 424 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 425 | 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] | 426 | ho_ctx_index, is_fail, vlib_get_thread_index (), |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 427 | (ctx) ? ctx_handle : ~0); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 428 | |
| 429 | ctx->tls_session_handle = session_handle (tls_session); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 430 | tls_session->opaque = ctx_handle; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 431 | tls_session->session_state = SESSION_STATE_READY; |
| 432 | |
| 433 | return tls_ctx_init_client (ctx); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* *INDENT-OFF* */ |
| 437 | static 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 | |
| 449 | int |
| 450 | tls_connect (transport_endpoint_t * tep) |
| 451 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 452 | vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 453 | session_endpoint_extended_t *sep; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 454 | tls_engine_type_t engine_type; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 455 | tls_main_t *tm = &tls_main; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 456 | app_worker_t *app_wrk; |
| 457 | clib_error_t *error; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 458 | application_t *app; |
| 459 | tls_ctx_t *ctx; |
| 460 | u32 ctx_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 461 | |
| 462 | sep = (session_endpoint_extended_t *) tep; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 463 | app_wrk = app_worker_get (sep->app_wrk_index); |
| 464 | app = application_get (app_wrk->app_index); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 465 | 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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 471 | |
| 472 | ctx_index = tls_ctx_half_open_alloc (); |
| 473 | ctx = tls_ctx_half_open_get (ctx_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 474 | ctx->parent_app_index = sep->app_wrk_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 475 | ctx->parent_app_api_context = sep->opaque; |
| 476 | ctx->tcp_is_ip4 = sep->is_ip4; |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 477 | if (sep->hostname) |
| 478 | { |
| 479 | ctx->srv_hostname = format (0, "%v", sep->hostname); |
| 480 | vec_terminate_c_string (ctx->srv_hostname); |
| 481 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 482 | tls_ctx_half_open_reader_unlock (); |
| 483 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 484 | app_worker_alloc_connects_segment_manager (app_wrk); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 485 | ctx->tls_ctx_engine = engine_type; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 486 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 487 | 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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 493 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 494 | 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] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | void |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 499 | tls_disconnect (u32 ctx_handle, u32 thread_index) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 500 | { |
| 501 | stream_session_t *tls_session, *app_session; |
| 502 | tls_ctx_t *ctx; |
| 503 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 504 | TLS_DBG (1, "Disconnecting %x", ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 505 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 506 | ctx = tls_ctx_get (ctx_handle); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 507 | 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 | |
| 521 | u32 |
| 522 | tls_start_listen (u32 app_listener_index, transport_endpoint_t * tep) |
| 523 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 524 | app_worker_t *tls_app_wrk, *app_wrk; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 525 | tls_main_t *tm = &tls_main; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 526 | session_handle_t tls_handle; |
| 527 | session_endpoint_extended_t *sep; |
| 528 | stream_session_t *tls_listener; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 529 | stream_session_t *app_listener; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 530 | tls_engine_type_t engine_type; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 531 | application_t *app, *tls_app; |
| 532 | tls_ctx_t *lctx; |
| 533 | u32 lctx_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 534 | |
| 535 | sep = (session_endpoint_extended_t *) tep; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 536 | app_wrk = app_worker_get (sep->app_wrk_index); |
| 537 | app = application_get (app_wrk->app_index); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 538 | 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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 545 | lctx_index = tls_listener_ctx_alloc (); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 546 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 547 | /* TODO hide this by calling vnet_bind() */ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 548 | tls_app = application_get (tm->app_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 549 | tls_app_wrk = application_get_default_worker (tls_app); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 550 | sep->transport_proto = TRANSPORT_PROTO_TCP; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 551 | if (app_worker_start_listen (tls_app_wrk, (session_endpoint_t *) sep, |
| 552 | &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 | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 582 | app_worker_stop_listen (lctx->tls_session_handle, tm->app_index); |
Ping Yu | decda5b | 2018-08-13 06:20:00 -0400 | [diff] [blame] | 583 | engine_type = lctx->tls_ctx_engine; |
| 584 | tls_vfts[engine_type].ctx_stop_listen (lctx); |
| 585 | |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 586 | tls_listener_ctx_free (lctx); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | transport_connection_t * |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 591 | tls_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 | |
| 598 | transport_connection_t * |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 599 | tls_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 | |
| 606 | u8 * |
| 607 | format_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 Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 617 | s = format (s, "[#%d][TLS] app %u child %u", child_ti, |
| 618 | ctx->parent_app_index, child_si); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 619 | return s; |
| 620 | } |
| 621 | |
| 622 | u8 * |
| 623 | format_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 | |
| 644 | u8 * |
| 645 | format_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 Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 649 | u32 listener_index, thread_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 650 | |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 651 | listen_session_parse_handle (ctx->tls_session_handle, &listener_index, |
| 652 | &thread_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 653 | return format (s, "[TLS] listener app %u child %u", ctx->parent_app_index, |
| 654 | listener_index); |
| 655 | } |
| 656 | |
| 657 | u8 * |
| 658 | format_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* */ |
| 668 | const static transport_proto_vft_t tls_proto = { |
| 669 | .open = tls_connect, |
| 670 | .close = tls_disconnect, |
| 671 | .bind = tls_start_listen, |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 672 | .get_connection = tls_connection_get, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 673 | .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 Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 683 | void |
| 684 | 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] | 685 | { |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 686 | vec_validate (tls_vfts, type); |
| 687 | tls_vfts[type] = *vft; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Dave Barach | cc5677b | 2018-03-28 19:08:45 -0400 | [diff] [blame] | 690 | static clib_error_t * |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 691 | tls_init (vlib_main_t * vm) |
| 692 | { |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 693 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 694 | 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 Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 698 | u32 fifo_size = 64 << 10; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 699 | u32 num_threads; |
| 700 | |
| 701 | num_threads = 1 /* main thread */ + vtm->n_threads; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 702 | |
| 703 | memset (a, 0, sizeof (*a)); |
| 704 | memset (options, 0, sizeof (options)); |
| 705 | |
| 706 | a->session_cb_vft = &tls_app_cb_vft; |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 707 | a->api_client_index = APP_INVALID_INDEX; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 708 | a->options = options; |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 709 | a->name = format (0, "tls"); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 710 | 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 Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 722 | if (!tm->ca_cert_path) |
| 723 | tm->ca_cert_path = TLS_CA_CERT_PATH; |
| 724 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 725 | tm->app_index = a->app_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 726 | clib_rwlock_init (&tm->half_open_rwlock); |
| 727 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 728 | vec_validate (tm->rx_bufs, num_threads - 1); |
| 729 | vec_validate (tm->tx_bufs, num_threads - 1); |
| 730 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 731 | 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 Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 735 | vec_free (a->name); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | VLIB_INIT_FUNCTION (tls_init); |
| 740 | |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 741 | static clib_error_t * |
| 742 | tls_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 | |
| 758 | VLIB_EARLY_CONFIG_FUNCTION (tls_config_fn, "tls"); |
Florin Coras | d77eee6 | 2018-03-07 08:49:27 -0800 | [diff] [blame] | 759 | |
| 760 | tls_main_t * |
| 761 | vnet_tls_get_main (void) |
| 762 | { |
| 763 | return &tls_main; |
| 764 | } |
| 765 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 766 | /* |
| 767 | * fd.io coding-style-patch-verification: ON |
| 768 | * |
| 769 | * Local Variables: |
| 770 | * eval: (c-set-style "gnu") |
| 771 | * End: |
| 772 | */ |