tls: fix handling of failed connects
[vpp.git] / src / vnet / tls / tls.c
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>
18 #include <vnet/tls/tls.h>
19
20 static tls_main_t tls_main;
21 static tls_engine_vft_t *tls_vfts;
22
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 }
40
41 int
42 tls_add_vpp_q_evt (svm_fifo_t * f, u8 evt_type)
43 {
44   if (svm_fifo_set_event (f))
45     session_send_io_evt_to_thread (f, evt_type);
46   return 0;
47 }
48
49 static inline int
50 tls_add_app_q_evt (app_worker_t * app, stream_session_t * app_session)
51 {
52   return application_send_event (app, app_session, FIFO_EVENT_APP_RX);
53 }
54
55 u32
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
67 tls_listener_ctx_free (tls_ctx_t * ctx)
68 {
69   pool_put (tls_main.listener_ctx_pool, ctx);
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
139 void
140 tls_notify_app_enqueue (tls_ctx_t * ctx, stream_session_t * app_session)
141 {
142   app_worker_t *app;
143   app = app_worker_get_if_valid (app_session->app_wrk_index);
144   if (PREDICT_TRUE (app != 0))
145     tls_add_app_q_evt (app, app_session);
146 }
147
148 int
149 tls_notify_app_accept (tls_ctx_t * ctx)
150 {
151   stream_session_t *app_listener, *app_session;
152   segment_manager_t *sm;
153   app_worker_t *app_wrk;
154   application_t *app;
155   tls_ctx_t *lctx;
156   int rv;
157
158   app_wrk = app_worker_get (ctx->parent_app_index);
159   app = application_get (app_wrk->app_index);
160   lctx = tls_listener_ctx_get (ctx->listener_ctx_index);
161
162   app_session = session_alloc (vlib_get_thread_index ());
163   app_session->app_wrk_index = ctx->parent_app_index;
164   app_session->connection_index = ctx->tls_ctx_handle;
165
166   app_listener = listen_session_get_from_handle (lctx->app_session_handle);
167   app_session->session_type = app_listener->session_type;
168   app_session->listener_index = app_listener->session_index;
169   sm = app_worker_get_listen_segment_manager (app_wrk, app_listener);
170   app_session->opaque = tls_main.app_index;
171
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;
178   ctx->app_session_handle = session_handle (app_session);
179   return app->cb_fns.session_accept_callback (app_session);
180 }
181
182 int
183 tls_notify_app_connected (tls_ctx_t * ctx, u8 is_failed)
184 {
185   int (*cb_fn) (u32, u32, stream_session_t *, u8);
186   stream_session_t *app_session;
187   segment_manager_t *sm;
188   app_worker_t *app_wrk;
189   application_t *app;
190
191   app_wrk = app_worker_get (ctx->parent_app_index);
192   app = application_get (app_wrk->app_index);
193   cb_fn = app->cb_fns.session_connected_callback;
194
195   if (is_failed)
196     goto failed;
197
198   sm = app_worker_get_connect_segment_manager (app_wrk);
199   app_session = session_alloc (vlib_get_thread_index ());
200   app_session->app_wrk_index = ctx->parent_app_index;
201   app_session->connection_index = ctx->tls_ctx_handle;
202   app_session->session_type =
203     session_type_from_proto_and_ip (TRANSPORT_PROTO_TLS, ctx->tcp_is_ip4);
204   app_session->opaque = tls_main.app_index;
205
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;
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");
216       tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
217     }
218
219   return 0;
220
221 failed:
222   tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
223   return cb_fn (ctx->parent_app_index, ctx->parent_app_api_context, 0,
224                 1 /* failed */ );
225 }
226
227 static inline void
228 tls_ctx_parse_handle (u32 ctx_handle, u32 * ctx_index, u32 * engine_type)
229 {
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);
248 }
249
250 static inline void
251 tls_ctx_free (tls_ctx_t * ctx)
252 {
253   vec_free (ctx->srv_hostname);
254   tls_vfts[ctx->tls_ctx_engine].ctx_free (ctx);
255 }
256
257 static inline tls_ctx_t *
258 tls_ctx_get (u32 ctx_handle)
259 {
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);
263 }
264
265 static inline tls_ctx_t *
266 tls_ctx_get_w_thread (u32 ctx_handle, u8 thread_index)
267 {
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);
271 }
272
273 static inline int
274 tls_ctx_init_server (tls_ctx_t * ctx)
275 {
276   return tls_vfts[ctx->tls_ctx_engine].ctx_init_server (ctx);
277 }
278
279 static inline int
280 tls_ctx_init_client (tls_ctx_t * ctx)
281 {
282   return tls_vfts[ctx->tls_ctx_engine].ctx_init_client (ctx);
283 }
284
285 static inline int
286 tls_ctx_write (tls_ctx_t * ctx, stream_session_t * app_session)
287 {
288   return tls_vfts[ctx->tls_ctx_engine].ctx_write (ctx, app_session);
289 }
290
291 static inline int
292 tls_ctx_read (tls_ctx_t * ctx, stream_session_t * tls_session)
293 {
294   return tls_vfts[ctx->tls_ctx_engine].ctx_read (ctx, tls_session);
295 }
296
297 static inline u8
298 tls_ctx_handshake_is_over (tls_ctx_t * ctx)
299 {
300   return tls_vfts[ctx->tls_ctx_engine].ctx_handshake_is_over (ctx);
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;
327   app_worker_t *app_wrk;
328   application_t *app;
329
330   ctx = tls_ctx_get (tls_session->opaque);
331   if (!tls_ctx_handshake_is_over (ctx))
332     {
333       stream_session_disconnect (tls_session);
334       return;
335     }
336   ctx->is_passive_close = 1;
337   app_wrk = app_worker_get (ctx->parent_app_index);
338   app = application_get (app_wrk->app_index);
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;
348   u32 ctx_handle;
349
350   tls_listener = listen_session_get (tls_session->listener_index);
351   lctx = tls_listener_ctx_get (tls_listener->opaque);
352
353   ctx_handle = tls_ctx_alloc (lctx->tls_ctx_engine);
354   ctx = tls_ctx_get (ctx_handle);
355   memcpy (ctx, lctx, sizeof (*lctx));
356   ctx->c_thread_index = vlib_get_thread_index ();
357   ctx->tls_ctx_handle = ctx_handle;
358   tls_session->session_state = SESSION_STATE_READY;
359   tls_session->opaque = ctx_handle;
360   ctx->tls_session_handle = session_handle (tls_session);
361   ctx->listener_ctx_index = tls_listener->opaque;
362
363   TLS_DBG (1, "Accept on listener %u new connection [%u]%x",
364            tls_listener->opaque, vlib_get_thread_index (), ctx_handle);
365
366   return tls_ctx_init_server (ctx);
367 }
368
369 int
370 tls_app_tx_callback (stream_session_t * app_session)
371 {
372   tls_ctx_t *ctx;
373   if (PREDICT_FALSE (app_session->session_state == SESSION_STATE_CLOSED))
374     return 0;
375   ctx = tls_ctx_get (app_session->connection_index);
376   tls_ctx_write (ctx, app_session);
377   return 0;
378 }
379
380 int
381 tls_app_rx_callback (stream_session_t * tls_session)
382 {
383   tls_ctx_t *ctx;
384
385   ctx = tls_ctx_get (tls_session->opaque);
386   tls_ctx_read (ctx, tls_session);
387   return 0;
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 {
394   tls_ctx_t *ho_ctx, *ctx;
395   u32 ctx_handle;
396
397   ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
398
399   if (is_fail)
400     {
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;
408       tls_ctx_half_open_reader_unlock ();
409       tls_ctx_half_open_free (ho_ctx_index);
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 */ );
414     }
415
416   ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine);
417   ctx = tls_ctx_get (ctx_handle);
418   clib_memcpy (ctx, ho_ctx, sizeof (*ctx));
419   tls_ctx_half_open_reader_unlock ();
420   tls_ctx_half_open_free (ho_ctx_index);
421
422   ctx->c_thread_index = vlib_get_thread_index ();
423   ctx->tls_ctx_handle = ctx_handle;
424
425   TLS_DBG (1, "TCP connect for %u returned %u. New connection [%u]%x",
426            ho_ctx_index, is_fail, vlib_get_thread_index (),
427            (ctx) ? ctx_handle : ~0);
428
429   ctx->tls_session_handle = session_handle (tls_session);
430   tls_session->opaque = ctx_handle;
431   tls_session->session_state = SESSION_STATE_READY;
432
433   return tls_ctx_init_client (ctx);
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 {
452   vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
453   session_endpoint_extended_t *sep;
454   tls_engine_type_t engine_type;
455   tls_main_t *tm = &tls_main;
456   app_worker_t *app_wrk;
457   clib_error_t *error;
458   application_t *app;
459   tls_ctx_t *ctx;
460   u32 ctx_index;
461
462   sep = (session_endpoint_extended_t *) tep;
463   app_wrk = app_worker_get (sep->app_wrk_index);
464   app = application_get (app_wrk->app_index);
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     }
471
472   ctx_index = tls_ctx_half_open_alloc ();
473   ctx = tls_ctx_half_open_get (ctx_index);
474   ctx->parent_app_index = sep->app_wrk_index;
475   ctx->parent_app_api_context = sep->opaque;
476   ctx->tcp_is_ip4 = sep->is_ip4;
477   if (sep->hostname)
478     {
479       ctx->srv_hostname = format (0, "%v", sep->hostname);
480       vec_terminate_c_string (ctx->srv_hostname);
481     }
482   tls_ctx_half_open_reader_unlock ();
483
484   app_worker_alloc_connects_segment_manager (app_wrk);
485   ctx->tls_ctx_engine = engine_type;
486
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);
493
494   TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
495   return 0;
496 }
497
498 void
499 tls_disconnect (u32 ctx_handle, u32 thread_index)
500 {
501   stream_session_t *tls_session, *app_session;
502   tls_ctx_t *ctx;
503
504   TLS_DBG (1, "Disconnecting %x", ctx_handle);
505
506   ctx = tls_ctx_get (ctx_handle);
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 {
524   app_worker_t *tls_app_wrk, *app_wrk;
525   tls_main_t *tm = &tls_main;
526   session_handle_t tls_handle;
527   session_endpoint_extended_t *sep;
528   stream_session_t *tls_listener;
529   stream_session_t *app_listener;
530   tls_engine_type_t engine_type;
531   application_t *app, *tls_app;
532   tls_ctx_t *lctx;
533   u32 lctx_index;
534
535   sep = (session_endpoint_extended_t *) tep;
536   app_wrk = app_worker_get (sep->app_wrk_index);
537   app = application_get (app_wrk->app_index);
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
545   lctx_index = tls_listener_ctx_alloc ();
546
547   /* TODO hide this by calling vnet_bind() */
548   tls_app = application_get (tm->app_index);
549   tls_app_wrk = application_get_default_worker (tls_app);
550   sep->transport_proto = TRANSPORT_PROTO_TCP;
551   if (app_worker_start_listen (tls_app_wrk, (session_endpoint_t *) sep,
552                                &tls_handle))
553     return ~0;
554
555   tls_listener = listen_session_get_from_handle (tls_handle);
556   tls_listener->opaque = lctx_index;
557
558   app_listener = listen_session_get (app_listener_index);
559
560   lctx = tls_listener_ctx_get (lctx_index);
561   lctx->parent_app_index = sep->app_wrk_index;
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;
565   lctx->tls_ctx_engine = engine_type;
566
567   tls_vfts[engine_type].ctx_start_listen (lctx);
568
569   TLS_DBG (1, "Started listening %d, engine type %d", lctx_index,
570            engine_type);
571   return lctx_index;
572 }
573
574 u32
575 tls_stop_listen (u32 lctx_index)
576 {
577   tls_main_t *tm = &tls_main;
578   tls_ctx_t *lctx;
579   tls_engine_type_t engine_type;
580
581   lctx = tls_listener_ctx_get (lctx_index);
582   app_worker_stop_listen (lctx->tls_session_handle, tm->app_index);
583   engine_type = lctx->tls_ctx_engine;
584   tls_vfts[engine_type].ctx_stop_listen (lctx);
585
586   tls_listener_ctx_free (lctx);
587   return 0;
588 }
589
590 transport_connection_t *
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 *
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
617   s = format (s, "[#%d][TLS] app %u child %u", child_ti,
618               ctx->parent_app_index, child_si);
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);
649   u32 listener_index, thread_index;
650
651   listen_session_parse_handle (ctx->tls_session_handle, &listener_index,
652                                &thread_index);
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,
672   .get_connection = tls_connection_get,
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
683 void
684 tls_register_engine (const tls_engine_vft_t * vft, tls_engine_type_t type)
685 {
686   vec_validate (tls_vfts, type);
687   tls_vfts[type] = *vft;
688 }
689
690 static clib_error_t *
691 tls_init (vlib_main_t * vm)
692 {
693   vlib_thread_main_t *vtm = vlib_get_thread_main ();
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;
698   u32 fifo_size = 64 << 10;
699   u32 num_threads;
700
701   num_threads = 1 /* main thread */  + vtm->n_threads;
702
703   memset (a, 0, sizeof (*a));
704   memset (options, 0, sizeof (options));
705
706   a->session_cb_vft = &tls_app_cb_vft;
707   a->api_client_index = APP_INVALID_INDEX;
708   a->options = options;
709   a->name = format (0, "tls");
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
722   if (!tm->ca_cert_path)
723     tm->ca_cert_path = TLS_CA_CERT_PATH;
724
725   tm->app_index = a->app_index;
726   clib_rwlock_init (&tm->half_open_rwlock);
727
728   vec_validate (tm->rx_bufs, num_threads - 1);
729   vec_validate (tm->tx_bufs, num_threads - 1);
730
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);
735   vec_free (a->name);
736   return 0;
737 }
738
739 VLIB_INIT_FUNCTION (tls_init);
740
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");
759
760 tls_main_t *
761 vnet_tls_get_main (void)
762 {
763   return &tls_main;
764 }
765
766 /*
767  * fd.io coding-style-patch-verification: ON
768  *
769  * Local Variables:
770  * eval: (c-set-style "gnu")
771  * End:
772  */