/* * Copyright (c) 2018-2019 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include static tls_main_t tls_main; static tls_engine_vft_t *tls_vfts; #define TLS_INVALID_HANDLE ~0 #define TLS_IDX_MASK 0x00FFFFFF #define TLS_ENGINE_TYPE_SHIFT 28 void tls_disconnect (u32 ctx_handle, u32 thread_index); void tls_disconnect_transport (tls_ctx_t * ctx) { vnet_disconnect_args_t a = { .handle = ctx->tls_session_handle, .app_index = tls_main.app_index, }; if (vnet_disconnect_session (&a)) clib_warning ("disconnect returned"); } crypto_engine_type_t tls_get_available_engine (void) { int i; for (i = 0; i < vec_len (tls_vfts); i++) { if (tls_vfts[i].ctx_alloc) return i; } return CRYPTO_ENGINE_NONE; } int tls_add_vpp_q_rx_evt (session_t * s) { if (svm_fifo_set_event (s->rx_fifo)) session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_RX); return 0; } int tls_add_vpp_q_builtin_rx_evt (session_t * s) { if (svm_fifo_set_event (s->rx_fifo)) session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_BUILTIN_RX); return 0; } int tls_add_vpp_q_tx_evt (session_t * s) { if (svm_fifo_set_event (s->tx_fifo)) session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX); return 0; } static inline int tls_add_app_q_evt (app_worker_t * app, session_t * app_session) { return app_worker_lock_and_send_event (app, app_session, SESSION_IO_EVT_RX); } u32 tls_listener_ctx_alloc (void) { tls_main_t *tm = &tls_main; tls_ctx_t *ctx; pool_get (tm->listener_ctx_pool, ctx); clib_memset (ctx, 0, sizeof (*ctx)); return ctx - tm->listener_ctx_pool; } void tls_listener_ctx_free (tls_ctx_t * ctx) { if (CLIB_DEBUG) memset (ctx, 0xfb, sizeof (*ctx)); pool_put (tls_main.listener_ctx_pool, ctx); } tls_ctx_t * tls_listener_ctx_get (u32 ctx_index) { return pool_elt_at_index (tls_main.listener_ctx_pool, ctx_index); } u32 tls_listener_ctx_index (tls_ctx_t * ctx) { return (ctx - tls_main.listener_ctx_pool); } u32 tls_ctx_half_open_alloc (void) { tls_main_t *tm = &tls_main; u8 will_expand = 0; tls_ctx_t *ctx; u32 ctx_index; pool_get_aligned_will_expand (tm->half_open_ctx_pool, will_expand, 0); if (PREDICT_FALSE (will_expand && vlib_num_workers ())) { clib_rwlock_writer_lock (&tm->half_open_rwlock); pool_get_zero (tm->half_open_ctx_pool, ctx); ctx->c_c_index = ctx - tm->half_open_ctx_pool; ctx_index = ctx->c_c_index; clib_rwlock_writer_unlock (&tm->half_open_rwlock); } else { /* reader lock assumption: only main thread will call pool_get */ clib_rwlock_reader_lock (&tm->half_open_rwlock); pool_get_zero (tm->half_open_ctx_pool, ctx); ctx->c_c_index = ctx - tm->half_open_ctx_pool; ctx_index = ctx->c_c_index; clib_rwlock_reader_unlock (&tm->half_open_rwlock); } return ctx_index; } void tls_ctx_half_open_free (u32 ho_index) { tls_main_t *tm = &tls_main; clib_rwlock_writer_lock (&tm->half_open_rwlock); pool_put_index (tls_main.half_open_ctx_pool, ho_index); clib_rwlock_writer_unlock (&tm->half_open_rwlock); } tls_ctx_t * tls_ctx_half_open_get (u32 ctx_index) { tls_main_t *tm = &tls_main; clib_rwlock_reader_lock (&tm->half_open_rwlock); return pool_elt_at_index (tm->half_open_ctx_pool, ctx_index); } void tls_ctx_half_open_reader_unlock () { clib_rwlock_reader_unlock (&tls_main.half_open_rwlock); } u32 tls_ctx_half_open_index (tls_ctx_t * ctx) { return (ctx - tls_main.half_open_ctx_pool); } void tls_notify_app_enqueue (tls_ctx_t * ctx, session_t * app_session) { app_worker_t *app_wrk; app_wrk = app_worker_get_if_valid (app_session->app_wrk_index); if (PREDICT_TRUE (app_wrk != 0)) tls_add_app_q_evt (app_wrk, app_session); } int tls_notify_app_accept (tls_ctx_t * ctx) { session_t *app_listener, *app_session; app_worker_t *app_wrk; tls_ctx_t *lctx; int rv; lctx = tls_listener_ctx_get (ctx->listener_ctx_index); app_listener = listen_session_get_from_handle (lctx->app_session_handle); app_session = session_get (ctx->c_s_index, ctx->c_thread_index); app_session->app_wrk_index = ctx->parent_app_wrk_index; app_session->connection_index = ctx->tls_ctx_handle; app_session->session_type = app_listener->session_type; app_session->listener_handle = listen_session_get_handle (app_listener); app_session->session_state = SESSION_STATE_ACCEPTING; if ((rv = app_worker_init_accepted (app_session))) { TLS_DBG (1, "failed to allocate fifos"); session_free (app_session); return rv; } ctx->app_session_handle = session_handle (app_session); ctx->parent_app_wrk_index = app_session->app_wrk_index; app_wrk = app_worker_get (app_session->app_wrk_index); return app_worker_accept_notify (app_wrk, app_session); } int tls_notify_app_connected (tls_ctx_t * ctx, session_error_t err) { session_t *app_session; app_worker_t *app_wrk; app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_index); if (!app_wrk) { tls_disconnect_transport (ctx); return -1; } if (err) { /* Free app session pre-allocated when transport was established */ if (ctx->tls_type == TRANSPORT_PROTO_TLS) session_free (session_get (ctx->c_s_index, ctx->c_thread_index)); ctx->no_app_session = 1; goto send_reply; } /* For DTLS the app session is not preallocated because the underlying udp * session might migrate to a different worker during the handshake */ if (ctx->tls_type == TRANSPORT_PROTO_DTLS) { session_type_t st; /* Cleanup half-open session as we don't get notification from udp */ session_half_open_delete_notify (&ctx->connection); app_session = session_alloc (ctx->c_thread_index); app_session->session_state = SESSION_STATE_CREATED; ctx->c_s_index = app_session->session_index; st = session_type_from_proto_and_ip (TRANSPORT_PROTO_DTLS, ctx->tcp_is_ip4); app_session->session_type = st; app_session->connection_index = ctx->tls_ctx_handle; } else { app_session = session_get (ctx->c_s_index, ctx->c_thread_index); } app_session->app_wrk_index = ctx->parent_app_wrk_index; if ((err = app_worker_init_connected (app_wrk, app_session))) goto failed; app_session->session_state = SESSION_STATE_READY; if (app_worker_connect_notify (app_wrk, app_session, SESSION_E_NONE, ctx->parent_app_api_context)) { TLS_DBG (1, "failed to notify app"); app_session->session_state = SESSION_STATE_CONNECTING; tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ()); return -1; } ctx->app_session_handle = session_handle (app_session); return 0; failed: ctx->no_app_session = 1; tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ()); send_reply: return app_worker_connect_notify (app_wrk, 0, err, ctx->parent_app_api_context); } static inline void tls_ctx_parse_handle (u32 ctx_handle, u32 * ctx_index, u32 * engine_type) { *ctx_index = ctx_handle & TLS_IDX_MASK; *engine_type = ctx_handle >> TLS_ENGINE_TYPE_SHIFT; } static inline crypto_engine_type_t tls_get_engine_type (crypto_engine_type_t requested, crypto_engine_type_t preferred) { if (requested != CRYPTO_ENGINE_NONE) { if (tls_vfts[requested].ctx_alloc) return r
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file
 * @brief IP prefix management on interfaces
 */

#ifndef included_ip_interface_h
#define included_ip_interface_h

#include <vnet/ip/lookup.h>

clib_error_t *ip_interface_address_add (ip_lookup_main_t * lm,
					u32 sw_if_index,
					void *address,
					u32 address_length,
					u32 * result_index);
clib_error_t *ip_interface_address_del (ip_lookup_main_t * lm,
					vnet_main_t * vnm,
					u32 addr_index, void *address,
					u32 address_length, u32 sw_if_index);
void *ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4);
void ip_interface_address_mark (void);
void ip_interface_address_sweep (void);
u32 ip_interface_address_find (ip_lookup_main_t * lm,
			       void *addr_fib, u32 address_length);
u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4);
walk_rc_t ip_interface_address_mark_one_interface (vnet_main_t *vnm,
						   vnet_sw_interface_t *si,
						   void *ctx);

