blob: e27535b85f416a250381501eea9a89b0e51b7aff [file] [log] [blame]
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +05301/*
2 **************************************************************************
3 * Copyright (c) 2013 - 2014, The Linux Foundation. All rights reserved.
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/*
18 * nss_ipsec.c
19 * NSS IPsec APIs
20 */
21
22#include "nss_tx_rx_common.h"
Murat Sezginea1a4352014-04-15 19:09:51 -070023#include "nss_ipsec.h"
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053024
25/*
26 **********************************
27 General APIs
28 **********************************
29 */
30
31#define nss_ipsec_warning(fmt, arg...) nss_warning("IPsec:"fmt, ##arg)
32#define nss_ipsec_info(fmt, arg...) nss_info("IPsec:"fmt, ##arg)
33#define nss_ipsec_trace(fmt, arg...) nss_trace("IPsec:"fmt, ##arg)
34
35/*
36 * nss_ipsec_set_msg_callback()
37 * this sets the message callback handler and its associated context
38 */
39static inline nss_tx_status_t nss_ipsec_set_msg_callback(struct nss_ctx_instance *nss_ctx, uint32_t if_num,
40 nss_ipsec_msg_callback_t cb, void *ipsec_ctx)
41{
42 struct nss_top_instance *nss_top;
43
44 nss_top = nss_ctx->nss_top;
45
46 switch (if_num) {
47 case NSS_IPSEC_ENCAP_IF_NUMBER:
48 nss_top->ipsec_encap_ctx = ipsec_ctx;
49 nss_top->ipsec_encap_callback = cb;
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +053050 break;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053051
52 case NSS_IPSEC_DECAP_IF_NUMBER:
53 nss_top->ipsec_decap_ctx = ipsec_ctx;
54 nss_top->ipsec_decap_callback = cb;
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +053055 break;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053056
57 default:
58 nss_ipsec_warning("%p: cannot 'set' message callback, incorrect I/F: %d", nss_ctx, if_num);
59 return NSS_TX_FAILURE;
60 }
61
62 return NSS_TX_SUCCESS;
63}
64
65/*
66 * nss_ipsec_get_msg_callback()
67 * this gets the message callback handler and its associated context
68 */
69static inline nss_ipsec_msg_callback_t nss_ipsec_get_msg_callback(struct nss_ctx_instance *nss_ctx, uint32_t if_num, void **ipsec_ctx)
70{
71 struct nss_top_instance *nss_top;
72
73 nss_top = nss_ctx->nss_top;
74
75 switch (if_num) {
76 case NSS_IPSEC_ENCAP_IF_NUMBER:
77 *ipsec_ctx = nss_top->ipsec_encap_ctx;
78 return nss_top->ipsec_encap_callback;
79
80 case NSS_IPSEC_DECAP_IF_NUMBER:
81 *ipsec_ctx = nss_top->ipsec_decap_ctx;
82 return nss_top->ipsec_decap_callback;
83
84 default:
85 *ipsec_ctx = NULL;
86 nss_ipsec_warning("%p: cannot 'get' message callback, incorrect I/F: %d", nss_ctx, if_num);
87 return NULL;
88 }
89}
90
91/*
92 **********************************
93 Rx APIs
94 **********************************
95 */
96
97/*
98 * nss_ipsec_msg_handler()
99 * this handles all the IPsec events and responses
100 */
101static void nss_ipsec_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data __attribute((unused)))
102{
103 struct nss_ipsec_msg *nim = (struct nss_ipsec_msg *)ncm;
104 nss_ipsec_msg_callback_t cb = NULL;
105 uint32_t if_num = ncm->interface;
106 void *ipsec_ctx;
107
108 /*
109 * Sanity check the message type
110 */
111 if (ncm->type > NSS_IPSEC_MSG_TYPE_MAX) {
112 nss_ipsec_warning("%p: rx message type out of range: %d", nss_ctx, ncm->type);
113 return;
114 }
115
116 if (ncm->len > sizeof(struct nss_ipsec_msg)) {
117 nss_ipsec_warning("%p: rx request for another interface: %d", nss_ctx, ncm->interface);
118 return;
119 }
120
121 if ((ncm->interface != NSS_IPSEC_ENCAP_IF_NUMBER) && (ncm->interface != NSS_IPSEC_DECAP_IF_NUMBER)) {
122 nss_ipsec_warning("%p: rx message request for another interface: %d", nss_ctx, ncm->interface);
123 return;
124 }
125
126 if (ncm->response == NSS_CMN_RESPONSE_LAST) {
127 nss_ipsec_warning("%p: rx message response for if %d, type %d, is invalid: %d", nss_ctx, ncm->interface,
128 ncm->type, ncm->response);
129 return;
130 }
131
132 /*
133 * Is this a notification? if, yes then fill up the callback and app_data from
134 * locally stored state
135 */
136 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
137 ncm->cb = (uint32_t)nss_ipsec_get_msg_callback(nss_ctx, if_num, &ipsec_ctx);
138 ncm->app_data = (uint32_t)ipsec_ctx;
139 }
140
141
142 nss_core_log_msg_failures(nss_ctx, ncm);
143
144 /*
145 * load, test & call
146 */
147 cb = (nss_ipsec_msg_callback_t)ncm->cb;
148 if (unlikely(!cb)) {
149 nss_ipsec_trace("%p: rx handler has been unregistered for i/f: %d", nss_ctx, ncm->interface);
150 return;
151 }
152 cb((void *)ncm->app_data, nim);
153}
154
155/*
156 **********************************
157 Tx APIs
158 **********************************
159 */
160
161/*
162 * nss_ipsec_tx_msg
163 * Send ipsec rule to NSS.
164 */
165nss_tx_status_t nss_ipsec_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_ipsec_msg *msg)
166{
167 struct nss_cmn_msg *ncm = &msg->cm;
168 struct nss_ipsec_msg *nim;
169 struct sk_buff *nbuf;
170 int32_t status;
171
172 nss_ipsec_info("%p: message %d for if %d\n", nss_ctx, ncm->type, ncm->interface);
173
174 NSS_VERIFY_CTX_MAGIC(nss_ctx);
175
176 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
177 nss_ipsec_warning("%p: tx message dropped as core not ready", nss_ctx);
178 return NSS_TX_FAILURE_NOT_READY;
179 }
180
181 if (NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_ipsec_msg)) {
182 nss_ipsec_warning("%p: tx message request is too large: %d (desired), %d (requested)", nss_ctx,
183 NSS_NBUF_PAYLOAD_SIZE, sizeof(struct nss_ipsec_msg));
184 return NSS_TX_FAILURE_TOO_LARGE;
185 }
186
187 if ((ncm->interface != NSS_IPSEC_ENCAP_IF_NUMBER) && (ncm->interface != NSS_IPSEC_DECAP_IF_NUMBER)) {
188 nss_ipsec_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface);
189 return NSS_TX_FAILURE;
190 }
191
192 if (ncm->type > NSS_IPSEC_MSG_TYPE_MAX) {
193 nss_ipsec_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type);
194 return NSS_TX_FAILURE;
195 }
196
197 if (ncm->len > sizeof(struct nss_ipsec_msg)) {
198 nss_ipsec_warning("%p: tx message request len for if %d, is bad: %d", nss_ctx, ncm->interface, ncm->len);
199 return NSS_TX_FAILURE_BAD_PARAM;
200 }
201
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530202 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530203 if (unlikely(!nbuf)) {
204 spin_lock_bh(&nss_ctx->nss_top->stats_lock);
205 nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++;
206 spin_unlock_bh(&nss_ctx->nss_top->stats_lock);
207 nss_ipsec_warning("%p: tx rule dropped as command allocation failed", nss_ctx);
208 return NSS_TX_FAILURE;
209 }
210
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530211 nss_ipsec_info("msg params version:%d, interface:%d, type:%d, cb:%d, app_data:%d, len:%d\n",
212 ncm->version, ncm->interface, ncm->type, ncm->cb, ncm->app_data, ncm->len);
213
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530214 nim = (struct nss_ipsec_msg *)skb_put(nbuf, sizeof(struct nss_ipsec_msg));
215 memcpy(nim, msg, sizeof(struct nss_ipsec_msg));
216
217 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
218 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530219 dev_kfree_skb_any(nbuf);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530220 nss_ipsec_warning("%p: tx Unable to enqueue message \n", nss_ctx);
221 return NSS_TX_FAILURE;
222 }
223
224 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
225 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
226
227 return NSS_TX_SUCCESS;
228}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530229EXPORT_SYMBOL(nss_ipsec_tx_msg);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530230
231/*
232 **********************************
233 Register APIs
234 **********************************
235 */
236
237/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530238 * nss_ipsec_notify_register()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530239 * register message notifier for the given interface (if_num)
240 */
241struct nss_ctx_instance *nss_ipsec_notify_register(uint32_t if_num, nss_ipsec_msg_callback_t cb, void *app_data)
242{
243 struct nss_ctx_instance *nss_ctx;
244
245 nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
246
247 if (if_num >= NSS_MAX_NET_INTERFACES) {
248 nss_ipsec_warning("%p: notfiy register received for invalid interface %d", nss_ctx, if_num);
249 return NULL;
250 }
251
252 if (nss_ipsec_set_msg_callback(nss_ctx, if_num, cb, app_data) != NSS_TX_SUCCESS) {
253 nss_ipsec_warning("%p: register failed\n", nss_ctx);
254 return NULL;
255 }
256
257 return nss_ctx;
258}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530259EXPORT_SYMBOL(nss_ipsec_notify_register);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530260
261/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530262 * nss_ipsec_notify_unregister()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530263 * unregister the IPsec notifier for the given interface number (if_num)
264 */
265void nss_ipsec_notify_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
266{
267 if (if_num >= NSS_MAX_NET_INTERFACES) {
268 nss_ipsec_warning("%p: notify unregister received for invalid interface %d", nss_ctx, if_num);
269 return;
270 }
271
272 if (nss_ipsec_set_msg_callback(nss_ctx, if_num, NULL, NULL) != NSS_TX_SUCCESS) {
273 nss_ipsec_warning("%p: unregister failed\n", nss_ctx);
274 return;
275 }
276}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530277EXPORT_SYMBOL(nss_ipsec_notify_unregister);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530278
279/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530280 * nss_ipsec_data_register()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530281 * register a data callback routine
282 */
283struct nss_ctx_instance *nss_ipsec_data_register(uint32_t if_num, nss_ipsec_buf_callback_t cb, void *app_data)
284{
285 struct nss_ctx_instance *nss_ctx;
286
287 nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
288
289 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)){
290 nss_ipsec_warning("%p: data register received for invalid interface %d", nss_ctx, if_num);
291 return NULL;
292 }
293
294 nss_ctx->nss_top->if_ctx[if_num] = app_data;
295 nss_ctx->nss_top->if_rx_callback[if_num] = cb;
296
297 return nss_ctx;
298}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530299EXPORT_SYMBOL(nss_ipsec_data_register);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530300
301/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530302 * nss_ipsec_data_unregister()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530303 * unregister a data callback routine
304 */
305void nss_ipsec_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
306{
307 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)){
308 nss_ipsec_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num);
309 return;
310 }
311
312 nss_ctx->nss_top->if_ctx[if_num] = NULL;
313 nss_ctx->nss_top->if_rx_callback[if_num] = NULL;
314}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530315EXPORT_SYMBOL(nss_ipsec_data_unregister);
316
317/*
318 * nss_ipsec_get_ctx()
319 * get NSS context instance for IPsec handle
320 */
321struct nss_ctx_instance *nss_ipsec_get_context(void)
322{
323 return &nss_top_main.nss[nss_top_main.ipsec_handler_id];
324}
325EXPORT_SYMBOL(nss_ipsec_get_context);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530326
327/*
328 * nss_ipsec_register_handler()
329 */
330void nss_ipsec_register_handler()
331{
332 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
333
334 nss_ipsec_set_msg_callback(nss_ctx, NSS_IPSEC_ENCAP_IF_NUMBER, NULL, NULL);
335 nss_core_register_handler(NSS_IPSEC_ENCAP_IF_NUMBER, nss_ipsec_msg_handler, NULL);
336
337 nss_ipsec_set_msg_callback(nss_ctx, NSS_IPSEC_DECAP_IF_NUMBER, NULL, NULL);
338 nss_core_register_handler(NSS_IPSEC_DECAP_IF_NUMBER, nss_ipsec_msg_handler, NULL);
339}
340