3 * Copyright (c) 2020 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include <openssl/bio.h>
17 #include <openssl/err.h>
18 #include <vnet/session/session.h>
19 #include <vnet/session/application_interface.h>
21 static inline session_t *
22 bio_session (BIO * bio)
24 return session_get_from_handle (pointer_to_uword (BIO_get_data (bio)));
28 bio_tls_alloc (BIO * bio)
30 BIO_set_init (bio, 0);
31 BIO_set_data (bio, 0);
32 BIO_set_flags (bio, 0);
33 BIO_set_shutdown (bio, 0);
38 bio_tls_free (BIO * bio)
43 if (BIO_get_shutdown (bio))
45 if (BIO_get_init (bio))
46 session_close (bio_session (bio));
47 BIO_set_init (bio, 0);
48 BIO_set_flags (bio, 0);
55 bio_tls_read (BIO * b, char *out, int outl)
60 if (PREDICT_FALSE (!out))
66 clib_warning ("no session");
71 rv = app_recv_stream_raw (s->rx_fifo, (u8 *) out, outl,
72 0 /* clear evt */ , 0 /* peek */ );
75 BIO_set_retry_read (b);
80 if (svm_fifo_needs_deq_ntf (s->rx_fifo, rv))
82 svm_fifo_clear_deq_ntf (s->rx_fifo);
83 session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_RX);
86 if (svm_fifo_is_empty_cons (s->rx_fifo))
87 svm_fifo_unset_event (s->rx_fifo);
89 BIO_clear_retry_flags (b);
95 bio_tls_write (BIO * b, const char *in, int inl)
101 if (PREDICT_FALSE (!in))
107 clib_warning ("no session");
112 mq = session_main_get_vpp_event_queue (s->thread_index);
113 rv = app_send_stream_raw (s->tx_fifo, mq, (u8 *) in, inl,
114 SESSION_IO_EVT_TX, 1 /* do_evt */ ,
118 BIO_set_retry_write (b);
123 BIO_clear_retry_flags (b);
129 bio_tls_ctrl (BIO * b, int cmd, long larg, void *ptr)
141 case BIO_CTRL_GET_CLOSE:
142 ret = BIO_get_shutdown (b);
144 case BIO_CTRL_SET_CLOSE:
145 BIO_set_shutdown (b, (int) larg);
151 case BIO_CTRL_PENDING:
162 BIO_new_tls (session_handle_t sh)
164 static BIO_METHOD *tls_bio_method;
168 tls_bio_method = BIO_meth_new (BIO_TYPE_SOCKET, "tls_bio");
169 BIO_meth_set_write (tls_bio_method, bio_tls_write);
170 BIO_meth_set_read (tls_bio_method, bio_tls_read);
171 BIO_meth_set_create (tls_bio_method, bio_tls_alloc);
172 BIO_meth_set_destroy (tls_bio_method, bio_tls_free);
173 BIO_meth_set_ctrl (tls_bio_method, bio_tls_ctrl);
175 b = BIO_new (tls_bio_method);
176 /* Initialize the BIO */
177 BIO_set_data (b, uword_to_pointer (sh, void *));
183 * fd.io coding-style-patch-verification: ON
186 * eval: (c-set-style "gnu")