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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <srtp/srtp.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/session.h>
20 static srtp_main_t srtp_main;
22 static void srtp_disconnect (u32 ctx_handle, u32 thread_index);
23 static void srtp_disconnect_transport (srtp_tc_t *ctx);
26 srtp_ctx_alloc_w_thread (u32 thread_index)
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;
36 static inline srtp_tc_t *
37 srtp_ctx_get_w_thread (u32 ctx_index, u32 thread_index)
39 return pool_elt_at_index (srtp_main.ctx_pool[thread_index], ctx_index);
43 srtp_init_policy (srtp_tc_t *ctx, transport_endpt_cfg_srtp_t *cfg)
45 transport_endpt_cfg_srtp_policy_t *sp_cfg;
49 for (i = 0; i < 2; i++)
51 sp = &ctx->srtp_policy[i];
52 sp_cfg = &cfg->policies[i];
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);
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;
68 srtp_ctx_free_policy (srtp_tc_t *ctx)
70 clib_mem_free (ctx->srtp_policy[0].key);
71 clib_mem_free (ctx->srtp_policy[1].key);
75 srtp_ctx_free (srtp_tc_t *ctx)
77 if (!ctx->is_migrated)
78 srtp_ctx_free_policy (ctx);
79 pool_put (srtp_main.ctx_pool[ctx->c_thread_index], ctx);
83 srtp_ctx_attach (u32 thread_index, void *ctx_ptr)
87 pool_get (srtp_main.ctx_pool[thread_index], ctx);
88 clib_memcpy (ctx, ctx_ptr, sizeof (*ctx));
90 ctx->c_thread_index = thread_index;
91 ctx->srtp_ctx_handle = ctx - srtp_main.ctx_pool[thread_index];
96 srtp_ctx_detach (srtp_tc_t *ctx)
100 sc = clib_mem_alloc (sizeof (*sc));
101 clib_memcpy (sc, ctx, sizeof (*sc));
107 srtp_listener_ctx_alloc (void)
109 srtp_main_t *sm = &srtp_main;
112 pool_get_zero (sm->listener_ctx_pool, ctx);
113 return ctx - sm->listener_ctx_pool;
117 srtp_listener_ctx_free (srtp_tc_t *ctx)
119 srtp_ctx_free_policy (ctx);
121 clib_memset (ctx, 0xfb, sizeof (*ctx));
122 pool_put (srtp_main.listener_ctx_pool, ctx);
126 srtp_listener_ctx_get (u32 ctx_index)
128 return pool_elt_at_index (srtp_main.listener_ctx_pool, ctx_index);
132 srtp_listener_ctx_index (srtp_tc_t *ctx)
134 return (ctx - srtp_main.listener_ctx_pool);
138 srtp_ctx_init_client (srtp_tc_t *ctx)
140 session_t *app_session;
141 app_worker_t *app_wrk;
144 if (srtp_create (&ctx->srtp_ctx, &ctx->srtp_policy[0]) != srtp_err_status_ok)
146 SRTP_DBG (0, "failed to init srtp ctx");
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);
157 if ((err = app_worker_init_connected (app_wrk, app_session)))
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))
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 ());
170 ctx->app_session_handle = session_handle (app_session);
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);
184 srtp_ctx_init_server (srtp_tc_t *ctx)
186 session_t *app_listener, *app_session;
187 app_worker_t *app_wrk;
191 if (srtp_create (&ctx->srtp_ctx, ctx->srtp_policy) != srtp_err_status_ok)
194 lctx = srtp_listener_ctx_get (ctx->listener_ctx_index);
195 app_listener = listen_session_get_from_handle (lctx->app_session_handle);
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;
204 if ((rv = app_worker_init_accepted (app_session)))
206 SRTP_DBG (1, "failed to allocate fifos");
207 session_free (app_session);
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);
217 srtp_ctx_deinit (srtp_tc_t *ctx)
219 if (srtp_dealloc (ctx->srtp_ctx))
220 SRTP_DBG (0, "%u failed to cleanup srtp state", ctx->c_c_index);
225 srtp_ctx_write (srtp_tc_t *ctx, session_t *app_session,
226 transport_send_params_t *sp)
228 u32 n_wrote = 0, to_deq, dgram_sz;
229 session_dgram_pre_hdr_t hdr;
230 app_session_transport_t at = {};
236 sp->max_burst_size = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
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);
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 < 2000);
247 ASSERT (to_deq >= hdr.data_length + SESSION_CONN_HDR_LEN);
249 dgram_sz = hdr.data_length + SESSION_CONN_HDR_LEN;
250 if (svm_fifo_max_enqueue_prod (us->tx_fifo) < dgram_sz + 1000)
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;
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);
265 rv = srtp_protect (ctx->srtp_ctx, buf, &len);
266 if (rv != srtp_err_status_ok)
268 SRTP_DBG (0, "failed to protect %u", rv);
272 rv = app_send_dgram_raw (us->tx_fifo, &at, mq, (u8 *) buf, len,
273 SESSION_IO_EVT_TX, 1 /* do_evt */,
283 if (svm_fifo_needs_deq_ntf (app_session->tx_fifo, n_wrote))
284 session_dequeue_notify (app_session);
288 if (svm_fifo_set_event (us->tx_fifo))
289 session_send_io_evt_to_thread (us->tx_fifo, SESSION_IO_EVT_TX);
292 if (PREDICT_FALSE (ctx->app_closed &&
293 !svm_fifo_max_enqueue_prod (us->rx_fifo)))
295 srtp_disconnect_transport (ctx);
296 session_transport_closed_notify (&ctx->connection);
299 return n_wrote > 0 ? clib_max (n_wrote / TRANSPORT_PACER_MIN_MSS, 1) : 0;
303 srtp_add_vpp_q_builtin_rx_evt (session_t *s)
305 if (svm_fifo_set_event (s->rx_fifo))
306 session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_BUILTIN_RX);
311 srtp_notify_app_enqueue (srtp_tc_t *ctx, session_t *app_session)
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);
320 srtp_ctx_read (srtp_tc_t *ctx, session_t *us)
322 app_session_transport_t at;
323 session_dgram_hdr_t hdr;
324 session_t *app_session;
329 app_session = session_get_from_handle (ctx->app_session_handle);
330 svm_fifo_fill_chunk_list (app_session->rx_fifo);
332 while (svm_fifo_max_dequeue_cons (us->rx_fifo) > 0)
334 if (svm_fifo_max_enqueue_prod (app_session->rx_fifo) < 2000)
336 srtp_add_vpp_q_builtin_rx_evt (us);
340 rv = app_recv_dgram_raw (us->rx_fifo, (u8 *) buf, 2000, &at,
341 0 /* clear evt */, 0 /* peek */);
345 rv = srtp_unprotect (ctx->srtp_ctx, buf, &len);
347 if (rv != srtp_err_status_ok)
349 SRTP_DBG (0, "failed to unprotect %d", rv);
354 hdr.data_length = len;
357 svm_fifo_seg_t segs[2] = { { (u8 *) &hdr, sizeof (hdr) }, { buf, len } };
359 rv = svm_fifo_enqueue_segments (app_session->rx_fifo, segs, 2,
360 0 /* allow partial */);
366 srtp_notify_app_enqueue (ctx, app_session);
372 srtp_add_segment_callback (u32 client_index, u64 segment_handle)
374 /* No-op for builtin */
379 srtp_del_segment_callback (u32 client_index, u64 segment_handle)
385 srtp_session_disconnect_callback (session_t *us)
387 clib_warning ("udp %u disconnected?", us->session_index);
391 srtp_session_reset_callback (session_t *us)
393 clib_warning ("udp %u reset?", us->session_index);
397 srtp_session_connected_callback (u32 srtp_app_index, u32 ctx_handle,
398 session_t *us, session_error_t err)
400 session_t *app_session;
404 ctx = srtp_ctx_get_w_thread (ctx_handle, 1 /* udp allocs on thread 1 */);
406 ctx->srtp_session_handle = session_handle (us);
407 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
408 us->opaque = ctx_handle;
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;
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;
420 return srtp_ctx_init_client (ctx);
424 srtp_session_accept_callback (session_t *us)
426 session_t *srtp_listener, *app_session;
427 srtp_tc_t *lctx, *ctx;
430 srtp_listener = listen_session_get_from_handle (us->listener_handle);
431 lctx = srtp_listener_ctx_get (srtp_listener->opaque);
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;
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,
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,
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;
455 SRTP_DBG (1, "Accept on listener %u new connection [%u]%x",
456 srtp_listener->opaque, vlib_get_thread_index (), ctx_handle);
458 return srtp_ctx_init_server (ctx);
462 srtp_app_rx_callback (session_t *us)
466 ctx = srtp_ctx_get_w_thread (us->opaque, us->thread_index);
467 srtp_ctx_read (ctx, us);
472 srtp_app_tx_callback (session_t *us)
476 ctx = srtp_ctx_get_w_thread (us->opaque, us->thread_index);
477 transport_connection_reschedule (&ctx->connection);
483 srtp_app_session_cleanup (session_t *s, session_cleanup_ntf_t ntf)
487 if (ntf == SESSION_CLEANUP_TRANSPORT)
489 /* Allow cleanup of tcp session */
490 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED)
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);
503 srtp_migrate_ctx_reply (void *arg)
505 u32 ctx_index = pointer_to_uword (arg);
508 ctx = srtp_ctx_get_w_thread (ctx_index, vlib_get_thread_index ());
513 srtp_migrate_ctx (void *arg)
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;
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 ());
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);
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);
536 /* Migrate app session as well */
537 session_dgram_connect_notify (&ctx->connection, old_thread_index,
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,
547 srtp_session_migrate_callback (session_t *us, session_handle_t new_sh)
549 u32 new_thread = session_thread_from_handle (new_sh);
550 srtp_tc_t *ctx, *cloned_ctx;
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);
558 session_send_rpc_evt_to_thread (new_thread, srtp_migrate_ctx,
559 (void *) cloned_ctx);
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;
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,
579 static clib_error_t *
580 srtp_enable (vlib_main_t *vm, u8 is_en)
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;
591 da->app_index = sm->app_index;
592 da->api_client_index = APP_INVALID_INDEX;
593 vnet_application_detach (da);
598 vec_validate (sm->ctx_pool, vlib_num_workers ());
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;
603 clib_memset (a, 0, sizeof (*a));
604 clib_memset (options, 0, sizeof (options));
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;
618 if (vnet_application_attach (a))
619 return clib_error_return (0, "failed to attach srtp app");
621 sm->app_index = a->app_index;
628 srtp_connect (transport_endpoint_cfg_t *tep)
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;
639 sep = (session_endpoint_cfg_t *) tep;
641 return SESSION_E_NOEXTCFG;
643 app_wrk = app_worker_get (sep->app_wrk_index);
644 app = application_get (app_wrk->app_index);
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;
653 srtp_init_policy (ctx, (transport_endpt_cfg_srtp_t *) sep->ext_cfg->data);
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)))
664 SRTP_DBG (1, "New connect request %u", ctx_index);
669 srtp_disconnect_transport (srtp_tc_t *ctx)
671 vnet_disconnect_args_t a = {
672 .handle = ctx->srtp_session_handle,
673 .app_index = srtp_main.app_index,
676 if (vnet_disconnect_session (&a))
677 SRTP_DBG (0, "disconnect returned");
681 srtp_disconnect (u32 ctx_handle, u32 thread_index)
683 session_t *app_session;
686 SRTP_DBG (1, "App disconnecting %x", ctx_handle);
688 ctx = srtp_ctx_get_w_thread (ctx_handle, thread_index);
690 app_session = session_get_from_handle (ctx->app_session_handle);
691 if (!svm_fifo_max_dequeue_cons (app_session->tx_fifo))
694 srtp_disconnect_transport (ctx);
695 session_transport_closed_notify (&ctx->connection);
699 /* Wait for all data to be written to udp */
705 srtp_start_listen (u32 app_listener_index, transport_endpoint_t *tep)
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;
719 sep = (session_endpoint_cfg_t *) tep;
721 return SESSION_E_NOEXTCFG;
723 app_wrk = app_worker_get (sep->app_wrk_index);
724 app = application_get (app_wrk->app_index);
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))
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;
741 app_listener = listen_session_get (app_listener_index);
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;
749 srtp_init_policy (lctx, (transport_endpt_cfg_srtp_t *) sep->ext_cfg->data);
751 SRTP_DBG (1, "Started listening %d", lctx_index);
756 srtp_stop_listen (u32 lctx_index)
758 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
759 transport_connection_t *lc;
764 lctx = srtp_listener_ctx_get (lctx_index);
766 /* Cleanup listener from session lookup table */
767 ls = session_get_from_handle (lctx->srtp_session_handle);
768 lc = session_get_transport (ls);
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);
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 */
782 if ((rv = vnet_unlisten (&a)))
783 SRTP_DBG (0, "unlisten returned %d", rv);
785 srtp_listener_ctx_free (lctx);
789 transport_connection_t *
790 srtp_connection_get (u32 ctx_index, u32 thread_index)
793 ctx = srtp_ctx_get_w_thread (ctx_index, thread_index);
794 return &ctx->connection;
797 transport_connection_t *
798 srtp_listener_get (u32 listener_index)
801 ctx = srtp_listener_ctx_get (listener_index);
802 return &ctx->connection;
806 srtp_custom_tx_callback (void *session, transport_send_params_t *sp)
808 session_t *app_session = (session_t *) session;
811 if (PREDICT_FALSE (app_session->session_state >=
812 SESSION_STATE_TRANSPORT_CLOSED))
816 ctx = srtp_ctx_get_w_thread (app_session->connection_index,
817 app_session->thread_index);
818 if (PREDICT_FALSE (ctx->is_migrated))
821 return srtp_ctx_write (ctx, app_session, sp);
825 format_srtp_ctx (u8 *s, va_list *args)
827 srtp_tc_t *ctx = va_arg (*args, srtp_tc_t *);
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);
839 format_srtp_listener_ctx (u8 *s, va_list *args)
841 session_t *udp_listener;
845 ctx = va_arg (*args, srtp_tc_t *);
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);
857 format_srtp_ctx_state (u8 *s, va_list *args)
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");
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");
875 s = format (s, "%s", "ESTABLISHED");
882 format_srtp_connection (u8 *s, va_list *args)
884 u32 ctx_index = va_arg (*args, u32);
885 u32 thread_index = va_arg (*args, u32);
886 u32 verbose = va_arg (*args, u32);
889 ctx = srtp_ctx_get_w_thread (ctx_index, thread_index);
893 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_srtp_ctx, ctx);
897 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_srtp_ctx_state, ctx);
899 s = format (s, "\n");
905 format_srtp_listener (u8 *s, va_list *args)
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);
912 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_srtp_listener_ctx, ctx);
914 s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_srtp_ctx_state, ctx);
919 format_srtp_half_open (u8 *s, va_list *args)
925 srtp_transport_endpoint_get (u32 ctx_handle, u32 thread_index,
926 transport_endpoint_t *tep, u8 is_lcl)
928 srtp_tc_t *ctx = srtp_ctx_get_w_thread (ctx_handle, thread_index);
929 session_t *udp_session;
931 udp_session = session_get_from_handle (ctx->srtp_session_handle);
932 session_get_endpoint (udp_session, tep, is_lcl);
936 srtp_transport_listener_endpoint_get (u32 ctx_handle,
937 transport_endpoint_t *tep, u8 is_lcl)
939 session_t *srtp_listener;
941 srtp_tc_t *ctx = srtp_listener_ctx_get (ctx_handle);
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);
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 = {
965 .tx_type = TRANSPORT_TX_INTERNAL,
966 .service_type = TRANSPORT_SERVICE_APP,
970 static clib_error_t *
971 srtp_transport_init (vlib_main_t *vm)
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);
980 VLIB_INIT_FUNCTION (srtp_transport_init);
982 VLIB_PLUGIN_REGISTER () = {
983 .version = VPP_BUILD_VER,
984 .description = "Secure Real-time Transport Protocol (SRTP)",
985 .default_disabled = 1,
989 * fd.io coding-style-patch-verification: ON
992 * eval: (c-set-style "gnu")