xref: /dpdk/lib/ipsec/ses.c (revision 2973dbf93b44981377540e89ca489d6ee1de0f74)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2018-2020 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <rte_ipsec.h>
699a2dd95SBruce Richardson #include "sa.h"
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson static int
session_check(struct rte_ipsec_session * ss)999a2dd95SBruce Richardson session_check(struct rte_ipsec_session *ss)
1099a2dd95SBruce Richardson {
1199a2dd95SBruce Richardson 	if (ss == NULL || ss->sa == NULL)
1299a2dd95SBruce Richardson 		return -EINVAL;
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson 	if (ss->type == RTE_SECURITY_ACTION_TYPE_NONE ||
1599a2dd95SBruce Richardson 		ss->type == RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) {
1699a2dd95SBruce Richardson 		if (ss->crypto.ses == NULL)
1799a2dd95SBruce Richardson 			return -EINVAL;
1899a2dd95SBruce Richardson 	} else {
1999a2dd95SBruce Richardson 		if (ss->security.ses == NULL)
2099a2dd95SBruce Richardson 			return -EINVAL;
2199a2dd95SBruce Richardson 		if ((ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
2299a2dd95SBruce Richardson 				ss->type ==
2399a2dd95SBruce Richardson 				RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) &&
2499a2dd95SBruce Richardson 				ss->security.ctx == NULL)
2599a2dd95SBruce Richardson 			return -EINVAL;
2699a2dd95SBruce Richardson 	}
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson 	return 0;
2999a2dd95SBruce Richardson }
3099a2dd95SBruce Richardson 
3199a2dd95SBruce Richardson int
rte_ipsec_session_prepare(struct rte_ipsec_session * ss)3299a2dd95SBruce Richardson rte_ipsec_session_prepare(struct rte_ipsec_session *ss)
3399a2dd95SBruce Richardson {
3499a2dd95SBruce Richardson 	int32_t rc;
3599a2dd95SBruce Richardson 	struct rte_ipsec_sa_pkt_func fp;
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson 	rc = session_check(ss);
3899a2dd95SBruce Richardson 	if (rc != 0)
3999a2dd95SBruce Richardson 		return rc;
4099a2dd95SBruce Richardson 
4199a2dd95SBruce Richardson 	rc = ipsec_sa_pkt_func_select(ss, ss->sa, &fp);
4299a2dd95SBruce Richardson 	if (rc != 0)
4399a2dd95SBruce Richardson 		return rc;
4499a2dd95SBruce Richardson 
4599a2dd95SBruce Richardson 	ss->pkt_func = fp;
4699a2dd95SBruce Richardson 
4799a2dd95SBruce Richardson 	if (ss->type == RTE_SECURITY_ACTION_TYPE_NONE)
482a440d6aSAkhil Goyal 		rte_cryptodev_sym_session_opaque_data_set(ss->crypto.ses,
492a440d6aSAkhil Goyal 			(uintptr_t)ss);
5099a2dd95SBruce Richardson 	else
51*2973dbf9SAkhil Goyal 		rte_security_session_opaque_data_set(ss->security.ses, (uintptr_t)ss);
5299a2dd95SBruce Richardson 
5399a2dd95SBruce Richardson 	return 0;
5499a2dd95SBruce Richardson }
55