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 <openssl/bio.h>
17 #include <openssl/err.h>
19 #include <vnet/session/session.h>
20 #include <vnet/session/application_interface.h>
22 static inline session_t *
23 bio_session (BIO *bio)
25 return session_get_from_handle (pointer_to_uword (BIO_get_data (bio)));
29 bio_dtls_alloc (BIO *bio)
31 BIO_set_init (bio, 0);
32 BIO_set_data (bio, 0);
33 BIO_set_flags (bio, 0);
34 BIO_set_shutdown (bio, 0);
39 bio_dtls_free (BIO *bio)
44 if (BIO_get_shutdown (bio))
46 if (BIO_get_init (bio))
47 session_close (bio_session (bio));
48 BIO_set_init (bio, 0);
49 BIO_set_flags (bio, 0);
55 bio_dtls_read (BIO *b, char *out, int outl)
57 app_session_transport_t at;
61 if (PREDICT_FALSE (!out))
67 clib_warning ("no session");
72 rv = app_recv_dgram_raw (s->rx_fifo, (u8 *) out, outl, &at,
73 0 /* clear evt */, 0 /* peek */);
77 BIO_set_retry_read (b);
82 if (svm_fifo_is_empty_cons (s->rx_fifo))
83 svm_fifo_unset_event (s->rx_fifo);
85 BIO_clear_retry_flags (b);
91 bio_dtls_write (BIO *b, const char *in, int inl)
93 app_session_transport_t at = { 0 };
98 if (PREDICT_FALSE (!in))
104 clib_warning ("no session");
109 mq = session_main_get_vpp_event_queue (s->thread_index);
110 rv = app_send_dgram_raw (s->tx_fifo, &at, mq, (u8 *) in, inl,
111 SESSION_IO_EVT_TX, 1 /* do_evt */, 0 /* noblock */);
115 BIO_set_retry_read (b);
120 BIO_clear_retry_flags (b);
126 dtls_dgram_overhead (BIO *b)
128 session_t *s = bio_session (b);
129 if (session_type_is_ip4 (s->session_type))
138 dtls_dgram_mss (BIO *b)
140 session_t *s = bio_session (b);
141 transport_send_params_t sp;
143 transport_connection_snd_params (session_get_transport (s), &sp);
149 bio_dtls_ctrl (BIO *b, int cmd, long larg, void *parg)
161 case BIO_CTRL_GET_CLOSE:
162 ret = BIO_get_shutdown (b);
164 case BIO_CTRL_SET_CLOSE:
165 BIO_set_shutdown (b, (int) larg);
167 case BIO_CTRL_PENDING:
168 case BIO_CTRL_WPENDING:
175 case BIO_CTRL_DGRAM_QUERY_MTU:
176 ret = dtls_dgram_mss (b);
178 case BIO_CTRL_DGRAM_SET_MTU:
181 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
184 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
185 ret = dtls_dgram_overhead (b);
195 BIO_new_dtls (session_handle_t sh)
197 static BIO_METHOD *dtls_bio_method;
200 if (!dtls_bio_method)
202 dtls_bio_method = BIO_meth_new (BIO_TYPE_SOCKET, "dtls_bio");
203 BIO_meth_set_write (dtls_bio_method, bio_dtls_write);
204 BIO_meth_set_read (dtls_bio_method, bio_dtls_read);
205 BIO_meth_set_create (dtls_bio_method, bio_dtls_alloc);
206 BIO_meth_set_destroy (dtls_bio_method, bio_dtls_free);
207 BIO_meth_set_ctrl (dtls_bio_method, bio_dtls_ctrl);
210 b = BIO_new (dtls_bio_method);
212 /* Initialize the BIO */
213 BIO_set_data (b, uword_to_pointer (sh, void *));
220 * fd.io coding-style-patch-verification: ON
223 * eval: (c-set-style "gnu")