always_inline void *
ip_interface_address_get_address (ip_lookup_main_t * lm,
				  ip_interface_address_t * a)
{
  return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key);
}

always_inline ip_interface_prefix_t *
ip_get_interface_prefix (ip_lookup_main_t * lm, ip_interface_prefix_key_t * k)
{
  uword *p = mhash_get (&lm->prefix_to_if_prefix_index, k);
  return p ? pool_elt_at_index (lm->if_prefix_pool, p[0]) : 0;
}

/* *INDENT-OFF* */
#define foreach_ip_interface_address(lm,a,sw_if_index,loop,body)        \
do {                                                                    \
    vnet_main_t *_vnm = vnet_get_main();                                \
    u32 _sw_if_index = sw_if_index;                                     \
    vnet_sw_interface_t *_swif;                                         \
    _swif = vnet_get_sw_interface (_vnm, _sw_if_index);                 \
                                                                        \
    /*                                                                  \
     * Loop => honor unnumbered interface addressing.                   \
     */                                                                 \
    if (_swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)               \
      {                                                                 \
        if (loop)                                                       \
          _sw_if_index = _swif->unnumbered_sw_if_index;                 \
        else                                                            \
          /* the interface is unnumbered, by the caller does not want   \
           * unnumbered interfaces considered/honoured */               \
          break;                                                        \
      }                                                                 \
    u32 _ia = ((vec_len((lm)->if_address_pool_index_by_sw_if_index)     \
                > (_sw_if_index)) ?                                     \
               vec_elt ((lm)->if_address_pool_index_by_sw_if_index,     \
                        (_sw_if_index)) :                               \
               (u32)~0);                                                \
    ip_interface_address_t * _a;                                        \
    while (_ia != ~0)                                                   \
    {                                                                   \
        _a = pool_elt_at_index ((lm)->if_address_pool, _ia);            \
        _ia = _a->next_this_sw_interface;                               \
        (a) = _a;                                                       \
        body;                                                           \
    }                                                                   \
} while (0)
/* *INDENT-ON* */

