blob: 2c24386ec23a431c548c5477da5759d51df41807 [file] [log] [blame]
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001/*
2 **************************************************************************
Tushar Mathurd09246f2015-02-24 15:53:16 +05303 * Copyright (c) 2014,2015, The Linux Foundation. All rights reserved.
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08004 * 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/* nss_ipsecmgr.c
18 * NSS to HLOS IPSec Manager
19 */
20#include <linux/types.h>
21#include <linux/ip.h>
Murat Sezgin3441e772015-10-26 11:55:57 -070022#include <linux/of.h>
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +053023#include <linux/ipv6.h>
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -080024#include <linux/skbuff.h>
25#include <linux/module.h>
26#include <linux/netdevice.h>
27#include <linux/rtnetlink.h>
28#include <asm/atomic.h>
29#include <nss_api_if.h>
30#include <nss_ipsec.h>
Amit Gupta2c5439c2015-08-21 20:52:02 +053031#include <nss_ipsecmgr.h>
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -080032
33#if defined(CONFIG_DYNAMIC_DEBUG)
34/*
35 * Compile messages for dynamic enable/disable
36 */
37#define nss_ipsecmgr_error(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__)
38#define nss_ipsecmgr_warn(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__)
39#define nss_ipsecmgr_info(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__)
40#define nss_ipsecmgr_trace(s, ...) pr_debug("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__)
41
42#else
43/*
44 * Statically compile messages at different levels
45 */
46#define nss_ipsecmgr_error(s, ...) { \
47 if (NSS_IPSECMGR_DEBUG_LEVEL < NSS_IPSECMGR_DEBUG_LVL_ERROR) { \
48 pr_alert("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
49 } \
50}
51#define nss_ipsecmgr_warn(s, ...) { \
52 if (NSS_IPSECMGR_DEBUG_LEVEL < NSS_IPSECMGR_DEBUG_LVL_WARN) { \
53 pr_warn("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
54 } \
55}
56#define nss_ipsecmgr_info(s, ...) { \
57 if (NSS_IPSECMGR_DEBUG_LEVEL < NSS_IPSECMGR_DEBUG_LVL_INFO) { \
58 pr_notice("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
59 } \
60}
61#define nss_ipsecmgr_trace(s, ...) { \
62 if (NSS_IPSECMGR_DEBUG_LEVEL < NSS_IPSECMGR_DEBUG_LVL_TRACE) { \
63 pr_info("%s[%d]:" s, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
64 } \
65}
66
67#endif /* !CONFIG_DYNAMIC_DEBUG */
68
69/* NSS IPsec entry state */
70enum nss_ipsecmgr_entry_state {
71 NSS_IPSECMGR_ENTRY_STATE_INIT = 0, /* init state of the entry */
72 NSS_IPSECMGR_ENTRY_STATE_VALID = 1, /* entry is valid */
73 NSS_IPSECMGR_ENTRY_STATE_INVALID = 2, /* entry is invalid */
74 NSS_IPSECMGR_ENTRY_STATE_MAX
75};
76
77/* IPsec table entry */
78struct nss_ipsecmgr_tbl_entry {
79 struct nss_ipsec_rule_sel sel; /* rule selector */
80 uint32_t sa_idx; /* index into SA table for this rule */
81 enum nss_ipsecmgr_entry_state state; /* state */
82};
83
84/* NSS IPsec table type */
85struct nss_ipsecmgr_tbl {
86 uint32_t total_tx; /* total packets tx'ed from ENCAP/DECAP */
87 uint32_t total_rx; /* total packets rx'ed at ENCAP/DECAP */
88 uint32_t total_dropped; /* total dropped packets at ENCAP/DECAP */
89 struct nss_ipsecmgr_tbl_entry entry[NSS_IPSEC_MAX_RULES]; /* table entry */
90 uint32_t count; /* number of entries */
91 spinlock_t lock;
92};
93
94/* NSS IPsec SA */
95struct nss_ipsec_sa {
96 atomic_t user; /* reference count */
97 uint32_t esp_spi; /* ESP SPI */
98
99 enum nss_ipsecmgr_rule_type type; /* IPsec rule type (ENCAP/DECAP) */
100
101 struct nss_ipsec_rule_data data; /* IPsec rule data */
102 struct nss_ipsec_sa_stats stats; /* SA statistics */
103};
104
105/* NSS IPsec manager private structure */
106struct nss_ipsecmgr_priv {
107 struct nss_ipsecmgr_tbl encap; /* encap table */
108 struct nss_ipsecmgr_tbl decap; /* decap table */
109 struct nss_ipsec_sa sa_tbl[NSS_IPSEC_MAX_SA]; /* global SA table */
110
111 void *cb_ctx; /* callback context */
112 nss_ipsecmgr_data_cb_t data_cb; /* data callback function */
113 nss_ipsecmgr_event_cb_t event_cb; /* event callback function */
114 struct nss_ctx_instance *nss_ctx; /* NSS context */
115 uint32_t nss_ifnum; /* NSS if num for data */
116};
117
118typedef bool (*nss_ipsecmgr_op_t)(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim);
119
120/* NSS IPsec manager operation (message types) complete routines */
121struct nss_ipsecmgr_ops {
122 nss_ipsecmgr_op_t verify_fn; /* verify function for the op */
123 nss_ipsecmgr_op_t commit_fn; /* commit function for the op */
124};
125
126/* driver global info */
127struct nss_ipsecmgr_drv {
128 atomic_t sa_count; /* number of SA's available for */
129};
130
131/*
132 **********************
133 * globals
134 **********************
135 */
136
137static struct nss_ipsecmgr_drv gbl_drv_ctx; /* global driver context */
138
139/*
140 **********************
141 * Helper Functions
142 **********************
143 */
144
145/*
146 * nss_ipsecmgr_sa_init()
147 * Initialize SA
148 */
149static void nss_ipsecmgr_sa_init(struct nss_ipsec_sa *sa)
150{
151 memset(sa, 0, sizeof(struct nss_ipsec_sa));
152 atomic_set(&sa->user, 0);
153}
154
155/*
156 * nss_ipsecmgr_sa_get()
157 * Get reference to SA at given index
158 */
159static struct nss_ipsec_sa *nss_ipsecmgr_sa_get(struct nss_ipsec_sa sa_tbl[], uint32_t sa_idx)
160{
161 struct nss_ipsec_sa *sa = &sa_tbl[sa_idx];
162
163 atomic_inc(&sa->user);
164
165 return sa;
166}
167
168/*
169 * nss_ipsecmgr_sa_put()
170 * Release reference to SA at given index
171 */
172static void nss_ipsecmgr_sa_put(struct nss_ipsec_sa sa_tbl[], uint32_t sa_idx)
173{
174 struct nss_ipsec_sa *sa = &sa_tbl[sa_idx];
175
176 if (atomic_read(&sa->user) == 0) {
177 nss_ipsecmgr_error("SA already freed for (%d)\n", sa_idx);
178 return;
179 }
180
181 if (atomic_dec_and_test(&sa->user)) {
182 nss_ipsecmgr_sa_init(sa);
183 }
184}
185
186/*
187 * nss_ipsecmgr_get_type()
188 * Get IPsec manager rule type (encap/decap)
189 */
190static enum nss_ipsecmgr_rule_type nss_ipsecmgr_get_type(uint16_t if_num)
191{
192 switch(if_num) {
193 case NSS_IPSEC_ENCAP_IF_NUMBER:
194 return NSS_IPSECMGR_RULE_TYPE_ENCAP;
195
196 case NSS_IPSEC_DECAP_IF_NUMBER:
197 return NSS_IPSECMGR_RULE_TYPE_DECAP;
198
199 default:
200 return NSS_IPSECMGR_RULE_TYPE_NONE;
201 }
202}
203
204/*
205 * nss_ipsecmgr_get_tbl()
206 * return the table associated with the interface number
207 */
208static inline struct nss_ipsecmgr_tbl *nss_ipsecmgr_get_tbl(struct nss_ipsecmgr_priv *priv, uint16_t if_num)
209{
210 switch (if_num) {
211 case NSS_IPSEC_ENCAP_IF_NUMBER:
212 return &priv->encap;
213
214 case NSS_IPSEC_DECAP_IF_NUMBER:
215 return &priv->decap;
216
217 default:
218 return NULL;
219 }
220}
221
222/*
223 * nss_ipsecmgr_copy_encap_add()
224 * prepare the IPsec message for encap add operation
225 */
226static void nss_ipsecmgr_copy_encap_add(struct net_device *dev, struct nss_ipsec_rule *msg, union nss_ipsecmgr_rule *rule)
227{
228 struct nss_ipsecmgr_encap_add *encap = &rule->encap_add;
229 struct nss_ipsec_rule_sel *sel = &msg->sel;
230 struct nss_ipsec_rule_oip *oip = &msg->oip;
231 struct nss_ipsec_rule_data *data = &msg->data;
232
233 /*
234 * Populate the selectors for encap direction
235 */
236 sel->ipv4_src = encap->inner_ipv4_src;
237 sel->ipv4_dst = encap->inner_ipv4_dst;
238
239 sel->src_port = encap->inner_src_port;
240 sel->dst_port = encap->inner_dst_port;
241
242 sel->ipv4_proto = encap->inner_ipv4_proto;
243
244 /*
245 * Populate the outer IP data for encap direction
246 */
247 oip->ipv4_dst = encap->outer_ipv4_dst;
248 oip->ipv4_src = encap->outer_ipv4_src;
249
250 oip->esp_spi = encap->esp_spi;
251
252 oip->ipv4_ttl = encap->outer_ipv4_ttl;
253
254 /*
255 * Populate the data part
256 */
257 data->cipher_algo = encap->cipher_algo;
258 data->esp_seq_skip = (encap->esp_seq_skip == 1);
259 data->esp_tail_skip = (encap->esp_tail_skip == 1);
260
261 data->esp_icv_len = encap->esp_icv_len;
262 data->auth_algo = encap->auth_algo;
263
264 data->crypto_index = encap->crypto_index;
265
266 data->nat_t_req = encap->nat_t_req;
267 data->use_pattern = encap->use_pattern;
268}
269
270/*
271 * nss_ipsecmgr_copy_decap_add()
272 * prepare the IPsec message for decap add operation
273 */
274static void nss_ipsecmgr_copy_decap_add(struct net_device *dev, struct nss_ipsec_rule *msg, union nss_ipsecmgr_rule *rule)
275{
276 struct nss_ipsecmgr_decap_add *decap = &rule->decap_add;
277 struct nss_ipsec_rule_sel *sel = &msg->sel;
278 struct nss_ipsec_rule_data *data = &msg->data;
279 /*
280 * Populate the selectors for encap direction
281 */
282 sel->ipv4_src = decap->outer_ipv4_src;
283 sel->ipv4_dst = decap->outer_ipv4_dst;
284
285 sel->esp_spi = decap->esp_spi;
286
287 sel->ipv4_proto = IPPROTO_ESP;
288
289 /*
290 * Populate the data part
291 */
292 data->cipher_algo = decap->cipher_algo;
293
294 data->esp_icv_len = decap->esp_icv_len;
295 data->esp_seq_skip = (decap->esp_seq_skip == 1);
296 data->esp_tail_skip = (decap->esp_tail_skip == 1);
297 data->auth_algo = decap->auth_algo;
298
299 data->crypto_index = decap->crypto_index;
300
301 data->nat_t_req = decap->nat_t_req;
302 if (data->nat_t_req) {
303 sel->dst_port = NSS_IPSECMGR_NATT_PORT_DATA;
304 }
305
306 data->window_size = decap->window_size;
307}
308
309/*
310 * nss_ipsecmgr_copy_encap_del()
311 * prepare the IPsec message for encap del operation
312 */
313static void nss_ipsecmgr_copy_encap_del(struct net_device *dev, struct nss_ipsec_rule *msg, union nss_ipsecmgr_rule *rule)
314{
315 struct nss_ipsecmgr_encap_del *encap = &rule->encap_del;
316 struct nss_ipsec_rule_sel *sel = &msg->sel;
Tushar Mathurd09246f2015-02-24 15:53:16 +0530317 struct nss_ipsec_rule_data *data = &msg->data;
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800318
319 /*
320 * Populate the selectors for encap direction
321 */
322 sel->ipv4_src = encap->inner_ipv4_src;
323 sel->ipv4_dst = encap->inner_ipv4_dst;
324
325 sel->src_port = encap->inner_src_port;
326 sel->dst_port = encap->inner_dst_port;
327
328 sel->ipv4_proto = encap->inner_ipv4_proto;
Tushar Mathurd09246f2015-02-24 15:53:16 +0530329
330 /*
331 * Populate the data part
332 */
333 data->use_pattern = encap->use_pattern;
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800334}
335
336/*
337 * nss_ipsecmgr_copy_decap_del()
338 * prepare the IPsec message for decap del operation
339 */
340static void nss_ipsecmgr_copy_decap_del(struct net_device *dev, struct nss_ipsec_rule *msg, union nss_ipsecmgr_rule *rule)
341{
342 struct nss_ipsecmgr_decap_del *decap = &rule->decap_del;
343 struct nss_ipsec_rule_sel *sel = &msg->sel;
344 /*
345 * Populate the selectors for encap direction
346 */
347 sel->ipv4_src = decap->outer_ipv4_src;
348 sel->ipv4_dst = decap->outer_ipv4_dst;
349
350 sel->esp_spi = decap->esp_spi;
351
352 sel->ipv4_proto = IPPROTO_ESP;
353}
354
355/*
356 **********************
357 * message handlers
358 **********************
359 */
360/*
361 * nss_ipsecmgr_verify_add()
362 * verify the Add operation before committing
363 */
364static bool nss_ipsecmgr_verify_add(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim)
365{
366 struct nss_ipsec_rule *rule = &nim->msg.push;
367 struct nss_ipsecmgr_tbl_entry *entry;
368 uint32_t tbl_idx;
Tushar Mathur26e87f12015-02-09 23:20:10 +0530369 uint32_t sa_idx;
370
371 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
372 return false;
373 }
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800374
375 tbl_idx = rule->rule_idx;
376 if (tbl_idx >= NSS_IPSEC_MAX_RULES) {
377 nss_ipsecmgr_error("table index out of range\n");
378 return false;
379 }
380
381 entry = &tbl->entry[tbl_idx];
382
383 /*
384 * XXX:Duplicate hit or hash collision; We need to handle
385 * hash collision
386 */
387 if (entry->state == NSS_IPSECMGR_ENTRY_STATE_VALID) {
388 return false;
389 }
390
391 /*
392 * Table full, XXX:must increment stats
393 */
394 if ((tbl->count + 1) >= NSS_IPSEC_MAX_RULES) {
395 return false;
396 }
397
Tushar Mathur26e87f12015-02-09 23:20:10 +0530398 sa_idx = rule->sa_idx;
399 if (sa_idx >= NSS_IPSEC_MAX_SA) {
400 nss_ipsecmgr_error("unable to add the sa_idx:%d\n", sa_idx);
401 return false;
402 }
403
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800404 return true;
405}
406
407/*
408 * nss_ipsecmgr_commit_add()
409 * commit the Add operation
410 */
411static bool nss_ipsecmgr_commit_add(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim)
412{
413 struct nss_ipsec_rule *rule = &nim->msg.push;
414 struct nss_ipsecmgr_tbl_entry *entry;
415 struct nss_ipsecmgr_priv *priv;
416 struct nss_ipsec_sa *sa;
417 uint32_t tbl_idx;
418 uint32_t sa_idx;
419
420 priv = netdev_priv(dev);
421
422 /* Reduce the number of availabe SA(s) as we have successfully added one */
423 atomic_dec(&gbl_drv_ctx.sa_count);
424
425 tbl_idx = rule->rule_idx;
426 entry = &tbl->entry[tbl_idx];
427
428 tbl->count++;
429
430 memcpy(&entry->sel, &rule->sel, sizeof(struct nss_ipsec_rule_sel));
431 entry->state = NSS_IPSECMGR_ENTRY_STATE_VALID;
432
433 /* increment SA use count */
434 sa_idx = rule->sa_idx;
435 sa = nss_ipsecmgr_sa_get(priv->sa_tbl, sa_idx);
436
437 /* copy rule data to SA */
438 memcpy(&sa->data, &rule->data, sizeof(struct nss_ipsec_rule_data));
439
440 /* save rule type (encap/decap) */
441 sa->type = nss_ipsecmgr_get_type(nim->cm.interface);
442
443 /* save ESP SPI in SA */
444 switch (nim->cm.interface) {
445 case NSS_IPSEC_ENCAP_IF_NUMBER:
446 sa->esp_spi = rule->oip.esp_spi;
447 break;
448
449 case NSS_IPSEC_DECAP_IF_NUMBER:
450 sa->esp_spi = rule->sel.esp_spi;
451 break;
452
453 default:
454 nss_ipsecmgr_error("invalid interface num = %d\n", nim->cm.interface);
455 return false;
456 }
457
458 /* save SA table index for this rule entry */
459 entry->sa_idx = sa_idx;
460
461 return true;
462}
463
464/*
465 * nss_ipsecmgr_verify_del()
466 * verify the del operation before committing
467 */
468static bool nss_ipsecmgr_verify_del(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim)
469{
470 struct nss_ipsec_rule *rule = &nim->msg.push;
471 struct nss_ipsecmgr_tbl_entry *entry;
472 uint32_t tbl_idx;
Tushar Mathur26e87f12015-02-09 23:20:10 +0530473 uint32_t sa_idx;
474
475 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
476 return false;
477 }
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800478
479 tbl_idx = rule->rule_idx;
480 if (tbl_idx >= NSS_IPSEC_MAX_RULES) {
481 nss_ipsecmgr_error("table index out of range\n");
482 return false;
483 }
484
485 entry = &tbl->entry[tbl_idx];
486
487 /*
488 * Entry already deleted, XXX:must increment stats
489 */
490 if (entry->state == NSS_IPSECMGR_ENTRY_STATE_INVALID) {
491 return false;
492 }
493
494 /*
495 * Entry never existed, XXX:must increment stats
496 */
497 if (entry->state == NSS_IPSECMGR_ENTRY_STATE_INIT) {
498 return false;
499 }
500
501 /*
502 * Table empty, XXX:must increment stats
503 */
504 if (tbl->count == 0) {
505 return false;
506 }
507
Tushar Mathur26e87f12015-02-09 23:20:10 +0530508 sa_idx = rule->sa_idx;
509 if (sa_idx >= NSS_IPSEC_MAX_SA) {
510 nss_ipsecmgr_error("unable to delete the sa_idx:%d\n", sa_idx);
511 return false;
512 }
513
514
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800515 return true;
516}
517
518/*
519 * nss_ipsecmgr_commit_del()
520 * commit the Del operation
521 */
522static bool nss_ipsecmgr_commit_del(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim)
523{
524 struct nss_ipsec_rule *rule = &nim->msg.push;
525 struct nss_ipsecmgr_tbl_entry *entry;
526 struct nss_ipsecmgr_priv *priv;
527 uint32_t tbl_idx;
528 uint32_t sa_idx;
529
530 priv = netdev_priv(dev);
531
532 /* Increase the number of available SA(s) as we have successfully freed one */
533 atomic_inc(&gbl_drv_ctx.sa_count);
534
535 tbl_idx = rule->rule_idx;
536 entry = &tbl->entry[tbl_idx];
537
538 tbl->count--;
539
540 memset(&entry->sel, 0, sizeof(struct nss_ipsec_rule_sel));
541 entry->state = NSS_IPSECMGR_ENTRY_STATE_INVALID;
542
543 /* decrement SA use count */
544 sa_idx = rule->sa_idx;
545 nss_ipsecmgr_sa_put(priv->sa_tbl, sa_idx);
546
547 return true;
548}
549
550/*
551 * nss_ipsecmgr_verify_stats()
552 * verify stats update operation
553 */
554static bool nss_ipsecmgr_verify_stats(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim)
555{
Tushar Mathur26e87f12015-02-09 23:20:10 +0530556 struct nss_ipsec_sa_stats *msg_stats = &nim->msg.stats;
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800557
558 /*
559 * Table empty, nothing to update
560 */
561 if (tbl->count == 0) {
562 return false;
563 }
564
Tushar Mathur26e87f12015-02-09 23:20:10 +0530565 if (msg_stats->sa_idx >= NSS_CRYPTO_MAX_IDXS) {
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800566 return false;
567 }
568
569 return true;
570}
571
572/*
573 * nss_ipsecmgr_commit_stats()
574 * commit stats for the SA
575 */
576static bool nss_ipsecmgr_commit_stats(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim)
577{
578 struct nss_ipsec_sa_stats *msg_stats = &nim->msg.stats;
579 struct nss_ipsec_sa_stats *sa_stats;
580 struct nss_ipsec_sa *sa;
581 struct nss_ipsecmgr_priv *priv;
582 struct nss_ipsecmgr_event event = {.type = NSS_IPSECMGR_EVENT_SA_STATS};
583 struct nss_ipsecmgr_sa_stats *event_stats = &event.data.stats;
584 nss_ipsecmgr_event_cb_t event_cb;
585 void *cb_ctx;
586 uint32_t sa_idx;
587
588 priv = netdev_priv(dev);
589
590 /* get reference to SA stats */
591 sa_idx = msg_stats->sa_idx;
592 sa = nss_ipsecmgr_sa_get(priv->sa_tbl, sa_idx);
593
594 sa_stats = &sa->stats;
595
596 /* copy SA statistics */
597 sa_stats->seqnum = msg_stats->seqnum;
598 memcpy(&sa_stats->pkts, &msg_stats->pkts, sizeof(struct nss_ipsec_pkt_stats));
599
600 /* execute event callback to send SA stats event */
601 event_cb = priv->event_cb;
602 cb_ctx = priv->cb_ctx;
603 if (event_cb && cb_ctx) {
604 event_stats->esp_spi = sa->esp_spi;
605 event_stats->seqnum = sa_stats->seqnum;
606 event_stats->crypto_index = sa->data.crypto_index;
607 event_stats->type = sa->type;
608
609 event_stats->pkts_processed = sa_stats->pkts.processed;
610 event_stats->pkts_dropped = sa_stats->pkts.dropped;
611 event_stats->pkts_failed = sa_stats->pkts.failed;
612
613 event_cb(cb_ctx, &event);
614 }
615
616 /* decrement SA use count */
617 nss_ipsecmgr_sa_put(priv->sa_tbl, sa_idx);
618
619 return true;
620}
621
622/*
623 * nss_ipsecmgr_verify_flush()
624 * verify the flush operation
625 */
626static bool nss_ipsecmgr_verify_flush(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim)
627{
628 return true;
629}
630
631/*
632 * nss_ipsecmgr_commit_flush()
633 * commit the flush operation
634 */
635static bool nss_ipsecmgr_commit_flush(struct net_device *dev, struct nss_ipsecmgr_tbl *tbl, struct nss_ipsec_msg *nim)
636{
637 struct nss_ipsecmgr_priv *priv;
638 struct nss_ipsec_sa *sa;
639 int32_t count;
640
641 priv = netdev_priv(dev);
642
Amit Gupta2c5439c2015-08-21 20:52:02 +0530643 atomic_add(tbl->count, &gbl_drv_ctx.sa_count);
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800644 tbl->count = 0;
645
646 for (count = 0, sa = &priv->sa_tbl[0]; count < NSS_IPSEC_MAX_SA; count++, sa++) {
647 nss_ipsecmgr_sa_init(sa);
648 }
649
650 return true;
651}
652
653/*
654 * callback operation
655 */
656const static struct nss_ipsecmgr_ops cb_ops[NSS_IPSEC_MSG_TYPE_MAX] = {
657 [NSS_IPSEC_MSG_TYPE_ADD_RULE] = {nss_ipsecmgr_verify_add, nss_ipsecmgr_commit_add},
658 [NSS_IPSEC_MSG_TYPE_DEL_RULE] = {nss_ipsecmgr_verify_del, nss_ipsecmgr_commit_del},
659 [NSS_IPSEC_MSG_TYPE_SYNC_STATS] = {nss_ipsecmgr_verify_stats, nss_ipsecmgr_commit_stats},
660 [NSS_IPSEC_MSG_TYPE_FLUSH_TUN] = {nss_ipsecmgr_verify_flush, nss_ipsecmgr_commit_flush}
661};
662
663/*
664 * nss_ipsecmgr_op_receive()
665 * asynchronous event reception
666 */
667static void nss_ipsecmgr_op_receive(void *app_data, struct nss_ipsec_msg *nim)
668{
669 struct net_device *tun_dev = (struct net_device *)app_data;
670 struct nss_ipsecmgr_priv *priv;
671 struct nss_ipsecmgr_tbl *tbl;
672 nss_ipsecmgr_op_t verify_fn;
673 nss_ipsecmgr_op_t commit_fn;
674 struct net_device *dev;
675
676 BUG_ON(tun_dev == NULL);
677 BUG_ON(nim == NULL);
678
679 /* this holds the ref_cnt for the device */
680 dev = dev_get_by_index(&init_net, nim->tunnel_id);
681 if (dev == NULL) {
682 nss_ipsecmgr_error("event received on deallocated I/F (%d)\n", nim->tunnel_id);
683 return;
684 }
685
686 if (dev != tun_dev) {
687 nss_ipsecmgr_error("event received on incorrect I/F (%d)\n", nim->tunnel_id);
688 goto done;
689 }
690
691 priv = netdev_priv(dev);
692
693 tbl = nss_ipsecmgr_get_tbl(priv, nim->cm.interface);
694 if (tbl == NULL) {
695 nss_ipsecmgr_error("invalid interface number(%d)\n", nim->cm.interface);
696 goto done;
697 }
698
699 verify_fn = cb_ops[nim->cm.type].verify_fn;
700 commit_fn = cb_ops[nim->cm.type].commit_fn;
701
702 if (!verify_fn || !commit_fn) {
703 nss_ipsecmgr_error("unhandled type (%d)\n", nim->cm.type);
704 goto done;
705 }
706
707 /*
708 * Now we can operate on the entry of the table
709 */
710 spin_lock_bh(&tbl->lock);
711
712 if (verify_fn(dev, tbl, nim) == true) {
713 commit_fn(dev, tbl, nim);
714 }
715
716 spin_unlock_bh(&tbl->lock);
717done:
718 /* release the device as we are done */
719 dev_put(dev);
720}
721
722/*
723 * nss_ipsecmgr_op_send()
724 * Push a IPsec rule to NSS
725 */
726static bool nss_ipsecmgr_op_send(struct net_device *dev, struct nss_ipsec_msg *nim, uint32_t if_num, enum nss_ipsec_msg_type type)
727{
728 struct nss_ipsecmgr_priv *priv;
729 nss_tx_status_t status;
730
731 priv = netdev_priv(dev);
732 if (priv->nss_ctx == NULL) {
733 nss_ipsecmgr_error("Tunnel registration or de-registration is underway\n");
734 return false;
735 }
736
737 nim->tunnel_id = dev->ifindex;
738
Sundarajan Srinivasan3933aae2015-01-30 10:44:26 -0800739 nss_ipsec_msg_init(nim, if_num, type, NSS_IPSEC_MSG_LEN, nss_ipsecmgr_op_receive, dev);
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800740
741 status = nss_ipsec_tx_msg(priv->nss_ctx, nim);
742 if (status != NSS_TX_SUCCESS) {
743 nss_ipsecmgr_error("unable to push rule(%d) for %s\n", type, dev->name);
744 return false;
745 }
746
747 return true;
748}
749
750/*
751 * nss_ipsecmgr_buf_receive()
752 * receive NSS exception packets
753 */
754static void nss_ipsecmgr_buf_receive(struct net_device *dev, struct sk_buff *skb, __attribute((unused)) struct napi_struct *napi)
755{
756 struct nss_ipsecmgr_priv *priv;
757 nss_ipsecmgr_data_cb_t cb_fn;
758 void *cb_ctx;
759 struct iphdr *ip;
760
761 BUG_ON(dev == NULL);
762 BUG_ON(skb == NULL);
763
764 /* hold the device till we process it */
765 dev_hold(dev);
766
767 /*
768 * XXX:need to ensure that the dev being accessed is not deleted
769 */
770 priv = netdev_priv(dev);
771
772 skb->dev = dev;
773
774 cb_fn = priv->data_cb;
775 cb_ctx = priv->cb_ctx;
776
777 /*
778 * if tunnel creator gave a callback then send the packet without
779 * any modifications to him
780 */
781 if (cb_fn && cb_ctx) {
782 cb_fn(cb_ctx, skb);
783 goto done;
784 }
785
786 ip = (struct iphdr *)skb->data;
Amit Gupta2c5439c2015-08-21 20:52:02 +0530787 if (unlikely((ip->version != IPVERSION) || (ip->ihl != 5))) {
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800788 nss_ipsecmgr_error("dropping packets(IP version:%x, Header len:%x)\n", ip->version, ip->ihl);
789 dev_kfree_skb_any(skb);
790 goto done;
791 }
792
Amit Gupta2c5439c2015-08-21 20:52:02 +0530793 /*
794 * Receiving an ESP packet indicates that NSS has performed the encapsulation
795 * but the post-routing rule is not present. This condition can't be taken care
796 * in Host we should flush the ENCAP rules and free the packet. This will force
797 * subsequent packets to follow the Slow path IPsec thus recreating the rules
798 */
799 if (unlikely(ip->protocol == IPPROTO_ESP)) {
800 nss_ipsecmgr_sa_flush(dev, NSS_IPSECMGR_RULE_TYPE_ENCAP);
801 dev_kfree_skb_any(skb);
802 goto done;
803 }
804
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800805 skb_reset_network_header(skb);
806 skb_reset_mac_header(skb);
807
808 skb->pkt_type = PACKET_HOST;
809 skb->protocol = cpu_to_be16(ETH_P_IP);
810 skb->skb_iif = dev->ifindex;
811
812 netif_receive_skb(skb);
813done:
814 /* release the device as we are done */
815 dev_put(dev);
816}
817
818/*
819 **********************
820 * Netdev ops
821 **********************
822 */
823
824/*
825 * nss_ipsecmgr_tunnel_init()
826 * initiallizes the tunnel
827 */
828static int nss_ipsecmgr_tunnel_init(struct net_device *dev)
829{
830 struct nss_ipsecmgr_priv *priv;
831
832 priv = netdev_priv(dev);
833
834 priv->nss_ctx = nss_ipsec_get_context();
835
836 return 0;
837}
838
839/*
840 * nss_ipsecmgr_tunnel_exit()
841 * deinitiallizes the tunnel
842 */
843static void nss_ipsecmgr_tunnel_exit(struct net_device *dev)
844{
845 struct nss_ipsecmgr_priv *priv;
846
847 priv = netdev_priv(dev);
848
849 priv->nss_ctx = NULL;
850}
851
852/*
853 * nss_ipsecmgr_tunnel_open()
854 * open the tunnel for usage
855 */
856static int nss_ipsecmgr_tunnel_open(struct net_device *dev)
857{
858 struct nss_ipsecmgr_priv *priv;
859
860 priv = netdev_priv(dev);
861
862 netif_start_queue(dev);
863
864 return 0;
865}
866
867/*
868 * nss_ipsecmgr_tunnel_stop()
869 * stop the IPsec tunnel
870 */
871static int nss_ipsecmgr_tunnel_stop(struct net_device *dev)
872{
873 struct nss_ipsecmgr_priv *priv;
874
875 priv = netdev_priv(dev);
876
877 netif_stop_queue(dev);
878
879 return 0;
880}
881
882/*
883 * nss_ipsecmgr_tunnel_xmit()
884 * tunnel transmit function
885 */
886static netdev_tx_t nss_ipsecmgr_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
887{
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800888 struct nss_ipsecmgr_priv *priv;
889 struct nss_ipsecmgr_tbl *encap;
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +0530890 int nhead, ntail;
891 uint32_t if_num;
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800892
893 if_num = NSS_IPSEC_ENCAP_IF_NUMBER;
894 priv = netdev_priv(dev);
895 encap = &priv->encap;
896
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +0530897 nhead = dev->needed_headroom;
898 ntail = dev->needed_tailroom;
899
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800900 if (skb_is_nonlinear(skb)) {
901 nss_ipsecmgr_error("%p: NSS IPSEC does not support fragments %p\n", priv->nss_ctx, skb);
902 goto fail;
903 }
904
905 /*
906 * Check if skb is shared
907 */
908 if (unlikely(skb_shared(skb))) {
909 nss_ipsecmgr_error("%p: Shared skb is not supported: %p\n", priv->nss_ctx, skb);
910 goto fail;
911 }
912
913 /*
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +0530914 * Check and expand the packet head room and tail room
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800915 */
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +0530916 if ((skb_headroom(skb) < nhead) && pskb_expand_head(skb, nhead, ntail, GFP_KERNEL)) {
917 nss_ipsecmgr_error("%p: unable to expand headroom & tailroom(0x%p)\n", priv->nss_ctx, skb);
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800918 goto fail;
919 }
920
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +0530921 /*
922 * Check and expand the packet tail room only
923 */
924 if ((skb_tailroom(skb) < ntail) && pskb_expand_head(skb, 0, ntail, GFP_KERNEL)) {
925 nss_ipsecmgr_error("%p: unable to expand tailroom(0x%p)\n", priv->nss_ctx, skb);
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800926 goto fail;
927 }
928
929 /*
930 * Check if packet is given starting from network header
931 */
932 if (skb->data != skb_network_header(skb)) {
933 nss_ipsecmgr_error("%p: 'Skb data is not starting from IP header", priv->nss_ctx);
934 goto fail;
935 }
936
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +0530937 switch (skb->protocol) {
938 case htons(ETH_P_IP):
939 BUG_ON(ip_hdr(skb)->ttl == 0);
940 break;
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800941
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +0530942 case htons(ETH_P_IPV6):
943 BUG_ON(ipv6_hdr(skb)->hop_limit == 0);
944 break;
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800945
Samarjeet Banerjee07c392c2015-09-18 21:26:12 +0530946 default:
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -0800947 goto fail;
948 }
949
950 /*
951 * Send the packet down
952 */
953 if (nss_ipsec_tx_buf(skb, if_num) != 0) {
954 /*
955 * TODO: NEED TO STOP THE QUEUE
956 */
957 goto fail;
958 }
959
960 /*
961 * Increment the tx count
962 */
963 encap->total_tx++;
964
965 return NETDEV_TX_OK;
966
967fail:
968 return NETDEV_TX_BUSY;
969}
970
971/*
972 * nss_ipsecmgr_tunnel_stats()
973 * get tunnel statistics
974 */
975static struct net_device_stats *nss_ipsecmgr_tunnel_stats(struct net_device *dev)
976{
977 struct net_device_stats *stats = &dev->stats;
978 struct nss_ipsecmgr_priv *priv;
979 struct nss_ipsec_sa *sa;
980 uint32_t i;
981
982 memset(stats, 0, sizeof(struct net_device_stats));
983
984 priv = netdev_priv(dev);
985
986 for (i = 0, sa = &priv->sa_tbl[0]; i < NSS_IPSEC_MAX_SA; i++, sa++) {
987 if (atomic_read(&sa->user) == 0) {
988 continue;
989 }
990
991 switch (sa->type) {
992 case NSS_IPSECMGR_RULE_TYPE_ENCAP:
993 stats->tx_packets += sa->stats.pkts.processed;
994 stats->tx_dropped += sa->stats.pkts.dropped;
995 break;
996
997 case NSS_IPSECMGR_RULE_TYPE_DECAP:
998 stats->rx_packets += sa->stats.pkts.processed;
999 stats->rx_dropped += sa->stats.pkts.dropped;
1000 break;
1001
1002 default:
1003 nss_ipsecmgr_error("unknown ipsec rule type\n");
1004 break;
1005 }
1006 }
1007
1008 return stats;
1009}
1010
1011/* NSS IPsec tunnel operation */
1012static const struct net_device_ops nss_ipsecmgr_tunnel_ops = {
1013 .ndo_init = nss_ipsecmgr_tunnel_init,
1014 .ndo_uninit = nss_ipsecmgr_tunnel_exit,
1015 .ndo_start_xmit = nss_ipsecmgr_tunnel_xmit,
1016 .ndo_open = nss_ipsecmgr_tunnel_open,
1017 .ndo_stop = nss_ipsecmgr_tunnel_stop,
1018 .ndo_get_stats = nss_ipsecmgr_tunnel_stats,
1019};
1020
1021/*
1022 * nss_ipsecmgr_tunnel_free()
1023 * free an existing IPsec tunnel interface
1024 */
1025static void nss_ipsecmgr_tunnel_free(struct net_device *dev)
1026{
1027 struct nss_ipsecmgr_priv *priv = netdev_priv(dev);
1028 struct nss_ipsecmgr_tbl *encap_tbl;
1029 struct nss_ipsecmgr_tbl *decap_tbl;
1030
1031 encap_tbl = &priv->encap;
1032 decap_tbl = &priv->decap;
1033
1034 nss_ipsecmgr_info("IPsec tunnel device(%s) freed\n", dev->name);
1035 nss_ipsecmgr_info("Entries left: encap(%d), decap(%d)\n", encap_tbl->count, decap_tbl->count);
1036
1037 free_netdev(dev);
1038}
1039
1040/*
1041 * nss_ipsecmr_setup_tunnel()
1042 * setup the IPsec tunnel
1043 */
1044static void nss_ipsecmgr_tunnel_setup(struct net_device *dev)
1045{
1046 dev->addr_len = ETH_ALEN;
1047 dev->mtu = NSS_IPSECMGR_TUN_MTU(ETH_DATA_LEN);
1048
1049 dev->hard_header_len = NSS_IPSECMGR_TUN_MAX_HDR_LEN;
1050 dev->needed_headroom = NSS_IPSECMGR_TUN_HEADROOM;
1051 dev->needed_tailroom = NSS_IPSECMGR_TUN_TAILROOM;
1052
1053 dev->type = NSS_IPSEC_ARPHRD_IPSEC;
1054
1055 dev->ethtool_ops = NULL;
1056 dev->header_ops = NULL;
1057 dev->netdev_ops = &nss_ipsecmgr_tunnel_ops;
1058
1059 dev->destructor = nss_ipsecmgr_tunnel_free;
1060
1061 /*
1062 * XXX:should get the MAC address from the ethernet device
1063 */
1064 memcpy(dev->dev_addr, "\xaa\xbb\xcc\xdd\xee\xff", dev->addr_len);
1065 memset(dev->broadcast, 0xff, dev->addr_len);
1066 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
1067}
1068
1069/*
1070 **********************
1071 * Exported Functions
1072 **********************
1073 */
1074
1075/*
1076 * nss_ipsecmgr_tunnel_add()
1077 * add a IPsec pseudo tunnel device
1078 */
1079struct net_device *nss_ipsecmgr_tunnel_add(void *cb_ctx, nss_ipsecmgr_data_cb_t data_cb, nss_ipsecmgr_event_cb_t event_cb)
1080{
1081 struct net_device *dev;
1082 struct nss_ipsecmgr_priv *priv;
1083 struct nss_ipsec_sa *sa;
1084 int status;
1085 int count;
1086 int32_t if_num;
1087
1088 /* Features denote the skb types supported */
1089 uint32_t features = 0;
1090
1091 dev = alloc_netdev(sizeof(struct nss_ipsecmgr_priv), NSS_IPSECMGR_TUN_NAME, nss_ipsecmgr_tunnel_setup);
1092 if (!dev) {
1093 nss_ipsecmgr_error("unable to allocate a tunnel device\n");
1094 return NULL;
1095 }
1096
1097 priv = netdev_priv(dev);
1098
1099 priv->cb_ctx = cb_ctx;
1100 priv->data_cb = data_cb;
1101 priv->event_cb = event_cb;
1102
1103 /* initialize SA stats table for this tunnel */
1104 for (count = 0, sa = &priv->sa_tbl[0]; count < NSS_IPSEC_MAX_SA; count++, sa++) {
1105 nss_ipsecmgr_sa_init(sa);
1106 }
1107
1108 spin_lock_init(&priv->encap.lock);
1109 spin_lock_init(&priv->decap.lock);
1110
1111 status = rtnl_is_locked() ? register_netdevice(dev) : register_netdev(dev);
1112 if (status < 0) {
1113 goto fail;
1114 }
1115
1116 if_num = nss_ipsec_get_interface(priv->nss_ctx);
1117 if (if_num < 0) {
1118 nss_ipsecmgr_error("Invalid nss interface :%d\n", if_num);
1119 return NULL;
1120 }
1121
1122 nss_ipsec_data_register(if_num, nss_ipsecmgr_buf_receive, dev, features);
1123
1124 /*
1125 * Store data interface number in private data
1126 */
1127 priv->nss_ifnum = if_num;
1128
1129 nss_ipsec_notify_register(NSS_IPSEC_ENCAP_IF_NUMBER, nss_ipsecmgr_op_receive, dev);
1130 nss_ipsec_notify_register(NSS_IPSEC_DECAP_IF_NUMBER, nss_ipsecmgr_op_receive, dev);
1131
1132 return dev;
1133
1134fail:
1135 free_netdev(dev);
1136
1137 return NULL;
1138}
1139EXPORT_SYMBOL(nss_ipsecmgr_tunnel_add);
1140
1141/*
1142 * nss_ipsecmgr_del_tunnel()
1143 * delete an existing IPsec tunnel
1144 */
1145bool nss_ipsecmgr_tunnel_del(struct net_device *dev)
1146{
1147 struct nss_ipsecmgr_priv *priv;
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001148 bool status;
1149
1150 priv = netdev_priv(dev);
1151
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001152 /*
1153 * Unregister the callbacks from the HLOS as we are no longer
1154 * interested in exception data & async messages
1155 */
1156 nss_ipsec_data_unregister(priv->nss_ctx, priv->nss_ifnum);
1157
1158 nss_ipsec_notify_unregister(priv->nss_ctx, NSS_IPSEC_ENCAP_IF_NUMBER);
1159 nss_ipsec_notify_unregister(priv->nss_ctx, NSS_IPSEC_DECAP_IF_NUMBER);
1160
1161 priv->data_cb = NULL;
1162 priv->event_cb = NULL;
1163
Amit Gupta2c5439c2015-08-21 20:52:02 +05301164 status = nss_ipsecmgr_sa_flush(dev, NSS_IPSECMGR_RULE_TYPE_ENCAP);
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001165 if (status != true) {
1166 nss_ipsecmgr_error("unable complete tunnel deletion, for ingress rules\n");
1167 goto fail;
1168 }
1169
Amit Gupta2c5439c2015-08-21 20:52:02 +05301170 status = nss_ipsecmgr_sa_flush(dev, NSS_IPSECMGR_RULE_TYPE_DECAP);
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001171 if (status != true) {
1172 nss_ipsecmgr_error("unable complete tunnel deletion, for egress rules\n");
1173 goto fail;
1174 }
1175
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001176 /*
1177 * The unregister should start here but the expectation is that the free would
1178 * happen when the reference count goes down to '0'
1179 */
1180 rtnl_is_locked() ? unregister_netdevice(dev) : unregister_netdev(dev);
1181
1182 return true;
1183fail:
1184 /* caller must retry */
1185 return false;
1186}
1187EXPORT_SYMBOL(nss_ipsecmgr_tunnel_del);
1188
1189/*
1190 * nss_ipsecmgr_sa_add()
1191 * add a new SA to the tunnel
1192 */
1193bool nss_ipsecmgr_sa_add(struct net_device *dev, union nss_ipsecmgr_rule *rule, enum nss_ipsecmgr_rule_type type)
1194{
1195 struct nss_ipsecmgr_priv *priv;
1196 struct nss_ipsec_msg nim;
1197 uint32_t if_num;
1198
1199 priv = netdev_priv(dev);
1200
1201 /* check if there are free SA(s) */
1202 if (atomic_read(&gbl_drv_ctx.sa_count) == 0) {
1203 return false;
1204 }
1205
1206 memset(&nim, 0, sizeof(struct nss_ipsec_msg));
1207
1208 /* XXX: some basic validation of the passed rule needs to happen */
1209
1210 switch (type) {
1211 case NSS_IPSECMGR_RULE_TYPE_ENCAP:
1212 if_num = NSS_IPSEC_ENCAP_IF_NUMBER;
1213 nss_ipsecmgr_copy_encap_add(dev, &nim.msg.push, rule);
1214 break;
1215
1216 case NSS_IPSECMGR_RULE_TYPE_DECAP:
1217 if_num = NSS_IPSEC_DECAP_IF_NUMBER;
1218 nss_ipsecmgr_copy_decap_add(dev, &nim.msg.push, rule);
1219 break;
1220
1221 default:
1222 nss_ipsecmgr_error("Unknown rule type(%d) for Add operation\n", type);
1223 return false;
1224 }
1225
1226 return nss_ipsecmgr_op_send(dev, &nim, if_num, NSS_IPSEC_MSG_TYPE_ADD_RULE);
1227
1228}
1229EXPORT_SYMBOL(nss_ipsecmgr_sa_add);
1230
1231/*
1232 * nss_ipsecmgr_sa_del()
1233 *
1234 */
1235bool nss_ipsecmgr_sa_del(struct net_device *dev, union nss_ipsecmgr_rule *rule, enum nss_ipsecmgr_rule_type type)
1236{
1237 struct nss_ipsecmgr_priv *priv;
1238 struct nss_ipsec_msg nim;
1239 uint32_t if_num;
1240
1241 priv = netdev_priv(dev);
1242
1243 /* check if all SA(s) are already freed */
1244 if (atomic_read(&gbl_drv_ctx.sa_count) == NSS_IPSEC_MAX_RULES) {
1245 return false;
1246 }
1247
1248 memset(&nim, 0, sizeof(struct nss_ipsec_msg));
1249
1250 /* XXX: some basic validation of the passed rule needs to happen */
1251
1252 switch (type) {
1253 case NSS_IPSECMGR_RULE_TYPE_ENCAP:
1254 if_num = NSS_IPSEC_ENCAP_IF_NUMBER;
1255 nss_ipsecmgr_copy_encap_del(dev, &nim.msg.push, rule);
1256 break;
1257
1258 case NSS_IPSECMGR_RULE_TYPE_DECAP:
1259 if_num = NSS_IPSEC_DECAP_IF_NUMBER;
1260 nss_ipsecmgr_copy_decap_del(dev, &nim.msg.push, rule);
1261 break;
1262
1263 default:
1264 nss_ipsecmgr_error("Unknown rule type(%d) for Del operation\n", type);
1265 return false;
1266 }
1267 return nss_ipsecmgr_op_send(dev, &nim, if_num, NSS_IPSEC_MSG_TYPE_DEL_RULE);
1268}
1269EXPORT_SYMBOL(nss_ipsecmgr_sa_del);
1270
Amit Gupta2c5439c2015-08-21 20:52:02 +05301271/*
1272 * nss_ipsecmgr_sa_flush()
1273 * flush NSS rules for all sa of a specific tunnel
1274 */
1275bool nss_ipsecmgr_sa_flush(struct net_device *dev, enum nss_ipsecmgr_rule_type type)
1276{
1277 struct nss_ipsec_msg nim;
1278 int status;
1279 uint32_t if_num;
1280
1281 BUG_ON(dev == NULL);
1282
1283 /*
1284 * prepare to flush all SA(s) associated with the tunne
1285 */
1286 memset(&nim, 0, sizeof(struct nss_ipsec_msg));
1287
1288 switch (type) {
1289 case NSS_IPSECMGR_RULE_TYPE_ENCAP:
1290 if_num = NSS_IPSEC_ENCAP_IF_NUMBER;
1291 break;
1292 case NSS_IPSECMGR_RULE_TYPE_DECAP:
1293 if_num = NSS_IPSEC_DECAP_IF_NUMBER;
1294 break;
1295 default:
1296 nss_ipsecmgr_error("Invalid rule type!: %d\n", type);
1297 return false;
1298 }
1299 status = nss_ipsecmgr_op_send(dev, &nim, if_num, NSS_IPSEC_MSG_TYPE_FLUSH_TUN);
1300 if (status != true) {
1301 nss_ipsecmgr_error("unable to flush sa rules for %d rule\n", type);
1302 return false;
1303 }
1304
1305 return true;
1306}
1307EXPORT_SYMBOL(nss_ipsecmgr_sa_flush);
1308
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001309static int __init nss_ipsecmgr_init(void)
1310{
Murat Sezgin3441e772015-10-26 11:55:57 -07001311#ifdef CONFIG_OF
1312 /*
1313 * If the node is not compatible, don't do anything.
1314 */
1315 if (!of_find_node_by_name(NULL, "nss-common")) {
1316 return 0;
1317 }
1318#endif
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001319 nss_ipsecmgr_info("NSS IPsec manager loaded: Build date %s\n", __DATE__);
1320
1321 memset(&gbl_drv_ctx, 0, sizeof(struct nss_ipsecmgr_drv));
1322
1323 atomic_set(&gbl_drv_ctx.sa_count, NSS_IPSEC_MAX_RULES);
1324
1325 return 0;
1326}
1327
1328
1329static void __exit nss_ipsecmgr_exit(void)
1330{
Murat Sezgin3441e772015-10-26 11:55:57 -07001331#ifdef CONFIG_OF
1332 /*
1333 * If the node is not compatible, don't do anything.
1334 */
1335 if (!of_find_node_by_name(NULL, "nss-common")) {
1336 return;
1337 }
1338#endif
Sundarajan Srinivasan1b03fe22014-12-02 13:20:56 -08001339 nss_ipsecmgr_info("NSS IPsec manager unloader\n");
1340}
1341
1342module_init(nss_ipsecmgr_init);
1343module_exit(nss_ipsecmgr_exit);