1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 NXP.
5  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of NXP nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <rte_malloc.h>
35 #include <rte_dev.h>
36 
37 #include "rte_security.h"
38 #include "rte_security_driver.h"
39 
40 struct rte_security_session *
41 rte_security_session_create(struct rte_security_ctx *instance,
42 			    struct rte_security_session_conf *conf,
43 			    struct rte_mempool *mp)
44 {
45 	struct rte_security_session *sess = NULL;
46 
47 	if (conf == NULL)
48 		return NULL;
49 
50 	RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_create, NULL);
51 
52 	if (rte_mempool_get(mp, (void **)&sess))
53 		return NULL;
54 
55 	if (instance->ops->session_create(instance->device, conf, sess, mp)) {
56 		rte_mempool_put(mp, (void *)sess);
57 		return NULL;
58 	}
59 	instance->sess_cnt++;
60 
61 	return sess;
62 }
63 
64 int
65 rte_security_session_update(struct rte_security_ctx *instance,
66 			    struct rte_security_session *sess,
67 			    struct rte_security_session_conf *conf)
68 {
69 	RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_update, -ENOTSUP);
70 	return instance->ops->session_update(instance->device, sess, conf);
71 }
72 
73 int
74 rte_security_session_stats_get(struct rte_security_ctx *instance,
75 			       struct rte_security_session *sess,
76 			       struct rte_security_stats *stats)
77 {
78 	RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_stats_get, -ENOTSUP);
79 	return instance->ops->session_stats_get(instance->device, sess, stats);
80 }
81 
82 int
83 rte_security_session_destroy(struct rte_security_ctx *instance,
84 			     struct rte_security_session *sess)
85 {
86 	int ret;
87 	struct rte_mempool *mp = rte_mempool_from_obj(sess);
88 
89 	RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_destroy, -ENOTSUP);
90 
91 	if (instance->sess_cnt)
92 		instance->sess_cnt--;
93 
94 	ret = instance->ops->session_destroy(instance->device, sess);
95 	if (!ret)
96 		rte_mempool_put(mp, (void *)sess);
97 
98 	return ret;
99 }
100 
101 int
102 rte_security_set_pkt_metadata(struct rte_security_ctx *instance,
103 			      struct rte_security_session *sess,
104 			      struct rte_mbuf *m, void *params)
105 {
106 	RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->set_pkt_metadata, -ENOTSUP);
107 	return instance->ops->set_pkt_metadata(instance->device,
108 					       sess, m, params);
109 }
110 
111 const struct rte_security_capability *
112 rte_security_capabilities_get(struct rte_security_ctx *instance)
113 {
114 	RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->capabilities_get, NULL);
115 	return instance->ops->capabilities_get(instance->device);
116 }
117 
118 const struct rte_security_capability *
119 rte_security_capability_get(struct rte_security_ctx *instance,
120 			    struct rte_security_capability_idx *idx)
121 {
122 	const struct rte_security_capability *capabilities;
123 	const struct rte_security_capability *capability;
124 	uint16_t i = 0;
125 
126 	RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->capabilities_get, NULL);
127 	capabilities = instance->ops->capabilities_get(instance->device);
128 
129 	if (capabilities == NULL)
130 		return NULL;
131 
132 	while ((capability = &capabilities[i++])->action
133 			!= RTE_SECURITY_ACTION_TYPE_NONE) {
134 		if (capability->action  == idx->action &&
135 				capability->protocol == idx->protocol) {
136 			if (idx->protocol == RTE_SECURITY_PROTOCOL_IPSEC) {
137 				if (capability->ipsec.proto ==
138 						idx->ipsec.proto &&
139 					capability->ipsec.mode ==
140 							idx->ipsec.mode &&
141 					capability->ipsec.direction ==
142 							idx->ipsec.direction)
143 					return capability;
144 			}
145 		}
146 	}
147 
148 	return NULL;
149 }
150