#endif /* included_ip_interface_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
(*args, u32); tls_ctx_t *ctx; ctx = tls_ctx_get_w_thread (ctx_index, thread_index); if (!ctx) return s; s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_ctx, ctx); if (verbose) { s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state, ctx); if (verbose > 1) s = format (s, "\n"); } return s; } u8 * format_tls_listener (u8 * s, va_list * args) { u32 tc_index = va_arg (*args, u32); u32 __clib_unused thread_index = va_arg (*args, u32); u32 verbose = va_arg (*args, u32); tls_ctx_t *ctx = tls_listener_ctx_get (tc_index); s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_listener_ctx, ctx); if (verbose) s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state, ctx); return s; } u8 * format_tls_half_open (u8 * s, va_list * args) { u32 ho_index = va_arg (*args, u32); u32 __clib_unused thread_index = va_arg (*args, u32); session_t *tcp_ho; tls_ctx_t *ho_ctx; ho_ctx = tls_ctx_half_open_get (ho_index); tcp_ho = session_get_from_handle (ho_ctx->tls_session_handle); s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u ts %d:%d", ho_ctx->c_thread_index, ho_ctx->c_s_index, "TLS", ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine, tcp_ho->thread_index, tcp_ho->session_index); tls_ctx_half_open_reader_unlock (); return s; } static void tls_transport_endpoint_get (u32 ctx_handle, u32 thread_index, transport_endpoint_t * tep, u8 is_lcl) { tls_ctx_t *ctx = tls_ctx_get_w_thread (ctx_handle, thread_index); session_t *tcp_session; tcp_session = session_get_from_handle (ctx->tls_session_handle); session_get_endpoint (tcp_session, tep, is_lcl); } static void tls_transport_listener_endpoint_get (u32 ctx_handle, transport_endpoint_t * tep, u8 is_lcl) { session_t *tls_listener; app_listener_t *al; tls_ctx_t *ctx = tls_listener_ctx_get (ctx_handle); al = app_listener_get_w_handle (ctx->tls_session_handle); tls_listener = app_listener_get_session (al); session_get_endpoint (tls_listener, tep, is_lcl); } static clib_error_t * tls_enable (vlib_main_t * vm, u8 is_en) { vnet_app_detach_args_t _da, *da = &_da; vnet_app_attach_args_t _a, *a = &_a; u64 options[APP_OPTIONS_N_OPTIONS]; tls_main_t *tm = &tls_main; u32 fifo_size = 128 << 12; if (!is_en) { da->app_index = tm->app_index; da->api_client_index = APP_INVALID_INDEX; vnet_application_detach (da); return 0; } fifo_size = tm->fifo_size ? tm->fifo_size : fifo_size; clib_memset (a, 0, sizeof (*a)); clib_memset (options, 0, sizeof (options)); a->session_cb_vft = &tls_app_cb_vft; a->api_client_index = APP_INVALID_INDEX; a->options = options; a->name = format (0, "tls"); a->options[APP_OPTIONS_SEGMENT_SIZE] = tm->first_seg_size; a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = tm->add_seg_size; a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size; a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size; a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP; if (vnet_application_attach (a)) { clib_warning ("failed to attach tls app"); return clib_error_return (0, "failed to attach tls app"); } tm->app_index = a->app_index; vec_free (a->name); return 0; } static const transport_proto_vft_t tls_proto = { .enable = tls_enable, .connect = tls_connect, .close = tls_disconnect, .start_listen = tls_start_listen, .stop_listen = tls_stop_listen, .get_connection = tls_connection_get, .get_listener = tls_listener_get, .get_half_open = tls_half_open_get, .cleanup_ho = tls_cleanup_ho, .custom_tx = tls_custom_tx_callback, .format_connection = format_tls_connection, .format_half_open = format_tls_half_open, .format_listener = format_tls_listener, .get_transport_endpoint = tls_transport_endpoint_get, .get_transport_listener_endpoint = tls_transport_listener_endpoint_get, .transport_options = { .name = "tls", .short_name = "J", .tx_type = TRANSPORT_TX_INTERNAL, .service_type = TRANSPORT_SERVICE_VC, }, }; int dtls_connect (transport_endpoint_cfg_t *tep) { vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs; transport_endpt_crypto_cfg_t *ccfg; crypto_engine_type_t engine_type; session_endpoint_cfg_t *sep; tls_main_t *tm = &tls_main; app_worker_t *app_wrk; application_t *app; tls_ctx_t *ctx; u32 ctx_handle; int rv; sep = (session_endpoint_cfg_t *) tep; if (!sep->ext_cfg) return -1; app_wrk = app_worker_get (sep->app_wrk_index); app = application_get (app_wrk->app_index); ccfg = &sep->ext_cfg->crypto; engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine); if (engine_type == CRYPTO_ENGINE_NONE) { clib_warning ("No tls engine_type available"); return -1; } ctx_handle = tls_ctx_alloc_w_thread (engine_type, transport_cl_thread ()); ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ()); ctx->parent_app_wrk_index = sep->app_wrk_index; ctx->parent_app_api_context = sep->opaque; ctx->tcp_is_ip4 = sep->is_ip4; ctx->ckpair_index = ccfg->ckpair_index; ctx->tls_type = sep->transport_proto; ctx->tls_ctx_handle = ctx_handle; ctx->c_proto = TRANSPORT_PROTO_DTLS; ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP; if (ccfg->hostname[0]) { ctx->srv_hostname = format (0, "%s", ccfg->hostname); vec_terminate_c_string (ctx->srv_hostname); } ctx->tls_ctx_engine = engine_type; clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t)); cargs->sep.transport_proto = TRANSPORT_PROTO_UDP; cargs->app_index = tm->app_index; cargs->api_context = ctx_handle; cargs->sep_ext.ns_index = app->ns_index; cargs->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED; if ((rv = vnet_connect (cargs))) return rv; TLS_DBG (1, "New DTLS connect request %x engine %d", ctx_handle, engine_type); return ctx_handle; } static transport_connection_t * dtls_half_open_get (u32 ho_index) { tls_ctx_t *ho_ctx; ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ()); return &ho_ctx->connection; } static void dtls_cleanup_callback (u32 ctx_index, u32 thread_index) { /* No op */ } static void dtls_cleanup_ho (u32 ho_index) { tls_ctx_t *ctx; ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ()); tls_ctx_free (ctx); } u8 * format_dtls_half_open (u8 *s, va_list *args) { u32 ho_index = va_arg (*args, u32); u32 __clib_unused thread_index = va_arg (*args, u32); tls_ctx_t *ho_ctx; session_t *us; ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ()); us = session_get_from_handle (ho_ctx->tls_session_handle); s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u us %d:%d", ho_ctx->c_thread_index, ho_ctx->c_s_index, "DTLS", ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine, us->thread_index, us->session_index); return s; } static const transport_proto_vft_t dtls_proto = { .enable = 0, .connect = dtls_connect, .close = tls_disconnect, .start_listen = tls_start_listen, .stop_listen = tls_stop_listen, .get_connection = tls_connection_get, .get_listener = tls_listener_get, .get_half_open = dtls_half_open_get, .custom_tx = tls_custom_tx_callback, .cleanup = dtls_cleanup_callback, .cleanup_ho = dtls_cleanup_ho, .format_connection = format_tls_connection, .format_half_open = format_dtls_half_open, .format_listener = format_tls_listener, .get_transport_endpoint = tls_transport_endpoint_get, .get_transport_listener_endpoint = tls_transport_listener_endpoint_get, .transport_options = { .name = "dtls", .short_name = "D", .tx_type = TRANSPORT_TX_INTERNAL, .service_type = TRANSPORT_SERVICE_VC, }, }; void tls_register_engine (const tls_engine_vft_t * vft, crypto_engine_type_t type) { vec_validate (tls_vfts, type); tls_vfts[type] = *vft; } static clib_error_t * tls_init (vlib_main_t * vm) { vlib_thread_main_t *vtm = vlib_get_thread_main (); tls_main_t *tm = &tls_main; u32 num_threads; num_threads = 1 /* main thread */ + vtm->n_threads; if (!tm->ca_cert_path) tm->ca_cert_path = TLS_CA_CERT_PATH; clib_rwlock_init (&tm->half_open_rwlock); vec_validate (tm->rx_bufs, num_threads - 1); vec_validate (tm->tx_bufs, num_threads - 1); tm->first_seg_size = 32 << 20; tm->add_seg_size = 256 << 20; transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto, FIB_PROTOCOL_IP4, ~0); transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto, FIB_PROTOCOL_IP6, ~0); transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto, FIB_PROTOCOL_IP4, ~0); transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto, FIB_PROTOCOL_IP6, ~0); return 0; } VLIB_INIT_FUNCTION (tls_init); static clib_error_t * tls_config_fn (vlib_main_t * vm, unformat_input_t * input) { tls_main_t *tm = &tls_main; uword tmp; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "use-test-cert-in-ca")) tm->use_test_cert_in_ca = 1; else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path)) ; else if (unformat (input, "first-segment-size %U", unformat_memory_size, &tm->first_seg_size)) ; else if (unformat (input, "add-segment-size %U", unformat_memory_size, &tm->add_seg_size)) ; else if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp)) { if (tmp >= 0x100000000ULL) { return clib_error_return (0, "fifo-size %llu (0x%llx) too large", tmp, tmp); } tm->fifo_size = tmp; } else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } return 0; } VLIB_EARLY_CONFIG_FUNCTION (tls_config_fn, "tls"); tls_main_t * vnet_tls_get_main (void) { return &tls_main; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */