vppinfra: refactor *_will_expand() functions
[vpp.git] / src / vnet / tls / tls.c
1 /*
2  * Copyright (c) 2018-2019 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   28
26
27 void tls_disconnect (u32 ctx_handle, u32 thread_index);
28
29 void
30 tls_disconnect_transport (tls_ctx_t * ctx)
31 {
32   vnet_disconnect_args_t a = {
33     .handle = ctx->tls_session_handle,
34     .app_index = tls_main.app_index,
35   };
36
37   if (vnet_disconnect_session (&a))
38     clib_warning ("disconnect returned");
39 }
40
41 crypto_engine_type_t
42 tls_get_available_engine (void)
43 {
44   int i;
45   for (i = 0; i < vec_len (tls_vfts); i++)
46     {
47       if (tls_vfts[i].ctx_alloc)
48         return i;
49     }
50   return CRYPTO_ENGINE_NONE;
51 }
52
53 int
54 tls_add_vpp_q_rx_evt (session_t * s)
55 {
56   if (svm_fifo_set_event (s->rx_fifo))
57     session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_RX);
58   return 0;
59 }
60
61 int
62 tls_add_vpp_q_builtin_rx_evt (session_t * s)
63 {
64   if (svm_fifo_set_event (s->rx_fifo))
65     session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
66   return 0;
67 }
68
69 int
70 tls_add_vpp_q_tx_evt (session_t * s)
71 {
72   if (svm_fifo_set_event (s->tx_fifo))
73     session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
74   return 0;
75 }
76
77 static inline int
78 tls_add_app_q_evt (app_worker_t * app, session_t * app_session)
79 {
80   return app_worker_lock_and_send_event (app, app_session, SESSION_IO_EVT_RX);
81 }
82
83 u32
84 tls_listener_ctx_alloc (void)
85 {
86   tls_main_t *tm = &tls_main;
87   tls_ctx_t *ctx;
88
89   pool_get (tm->listener_ctx_pool, ctx);
90   clib_memset (ctx, 0, sizeof (*ctx));
91   return ctx - tm->listener_ctx_pool;
92 }
93
94 void
95 tls_listener_ctx_free (tls_ctx_t * ctx)
96 {
97   if (CLIB_DEBUG)
98     memset (ctx, 0xfb, sizeof (*ctx));
99   pool_put (tls_main.listener_ctx_pool, ctx);
100 }
101
102 tls_ctx_t *
103 tls_listener_ctx_get (u32 ctx_index)
104 {
105   return pool_elt_at_index (tls_main.listener_ctx_pool, ctx_index);
106 }
107
108 u32
109 tls_listener_ctx_index (tls_ctx_t * ctx)
110 {
111   return (ctx - tls_main.listener_ctx_pool);
112 }
113
114 u32
115 tls_ctx_half_open_alloc (void)
116 {
117   tls_main_t *tm = &tls_main;
118   u8 will_expand = pool_get_will_expand (tm->half_open_ctx_pool);
119   tls_ctx_t *ctx;
120   u32 ctx_index;
121
122   if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
123     {
124       clib_rwlock_writer_lock (&tm->half_open_rwlock);
125       pool_get_zero (tm->half_open_ctx_pool, ctx);
126       ctx->c_c_index = ctx - tm->half_open_ctx_pool;
127       ctx_index = ctx->c_c_index;
128       clib_rwlock_writer_unlock (&tm->half_open_rwlock);
129     }
130   else
131     {
132       /* reader lock assumption: only main thread will call pool_get */
133       clib_rwlock_reader_lock (&tm->half_open_rwlock);
134       pool_get_zero (tm->half_open_ctx_pool, ctx);
135       ctx->c_c_index = ctx - tm->half_open_ctx_pool;
136       ctx_index = ctx->c_c_index;
137       clib_rwlock_reader_unlock (&tm->half_open_rwlock);
138     }
139   return ctx_index;
140 }
141
142 void
143 tls_ctx_half_open_free (u32 ho_index)
144 {
145   tls_main_t *tm = &tls_main;
146   clib_rwlock_writer_lock (&tm->half_open_rwlock);
147   pool_put_index (tls_main.half_open_ctx_pool, ho_index);
148   clib_rwlock_writer_unlock (&tm->half_open_rwlock);
149 }
150
151 tls_ctx_t *
152 tls_ctx_half_open_get (u32 ctx_index)
153 {
154   tls_main_t *tm = &tls_main;
155   clib_rwlock_reader_lock (&tm->half_open_rwlock);
156   return pool_elt_at_index (tm->half_open_ctx_pool, ctx_index);
157 }
158
159 void
160 tls_ctx_half_open_reader_unlock ()
161 {
162   clib_rwlock_reader_unlock (&tls_main.half_open_rwlock);
163 }
164
165 u32
166 tls_ctx_half_open_index (tls_ctx_t * ctx)
167 {
168   return (ctx - tls_main.half_open_ctx_pool);
169 }
170
171 void
172 tls_notify_app_enqueue (tls_ctx_t * ctx, session_t * app_session)
173 {
174   app_worker_t *app_wrk;
175   app_wrk = app_worker_get_if_valid (app_session->app_wrk_index);
176   if (PREDICT_TRUE (app_wrk != 0))
177     tls_add_app_q_evt (app_wrk, app_session);
178 }
179
180 int
181 tls_notify_app_accept (tls_ctx_t * ctx)
182 {
183   session_t *app_listener, *app_session;
184   app_worker_t *app_wrk;
185   tls_ctx_t *lctx;
186   int rv;
187
188   lctx = tls_listener_ctx_get (ctx->listener_ctx_index);
189   app_listener = listen_session_get_from_handle (lctx->app_session_handle);
190
191   app_session = session_get (ctx->c_s_index, ctx->c_thread_index);
192   app_session->app_wrk_index = ctx->parent_app_wrk_index;
193   app_session->connection_index = ctx->tls_ctx_handle;
194   app_session->session_type = app_listener->session_type;
195   app_session->listener_handle = listen_session_get_handle (app_listener);
196   app_session->session_state = SESSION_STATE_ACCEPTING;
197
198   if ((rv = app_worker_init_accepted (app_session)))
199     {
200       TLS_DBG (1, "failed to allocate fifos");
201       session_free (app_session);
202       return rv;
203     }
204   ctx->app_session_handle = session_handle (app_session);
205   ctx->parent_app_wrk_index = app_session->app_wrk_index;
206   app_wrk = app_worker_get (app_session->app_wrk_index);
207   return app_worker_accept_notify (app_wrk, app_session);
208 }
209
210 int
211 tls_notify_app_connected (tls_ctx_t * ctx, session_error_t err)
212 {
213   session_t *app_session;
214   app_worker_t *app_wrk;
215
216   app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_index);
217   if (!app_wrk)
218     {
219       tls_disconnect_transport (ctx);
220       return -1;
221     }
222
223   if (err)
224     {
225       /* Free app session pre-allocated when transport was established */
226       if (ctx->tls_type == TRANSPORT_PROTO_TLS)
227         session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
228       ctx->no_app_session = 1;
229       goto send_reply;
230     }
231
232   /* For DTLS the app session is not preallocated because the underlying udp
233    * session might migrate to a different worker during the handshake */
234   if (ctx->tls_type == TRANSPORT_PROTO_DTLS)
235     {
236       session_type_t st;
237       /* Cleanup half-open session as we don't get notification from udp */
238       session_half_open_delete_notify (&ctx->connection);
239       app_session = session_alloc (ctx->c_thread_index);
240       app_session->session_state = SESSION_STATE_CREATED;
241       ctx->c_s_index = app_session->session_index;
242       st =
243         session_type_from_proto_and_ip (TRANSPORT_PROTO_DTLS, ctx->tcp_is_ip4);
244       app_session->session_type = st;
245       app_session->connection_index = ctx->tls_ctx_handle;
246     }
247   else
248     {
249       app_session = session_get (ctx->c_s_index, ctx->c_thread_index);
250     }
251
252   app_session->app_wrk_index = ctx->parent_app_wrk_index;
253
254   if ((err = app_worker_init_connected (app_wrk, app_session)))
255     goto failed;
256
257   app_session->session_state = SESSION_STATE_READY;
258   if (app_worker_connect_notify (app_wrk, app_session,
259                                  SESSION_E_NONE, ctx->parent_app_api_context))
260     {
261       TLS_DBG (1, "failed to notify app");
262       app_session->session_state = SESSION_STATE_CONNECTING;
263       tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
264       return -1;
265     }
266
267   ctx->app_session_handle = session_handle (app_session);
268
269   return 0;
270
271 failed:
272   ctx->no_app_session = 1;
273   tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
274 send_reply:
275   return app_worker_connect_notify (app_wrk, 0, err,
276                                     ctx->parent_app_api_context);
277 }
278
279 static inline void
280 tls_ctx_parse_handle (u32 ctx_handle, u32 * ctx_index, u32 * engine_type)
281 {
282   *ctx_index = ctx_handle & TLS_IDX_MASK;
283   *engine_type = ctx_handle >> TLS_ENGINE_TYPE_SHIFT;
284 }
285
286 static inline crypto_engine_type_t
287 tls_get_engine_type (crypto_engine_type_t requested,
288                      crypto_engine_type_t preferred)
289 {
290   if (requested != CRYPTO_ENGINE_NONE)
291     {
292       if (tls_vfts[requested].ctx_alloc)
293         return requested;
294       return CRYPTO_ENGINE_NONE;
295     }
296   if (!tls_vfts[preferred].ctx_alloc)
297     return tls_get_available_engine ();
298   return preferred;
299 }
300
301 static inline u32
302 tls_ctx_alloc (crypto_engine_type_t engine_type)
303 {
304   u32 ctx_index;
305   ctx_index = tls_vfts[engine_type].ctx_alloc ();
306   return (((u32) engine_type << TLS_ENGINE_TYPE_SHIFT) | ctx_index);
307 }
308
309 static inline u32
310 tls_ctx_alloc_w_thread (crypto_engine_type_t engine_type, u32 thread_index)
311 {
312   u32 ctx_index;
313   ctx_index = tls_vfts[engine_type].ctx_alloc_w_thread (thread_index);
314   return (((u32) engine_type << TLS_ENGINE_TYPE_SHIFT) | ctx_index);
315 }
316
317 static inline u32
318 tls_ctx_attach (crypto_engine_type_t engine_type, u32 thread_index, void *ctx)
319 {
320   u32 ctx_index;
321   ctx_index = tls_vfts[engine_type].ctx_attach (thread_index, ctx);
322   return (((u32) engine_type << TLS_ENGINE_TYPE_SHIFT) | ctx_index);
323 }
324
325 static inline void *
326 tls_ctx_detach (tls_ctx_t *ctx)
327 {
328   return tls_vfts[ctx->tls_ctx_engine].ctx_detach (ctx);
329 }
330
331 static inline tls_ctx_t *
332 tls_ctx_get (u32 ctx_handle)
333 {
334   u32 ctx_index, engine_type;
335   tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type);
336   return tls_vfts[engine_type].ctx_get (ctx_index);
337 }
338
339 static inline tls_ctx_t *
340 tls_ctx_get_w_thread (u32 ctx_handle, u8 thread_index)
341 {
342   u32 ctx_index, engine_type;
343   tls_ctx_parse_handle (ctx_handle, &ctx_index, &engine_type);
344   return tls_vfts[engine_type].ctx_get_w_thread (ctx_index, thread_index);
345 }
346
347 static inline int
348 tls_ctx_init_server (tls_ctx_t * ctx)
349 {
350   return tls_vfts[ctx->tls_ctx_engine].ctx_init_server (ctx);
351 }
352
353 static inline int
354 tls_ctx_init_client (tls_ctx_t * ctx)
355 {
356   return tls_vfts[ctx->tls_ctx_engine].ctx_init_client (ctx);
357 }
358
359 static inline int
360 tls_ctx_write (tls_ctx_t * ctx, session_t * app_session,
361                transport_send_params_t * sp)
362 {
363   u32 n_wrote;
364
365   sp->max_burst_size = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
366   n_wrote = tls_vfts[ctx->tls_ctx_engine].ctx_write (ctx, app_session, sp);
367   sp->bytes_dequeued = n_wrote;
368   return n_wrote > 0 ? clib_max (n_wrote / TRANSPORT_PACER_MIN_MSS, 1) : 0;
369 }
370
371 static inline int
372 tls_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
373 {
374   return tls_vfts[ctx->tls_ctx_engine].ctx_read (ctx, tls_session);
375 }
376
377 static inline int
378 tls_ctx_transport_close (tls_ctx_t * ctx)
379 {
380   return tls_vfts[ctx->tls_ctx_engine].ctx_transport_close (ctx);
381 }
382
383 static inline int
384 tls_ctx_app_close (tls_ctx_t * ctx)
385 {
386   return tls_vfts[ctx->tls_ctx_engine].ctx_app_close (ctx);
387 }
388
389 void
390 tls_ctx_free (tls_ctx_t * ctx)
391 {
392   tls_vfts[ctx->tls_ctx_engine].ctx_free (ctx);
393 }
394
395 u8
396 tls_ctx_handshake_is_over (tls_ctx_t * ctx)
397 {
398   return tls_vfts[ctx->tls_ctx_engine].ctx_handshake_is_over (ctx);
399 }
400
401 void
402 tls_notify_app_io_error (tls_ctx_t *ctx)
403 {
404   ASSERT (tls_ctx_handshake_is_over (ctx));
405
406   session_transport_reset_notify (&ctx->connection);
407   session_transport_closed_notify (&ctx->connection);
408   tls_disconnect_transport (ctx);
409 }
410
411 void
412 tls_session_reset_callback (session_t * s)
413 {
414   tls_ctx_t *ctx;
415   transport_connection_t *tc;
416   session_t *app_session;
417
418   ctx = tls_ctx_get (s->opaque);
419   ctx->is_passive_close = 1;
420   tc = &ctx->connection;
421   if (tls_ctx_handshake_is_over (ctx))
422     {
423       session_transport_reset_notify (tc);
424       session_transport_closed_notify (tc);
425       tls_disconnect_transport (ctx);
426     }
427   else
428     if ((app_session =
429          session_get_if_valid (ctx->c_s_index, ctx->c_thread_index)))
430     {
431       session_free (app_session);
432       ctx->c_s_index = SESSION_INVALID_INDEX;
433       tls_disconnect_transport (ctx);
434     }
435 }
436
437 static void
438 tls_session_cleanup_ho (session_t *s)
439 {
440   tls_ctx_t *ctx;
441   u32 ho_index;
442
443   /* session opaque stores the opaque passed on connect */
444   ho_index = s->opaque;
445   ctx = tls_ctx_half_open_get (ho_index);
446   session_half_open_delete_notify (&ctx->connection);
447   tls_ctx_half_open_reader_unlock ();
448   tls_ctx_half_open_free (ho_index);
449 }
450
451 int
452 tls_add_segment_callback (u32 client_index, u64 segment_handle)
453 {
454   /* No-op for builtin */
455   return 0;
456 }
457
458 int
459 tls_del_segment_callback (u32 client_index, u64 segment_handle)
460 {
461   return 0;
462 }
463
464 void
465 tls_session_disconnect_callback (session_t * tls_session)
466 {
467   tls_ctx_t *ctx;
468
469   TLS_DBG (1, "TCP disconnecting handle %x session %u", tls_session->opaque,
470            tls_session->session_index);
471
472   ASSERT (tls_session->thread_index == vlib_get_thread_index ()
473           || vlib_thread_is_main_w_barrier ());
474
475   ctx = tls_ctx_get_w_thread (tls_session->opaque, tls_session->thread_index);
476   ctx->is_passive_close = 1;
477   tls_ctx_transport_close (ctx);
478 }
479
480 int
481 tls_session_accept_callback (session_t * tls_session)
482 {
483   session_t *tls_listener, *app_session;
484   tls_ctx_t *lctx, *ctx;
485   u32 ctx_handle;
486
487   tls_listener =
488     listen_session_get_from_handle (tls_session->listener_handle);
489   lctx = tls_listener_ctx_get (tls_listener->opaque);
490
491   ctx_handle = tls_ctx_alloc (lctx->tls_ctx_engine);
492   ctx = tls_ctx_get (ctx_handle);
493   memcpy (ctx, lctx, sizeof (*lctx));
494   ctx->c_thread_index = vlib_get_thread_index ();
495   ctx->tls_ctx_handle = ctx_handle;
496   tls_session->session_state = SESSION_STATE_READY;
497   tls_session->opaque = ctx_handle;
498   ctx->tls_session_handle = session_handle (tls_session);
499   ctx->listener_ctx_index = tls_listener->opaque;
500   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
501   ctx->ckpair_index = lctx->ckpair_index;
502
503   /* Preallocate app session. Avoids allocating a session post handshake
504    * on tls_session rx and potentially invalidating the session pool */
505   app_session = session_alloc (ctx->c_thread_index);
506   app_session->session_state = SESSION_STATE_CREATED;
507   ctx->c_s_index = app_session->session_index;
508
509   TLS_DBG (1, "Accept on listener %u new connection [%u]%x",
510            tls_listener->opaque, vlib_get_thread_index (), ctx_handle);
511
512   return tls_ctx_init_server (ctx);
513 }
514
515 int
516 tls_app_rx_callback (session_t * tls_session)
517 {
518   tls_ctx_t *ctx;
519
520   /* DTLS session migrating, wait for next notification */
521   if (PREDICT_FALSE (tls_session->flags & SESSION_F_IS_MIGRATING))
522     return 0;
523
524   ctx = tls_ctx_get (tls_session->opaque);
525   if (PREDICT_FALSE (ctx->no_app_session))
526     {
527       TLS_DBG (1, "Local App closed");
528       return 0;
529     }
530   tls_ctx_read (ctx, tls_session);
531   return 0;
532 }
533
534 int
535 tls_app_tx_callback (session_t * tls_session)
536 {
537   tls_ctx_t *ctx;
538
539   ctx = tls_ctx_get (tls_session->opaque);
540   transport_connection_reschedule (&ctx->connection);
541
542   return 0;
543 }
544
545 int
546 tls_session_connected_cb (u32 tls_app_index, u32 ho_ctx_index,
547                           session_t *tls_session, session_error_t err)
548 {
549   session_t *app_session;
550   tls_ctx_t *ho_ctx, *ctx;
551   session_type_t st;
552   u32 ctx_handle;
553
554   ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
555
556   ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine);
557   ctx = tls_ctx_get (ctx_handle);
558   clib_memcpy_fast (ctx, ho_ctx, sizeof (*ctx));
559   /* Half-open freed on tcp half-open cleanup notification */
560   tls_ctx_half_open_reader_unlock ();
561
562   ctx->c_thread_index = vlib_get_thread_index ();
563   ctx->tls_ctx_handle = ctx_handle;
564   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
565
566   TLS_DBG (1, "TCP connect for %u returned %u. New connection [%u]%x",
567            ho_ctx_index, err, vlib_get_thread_index (),
568            (ctx) ? ctx_handle : ~0);
569
570   ctx->tls_session_handle = session_handle (tls_session);
571   tls_session->opaque = ctx_handle;
572   tls_session->session_state = SESSION_STATE_READY;
573
574   /* Preallocate app session. Avoids allocating a session post handshake
575    * on tls_session rx and potentially invalidating the session pool */
576   app_session = session_alloc (ctx->c_thread_index);
577   app_session->session_state = SESSION_STATE_CREATED;
578   ctx->c_s_index = app_session->session_index;
579   st = session_type_from_proto_and_ip (TRANSPORT_PROTO_TLS, ctx->tcp_is_ip4);
580   app_session->session_type = st;
581   app_session->connection_index = ctx->tls_ctx_handle;
582
583   return tls_ctx_init_client (ctx);
584 }
585
586 int
587 dtls_session_connected_cb (u32 app_wrk_index, u32 ctx_handle, session_t *us,
588                            session_error_t err)
589 {
590   tls_ctx_t *ctx;
591
592   ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ());
593
594   ctx->tls_session_handle = session_handle (us);
595   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
596   us->opaque = ctx_handle;
597
598   /* We don't preallocate the app session because the udp session might
599    * actually migrate to a different worker at the end of the handshake */
600
601   return tls_ctx_init_client (ctx);
602 }
603
604 int
605 tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index,
606                                 session_t *tls_session, session_error_t err)
607 {
608   if (err)
609     {
610       app_worker_t *app_wrk;
611       tls_ctx_t *ho_ctx;
612       u32 api_context;
613
614       ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
615       app_wrk = app_worker_get_if_valid (ho_ctx->parent_app_wrk_index);
616       if (app_wrk)
617         {
618           api_context = ho_ctx->parent_app_api_context;
619           app_worker_connect_notify (app_wrk, 0, err, api_context);
620         }
621       tls_ctx_half_open_reader_unlock ();
622
623       return 0;
624     }
625
626   if (session_get_transport_proto (tls_session) == TRANSPORT_PROTO_TCP)
627     return tls_session_connected_cb (tls_app_index, ho_ctx_index, tls_session,
628                                      err);
629   else
630     return dtls_session_connected_cb (tls_app_index, ho_ctx_index, tls_session,
631                                       err);
632 }
633
634 static void
635 tls_app_session_cleanup (session_t * s, session_cleanup_ntf_t ntf)
636 {
637   tls_ctx_t *ctx;
638
639   if (ntf == SESSION_CLEANUP_TRANSPORT)
640     {
641       /* Allow cleanup of tcp session */
642       if (s->session_state == SESSION_STATE_TRANSPORT_DELETED)
643         session_close (s);
644       return;
645     }
646
647   ctx = tls_ctx_get (s->opaque);
648   if (!ctx->no_app_session)
649     session_transport_delete_notify (&ctx->connection);
650   tls_ctx_free (ctx);
651 }
652
653 static void
654 dtls_migrate_ctx (void *arg)
655 {
656   tls_ctx_t *ctx = (tls_ctx_t *) arg;
657   u32 ctx_handle, thread_index;
658   session_t *us;
659
660   thread_index = session_thread_from_handle (ctx->tls_session_handle);
661   ASSERT (thread_index == vlib_get_thread_index ());
662
663   ctx_handle = tls_ctx_attach (ctx->tls_ctx_engine, thread_index, ctx);
664   ctx = tls_ctx_get_w_thread (ctx_handle, thread_index);
665   ctx->tls_ctx_handle = ctx_handle;
666
667   us = session_get_from_handle (ctx->tls_session_handle);
668   us->opaque = ctx_handle;
669   us->flags &= ~SESSION_F_IS_MIGRATING;
670
671   /* Probably the app detached while the session was migrating. Cleanup */
672   if (session_half_open_migrated_notify (&ctx->connection))
673     {
674       ctx->no_app_session = 1;
675       tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
676       return;
677     }
678
679   if (svm_fifo_max_dequeue (us->tx_fifo))
680     session_send_io_evt_to_thread (us->tx_fifo, SESSION_IO_EVT_TX);
681 }
682
683 static void
684 dtls_session_migrate_callback (session_t *us, session_handle_t new_sh)
685 {
686   u32 new_thread = session_thread_from_handle (new_sh);
687   tls_ctx_t *ctx, *cloned_ctx;
688
689   /* Migrate dtls context to new thread */
690   ctx = tls_ctx_get_w_thread (us->opaque, us->thread_index);
691   ctx->tls_session_handle = new_sh;
692   cloned_ctx = tls_ctx_detach (ctx);
693   ctx->is_migrated = 1;
694   session_half_open_migrate_notify (&ctx->connection);
695
696   session_send_rpc_evt_to_thread (new_thread, dtls_migrate_ctx,
697                                   (void *) cloned_ctx);
698
699   tls_ctx_free (ctx);
700 }
701
702 static session_cb_vft_t tls_app_cb_vft = {
703   .session_accept_callback = tls_session_accept_callback,
704   .session_disconnect_callback = tls_session_disconnect_callback,
705   .session_connected_callback = tls_session_connected_callback,
706   .session_reset_callback = tls_session_reset_callback,
707   .half_open_cleanup_callback = tls_session_cleanup_ho,
708   .add_segment_callback = tls_add_segment_callback,
709   .del_segment_callback = tls_del_segment_callback,
710   .builtin_app_rx_callback = tls_app_rx_callback,
711   .builtin_app_tx_callback = tls_app_tx_callback,
712   .session_migrate_callback = dtls_session_migrate_callback,
713   .session_cleanup_callback = tls_app_session_cleanup,
714 };
715
716 int
717 tls_connect (transport_endpoint_cfg_t * tep)
718 {
719   vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
720   transport_endpt_crypto_cfg_t *ccfg;
721   crypto_engine_type_t engine_type;
722   session_endpoint_cfg_t *sep;
723   tls_main_t *tm = &tls_main;
724   app_worker_t *app_wrk;
725   application_t *app;
726   tls_ctx_t *ctx;
727   u32 ctx_index;
728   int rv;
729
730   sep = (session_endpoint_cfg_t *) tep;
731   if (!sep->ext_cfg)
732     return SESSION_E_NOEXTCFG;
733
734   app_wrk = app_worker_get (sep->app_wrk_index);
735   app = application_get (app_wrk->app_index);
736
737   ccfg = &sep->ext_cfg->crypto;
738   engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
739   if (engine_type == CRYPTO_ENGINE_NONE)
740     {
741       clib_warning ("No tls engine_type available");
742       return SESSION_E_NOCRYPTOENG;
743     }
744
745   ctx_index = tls_ctx_half_open_alloc ();
746   ctx = tls_ctx_half_open_get (ctx_index);
747   ctx->parent_app_wrk_index = sep->app_wrk_index;
748   ctx->parent_app_api_context = sep->opaque;
749   ctx->tcp_is_ip4 = sep->is_ip4;
750   ctx->tls_type = sep->transport_proto;
751   ctx->ckpair_index = ccfg->ckpair_index;
752   ctx->c_proto = TRANSPORT_PROTO_TLS;
753   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
754   if (ccfg->hostname[0])
755     {
756       ctx->srv_hostname = format (0, "%s", ccfg->hostname);
757       vec_terminate_c_string (ctx->srv_hostname);
758     }
759   tls_ctx_half_open_reader_unlock ();
760
761   ctx->tls_ctx_engine = engine_type;
762
763   clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
764   cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
765   cargs->app_index = tm->app_index;
766   cargs->api_context = ctx_index;
767   cargs->sep_ext.ns_index = app->ns_index;
768   if ((rv = vnet_connect (cargs)))
769     return rv;
770
771   /* Track half-open tcp session in case we need to clean it up */
772   ctx->tls_session_handle = cargs->sh;
773
774   TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
775   return ctx_index;
776 }
777
778 void
779 tls_disconnect (u32 ctx_handle, u32 thread_index)
780 {
781   tls_ctx_t *ctx;
782
783   TLS_DBG (1, "Disconnecting %x", ctx_handle);
784
785   ctx = tls_ctx_get (ctx_handle);
786   tls_ctx_app_close (ctx);
787 }
788
789 u32
790 tls_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
791 {
792   vnet_listen_args_t _bargs, *args = &_bargs;
793   transport_endpt_crypto_cfg_t *ccfg;
794   app_worker_t *app_wrk;
795   tls_main_t *tm = &tls_main;
796   session_handle_t tls_al_handle;
797   session_endpoint_cfg_t *sep;
798   session_t *tls_listener;
799   session_t *app_listener;
800   crypto_engine_type_t engine_type;
801   application_t *app;
802   app_listener_t *al;
803   tls_ctx_t *lctx;
804   u32 lctx_index;
805   int rv;
806
807   sep = (session_endpoint_cfg_t *) tep;
808   if (!sep->ext_cfg)
809     return SESSION_E_NOEXTCFG;
810
811   app_wrk = app_worker_get (sep->app_wrk_index);
812   app = application_get (app_wrk->app_index);
813
814   ccfg = &sep->ext_cfg->crypto;
815   engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
816   if (engine_type == CRYPTO_ENGINE_NONE)
817     {
818       clib_warning ("No tls engine_type available");
819       return SESSION_E_NOCRYPTOENG;
820     }
821
822   clib_memset (args, 0, sizeof (*args));
823   args->app_index = tm->app_index;
824   args->sep_ext = *sep;
825   args->sep_ext.ns_index = app->ns_index;
826   args->sep_ext.transport_proto = TRANSPORT_PROTO_TCP;
827   if (sep->transport_proto == TRANSPORT_PROTO_DTLS)
828     {
829       args->sep_ext.transport_proto = TRANSPORT_PROTO_UDP;
830       args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
831     }
832   if ((rv = vnet_listen (args)))
833     return rv;
834
835   lctx_index = tls_listener_ctx_alloc ();
836   tls_al_handle = args->handle;
837   al = app_listener_get_w_handle (tls_al_handle);
838   tls_listener = app_listener_get_session (al);
839   tls_listener->opaque = lctx_index;
840
841   app_listener = listen_session_get (app_listener_index);
842
843   lctx = tls_listener_ctx_get (lctx_index);
844   lctx->parent_app_wrk_index = sep->app_wrk_index;
845   lctx->tls_session_handle = tls_al_handle;
846   lctx->app_session_handle = listen_session_get_handle (app_listener);
847   lctx->tcp_is_ip4 = sep->is_ip4;
848   lctx->tls_ctx_engine = engine_type;
849   lctx->tls_type = sep->transport_proto;
850   lctx->ckpair_index = ccfg->ckpair_index;
851   lctx->c_s_index = app_listener_index;
852   lctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
853
854   if (tls_vfts[engine_type].ctx_start_listen (lctx))
855     {
856       vnet_unlisten_args_t a = {
857         .handle = lctx->tls_session_handle,
858         .app_index = tls_main.app_index,
859         .wrk_map_index = 0
860       };
861       if ((vnet_unlisten (&a)))
862         clib_warning ("unlisten returned");
863       tls_listener_ctx_free (lctx);
864       lctx_index = SESSION_INVALID_INDEX;
865     }
866
867   TLS_DBG (1, "Started listening %d, engine type %d", lctx_index,
868            engine_type);
869   return lctx_index;
870 }
871
872 u32
873 tls_stop_listen (u32 lctx_index)
874 {
875   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
876   crypto_engine_type_t engine_type;
877   transport_connection_t *lc;
878   tls_ctx_t *lctx;
879   session_t *ls;
880   int rv;
881
882   lctx = tls_listener_ctx_get (lctx_index);
883
884   /* Cleanup listener from session lookup table */
885   ls = session_get_from_handle (lctx->tls_session_handle);
886   lc = session_get_transport (ls);
887
888   sep.fib_index = lc->fib_index;
889   sep.port = lc->lcl_port;
890   sep.is_ip4 = lc->is_ip4;
891   sep.transport_proto = lctx->tls_type;
892   clib_memcpy (&sep.ip, &lc->lcl_ip, sizeof (lc->lcl_ip));
893   session_lookup_del_session_endpoint2 (&sep);
894
895   vnet_unlisten_args_t a = {
896     .handle = lctx->tls_session_handle,
897     .app_index = tls_main.app_index,
898     .wrk_map_index = 0          /* default wrk */
899   };
900   if ((rv = vnet_unlisten (&a)))
901     clib_warning ("unlisten returned %d", rv);
902
903   engine_type = lctx->tls_ctx_engine;
904   tls_vfts[engine_type].ctx_stop_listen (lctx);
905
906   tls_listener_ctx_free (lctx);
907   return 0;
908 }
909
910 transport_connection_t *
911 tls_connection_get (u32 ctx_index, u32 thread_index)
912 {
913   tls_ctx_t *ctx;
914   ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
915   return &ctx->connection;
916 }
917
918 transport_connection_t *
919 tls_listener_get (u32 listener_index)
920 {
921   tls_ctx_t *ctx;
922   ctx = tls_listener_ctx_get (listener_index);
923   return &ctx->connection;
924 }
925
926 static transport_connection_t *
927 tls_half_open_get (u32 ho_index)
928 {
929   tls_main_t *tm = &tls_main;
930   tls_ctx_t *ctx;
931   ctx = tls_ctx_half_open_get (ho_index);
932   clib_rwlock_reader_unlock (&tm->half_open_rwlock);
933   return &ctx->connection;
934 }
935
936 static void
937 tls_cleanup_ho (u32 ho_index)
938 {
939   tls_main_t *tm = &tls_main;
940   session_handle_t tcp_sh;
941   tls_ctx_t *ctx;
942
943   ctx = tls_ctx_half_open_get (ho_index);
944   tcp_sh = ctx->tls_session_handle;
945   clib_rwlock_reader_unlock (&tm->half_open_rwlock);
946   session_cleanup_half_open (tcp_sh);
947   tls_ctx_half_open_free (ho_index);
948 }
949
950 int
951 tls_custom_tx_callback (void *session, transport_send_params_t * sp)
952 {
953   session_t *app_session = (session_t *) session;
954   tls_ctx_t *ctx;
955
956   if (PREDICT_FALSE (app_session->session_state
957                      >= SESSION_STATE_TRANSPORT_CLOSED))
958     return 0;
959
960   ctx = tls_ctx_get (app_session->connection_index);
961   return tls_ctx_write (ctx, app_session, sp);
962 }
963
964 u8 *
965 format_tls_ctx (u8 * s, va_list * args)
966 {
967   u32 tcp_si, tcp_ti, ctx_index, ctx_engine;
968   tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *);
969   char *proto;
970
971   proto = ctx->tls_type == TRANSPORT_PROTO_TLS ? "TLS" : "DTLS";
972   session_parse_handle (ctx->tls_session_handle, &tcp_si, &tcp_ti);
973   tls_ctx_parse_handle (ctx->tls_ctx_handle, &ctx_index, &ctx_engine);
974   s =
975     format (s, "[%d:%d][%s] app_wrk %u index %u engine %u ts %d:%d",
976             ctx->c_thread_index, ctx->c_s_index, proto,
977             ctx->parent_app_wrk_index, ctx_index, ctx_engine, tcp_ti, tcp_si);
978
979   return s;
980 }
981
982 static u8 *
983 format_tls_listener_ctx (u8 * s, va_list * args)
984 {
985   session_t *tls_listener;
986   app_listener_t *al;
987   tls_ctx_t *ctx;
988   char *proto;
989
990   ctx = va_arg (*args, tls_ctx_t *);
991
992   proto = ctx->tls_type == TRANSPORT_PROTO_TLS ? "TLS" : "DTLS";
993   al = app_listener_get_w_handle (ctx->tls_session_handle);
994   tls_listener = app_listener_get_session (al);
995   s = format (s, "[%d:%d][%s] app_wrk %u engine %u ts %d:%d",
996               ctx->c_thread_index, ctx->c_s_index, proto,
997               ctx->parent_app_wrk_index, ctx->tls_ctx_engine,
998               tls_listener->thread_index, tls_listener->session_index);
999
1000   return s;
1001 }
1002
1003 static u8 *
1004 format_tls_ctx_state (u8 * s, va_list * args)
1005 {
1006   tls_ctx_t *ctx;
1007   session_t *ts;
1008
1009   ctx = va_arg (*args, tls_ctx_t *);
1010   ts = session_get (ctx->c_s_index, ctx->c_thread_index);
1011   if (ts->session_state == SESSION_STATE_LISTENING)
1012     s = format (s, "%s", "LISTEN");
1013   else
1014     {
1015       if (ts->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
1016         s = format (s, "%s", "CLOSED");
1017       else if (ts->session_state == SESSION_STATE_APP_CLOSED)
1018         s = format (s, "%s", "APP-CLOSED");
1019       else if (ts->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1020         s = format (s, "%s", "CLOSING");
1021       else if (tls_ctx_handshake_is_over (ctx))
1022         s = format (s, "%s", "ESTABLISHED");
1023       else
1024         s = format (s, "%s", "HANDSHAKE");
1025     }
1026
1027   return s;
1028 }
1029
1030 u8 *
1031 format_tls_connection (u8 * s, va_list * args)
1032 {
1033   u32 ctx_index = va_arg (*args, u32);
1034   u32 thread_index = va_arg (*args, u32);
1035   u32 verbose = va_arg (*args, u32);
1036   tls_ctx_t *ctx;
1037
1038   ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
1039   if (!ctx)
1040     return s;
1041
1042   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_ctx, ctx);
1043   if (verbose)
1044     {
1045       s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state,
1046                   ctx);
1047       if (verbose > 1)
1048         s = format (s, "\n");
1049     }
1050   return s;
1051 }
1052
1053 u8 *
1054 format_tls_listener (u8 * s, va_list * args)
1055 {
1056   u32 tc_index = va_arg (*args, u32);
1057   u32 __clib_unused thread_index = va_arg (*args, u32);
1058   u32 verbose = va_arg (*args, u32);
1059   tls_ctx_t *ctx = tls_listener_ctx_get (tc_index);
1060
1061   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_listener_ctx, ctx);
1062   if (verbose)
1063     s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state, ctx);
1064   return s;
1065 }
1066
1067 u8 *
1068 format_tls_half_open (u8 * s, va_list * args)
1069 {
1070   u32 ho_index = va_arg (*args, u32);
1071   u32 __clib_unused thread_index = va_arg (*args, u32);
1072   session_t *tcp_ho;
1073   tls_ctx_t *ho_ctx;
1074
1075   ho_ctx = tls_ctx_half_open_get (ho_index);
1076
1077   tcp_ho = session_get_from_handle (ho_ctx->tls_session_handle);
1078   s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u ts %d:%d",
1079               ho_ctx->c_thread_index, ho_ctx->c_s_index, "TLS",
1080               ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
1081               tcp_ho->thread_index, tcp_ho->session_index);
1082
1083   tls_ctx_half_open_reader_unlock ();
1084   return s;
1085 }
1086
1087 static void
1088 tls_transport_endpoint_get (u32 ctx_handle, u32 thread_index,
1089                             transport_endpoint_t * tep, u8 is_lcl)
1090 {
1091   tls_ctx_t *ctx = tls_ctx_get_w_thread (ctx_handle, thread_index);
1092   session_t *tcp_session;
1093
1094   tcp_session = session_get_from_handle (ctx->tls_session_handle);
1095   session_get_endpoint (tcp_session, tep, is_lcl);
1096 }
1097
1098 static void
1099 tls_transport_listener_endpoint_get (u32 ctx_handle,
1100                                      transport_endpoint_t * tep, u8 is_lcl)
1101 {
1102   session_t *tls_listener;
1103   app_listener_t *al;
1104   tls_ctx_t *ctx = tls_listener_ctx_get (ctx_handle);
1105
1106   al = app_listener_get_w_handle (ctx->tls_session_handle);
1107   tls_listener = app_listener_get_session (al);
1108   session_get_endpoint (tls_listener, tep, is_lcl);
1109 }
1110
1111 static clib_error_t *
1112 tls_enable (vlib_main_t * vm, u8 is_en)
1113 {
1114   vnet_app_detach_args_t _da, *da = &_da;
1115   vnet_app_attach_args_t _a, *a = &_a;
1116   u64 options[APP_OPTIONS_N_OPTIONS];
1117   tls_main_t *tm = &tls_main;
1118   u32 fifo_size = 128 << 12;
1119
1120   if (!is_en)
1121     {
1122       da->app_index = tm->app_index;
1123       da->api_client_index = APP_INVALID_INDEX;
1124       vnet_application_detach (da);
1125       return 0;
1126     }
1127
1128   fifo_size = tm->fifo_size ? tm->fifo_size : fifo_size;
1129
1130   clib_memset (a, 0, sizeof (*a));
1131   clib_memset (options, 0, sizeof (options));
1132
1133   a->session_cb_vft = &tls_app_cb_vft;
1134   a->api_client_index = APP_INVALID_INDEX;
1135   a->options = options;
1136   a->name = format (0, "tls");
1137   a->options[APP_OPTIONS_SEGMENT_SIZE] = tm->first_seg_size;
1138   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = tm->add_seg_size;
1139   a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
1140   a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
1141   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1142   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1143   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1144
1145   if (vnet_application_attach (a))
1146     {
1147       clib_warning ("failed to attach tls app");
1148       return clib_error_return (0, "failed to attach tls app");
1149     }
1150
1151   tm->app_index = a->app_index;
1152   vec_free (a->name);
1153
1154   return 0;
1155 }
1156
1157 static const transport_proto_vft_t tls_proto = {
1158   .enable = tls_enable,
1159   .connect = tls_connect,
1160   .close = tls_disconnect,
1161   .start_listen = tls_start_listen,
1162   .stop_listen = tls_stop_listen,
1163   .get_connection = tls_connection_get,
1164   .get_listener = tls_listener_get,
1165   .get_half_open = tls_half_open_get,
1166   .cleanup_ho = tls_cleanup_ho,
1167   .custom_tx = tls_custom_tx_callback,
1168   .format_connection = format_tls_connection,
1169   .format_half_open = format_tls_half_open,
1170   .format_listener = format_tls_listener,
1171   .get_transport_endpoint = tls_transport_endpoint_get,
1172   .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
1173   .transport_options = {
1174     .name = "tls",
1175     .short_name = "J",
1176     .tx_type = TRANSPORT_TX_INTERNAL,
1177     .service_type = TRANSPORT_SERVICE_VC,
1178   },
1179 };
1180
1181 int
1182 dtls_connect (transport_endpoint_cfg_t *tep)
1183 {
1184   vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
1185   transport_endpt_crypto_cfg_t *ccfg;
1186   crypto_engine_type_t engine_type;
1187   session_endpoint_cfg_t *sep;
1188   tls_main_t *tm = &tls_main;
1189   app_worker_t *app_wrk;
1190   application_t *app;
1191   tls_ctx_t *ctx;
1192   u32 ctx_handle;
1193   int rv;
1194
1195   sep = (session_endpoint_cfg_t *) tep;
1196   if (!sep->ext_cfg)
1197     return -1;
1198
1199   app_wrk = app_worker_get (sep->app_wrk_index);
1200   app = application_get (app_wrk->app_index);
1201
1202   ccfg = &sep->ext_cfg->crypto;
1203   engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
1204   if (engine_type == CRYPTO_ENGINE_NONE)
1205     {
1206       clib_warning ("No tls engine_type available");
1207       return -1;
1208     }
1209
1210   ctx_handle = tls_ctx_alloc_w_thread (engine_type, transport_cl_thread ());
1211   ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ());
1212   ctx->parent_app_wrk_index = sep->app_wrk_index;
1213   ctx->parent_app_api_context = sep->opaque;
1214   ctx->tcp_is_ip4 = sep->is_ip4;
1215   ctx->ckpair_index = ccfg->ckpair_index;
1216   ctx->tls_type = sep->transport_proto;
1217   ctx->tls_ctx_handle = ctx_handle;
1218   ctx->c_proto = TRANSPORT_PROTO_DTLS;
1219   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1220   if (ccfg->hostname[0])
1221     {
1222       ctx->srv_hostname = format (0, "%s", ccfg->hostname);
1223       vec_terminate_c_string (ctx->srv_hostname);
1224     }
1225
1226   ctx->tls_ctx_engine = engine_type;
1227
1228   clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
1229   cargs->sep.transport_proto = TRANSPORT_PROTO_UDP;
1230   cargs->app_index = tm->app_index;
1231   cargs->api_context = ctx_handle;
1232   cargs->sep_ext.ns_index = app->ns_index;
1233   cargs->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
1234   if ((rv = vnet_connect (cargs)))
1235     return rv;
1236
1237   TLS_DBG (1, "New DTLS connect request %x engine %d", ctx_handle,
1238            engine_type);
1239   return ctx_handle;
1240 }
1241
1242 static transport_connection_t *
1243 dtls_half_open_get (u32 ho_index)
1244 {
1245   tls_ctx_t *ho_ctx;
1246   ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
1247   return &ho_ctx->connection;
1248 }
1249
1250 static void
1251 dtls_cleanup_callback (u32 ctx_index, u32 thread_index)
1252 {
1253   /* No op */
1254 }
1255
1256 static void
1257 dtls_cleanup_ho (u32 ho_index)
1258 {
1259   tls_ctx_t *ctx;
1260   ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
1261   tls_ctx_free (ctx);
1262 }
1263
1264 u8 *
1265 format_dtls_half_open (u8 *s, va_list *args)
1266 {
1267   u32 ho_index = va_arg (*args, u32);
1268   u32 __clib_unused thread_index = va_arg (*args, u32);
1269   tls_ctx_t *ho_ctx;
1270   session_t *us;
1271
1272   ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
1273
1274   us = session_get_from_handle (ho_ctx->tls_session_handle);
1275   s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u us %d:%d",
1276               ho_ctx->c_thread_index, ho_ctx->c_s_index, "DTLS",
1277               ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
1278               us->thread_index, us->session_index);
1279
1280   return s;
1281 }
1282
1283 static const transport_proto_vft_t dtls_proto = {
1284   .enable = 0,
1285   .connect = dtls_connect,
1286   .close = tls_disconnect,
1287   .start_listen = tls_start_listen,
1288   .stop_listen = tls_stop_listen,
1289   .get_connection = tls_connection_get,
1290   .get_listener = tls_listener_get,
1291   .get_half_open = dtls_half_open_get,
1292   .custom_tx = tls_custom_tx_callback,
1293   .cleanup = dtls_cleanup_callback,
1294   .cleanup_ho = dtls_cleanup_ho,
1295   .format_connection = format_tls_connection,
1296   .format_half_open = format_dtls_half_open,
1297   .format_listener = format_tls_listener,
1298   .get_transport_endpoint = tls_transport_endpoint_get,
1299   .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
1300   .transport_options = {
1301     .name = "dtls",
1302     .short_name = "D",
1303     .tx_type = TRANSPORT_TX_INTERNAL,
1304     .service_type = TRANSPORT_SERVICE_VC,
1305   },
1306 };
1307
1308 void
1309 tls_register_engine (const tls_engine_vft_t * vft, crypto_engine_type_t type)
1310 {
1311   vec_validate (tls_vfts, type);
1312   tls_vfts[type] = *vft;
1313 }
1314
1315 static clib_error_t *
1316 tls_init (vlib_main_t * vm)
1317 {
1318   vlib_thread_main_t *vtm = vlib_get_thread_main ();
1319   tls_main_t *tm = &tls_main;
1320   u32 num_threads;
1321
1322   num_threads = 1 /* main thread */  + vtm->n_threads;
1323
1324   if (!tm->ca_cert_path)
1325     tm->ca_cert_path = TLS_CA_CERT_PATH;
1326
1327   clib_rwlock_init (&tm->half_open_rwlock);
1328
1329   vec_validate (tm->rx_bufs, num_threads - 1);
1330   vec_validate (tm->tx_bufs, num_threads - 1);
1331
1332   tm->first_seg_size = 32 << 20;
1333   tm->add_seg_size = 256 << 20;
1334
1335   transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
1336                                FIB_PROTOCOL_IP4, ~0);
1337   transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
1338                                FIB_PROTOCOL_IP6, ~0);
1339
1340   transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
1341                                FIB_PROTOCOL_IP4, ~0);
1342   transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
1343                                FIB_PROTOCOL_IP6, ~0);
1344   return 0;
1345 }
1346
1347 VLIB_INIT_FUNCTION (tls_init);
1348
1349 static clib_error_t *
1350 tls_config_fn (vlib_main_t * vm, unformat_input_t * input)
1351 {
1352   tls_main_t *tm = &tls_main;
1353   uword tmp;
1354   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1355     {
1356       if (unformat (input, "use-test-cert-in-ca"))
1357         tm->use_test_cert_in_ca = 1;
1358       else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path))
1359         ;
1360       else if (unformat (input, "first-segment-size %U", unformat_memory_size,
1361                          &tm->first_seg_size))
1362         ;
1363       else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1364                          &tm->add_seg_size))
1365         ;
1366       else if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
1367         {
1368           if (tmp >= 0x100000000ULL)
1369             {
1370               return clib_error_return
1371                 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
1372             }
1373           tm->fifo_size = tmp;
1374         }
1375       else
1376         return clib_error_return (0, "unknown input `%U'",
1377                                   format_unformat_error, input);
1378     }
1379   return 0;
1380 }
1381
1382 VLIB_EARLY_CONFIG_FUNCTION (tls_config_fn, "tls");
1383
1384 tls_main_t *
1385 vnet_tls_get_main (void)
1386 {
1387   return &tls_main;
1388 }
1389
1390 /*
1391  * fd.io coding-style-patch-verification: ON
1392  *
1393  * Local Variables:
1394  * eval: (c-set-style "gnu")
1395  * End:
1396  */