blob: f8441a5d15fb404ee3b965ebb902e870440b7e45 [file] [log] [blame]
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +05301/*
2 **************************************************************************
Stephen Wangaed46332016-12-12 17:29:03 -08003 * Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +05304 * 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
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800116 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipsec_msg)) {
117 nss_ipsec_warning("%p: rx message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530118 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) {
Stephen Wangaed46332016-12-12 17:29:03 -0800137 ncm->cb = (nss_ptr_t)nss_ipsec_get_msg_callback(nss_ctx, if_num, &ipsec_ctx);
138 ncm->app_data = (nss_ptr_t)ipsec_ctx;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530139 }
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,
Stephen Wangaed46332016-12-12 17:29:03 -0800183 NSS_NBUF_PAYLOAD_SIZE, (int)sizeof(struct nss_ipsec_msg));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530184 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
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800197 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipsec_msg)) {
198 nss_ipsec_warning("%p: tx message request len for if %d, is bad: %d", nss_ctx, ncm->interface, nss_cmn_get_msg_len(ncm));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530199 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)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800204 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530205 nss_ipsec_warning("%p: tx rule dropped as command allocation failed", nss_ctx);
206 return NSS_TX_FAILURE;
207 }
208
Stephen Wangaed46332016-12-12 17:29:03 -0800209 nss_ipsec_info("msg params version:%d, interface:%d, type:%d, cb:%p, app_data:%p, len:%d\n",
210 ncm->version, ncm->interface, ncm->type, (void *)ncm->cb, (void *)ncm->app_data, ncm->len);
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530211
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530212 nim = (struct nss_ipsec_msg *)skb_put(nbuf, sizeof(struct nss_ipsec_msg));
213 memcpy(nim, msg, sizeof(struct nss_ipsec_msg));
214
215 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
216 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530217 dev_kfree_skb_any(nbuf);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530218 nss_ipsec_warning("%p: tx Unable to enqueue message \n", nss_ctx);
219 return NSS_TX_FAILURE;
220 }
221
Stephen Wang90c67de2016-04-26 15:15:59 -0700222 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530223
224 return NSS_TX_SUCCESS;
225}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530226EXPORT_SYMBOL(nss_ipsec_tx_msg);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530227
228/*
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530229 * nss_ipsec_tx_buf
230 * Send data packet for ipsec processing
231 */
Stephen Wang83e48752015-03-03 18:41:40 -0800232nss_tx_status_t nss_ipsec_tx_buf(struct sk_buff *skb, uint32_t if_num)
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530233{
234 int32_t status;
235 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530236
Stephen Wang83e48752015-03-03 18:41:40 -0800237 nss_trace("%p: IPsec If Tx packet, id:%d, data=%p", nss_ctx, if_num, skb->data);
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530238
239 NSS_VERIFY_CTX_MAGIC(nss_ctx);
240 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
241 nss_warning("%p: 'IPsec If Tx' packet dropped as core not ready", nss_ctx);
242 return NSS_TX_FAILURE_NOT_READY;
243 }
244
Stephen Wang83e48752015-03-03 18:41:40 -0800245 status = nss_core_send_buffer(nss_ctx, if_num, skb, NSS_IF_DATA_QUEUE_0, H2N_BUFFER_PACKET, 0);
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530246 if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) {
247 nss_warning("%p: Unable to enqueue 'IPsec If Tx' packet\n", nss_ctx);
248 if (status == NSS_CORE_STATUS_FAILURE_QUEUE) {
249 return NSS_TX_FAILURE_QUEUE;
250 }
251
252 return NSS_TX_FAILURE;
253 }
254
255 /*
256 * Kick the NSS awake so it can process our new entry.
257 */
Stephen Wang90c67de2016-04-26 15:15:59 -0700258 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530259 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_PACKET]);
260 return NSS_TX_SUCCESS;
261}
262EXPORT_SYMBOL(nss_ipsec_tx_buf);
263
264/*
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530265 **********************************
266 Register APIs
267 **********************************
268 */
269
270/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530271 * nss_ipsec_notify_register()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530272 * register message notifier for the given interface (if_num)
273 */
274struct nss_ctx_instance *nss_ipsec_notify_register(uint32_t if_num, nss_ipsec_msg_callback_t cb, void *app_data)
275{
276 struct nss_ctx_instance *nss_ctx;
277
278 nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
279
280 if (if_num >= NSS_MAX_NET_INTERFACES) {
281 nss_ipsec_warning("%p: notfiy register received for invalid interface %d", nss_ctx, if_num);
282 return NULL;
283 }
284
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530285 /*
286 * avoid multiple registeration for multiple tunnels
287 */
Arunkumar Tb46f27d2016-04-05 16:41:32 +0530288 if (nss_ctx->nss_top->ipsec_encap_callback && nss_ctx->nss_top->ipsec_decap_callback) {
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530289 return nss_ctx;
290 }
291
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530292 if (nss_ipsec_set_msg_callback(nss_ctx, if_num, cb, app_data) != NSS_TX_SUCCESS) {
293 nss_ipsec_warning("%p: register failed\n", nss_ctx);
294 return NULL;
295 }
296
297 return nss_ctx;
298}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530299EXPORT_SYMBOL(nss_ipsec_notify_register);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530300
301/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530302 * nss_ipsec_notify_unregister()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530303 * unregister the IPsec notifier for the given interface number (if_num)
304 */
305void nss_ipsec_notify_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
306{
307 if (if_num >= NSS_MAX_NET_INTERFACES) {
308 nss_ipsec_warning("%p: notify unregister received for invalid interface %d", nss_ctx, if_num);
309 return;
310 }
311
312 if (nss_ipsec_set_msg_callback(nss_ctx, if_num, NULL, NULL) != NSS_TX_SUCCESS) {
313 nss_ipsec_warning("%p: unregister failed\n", nss_ctx);
314 return;
315 }
316}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530317EXPORT_SYMBOL(nss_ipsec_notify_unregister);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530318
319/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530320 * nss_ipsec_data_register()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530321 * register a data callback routine
322 */
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800323struct nss_ctx_instance *nss_ipsec_data_register(uint32_t if_num, nss_ipsec_buf_callback_t cb, struct net_device *netdev, uint32_t features)
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530324{
325 struct nss_ctx_instance *nss_ctx;
326
327 nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
328
329 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)){
330 nss_ipsec_warning("%p: data register received for invalid interface %d", nss_ctx, if_num);
331 return NULL;
332 }
333
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530334 /*
335 * avoid multiple registeration for multiple tunnels
336 */
Stephen Wang84e0e992016-09-07 12:31:40 -0700337 if (nss_ctx->subsys_dp_register[if_num].cb) {
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530338 return nss_ctx;
339 }
340
Stephen Wang84e0e992016-09-07 12:31:40 -0700341 nss_ctx->subsys_dp_register[if_num].cb = cb;
342 nss_ctx->subsys_dp_register[if_num].app_data = NULL;
343 nss_ctx->subsys_dp_register[if_num].ndev = netdev;
344 nss_ctx->subsys_dp_register[if_num].features = features;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530345
346 return nss_ctx;
347}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530348EXPORT_SYMBOL(nss_ipsec_data_register);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530349
350/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530351 * nss_ipsec_data_unregister()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530352 * unregister a data callback routine
353 */
354void nss_ipsec_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
355{
356 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)){
357 nss_ipsec_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num);
358 return;
359 }
360
Stephen Wang84e0e992016-09-07 12:31:40 -0700361 nss_ctx->subsys_dp_register[if_num].cb = NULL;
362 nss_ctx->subsys_dp_register[if_num].app_data = NULL;
363 nss_ctx->subsys_dp_register[if_num].ndev = NULL;
364 nss_ctx->subsys_dp_register[if_num].features = 0;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530365}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530366EXPORT_SYMBOL(nss_ipsec_data_unregister);
367
368/*
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530369 * nss_ipsec_get_interface_num()
370 * Get the NSS interface number on which ipsec user shall register
371 */
372int32_t nss_ipsec_get_interface(struct nss_ctx_instance *nss_ctx)
373{
374 /*
375 * Check on which core is ipsec enabled
376 */
377 switch(nss_ctx->id) {
378 case 0:
379 return NSS_IPSEC_RULE_INTERFACE;
380
381 case 1:
382 return NSS_C2C_TX_INTERFACE;
383 }
384
385 return -1;
386}
387EXPORT_SYMBOL(nss_ipsec_get_interface);
388
389/*
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530390 * nss_ipsec_get_ctx()
391 * get NSS context instance for IPsec handle
392 */
393struct nss_ctx_instance *nss_ipsec_get_context(void)
394{
395 return &nss_top_main.nss[nss_top_main.ipsec_handler_id];
396}
397EXPORT_SYMBOL(nss_ipsec_get_context);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530398
399/*
400 * nss_ipsec_register_handler()
401 */
402void nss_ipsec_register_handler()
403{
404 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
405
406 nss_ipsec_set_msg_callback(nss_ctx, NSS_IPSEC_ENCAP_IF_NUMBER, NULL, NULL);
407 nss_core_register_handler(NSS_IPSEC_ENCAP_IF_NUMBER, nss_ipsec_msg_handler, NULL);
408
409 nss_ipsec_set_msg_callback(nss_ctx, NSS_IPSEC_DECAP_IF_NUMBER, NULL, NULL);
410 nss_core_register_handler(NSS_IPSEC_DECAP_IF_NUMBER, nss_ipsec_msg_handler, NULL);
411}
412
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700413/*
414 * nss_ipsec_msg_init()
415 * Initialize ipsec message.
416 */
417void nss_ipsec_msg_init(struct nss_ipsec_msg *nim, uint16_t if_num, uint32_t type, uint32_t len,
Sundarajan Srinivasan30a53d42015-01-30 10:52:08 -0800418 nss_ipsec_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700419{
420 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
421}
422EXPORT_SYMBOL(nss_ipsec_msg_init);