srtp: basic implementation based on libsrtp2
[vpp.git] / src / plugins / srtp / srtp.c
1 /*
2  * Copyright (c) 2021 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 <srtp/srtp.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/session.h>
19
20 static srtp_main_t srtp_main;
21
22 static void srtp_disconnect (u32 ctx_handle, u32 thread_index);
23 static void srtp_disconnect_transport (srtp_tc_t *ctx);
24
25 static inline u32
26 srtp_ctx_alloc_w_thread (u32 thread_index)
27 {
28   srtp_tc_t *ctx;
29   pool_get_zero (srtp_main.ctx_pool[thread_index], ctx);
30   ctx->c_thread_index = thread_index;
31   ctx->srtp_ctx_handle = ctx - srtp_main.ctx_pool[thread_index];
32   ctx->app_session_handle = SESSION_INVALID_HANDLE;
33   return ctx->srtp_ctx_handle;
34 }
35
36 static inline srtp_tc_t *
37 srtp_ctx_get_w_thread (u32 ctx_index, u32 thread_index)
38 {
39   return pool_elt_at_index (srtp_main.ctx_pool[thread_index], ctx_index);
40 }
41
42 static void
43 srtp_init_policy (srtp_tc_t *ctx, transport_endpt_cfg_srtp_t *cfg)
44 {
45   transport_endpt_cfg_srtp_policy_t *sp_cfg;
46   srtp_policy_t *sp;
47   int i;
48
49   for (i = 0; i < 2; i++)
50     {
51       sp = &ctx->srtp_policy[i];
52       sp_cfg = &cfg->policies[i];
53
54       srtp_crypto_policy_set_rtp_default (&sp->rtp);
55       srtp_crypto_policy_set_rtcp_default (&sp->rtcp);
56       sp->ssrc.type = sp_cfg->ssrc_type;
57       sp->ssrc.value = sp_cfg->ssrc_value;
58       sp->key = clib_mem_alloc (sp_cfg->key_len);
59       clib_memcpy (sp->key, sp_cfg->key, sp_cfg->key_len);
60       sp->ekt = 0;
61       sp->next = i < 1 ? &ctx->srtp_policy[i + 1] : 0;
62       sp->window_size = sp_cfg->window_size;
63       sp->allow_repeat_tx = sp_cfg->allow_repeat_tx;
64     }
65 }
66
67 static void
68 srtp_ctx_free_policy (srtp_tc_t *ctx)
69 {
70   clib_mem_free (ctx->srtp_policy[0].key);
71   clib_mem_free (ctx->srtp_policy[1].key);
72 }
73
74 void
75 srtp_ctx_free (srtp_tc_t *ctx)
76 {
77   if (!ctx->is_migrated)
78     srtp_ctx_free_policy (ctx);
79   pool_put (srtp_main.ctx_pool[ctx->c_thread_index], ctx);
80 }
81
82 static inline u32
83 srtp_ctx_attach (u32 thread_index, void *ctx_ptr)
84 {
85   srtp_tc_t *ctx;
86
87   pool_get (srtp_main.ctx_pool[thread_index], ctx);
88   clib_memcpy (ctx, ctx_ptr, sizeof (*ctx));
89
90   ctx->c_thread_index = thread_index;
91   ctx->srtp_ctx_handle = ctx - srtp_main.ctx_pool[thread_index];
92   return 0;
93 }
94
95 static inline void *
96 srtp_ctx_detach (srtp_tc_t *ctx)
97 {
98   srtp_tc_t *sc;
99
100   sc = clib_mem_alloc (sizeof (*sc));
101   clib_memcpy (sc, ctx, sizeof (*sc));
102
103   return sc;
104 }
105
106 u32
107 srtp_listener_ctx_alloc (void)
108 {
109   srtp_main_t *sm = &srtp_main;
110   srtp_tc_t *ctx;
111
112   pool_get_zero (sm->listener_ctx_pool, ctx);
113   return ctx - sm->listener_ctx_pool;
114 }
115
116 void
117 srtp_listener_ctx_free (srtp_tc_t *ctx)
118 {
119   srtp_ctx_free_policy (ctx);
120   if (CLIB_DEBUG)
121     clib_memset (ctx, 0xfb, sizeof (*ctx));
122   pool_put (srtp_main.listener_ctx_pool, ctx);
123 }
124
125 srtp_tc_t *
126 srtp_listener_ctx_get (u32 ctx_index)
127 {
128   return pool_elt_at_index (srtp_main.listener_ctx_pool, ctx_index);
129 }
130
131 u32
132 srtp_listener_ctx_index (srtp_tc_t *ctx)
133 {
134   return (ctx - srtp_main.listener_ctx_pool);
135 }
136
137 static int
138 srtp_ctx_init_client (srtp_tc_t *ctx)
139 {
140   session_t *app_session;
141   app_worker_t *app_wrk;
142   session_error_t err;
143
144   if (srtp_create (&ctx->srtp_ctx, &ctx->srtp_policy[0]) != srtp_err_status_ok)
145     {
146       SRTP_DBG (0, "failed to init srtp ctx");
147       return -1;
148     }
149
150   app_wrk = app_worker_get (ctx->parent_app_wrk_index);
151   app_session = session_get (ctx->c_s_index, ctx->c_thread_index);
152   app_session->app_wrk_index = ctx->parent_app_wrk_index;
153   app_session->connection_index = ctx->srtp_ctx_handle;
154   app_session->session_type =
155     session_type_from_proto_and_ip (TRANSPORT_PROTO_SRTP, ctx->udp_is_ip4);
156
157   if ((err = app_worker_init_connected (app_wrk, app_session)))
158     goto failed;
159
160   app_session->session_state = SESSION_STATE_READY;
161   if (app_worker_connect_notify (app_wrk, app_session, SESSION_E_NONE,
162                                  ctx->parent_app_api_context))
163     {
164       SRTP_DBG (0, "failed to notify app");
165       app_session->session_state = SESSION_STATE_CONNECTING;
166       srtp_disconnect (ctx->srtp_ctx_handle, vlib_get_thread_index ());
167       return -1;
168     }
169
170   ctx->app_session_handle = session_handle (app_session);
171
172   return 0;
173
174 failed:
175   /* Free app session pre-allocated when transport was established */
176   session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
177   ctx->no_app_session = 1;
178   srtp_disconnect (ctx->srtp_ctx_handle, vlib_get_thread_index ());
179   return app_worker_connect_notify (app_wrk, 0, err,
180                                     ctx->parent_app_api_context);
181 }
182
183 static int
184 srtp_ctx_init_server (srtp_tc_t *ctx)
185 {
186   session_t *app_listener, *app_session;
187   app_worker_t *app_wrk;
188   srtp_tc_t *lctx;
189   int rv;
190
191   if (srtp_create (&ctx->srtp_ctx, ctx->srtp_policy) != srtp_err_status_ok)
192     return -1;
193
194   lctx = srtp_listener_ctx_get (ctx->listener_ctx_index);
195   app_listener = listen_session_get_from_handle (lctx->app_session_handle);
196
197   app_session = session_get (ctx->c_s_index, ctx->c_thread_index);
198   app_session->app_wrk_index = ctx->parent_app_wrk_index;
199   app_session->connection_index = ctx->srtp_ctx_handle;
200   app_session->session_type = app_listener->session_type;
201   app_session->listener_handle = listen_session_get_handle (app_listener);
202   app_session->session_state = SESSION_STATE_ACCEPTING;
203
204   if ((rv = app_worker_init_accepted (app_session)))
205     {
206       SRTP_DBG (1, "failed to allocate fifos");
207       session_free (app_session);
208       return rv;
209     }
210   ctx->app_session_handle = session_handle (app_session);
211   ctx->parent_app_wrk_index = app_session->app_wrk_index;
212   app_wrk = app_worker_get (app_session->app_wrk_index);
213   return app_worker_accept_notify (app_wrk, app_session);
214 }
215
216 static int
217 srtp_ctx_deinit (srtp_tc_t *ctx)
218 {
219   if (srtp_dealloc (ctx->srtp_ctx))
220     SRTP_DBG (0, "%u failed to cleanup srtp state", ctx->c_c_index);
221   return 0;
222 }
223
224 static inline int
225 srtp_ctx_write (srtp_tc_t *ctx, session_t *app_session,
226                 transport_send_params_t *sp)
227 {
228   u32 n_wrote = 0, to_deq, dgram_sz;
229   session_dgram_pre_hdr_t hdr;
230   app_session_transport_t at;
231   svm_msg_q_t *mq;
232   session_t *us;
233   u8 buf[2000];
234   int rv, len;
235
236   sp->max_burst_size = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
237
238   us = session_get_from_handle (ctx->srtp_session_handle);
239   to_deq = svm_fifo_max_dequeue_cons (app_session->tx_fifo);
240   mq = session_main_get_vpp_event_queue (us->thread_index);
241
242   while (to_deq > 0)
243     {
244       /* Peeking only pre-header dgram because the session is connected */
245       rv = svm_fifo_peek (app_session->tx_fifo, 0, sizeof (hdr), (u8 *) &hdr);
246       ASSERT (rv == sizeof (hdr) && hdr.data_length < vec_len (buf));
247       ASSERT (to_deq >= hdr.data_length + SESSION_CONN_HDR_LEN);
248
249       dgram_sz = hdr.data_length + SESSION_CONN_HDR_LEN;
250       if (svm_fifo_max_enqueue_prod (us->tx_fifo) < dgram_sz + 1000)
251         {
252           svm_fifo_add_want_deq_ntf (us->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
253           transport_connection_deschedule (&ctx->connection);
254           sp->flags |= TRANSPORT_SND_F_DESCHED;
255           goto done;
256         }
257
258       rv = svm_fifo_peek (app_session->tx_fifo, SESSION_CONN_HDR_LEN,
259                           hdr.data_length, buf);
260       ASSERT (rv == hdr.data_length);
261       svm_fifo_dequeue_drop (app_session->tx_fifo, dgram_sz);
262
263       len = rv;
264
265       rv = srtp_protect (ctx->srtp_ctx, buf, &len);
266       if (rv != srtp_err_status_ok)
267         {
268           SRTP_DBG (0, "failed to protect %u", rv);
269           return 0;
270         }
271
272       rv = app_send_dgram_raw (us->tx_fifo, &at, mq, (u8 *) buf, len,
273                                SESSION_IO_EVT_TX, 1 /* do_evt */,
274                                0 /* noblock */);
275       ASSERT (rv == len);
276
277       n_wrote += rv;
278       to_deq -= dgram_sz;
279     }
280
281 done:
282
283   if (svm_fifo_needs_deq_ntf (app_session->tx_fifo, n_wrote))
284     session_dequeue_notify (app_session);
285
286   if (n_wrote)
287     {
288       if (svm_fifo_set_event (us->tx_fifo))
289         session_send_io_evt_to_thread (us->tx_fifo, SESSION_IO_EVT_TX);
290     }
291
292   if (PREDICT_FALSE (ctx->app_closed &&
293                      !svm_fifo_max_enqueue_prod (us->rx_fifo)))
294     {
295       srtp_disconnect_transport (ctx);
296       session_transport_closed_notify (&ctx->connection);
297     }
298
299   return n_wrote > 0 ? clib_max (n_wrote / TRANSPORT_PACER_MIN_MSS, 1) : 0;
300 }
301
302 int
303 srtp_add_vpp_q_builtin_rx_evt (session_t *s)
304 {
305   if (svm_fifo_set_event (s->rx_fifo))
306     session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
307   return 0;
308 }
309
310 void
311 srtp_notify_app_enqueue (srtp_tc_t *ctx, session_t *app_session)
312 {
313   app_worker_t *app_wrk;
314   app_wrk = app_worker_get_if_valid (app_session->app_wrk_index);
315   if (PREDICT_TRUE (app_wrk != 0))
316     app_worker_lock_and_send_event (app_wrk, app_session, SESSION_IO_EVT_RX);
317 }
318
319 static inline int
320 srtp_ctx_read (srtp_tc_t *ctx, session_t *us)
321 {
322   app_session_transport_t at;
323   session_dgram_hdr_t hdr;
324   session_t *app_session;
325   u32 n_read = 0;
326   u8 buf[2000];
327   int rv, len;
328
329   app_session = session_get_from_handle (ctx->app_session_handle);
330   svm_fifo_fill_chunk_list (app_session->rx_fifo);
331
332   while (svm_fifo_max_dequeue_cons (us->rx_fifo) > 0)
333     {
334       if (svm_fifo_max_enqueue_prod (app_session->rx_fifo) < 2000)
335         {
336           srtp_add_vpp_q_builtin_rx_evt (us);
337           goto done;
338         }
339
340       rv = app_recv_dgram_raw (us->rx_fifo, (u8 *) buf, 2000, &at,
341                                0 /* clear evt */, 0 /* peek */);
342       ASSERT (rv > 0);
343       len = rv;
344
345       rv = srtp_unprotect (ctx->srtp_ctx, buf, &len);
346
347       if (rv != srtp_err_status_ok)
348         {
349           SRTP_DBG (0, "failed to unprotect %d", rv);
350           return 0;
351         }
352       n_read += len;
353
354       hdr.data_length = len;
355       hdr.data_offset = 0;
356
357       svm_fifo_seg_t segs[2] = { { (u8 *) &hdr, sizeof (hdr) }, { buf, len } };
358
359       rv = svm_fifo_enqueue_segments (app_session->rx_fifo, segs, 2,
360                                       0 /* allow partial */);
361       ASSERT (rv > 0);
362     }
363
364 done:
365
366   srtp_notify_app_enqueue (ctx, app_session);
367
368   return n_read;
369 }
370
371 int
372 srtp_add_segment_callback (u32 client_index, u64 segment_handle)
373 {
374   /* No-op for builtin */
375   return 0;
376 }
377
378 int
379 srtp_del_segment_callback (u32 client_index, u64 segment_handle)
380 {
381   return 0;
382 }
383
384 void
385 srtp_session_disconnect_callback (session_t *us)
386 {
387   clib_warning ("udp %u disconnected?", us->session_index);
388 }
389
390 void
391 srtp_session_reset_callback (session_t *us)
392 {
393   clib_warning ("udp %u reset?", us->session_index);
394 }
395
396 static int
397 srtp_session_connected_callback (u32 srtp_app_index, u32 ctx_handle,
398                                  session_t *us, session_error_t err)
399 {
400   session_t *app_session;
401   session_type_t st;
402   srtp_tc_t *ctx;
403
404   ctx = srtp_ctx_get_w_thread (ctx_handle, 1 /* udp allocs on thread 1 */);
405
406   ctx->srtp_session_handle = session_handle (us);
407   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
408   us->opaque = ctx_handle;
409
410   /* Preallocate app session. Avoids allocating a session on srtp_session rx
411    * and potentially invalidating the session pool */
412   app_session = session_alloc (ctx->c_thread_index);
413   app_session->session_state = SESSION_STATE_CREATED;
414   ctx->c_s_index = app_session->session_index;
415
416   st = session_type_from_proto_and_ip (TRANSPORT_PROTO_SRTP, ctx->udp_is_ip4);
417   app_session->session_type = st;
418   app_session->connection_index = ctx->srtp_ctx_handle;
419
420   return srtp_ctx_init_client (ctx);
421 }
422
423 int
424 srtp_session_accept_callback (session_t *us)
425 {
426   session_t *srtp_listener, *app_session;
427   srtp_tc_t *lctx, *ctx;
428   u32 ctx_handle;
429
430   srtp_listener = listen_session_get_from_handle (us->listener_handle);
431   lctx = srtp_listener_ctx_get (srtp_listener->opaque);
432
433   ctx_handle = srtp_ctx_alloc_w_thread (us->thread_index);
434   ctx = srtp_ctx_get_w_thread (ctx_handle, us->thread_index);
435   clib_memcpy_fast (ctx, lctx, sizeof (*lctx));
436   ctx->c_thread_index = vlib_get_thread_index ();
437   ctx->srtp_ctx_handle = ctx_handle;
438   us->session_state = SESSION_STATE_READY;
439   us->opaque = ctx_handle;
440   ctx->srtp_session_handle = session_handle (us);
441   ctx->listener_ctx_index = srtp_listener->opaque;
442   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
443
444   ctx->srtp_policy[0].key = clib_mem_alloc (SRTP_MAX_KEYLEN);
445   clib_memcpy (ctx->srtp_policy[0].key, lctx->srtp_policy[0].key,
446                SRTP_MAX_KEYLEN);
447   ctx->srtp_policy[1].key = clib_mem_alloc (SRTP_MAX_KEYLEN);
448   clib_memcpy (ctx->srtp_policy[1].key, lctx->srtp_policy[1].key,
449                SRTP_MAX_KEYLEN);
450
451   app_session = session_alloc (ctx->c_thread_index);
452   app_session->session_state = SESSION_STATE_CREATED;
453   ctx->c_s_index = app_session->session_index;
454
455   SRTP_DBG (1, "Accept on listener %u new connection [%u]%x",
456             srtp_listener->opaque, vlib_get_thread_index (), ctx_handle);
457
458   return srtp_ctx_init_server (ctx);
459 }
460
461 int
462 srtp_app_rx_callback (session_t *us)
463 {
464   srtp_tc_t *ctx;
465
466   ctx = srtp_ctx_get_w_thread (us->opaque, us->thread_index);
467   srtp_ctx_read (ctx, us);
468   return 0;
469 }
470
471 int
472 srtp_app_tx_callback (session_t *us)
473 {
474   srtp_tc_t *ctx;
475
476   ctx = srtp_ctx_get_w_thread (us->opaque, us->thread_index);
477   transport_connection_reschedule (&ctx->connection);
478
479   return 0;
480 }
481
482 static void
483 srtp_app_session_cleanup (session_t *s, session_cleanup_ntf_t ntf)
484 {
485   srtp_tc_t *ctx;
486
487   if (ntf == SESSION_CLEANUP_TRANSPORT)
488     {
489       /* Allow cleanup of tcp session */
490       if (s->session_state == SESSION_STATE_TRANSPORT_DELETED)
491         session_close (s);
492       return;
493     }
494
495   ctx = srtp_ctx_get_w_thread (s->opaque, s->thread_index);
496   if (!ctx->no_app_session)
497     session_transport_delete_notify (&ctx->connection);
498   srtp_ctx_deinit (ctx);
499   srtp_ctx_free (ctx);
500 }
501
502 static void
503 srtp_migrate_ctx_reply (void *arg)
504 {
505   u32 ctx_index = pointer_to_uword (arg);
506   srtp_tc_t *ctx;
507
508   ctx = srtp_ctx_get_w_thread (ctx_index, vlib_get_thread_index ());
509   srtp_ctx_free (ctx);
510 }
511
512 static void
513 srtp_migrate_ctx (void *arg)
514 {
515   u32 ctx_handle, thread_index, old_thread_index, old_ctx_index;
516   srtp_tc_t *ctx = (srtp_tc_t *) arg;
517   session_t *us, *new_app_session;
518   void *rargs;
519
520   old_thread_index = ctx->c_thread_index;
521   old_ctx_index = ctx->c_c_index;
522   thread_index = session_thread_from_handle (ctx->srtp_session_handle);
523   ASSERT (thread_index == vlib_get_thread_index ());
524
525   ctx_handle = srtp_ctx_attach (thread_index, ctx);
526   ctx = srtp_ctx_get_w_thread (ctx_handle, thread_index);
527   ctx->srtp_ctx_handle = ctx_handle;
528   SRTP_DBG (1, "migrated ctx handle %u", ctx_handle);
529
530   us = session_get_from_handle (ctx->srtp_session_handle);
531   us->opaque = ctx_handle;
532   us->flags &= ~SESSION_F_IS_MIGRATING;
533   if (svm_fifo_max_dequeue (us->tx_fifo))
534     session_send_io_evt_to_thread (us->tx_fifo, SESSION_IO_EVT_TX);
535
536   /* Migrate app session as well */
537   session_dgram_connect_notify (&ctx->connection, old_thread_index,
538                                 &new_app_session);
539
540   /* Call back original thread and ask for cleanup */
541   rargs = uword_to_pointer ((uword) old_ctx_index, void *);
542   session_send_rpc_evt_to_thread (old_thread_index, srtp_migrate_ctx_reply,
543                                   rargs);
544 }
545
546 static void
547 srtp_session_migrate_callback (session_t *us, session_handle_t new_sh)
548 {
549   u32 new_thread = session_thread_from_handle (new_sh);
550   srtp_tc_t *ctx, *cloned_ctx;
551
552   ctx = srtp_ctx_get_w_thread (us->opaque, us->thread_index);
553   ctx->srtp_session_handle = new_sh;
554   cloned_ctx = srtp_ctx_detach (ctx);
555   SRTP_DBG (1, "ctx %u attached to udp %x session migrating",
556             cloned_ctx->c_c_index, new_sh);
557
558   session_send_rpc_evt_to_thread (new_thread, srtp_migrate_ctx,
559                                   (void *) cloned_ctx);
560
561   /* Can't free ctx now because app might be sending as srtp is not
562    * connection oriented, so it won't wait for a handshake */
563   ctx->is_migrated = 1;
564 }
565
566 static session_cb_vft_t srtp_app_cb_vft = {
567   .session_accept_callback = srtp_session_accept_callback,
568   .session_disconnect_callback = srtp_session_disconnect_callback,
569   .session_connected_callback = srtp_session_connected_callback,
570   .session_reset_callback = srtp_session_reset_callback,
571   .add_segment_callback = srtp_add_segment_callback,
572   .del_segment_callback = srtp_del_segment_callback,
573   .builtin_app_rx_callback = srtp_app_rx_callback,
574   .builtin_app_tx_callback = srtp_app_tx_callback,
575   .session_migrate_callback = srtp_session_migrate_callback,
576   .session_cleanup_callback = srtp_app_session_cleanup,
577 };
578
579 static clib_error_t *
580 srtp_enable (vlib_main_t *vm, u8 is_en)
581 {
582   u32 add_segment_size = 256 << 20, first_seg_size = 32 << 20;
583   vnet_app_detach_args_t _da, *da = &_da;
584   vnet_app_attach_args_t _a, *a = &_a;
585   u64 options[APP_OPTIONS_N_OPTIONS];
586   srtp_main_t *sm = &srtp_main;
587   u32 fifo_size = 128 << 12;
588
589   if (!is_en)
590     {
591       da->app_index = sm->app_index;
592       da->api_client_index = APP_INVALID_INDEX;
593       vnet_application_detach (da);
594       return 0;
595     }
596
597   srtp_init ();
598   vec_validate (sm->ctx_pool, vlib_num_workers ());
599
600   first_seg_size = sm->first_seg_size ? sm->first_seg_size : first_seg_size;
601   fifo_size = sm->fifo_size ? sm->fifo_size : fifo_size;
602
603   clib_memset (a, 0, sizeof (*a));
604   clib_memset (options, 0, sizeof (options));
605
606   a->session_cb_vft = &srtp_app_cb_vft;
607   a->api_client_index = APP_INVALID_INDEX;
608   a->options = options;
609   a->name = format (0, "srtp");
610   a->options[APP_OPTIONS_SEGMENT_SIZE] = first_seg_size;
611   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = add_segment_size;
612   a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
613   a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
614   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
615   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
616   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
617
618   if (vnet_application_attach (a))
619     return clib_error_return (0, "failed to attach srtp app");
620
621   sm->app_index = a->app_index;
622   vec_free (a->name);
623
624   return 0;
625 }
626
627 int
628 srtp_connect (transport_endpoint_cfg_t *tep)
629 {
630   vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
631   session_endpoint_cfg_t *sep;
632   srtp_main_t *sm = &srtp_main;
633   app_worker_t *app_wrk;
634   application_t *app;
635   srtp_tc_t *ctx;
636   u32 ctx_index;
637   int rv;
638
639   sep = (session_endpoint_cfg_t *) tep;
640   if (!sep->ext_cfg)
641     return SESSION_E_NOEXTCFG;
642
643   app_wrk = app_worker_get (sep->app_wrk_index);
644   app = application_get (app_wrk->app_index);
645
646   ctx_index = srtp_ctx_alloc_w_thread (1 /* because of udp */);
647   ctx = srtp_ctx_get_w_thread (ctx_index, 1);
648   ctx->parent_app_wrk_index = sep->app_wrk_index;
649   ctx->parent_app_api_context = sep->opaque;
650   ctx->udp_is_ip4 = sep->is_ip4;
651   ctx->srtp_ctx_handle = ctx_index;
652
653   srtp_init_policy (ctx, (transport_endpt_cfg_srtp_t *) sep->ext_cfg->data);
654
655   clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
656   cargs->sep.transport_proto = TRANSPORT_PROTO_UDP;
657   cargs->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
658   cargs->app_index = sm->app_index;
659   cargs->api_context = ctx_index;
660   cargs->sep_ext.ns_index = app->ns_index;
661   if ((rv = vnet_connect (cargs)))
662     return rv;
663
664   SRTP_DBG (1, "New connect request %u", ctx_index);
665   return 0;
666 }
667
668 static void
669 srtp_disconnect_transport (srtp_tc_t *ctx)
670 {
671   vnet_disconnect_args_t a = {
672     .handle = ctx->srtp_session_handle,
673     .app_index = srtp_main.app_index,
674   };
675
676   if (vnet_disconnect_session (&a))
677     SRTP_DBG (0, "disconnect returned");
678 }
679
680 static void
681 srtp_disconnect (u32 ctx_handle, u32 thread_index)
682 {
683   session_t *app_session;
684   srtp_tc_t *ctx;
685
686   SRTP_DBG (1, "App disconnecting %x", ctx_handle);
687
688   ctx = srtp_ctx_get_w_thread (ctx_handle, thread_index);
689
690   app_session = session_get_from_handle (ctx->app_session_handle);
691   if (!svm_fifo_max_dequeue_cons (app_session->tx_fifo))
692     {
693       /* Confirm close */
694       srtp_disconnect_transport (ctx);
695       session_transport_closed_notify (&ctx->connection);
696     }
697   else
698     {
699       /* Wait for all data to be written to udp */
700       ctx->app_closed = 1;
701     }
702 }
703
704 static u32
705 srtp_start_listen (u32 app_listener_index, transport_endpoint_t *tep)
706 {
707   vnet_listen_args_t _bargs, *args = &_bargs;
708   session_handle_t udp_al_handle;
709   srtp_main_t *sm = &srtp_main;
710   session_endpoint_cfg_t *sep;
711   session_t *srtp_listener;
712   session_t *app_listener;
713   app_worker_t *app_wrk;
714   application_t *app;
715   app_listener_t *al;
716   srtp_tc_t *lctx;
717   u32 lctx_index;
718
719   sep = (session_endpoint_cfg_t *) tep;
720   if (!sep->ext_cfg)
721     return SESSION_E_NOEXTCFG;
722
723   app_wrk = app_worker_get (sep->app_wrk_index);
724   app = application_get (app_wrk->app_index);
725
726   clib_memset (args, 0, sizeof (*args));
727   args->app_index = sm->app_index;
728   args->sep_ext = *sep;
729   args->sep_ext.ns_index = app->ns_index;
730   args->sep_ext.transport_proto = TRANSPORT_PROTO_UDP;
731   args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
732   if (vnet_listen (args))
733     return -1;
734
735   lctx_index = srtp_listener_ctx_alloc ();
736   udp_al_handle = args->handle;
737   al = app_listener_get_w_handle (udp_al_handle);
738   srtp_listener = app_listener_get_session (al);
739   srtp_listener->opaque = lctx_index;
740
741   app_listener = listen_session_get (app_listener_index);
742
743   lctx = srtp_listener_ctx_get (lctx_index);
744   lctx->parent_app_wrk_index = sep->app_wrk_index;
745   lctx->srtp_session_handle = udp_al_handle;
746   lctx->app_session_handle = listen_session_get_handle (app_listener);
747   lctx->udp_is_ip4 = sep->is_ip4;
748
749   srtp_init_policy (lctx, (transport_endpt_cfg_srtp_t *) sep->ext_cfg->data);
750
751   SRTP_DBG (1, "Started listening %d", lctx_index);
752   return lctx_index;
753 }
754
755 u32
756 srtp_stop_listen (u32 lctx_index)
757 {
758   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
759   transport_connection_t *lc;
760   srtp_tc_t *lctx;
761   session_t *ls;
762   int rv;
763
764   lctx = srtp_listener_ctx_get (lctx_index);
765
766   /* Cleanup listener from session lookup table */
767   ls = session_get_from_handle (lctx->srtp_session_handle);
768   lc = session_get_transport (ls);
769
770   sep.fib_index = lc->fib_index;
771   sep.port = lc->lcl_port;
772   sep.is_ip4 = lc->is_ip4;
773   sep.transport_proto = TRANSPORT_PROTO_SRTP;
774   clib_memcpy (&sep.ip, &lc->lcl_ip, sizeof (lc->lcl_ip));
775   session_lookup_del_session_endpoint2 (&sep);
776
777   vnet_unlisten_args_t a = {
778     .handle = lctx->srtp_session_handle,
779     .app_index = srtp_main.app_index,
780     .wrk_map_index = 0 /* default wrk */
781   };
782   if ((rv = vnet_unlisten (&a)))
783     SRTP_DBG (0, "unlisten returned %d", rv);
784
785   srtp_listener_ctx_free (lctx);
786   return 0;
787 }
788
789 transport_connection_t *
790 srtp_connection_get (u32 ctx_index, u32 thread_index)
791 {
792   srtp_tc_t *ctx;
793   ctx = srtp_ctx_get_w_thread (ctx_index, thread_index);
794   return &ctx->connection;
795 }
796
797 transport_connection_t *
798 srtp_listener_get (u32 listener_index)
799 {
800   srtp_tc_t *ctx;
801   ctx = srtp_listener_ctx_get (listener_index);
802   return &ctx->connection;
803 }
804
805 int
806 srtp_custom_tx_callback (void *session, transport_send_params_t *sp)
807 {
808   session_t *app_session = (session_t *) session;
809   srtp_tc_t *ctx;
810
811   if (PREDICT_FALSE (app_session->session_state >=
812                      SESSION_STATE_TRANSPORT_CLOSED))
813     return 0;
814
815   sp->flags = 0;
816   ctx = srtp_ctx_get_w_thread (app_session->connection_index,
817                                app_session->thread_index);
818   if (PREDICT_FALSE (ctx->is_migrated))
819     return 0;
820
821   return srtp_ctx_write (ctx, app_session, sp);
822 }
823
824 u8 *
825 format_srtp_ctx (u8 *s, va_list *args)
826 {
827   srtp_tc_t *ctx = va_arg (*args, srtp_tc_t *);
828   u32 udp_si, udp_ti;
829
830   session_parse_handle (ctx->srtp_session_handle, &udp_si, &udp_ti);
831   s = format (s, "[%d:%d][SRTP] app_wrk %u index %u udp %d:%d",
832               ctx->c_thread_index, ctx->c_s_index, ctx->parent_app_wrk_index,
833               ctx->srtp_ctx_handle, udp_ti, udp_si);
834
835   return s;
836 }
837
838 static u8 *
839 format_srtp_listener_ctx (u8 *s, va_list *args)
840 {
841   session_t *udp_listener;
842   app_listener_t *al;
843   srtp_tc_t *ctx;
844
845   ctx = va_arg (*args, srtp_tc_t *);
846
847   al = app_listener_get_w_handle (ctx->srtp_session_handle);
848   udp_listener = app_listener_get_session (al);
849   s = format (s, "[%d:%d][SRTP] app_wrk %u udp %d:%d", ctx->c_thread_index,
850               ctx->c_s_index, ctx->parent_app_wrk_index,
851               udp_listener->thread_index, udp_listener->session_index);
852
853   return s;
854 }
855
856 static u8 *
857 format_srtp_ctx_state (u8 *s, va_list *args)
858 {
859   srtp_tc_t *ctx;
860   session_t *us;
861
862   ctx = va_arg (*args, srtp_tc_t *);
863   us = session_get (ctx->c_s_index, ctx->c_thread_index);
864   if (us->session_state == SESSION_STATE_LISTENING)
865     s = format (s, "%s", "LISTEN");
866   else
867     {
868       if (us->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
869         s = format (s, "%s", "CLOSED");
870       else if (us->session_state == SESSION_STATE_APP_CLOSED)
871         s = format (s, "%s", "APP-CLOSED");
872       else if (us->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
873         s = format (s, "%s", "CLOSING");
874       else
875         s = format (s, "%s", "ESTABLISHED");
876     }
877
878   return s;
879 }
880
881 u8 *
882 format_srtp_connection (u8 *s, va_list *args)
883 {
884   u32 ctx_index = va_arg (*args, u32);
885   u32 thread_index = va_arg (*args, u32);
886   u32 verbose = va_arg (*args, u32);
887   srtp_tc_t *ctx;
888
889   ctx = srtp_ctx_get_w_thread (ctx_index, thread_index);
890   if (!ctx)
891     return s;
892
893   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_srtp_ctx, ctx);
894   if (verbose)
895     {
896       s =
897         format (s, "%-" SESSION_CLI_STATE_LEN "U", format_srtp_ctx_state, ctx);
898       if (verbose > 1)
899         s = format (s, "\n");
900     }
901   return s;
902 }
903
904 u8 *
905 format_srtp_listener (u8 *s, va_list *args)
906 {
907   u32 tc_index = va_arg (*args, u32);
908   u32 __clib_unused thread_index = va_arg (*args, u32);
909   u32 verbose = va_arg (*args, u32);
910   srtp_tc_t *ctx = srtp_listener_ctx_get (tc_index);
911
912   s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_srtp_listener_ctx, ctx);
913   if (verbose)
914     s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_srtp_ctx_state, ctx);
915   return s;
916 }
917
918 u8 *
919 format_srtp_half_open (u8 *s, va_list *args)
920 {
921   return 0;
922 }
923
924 static void
925 srtp_transport_endpoint_get (u32 ctx_handle, u32 thread_index,
926                              transport_endpoint_t *tep, u8 is_lcl)
927 {
928   srtp_tc_t *ctx = srtp_ctx_get_w_thread (ctx_handle, thread_index);
929   session_t *udp_session;
930
931   udp_session = session_get_from_handle (ctx->srtp_session_handle);
932   session_get_endpoint (udp_session, tep, is_lcl);
933 }
934
935 static void
936 srtp_transport_listener_endpoint_get (u32 ctx_handle,
937                                       transport_endpoint_t *tep, u8 is_lcl)
938 {
939   session_t *srtp_listener;
940   app_listener_t *al;
941   srtp_tc_t *ctx = srtp_listener_ctx_get (ctx_handle);
942
943   al = app_listener_get_w_handle (ctx->srtp_session_handle);
944   srtp_listener = app_listener_get_session (al);
945   session_get_endpoint (srtp_listener, tep, is_lcl);
946 }
947
948 static const transport_proto_vft_t srtp_proto = {
949   .enable = srtp_enable,
950   .connect = srtp_connect,
951   .close = srtp_disconnect,
952   .start_listen = srtp_start_listen,
953   .stop_listen = srtp_stop_listen,
954   .get_connection = srtp_connection_get,
955   .get_listener = srtp_listener_get,
956   .custom_tx = srtp_custom_tx_callback,
957   .format_connection = format_srtp_connection,
958   .format_half_open = format_srtp_half_open,
959   .format_listener = format_srtp_listener,
960   .get_transport_endpoint = srtp_transport_endpoint_get,
961   .get_transport_listener_endpoint = srtp_transport_listener_endpoint_get,
962   .transport_options = {
963     .name = "srtp",
964     .short_name = "R",
965     .tx_type = TRANSPORT_TX_INTERNAL,
966     .service_type = TRANSPORT_SERVICE_APP,
967   },
968 };
969
970 static clib_error_t *
971 srtp_transport_init (vlib_main_t *vm)
972 {
973   transport_register_protocol (TRANSPORT_PROTO_SRTP, &srtp_proto,
974                                FIB_PROTOCOL_IP4, ~0);
975   transport_register_protocol (TRANSPORT_PROTO_SRTP, &srtp_proto,
976                                FIB_PROTOCOL_IP6, ~0);
977   return 0;
978 }
979
980 VLIB_INIT_FUNCTION (srtp_transport_init);
981
982 VLIB_PLUGIN_REGISTER () = {
983   .version = VPP_BUILD_VER,
984   .description = "Secure Real-time Transport Protocol (SRTP)",
985   /* .default_disabled = 1, */
986 };
987
988 /*
989  * fd.io coding-style-patch-verification: ON
990  *
991  * Local Variables:
992  * eval: (c-set-style "gnu")
993  * End:
994  */