Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 4 | * Permission to use, copy, modify, and/or distribute this software for |
| 5 | * any purpose with or without fee is hereby granted, provided that the |
| 6 | * above copyright notice and this permission notice appear in all copies. |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 13 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | ************************************************************************** |
| 15 | */ |
| 16 | |
| 17 | #include "nss_tx_rx_common.h" |
| 18 | #include "nss_dtls_stats.h" |
Sachin Shashidhar | dd91a4d | 2018-06-04 14:52:12 -0700 | [diff] [blame] | 19 | #include "nss_dtls_log.h" |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 20 | |
| 21 | #define NSS_DTLS_TX_TIMEOUT 3000 /* 3 Seconds */ |
| 22 | |
| 23 | /* |
| 24 | * Data structures to store DTLS nss debug stats |
| 25 | */ |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 26 | static DEFINE_SPINLOCK(nss_dtls_session_stats_lock); |
| 27 | static struct nss_dtls_stats_session session_stats[NSS_MAX_DTLS_SESSIONS]; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Private data structure |
| 31 | */ |
| 32 | static struct nss_dtls_pvt { |
| 33 | struct semaphore sem; |
| 34 | struct completion complete; |
| 35 | int response; |
| 36 | void *cb; |
| 37 | void *app_data; |
| 38 | } dtls_pvt; |
| 39 | |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 40 | /* |
| 41 | * nss_dtls_verify_if_num() |
| 42 | * Verify if_num passed to us. |
| 43 | */ |
| 44 | static bool nss_dtls_verify_if_num(uint32_t if_num) |
| 45 | { |
| 46 | if (nss_is_dynamic_interface(if_num) == false) |
| 47 | return false; |
| 48 | |
Stephen Wang | e8b8d0d | 2017-02-24 17:05:22 -0800 | [diff] [blame] | 49 | if (nss_dynamic_interface_get_type(nss_dtls_get_context(), if_num) |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 50 | != NSS_DYNAMIC_INTERFACE_TYPE_DTLS) |
| 51 | return false; |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * nss_dtls_session_stats_sync |
| 58 | * Per DTLS session debug stats |
| 59 | */ |
| 60 | static void nss_dtls_session_stats_sync(struct nss_ctx_instance *nss_ctx, |
| 61 | struct nss_dtls_session_stats *stats_msg, |
| 62 | uint16_t if_num) |
| 63 | { |
| 64 | int i; |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 65 | struct nss_dtls_stats_session *s = NULL; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 66 | |
| 67 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 68 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 69 | spin_lock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 70 | for (i = 0; i < NSS_MAX_DTLS_SESSIONS; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 71 | if (session_stats[i].if_num != if_num) { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 72 | continue; |
| 73 | } |
| 74 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 75 | s = &session_stats[i]; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 76 | break; |
| 77 | } |
| 78 | |
| 79 | if (!s) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 80 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 84 | s->stats[NSS_DTLS_STATS_SESSION_RX_PKTS] += stats_msg->node_stats.rx_packets; |
| 85 | s->stats[NSS_DTLS_STATS_SESSION_TX_PKTS] += stats_msg->node_stats.tx_packets; |
ratheesh kannoth | 93ba95c | 2017-07-13 15:52:52 +0530 | [diff] [blame] | 86 | for (i = 0; i < NSS_MAX_NUM_PRI; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 87 | s->stats[NSS_DTLS_STATS_SESSION_RX_QUEUE_0_DROPPED + i] += stats_msg->node_stats.rx_dropped[i]; |
ratheesh kannoth | 93ba95c | 2017-07-13 15:52:52 +0530 | [diff] [blame] | 88 | } |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 89 | s->stats[NSS_DTLS_STATS_SESSION_RX_AUTH_DONE] += stats_msg->rx_auth_done; |
| 90 | s->stats[NSS_DTLS_STATS_SESSION_TX_AUTH_DONE] += stats_msg->tx_auth_done; |
| 91 | s->stats[NSS_DTLS_STATS_SESSION_RX_CIPHER_DONE] += stats_msg->rx_cipher_done; |
| 92 | s->stats[NSS_DTLS_STATS_SESSION_TX_CIPHER_DONE] += stats_msg->tx_cipher_done; |
| 93 | s->stats[NSS_DTLS_STATS_SESSION_RX_CBUF_ALLOC_FAIL] += stats_msg->rx_cbuf_alloc_fail; |
| 94 | s->stats[NSS_DTLS_STATS_SESSION_TX_CBUF_ALLOC_FAIL] += stats_msg->tx_cbuf_alloc_fail; |
| 95 | s->stats[NSS_DTLS_STATS_SESSION_TX_CENQUEUE_FAIL] += stats_msg->tx_cenqueue_fail; |
| 96 | s->stats[NSS_DTLS_STATS_SESSION_RX_CENQUEUE_FAIL] += stats_msg->rx_cenqueue_fail; |
| 97 | s->stats[NSS_DTLS_STATS_SESSION_TX_DROPPED_HROOM] += stats_msg->tx_dropped_hroom; |
| 98 | s->stats[NSS_DTLS_STATS_SESSION_TX_DROPPED_TROOM] += stats_msg->tx_dropped_troom; |
| 99 | s->stats[NSS_DTLS_STATS_SESSION_TX_FORWARD_ENQUEUE_FAIL] += stats_msg->tx_forward_enqueue_fail; |
| 100 | s->stats[NSS_DTLS_STATS_SESSION_RX_FORWARD_ENQUEUE_FAIL] += stats_msg->rx_forward_enqueue_fail; |
| 101 | s->stats[NSS_DTLS_STATS_SESSION_RX_INVALID_VERSION] += stats_msg->rx_invalid_version; |
| 102 | s->stats[NSS_DTLS_STATS_SESSION_RX_INVALID_EPOCH] += stats_msg->rx_invalid_epoch; |
| 103 | s->stats[NSS_DTLS_STATS_SESSION_RX_MALFORMED] += stats_msg->rx_malformed; |
| 104 | s->stats[NSS_DTLS_STATS_SESSION_RX_CIPHER_FAIL] += stats_msg->rx_cipher_fail; |
| 105 | s->stats[NSS_DTLS_STATS_SESSION_RX_AUTH_FAIL] += stats_msg->rx_auth_fail; |
| 106 | s->stats[NSS_DTLS_STATS_SESSION_RX_CAPWAP_CLASSIFY_FAIL] += stats_msg->rx_capwap_classify_fail; |
| 107 | s->stats[NSS_DTLS_STATS_SESSION_RX_SINGLE_REC_DGRAM] += stats_msg->rx_single_rec_dgram; |
| 108 | s->stats[NSS_DTLS_STATS_SESSION_RX_MULTI_REC_DGRAM] += stats_msg->rx_multi_rec_dgram; |
| 109 | s->stats[NSS_DTLS_STATS_SESSION_RX_REPLAY_FAIL] += stats_msg->rx_replay_fail; |
| 110 | s->stats[NSS_DTLS_STATS_SESSION_RX_REPLAY_DUPLICATE] += stats_msg->rx_replay_duplicate; |
| 111 | s->stats[NSS_DTLS_STATS_SESSION_RX_REPLAY_OUT_OF_WINDOW] += stats_msg->rx_replay_out_of_window; |
| 112 | s->stats[NSS_DTLS_STATS_SESSION_OUTFLOW_QUEUE_FULL] += stats_msg->outflow_queue_full; |
| 113 | s->stats[NSS_DTLS_STATS_SESSION_DECAP_QUEUE_FULL] += stats_msg->decap_queue_full; |
| 114 | s->stats[NSS_DTLS_STATS_SESSION_PBUF_ALLOC_FAIL] += stats_msg->pbuf_alloc_fail; |
| 115 | s->stats[NSS_DTLS_STATS_SESSION_PBUF_COPY_FAIL] += stats_msg->pbuf_copy_fail; |
| 116 | s->stats[NSS_DTLS_STATS_SESSION_EPOCH] = stats_msg->epoch; |
| 117 | s->stats[NSS_DTLS_STATS_SESSION_TX_SEQ_HIGH] = stats_msg->tx_seq_high; |
| 118 | s->stats[NSS_DTLS_STATS_SESSION_TX_SEQ_LOW] = stats_msg->tx_seq_low; |
| 119 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 123 | * nss_dtls_session_stats_get() |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 124 | * Get session DTLS statitics. |
| 125 | */ |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 126 | void nss_dtls_session_stats_get(struct nss_dtls_stats_session *stats) |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 127 | { |
| 128 | int i; |
| 129 | |
| 130 | if (!stats) { |
| 131 | nss_warning("No memory to copy dtls session stats"); |
| 132 | return; |
| 133 | } |
| 134 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 135 | spin_lock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 136 | for (i = 0; i < NSS_MAX_DTLS_SESSIONS; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 137 | if (session_stats[i].valid) { |
| 138 | memcpy(stats, &session_stats[i], |
| 139 | sizeof(struct nss_dtls_stats_session)); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 140 | stats++; |
| 141 | } |
| 142 | } |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 143 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * nss_dtls_handler() |
| 148 | * Handle NSS -> HLOS messages for dtls tunnel |
| 149 | */ |
| 150 | static void nss_dtls_handler(struct nss_ctx_instance *nss_ctx, |
| 151 | struct nss_cmn_msg *ncm, |
| 152 | __attribute__((unused))void *app_data) |
| 153 | { |
| 154 | struct nss_dtls_msg *ntm = (struct nss_dtls_msg *)ncm; |
| 155 | void *ctx; |
| 156 | |
| 157 | nss_dtls_msg_callback_t cb; |
| 158 | |
| 159 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 160 | BUG_ON(!nss_dtls_verify_if_num(ncm->interface)); |
| 161 | |
| 162 | /* |
| 163 | * Is this a valid request/response packet? |
| 164 | */ |
| 165 | if (ncm->type >= NSS_DTLS_MSG_MAX) { |
| 166 | nss_warning("%p: received invalid message %d " |
| 167 | "for DTLS interface %d", |
| 168 | nss_ctx, ncm->type, ncm->interface); |
| 169 | return; |
| 170 | } |
| 171 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 172 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_dtls_msg)) { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 173 | nss_warning("%p: dtls message length is invalid: %d", |
| 174 | nss_ctx, ncm->len); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | switch (ntm->cm.type) { |
| 179 | case NSS_DTLS_MSG_SESSION_STATS: |
| 180 | nss_dtls_session_stats_sync(nss_ctx, |
| 181 | &ntm->msg.stats, |
| 182 | ncm->interface); |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Update the callback and app_data for NOTIFY messages |
| 188 | */ |
Suruchi Agarwal | e4ad24a | 2018-06-11 12:03:46 +0530 | [diff] [blame] | 189 | if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 190 | ncm->cb = (nss_ptr_t)nss_ctx->nss_top->dtls_msg_callback; |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 191 | ncm->app_data = (nss_ptr_t)nss_ctx->subsys_dp_register[ncm->interface].app_data; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Log failures |
| 196 | */ |
| 197 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 198 | |
| 199 | /* |
Sachin Shashidhar | dd91a4d | 2018-06-04 14:52:12 -0700 | [diff] [blame] | 200 | * Trace messages. |
| 201 | */ |
| 202 | nss_dtls_log_rx_msg(ntm); |
| 203 | |
| 204 | /* |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 205 | * callback |
| 206 | */ |
| 207 | cb = (nss_dtls_msg_callback_t)ncm->cb; |
| 208 | ctx = (void *)ncm->app_data; |
| 209 | |
| 210 | /* |
| 211 | * call dtls session callback |
| 212 | */ |
| 213 | if (!cb) { |
| 214 | nss_warning("%p: No callback for dtls session interface %d", |
| 215 | nss_ctx, ncm->interface); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | cb(ctx, ntm); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * nss_dtls_callback() |
| 224 | * Callback to handle the completion of NSS->HLOS messages. |
| 225 | */ |
| 226 | static void nss_dtls_callback(void *app_data, struct nss_dtls_msg *nim) |
| 227 | { |
| 228 | nss_dtls_msg_callback_t callback = (nss_dtls_msg_callback_t)dtls_pvt.cb; |
| 229 | void *data = dtls_pvt.app_data; |
| 230 | |
| 231 | dtls_pvt.cb = NULL; |
| 232 | dtls_pvt.app_data = NULL; |
| 233 | |
| 234 | if (nim->cm.response != NSS_CMN_RESPONSE_ACK) { |
| 235 | nss_warning("dtls Error response %d\n", nim->cm.response); |
| 236 | |
| 237 | dtls_pvt.response = NSS_TX_FAILURE; |
| 238 | if (callback) { |
| 239 | callback(data, nim); |
| 240 | } |
| 241 | |
| 242 | complete(&dtls_pvt.complete); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | dtls_pvt.response = NSS_TX_SUCCESS; |
| 247 | if (callback) { |
| 248 | callback(data, nim); |
| 249 | } |
| 250 | |
| 251 | complete(&dtls_pvt.complete); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * nss_dtls_tx_buf() |
| 256 | * Transmit buffer over DTLS interface |
| 257 | */ |
| 258 | nss_tx_status_t nss_dtls_tx_buf(struct sk_buff *skb, uint32_t if_num, |
| 259 | struct nss_ctx_instance *nss_ctx) |
| 260 | { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 261 | BUG_ON(!nss_dtls_verify_if_num(if_num)); |
| 262 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 263 | return nss_core_send_packet(nss_ctx, skb, if_num, H2N_BIT_FLAG_VIRTUAL_BUFFER); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 264 | } |
| 265 | EXPORT_SYMBOL(nss_dtls_tx_buf); |
| 266 | |
| 267 | /* |
| 268 | * nss_dtls_tx_msg() |
| 269 | * Transmit a DTLS message to NSS firmware |
| 270 | */ |
| 271 | nss_tx_status_t nss_dtls_tx_msg(struct nss_ctx_instance *nss_ctx, |
| 272 | struct nss_dtls_msg *msg) |
| 273 | { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 274 | struct nss_cmn_msg *ncm = &msg->cm; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * Sanity check the message |
| 278 | */ |
| 279 | BUG_ON(!nss_dtls_verify_if_num(ncm->interface)); |
| 280 | |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 281 | if (ncm->type > NSS_DTLS_MSG_MAX) { |
| 282 | nss_warning("%p: dtls message type out of range: %d", |
| 283 | nss_ctx, ncm->type); |
| 284 | return NSS_TX_FAILURE; |
| 285 | } |
| 286 | |
Sachin Shashidhar | dd91a4d | 2018-06-04 14:52:12 -0700 | [diff] [blame] | 287 | /* |
| 288 | * Trace messages. |
| 289 | */ |
| 290 | nss_dtls_log_tx_msg(msg); |
| 291 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 292 | return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 293 | } |
| 294 | EXPORT_SYMBOL(nss_dtls_tx_msg); |
| 295 | |
| 296 | /* |
| 297 | * nss_dtls_tx_msg() |
| 298 | * Transmit a DTLS message to NSS firmware synchronously. |
| 299 | */ |
| 300 | nss_tx_status_t nss_dtls_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_dtls_msg *msg) |
| 301 | { |
| 302 | |
| 303 | nss_tx_status_t status; |
| 304 | int ret = 0; |
| 305 | |
| 306 | down(&dtls_pvt.sem); |
| 307 | dtls_pvt.cb = (void *)msg->cm.cb; |
| 308 | dtls_pvt.app_data = (void *)msg->cm.app_data; |
| 309 | |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 310 | msg->cm.cb = (nss_ptr_t)nss_dtls_callback; |
| 311 | msg->cm.app_data = (nss_ptr_t)NULL; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 312 | |
| 313 | status = nss_dtls_tx_msg(nss_ctx, msg); |
| 314 | if (status != NSS_TX_SUCCESS) { |
| 315 | nss_warning("%p: dtls_tx_msg failed\n", nss_ctx); |
| 316 | up(&dtls_pvt.sem); |
| 317 | return status; |
| 318 | } |
| 319 | |
| 320 | ret = wait_for_completion_timeout(&dtls_pvt.complete, msecs_to_jiffies(NSS_DTLS_TX_TIMEOUT)); |
| 321 | |
| 322 | if (!ret) { |
| 323 | nss_warning("%p: DTLS msg tx failed due to timeout\n", nss_ctx); |
| 324 | dtls_pvt.response = NSS_TX_FAILURE; |
| 325 | } |
| 326 | |
| 327 | status = dtls_pvt.response; |
| 328 | up(&dtls_pvt.sem); |
| 329 | return status; |
| 330 | } |
| 331 | EXPORT_SYMBOL(nss_dtls_tx_msg_sync); |
| 332 | |
| 333 | /* |
| 334 | *********************************** |
| 335 | * Register/Unregister/Miscellaneous APIs |
| 336 | *********************************** |
| 337 | */ |
| 338 | |
| 339 | /* |
| 340 | * nss_dtls_register_if() |
| 341 | */ |
| 342 | struct nss_ctx_instance *nss_dtls_register_if(uint32_t if_num, |
| 343 | nss_dtls_data_callback_t cb, |
| 344 | nss_dtls_msg_callback_t ev_cb, |
| 345 | struct net_device *netdev, |
| 346 | uint32_t features, |
| 347 | void *app_ctx) |
| 348 | { |
| 349 | int32_t i; |
| 350 | |
| 351 | struct nss_ctx_instance *nss_ctx = nss_dtls_get_context(); |
| 352 | |
| 353 | BUG_ON(!nss_dtls_verify_if_num(if_num)); |
| 354 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 355 | spin_lock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 356 | for (i = 0; i < NSS_MAX_DTLS_SESSIONS; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 357 | if (!session_stats[i].valid) { |
| 358 | session_stats[i].valid = true; |
| 359 | session_stats[i].if_num = if_num; |
| 360 | session_stats[i].if_index = netdev->ifindex; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 361 | break; |
| 362 | } |
| 363 | } |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 364 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 365 | |
| 366 | if (i == NSS_MAX_DTLS_SESSIONS) { |
| 367 | nss_warning("%p: Cannot find free slot for " |
| 368 | "DTLS session stats, I/F:%u\n", nss_ctx, if_num); |
| 369 | return NULL; |
| 370 | } |
| 371 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 372 | if (nss_ctx->subsys_dp_register[if_num].ndev) { |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 373 | nss_warning("%p: Cannot find free slot for " |
| 374 | "DTLS NSS I/F:%u\n", nss_ctx, if_num); |
| 375 | |
| 376 | return NULL; |
| 377 | } |
| 378 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 379 | nss_core_register_subsys_dp(nss_ctx, if_num, cb, NULL, app_ctx, netdev, features); |
Tanmay V Jagdale | a8533d5 | 2018-04-24 21:40:02 +0530 | [diff] [blame] | 380 | nss_ctx->subsys_dp_register[if_num].type = NSS_DYNAMIC_INTERFACE_TYPE_DTLS; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 381 | |
| 382 | nss_top_main.dtls_msg_callback = ev_cb; |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 383 | nss_core_register_handler(nss_ctx, if_num, nss_dtls_handler, app_ctx); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 384 | |
Stephen Wang | 90c67de | 2016-04-26 15:15:59 -0700 | [diff] [blame] | 385 | return nss_ctx; |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 386 | } |
| 387 | EXPORT_SYMBOL(nss_dtls_register_if); |
| 388 | |
| 389 | /* |
| 390 | * nss_dtls_unregister_if() |
| 391 | */ |
| 392 | void nss_dtls_unregister_if(uint32_t if_num) |
| 393 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 394 | struct nss_ctx_instance *nss_ctx = nss_dtls_get_context(); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 395 | int32_t i; |
| 396 | |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 397 | BUG_ON(!nss_dtls_verify_if_num(if_num)); |
| 398 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 399 | spin_lock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 400 | for (i = 0; i < NSS_MAX_DTLS_SESSIONS; i++) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 401 | if (session_stats[i].if_num == if_num) { |
| 402 | memset(&session_stats[i], 0, |
| 403 | sizeof(struct nss_dtls_stats_session)); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | } |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 407 | spin_unlock_bh(&nss_dtls_session_stats_lock); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 408 | |
| 409 | if (i == NSS_MAX_DTLS_SESSIONS) { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 410 | nss_warning("%p: Cannot find debug stats for DTLS session %d\n", nss_ctx, if_num); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 414 | if (!nss_ctx->subsys_dp_register[if_num].ndev) { |
| 415 | nss_warning("%p: Cannot find registered netdev for DTLS NSS I/F:%u\n", nss_ctx, if_num); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 416 | |
| 417 | return; |
| 418 | } |
| 419 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 420 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 421 | |
| 422 | nss_top_main.dtls_msg_callback = NULL; |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 423 | nss_core_unregister_handler(nss_ctx, if_num); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 424 | } |
| 425 | EXPORT_SYMBOL(nss_dtls_unregister_if); |
| 426 | |
| 427 | /* |
| 428 | * nss_get_dtls_context() |
| 429 | */ |
| 430 | struct nss_ctx_instance *nss_dtls_get_context(void) |
| 431 | { |
| 432 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.dtls_handler_id]; |
| 433 | } |
| 434 | EXPORT_SYMBOL(nss_dtls_get_context); |
| 435 | |
| 436 | /* |
| 437 | * nss_dtls_msg_init() |
| 438 | * Initialize nss_dtls msg. |
| 439 | */ |
| 440 | void nss_dtls_msg_init(struct nss_dtls_msg *ncm, uint16_t if_num, |
| 441 | uint32_t type, uint32_t len, void *cb, void *app_data) |
| 442 | { |
| 443 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 444 | } |
| 445 | EXPORT_SYMBOL(nss_dtls_msg_init); |
| 446 | |
| 447 | /* |
| 448 | * nss_dtls_get_ifnum_with_coreid() |
| 449 | */ |
| 450 | int32_t nss_dtls_get_ifnum_with_coreid(int32_t if_num) |
| 451 | { |
| 452 | struct nss_ctx_instance *nss_ctx = nss_dtls_get_context(); |
| 453 | |
| 454 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 455 | return NSS_INTERFACE_NUM_APPEND_COREID(nss_ctx, if_num); |
| 456 | } |
| 457 | EXPORT_SYMBOL(nss_dtls_get_ifnum_with_coreid); |
| 458 | |
| 459 | /* |
| 460 | * nss_dtls_register_handler() |
| 461 | */ |
| 462 | void nss_dtls_register_handler(void) |
| 463 | { |
| 464 | sema_init(&dtls_pvt.sem, 1); |
| 465 | init_completion(&dtls_pvt.complete); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 466 | |
| 467 | nss_dtls_stats_dentry_create(); |
Tushar Mathur | ff8741b | 2015-12-02 20:28:59 +0530 | [diff] [blame] | 468 | } |