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