5 * Copyright(c) 2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _RTE_SECURITY_H_
35 #define _RTE_SECURITY_H_
38 * @file rte_security.h
39 * @b EXPERIMENTAL: this API may change without prior notice
41 * RTE Security Common Definitions
49 #include <sys/types.h>
51 #include <netinet/in.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip6.h>
55 #include <rte_compat.h>
56 #include <rte_common.h>
57 #include <rte_crypto.h>
59 #include <rte_memory.h>
60 #include <rte_mempool.h>
62 /** IPSec protocol mode */
63 enum rte_security_ipsec_sa_mode {
64 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT = 1,
65 /**< IPSec Transport mode */
66 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
67 /**< IPSec Tunnel mode */
71 enum rte_security_ipsec_sa_protocol {
72 RTE_SECURITY_IPSEC_SA_PROTO_AH = 1,
74 RTE_SECURITY_IPSEC_SA_PROTO_ESP,
78 /** IPSEC tunnel type */
79 enum rte_security_ipsec_tunnel_type {
80 RTE_SECURITY_IPSEC_TUNNEL_IPV4 = 1,
81 /**< Outer header is IPv4 */
82 RTE_SECURITY_IPSEC_TUNNEL_IPV6,
83 /**< Outer header is IPv6 */
87 * Security context for crypto/eth devices
89 * Security instance for each driver to register security operations.
90 * The application can get the security context from the crypto/eth device id
91 * using the APIs rte_cryptodev_get_sec_ctx()/rte_eth_dev_get_sec_ctx()
92 * This structure is used to identify the device(crypto/eth) for which the
93 * security operations need to be performed.
95 struct rte_security_ctx {
97 /**< Crypto/ethernet device attached */
98 const struct rte_security_ops *ops;
99 /**< Pointer to security ops for the device */
101 /**< Number of sessions attached to this context */
105 * IPSEC tunnel parameters
107 * These parameters are used to build outbound tunnel headers.
109 struct rte_security_ipsec_tunnel_param {
110 enum rte_security_ipsec_tunnel_type type;
111 /**< Tunnel type: IPv4 or IPv6 */
115 struct in_addr src_ip;
116 /**< IPv4 source address */
117 struct in_addr dst_ip;
118 /**< IPv4 destination address */
120 /**< IPv4 Differentiated Services Code Point */
122 /**< IPv4 Don't Fragment bit */
124 /**< IPv4 Time To Live */
126 /**< IPv4 header parameters */
128 struct in6_addr src_addr;
129 /**< IPv6 source address */
130 struct in6_addr dst_addr;
131 /**< IPv6 destination address */
133 /**< IPv6 Differentiated Services Code Point */
135 /**< IPv6 flow label */
137 /**< IPv6 hop limit */
139 /**< IPv6 header parameters */
144 * IPsec Security Association option flags
146 struct rte_security_ipsec_sa_options {
147 /**< Extended Sequence Numbers (ESN)
149 * * 1: Use extended (64 bit) sequence numbers
150 * * 0: Use normal sequence numbers
154 /**< UDP encapsulation
156 * * 1: Do UDP encapsulation/decapsulation so that IPSEC packets can
157 * traverse through NAT boxes.
158 * * 0: No UDP encapsulation
160 uint32_t udp_encap : 1;
164 * * 1: Copy IPv4 or IPv6 DSCP bits from inner IP header to
165 * the outer IP header in encapsulation, and vice versa in
167 * * 0: Do not change DSCP field.
169 uint32_t copy_dscp : 1;
171 /**< Copy IPv6 Flow Label
173 * * 1: Copy IPv6 flow label from inner IPv6 header to the
175 * * 0: Outer header is not modified.
177 uint32_t copy_flabel : 1;
179 /**< Copy IPv4 Don't Fragment bit
181 * * 1: Copy the DF bit from the inner IPv4 header to the outer
183 * * 0: Outer header is not modified.
185 uint32_t copy_df : 1;
187 /**< Decrement inner packet Time To Live (TTL) field
189 * * 1: In tunnel mode, decrement inner packet IPv4 TTL or
190 * IPv6 Hop Limit after tunnel decapsulation, or before tunnel
192 * * 0: Inner packet is not modified.
194 uint32_t dec_ttl : 1;
197 /** IPSec security association direction */
198 enum rte_security_ipsec_sa_direction {
199 RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
200 /**< Encrypt and generate digest */
201 RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
202 /**< Verify digest and decrypt */
206 * IPsec security association configuration data.
208 * This structure contains data required to create an IPsec SA security session.
210 struct rte_security_ipsec_xform {
212 /**< SA security parameter index */
215 struct rte_security_ipsec_sa_options options;
216 /**< various SA options */
217 enum rte_security_ipsec_sa_direction direction;
218 /**< IPSec SA Direction - Egress/Ingress */
219 enum rte_security_ipsec_sa_protocol proto;
220 /**< IPsec SA Protocol - AH/ESP */
221 enum rte_security_ipsec_sa_mode mode;
222 /**< IPsec SA Mode - transport/tunnel */
223 struct rte_security_ipsec_tunnel_param tunnel;
224 /**< Tunnel parameters, NULL for transport mode */
228 * MACsec security session configuration
230 struct rte_security_macsec_xform {
236 * Security session action type.
238 enum rte_security_session_action_type {
239 RTE_SECURITY_ACTION_TYPE_NONE,
240 /**< No security actions */
241 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
242 /**< Crypto processing for security protocol is processed inline
243 * during transmission
245 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
246 /**< All security protocol processing is performed inline during
249 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL
250 /**< All security protocol processing including crypto is performed
251 * on a lookaside accelerator
255 /** Security session protocol definition */
256 enum rte_security_session_protocol {
257 RTE_SECURITY_PROTOCOL_IPSEC = 1,
258 /**< IPsec Protocol */
259 RTE_SECURITY_PROTOCOL_MACSEC,
260 /**< MACSec Protocol */
264 * Security session configuration
266 struct rte_security_session_conf {
267 enum rte_security_session_action_type action_type;
268 /**< Type of action to be performed on the session */
269 enum rte_security_session_protocol protocol;
270 /**< Security protocol to be configured */
273 struct rte_security_ipsec_xform ipsec;
274 struct rte_security_macsec_xform macsec;
276 /**< Configuration parameters for security session */
277 struct rte_crypto_sym_xform *crypto_xform;
278 /**< Security Session Crypto Transformations */
280 /**< Application specific userdata to be saved with session */
283 struct rte_security_session {
284 void *sess_private_data;
285 /**< Private session material */
289 * Create security session as specified by the session configuration
291 * @param instance security instance
292 * @param conf session configuration parameters
293 * @param mp mempool to allocate session objects from
295 * - On success, pointer to session
298 struct rte_security_session * __rte_experimental
299 rte_security_session_create(struct rte_security_ctx *instance,
300 struct rte_security_session_conf *conf,
301 struct rte_mempool *mp);
304 * Update security session as specified by the session configuration
306 * @param instance security instance
307 * @param sess session to update parameters
308 * @param conf update configuration parameters
310 * - On success returns 0
311 * - On failure return errno
313 int __rte_experimental
314 rte_security_session_update(struct rte_security_ctx *instance,
315 struct rte_security_session *sess,
316 struct rte_security_session_conf *conf);
319 * Get the size of the security session data for a device.
321 * @param instance security instance.
324 * - Size of the private data, if successful
325 * - 0 if device is invalid or does not support the operation.
327 unsigned int __rte_experimental
328 rte_security_session_get_size(struct rte_security_ctx *instance);
331 * Free security session header and the session private data and
332 * return it to its original mempool.
334 * @param instance security instance
335 * @param sess security session to freed
339 * - -EINVAL if session is NULL.
340 * - -EBUSY if not all device private data has been freed.
342 int __rte_experimental
343 rte_security_session_destroy(struct rte_security_ctx *instance,
344 struct rte_security_session *sess);
347 * Updates the buffer with device-specific defined metadata
349 * @param instance security instance
350 * @param sess security session
351 * @param mb packet mbuf to set metadata on.
352 * @param params device-specific defined parameters
353 * required for metadata
356 * - On success, zero.
357 * - On failure, a negative value.
359 int __rte_experimental
360 rte_security_set_pkt_metadata(struct rte_security_ctx *instance,
361 struct rte_security_session *sess,
362 struct rte_mbuf *mb, void *params);
365 * Get userdata associated with the security session which processed the
366 * packet. This userdata would be registered while creating the session, and
367 * application can use this to identify the SA etc. Device-specific metadata
368 * in the mbuf would be used for this.
370 * This is valid only for inline processed ingress packets.
372 * @param instance security instance
373 * @param md device-specific metadata set in mbuf
376 * - On success, userdata
379 void * __rte_experimental
380 rte_security_get_userdata(struct rte_security_ctx *instance, uint64_t md);
383 * Attach a session to a symmetric crypto operation
385 * @param sym_op crypto operation
386 * @param sess security session
388 static inline int __rte_experimental
389 __rte_security_attach_session(struct rte_crypto_sym_op *sym_op,
390 struct rte_security_session *sess)
392 sym_op->sec_session = sess;
397 static inline void * __rte_experimental
398 get_sec_session_private_data(const struct rte_security_session *sess)
400 return sess->sess_private_data;
403 static inline void __rte_experimental
404 set_sec_session_private_data(struct rte_security_session *sess,
407 sess->sess_private_data = private_data;
411 * Attach a session to a crypto operation.
412 * This API is needed only in case of RTE_SECURITY_SESS_CRYPTO_PROTO_OFFLOAD
413 * For other rte_security_session_action_type, ol_flags in rte_mbuf may be
414 * defined to perform security operations.
416 * @param op crypto operation
417 * @param sess security session
419 static inline int __rte_experimental
420 rte_security_attach_session(struct rte_crypto_op *op,
421 struct rte_security_session *sess)
423 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
426 op->sess_type = RTE_CRYPTO_OP_SECURITY_SESSION;
428 return __rte_security_attach_session(op->sym, sess);
431 struct rte_security_macsec_stats {
435 struct rte_security_ipsec_stats {
440 struct rte_security_stats {
441 enum rte_security_session_protocol protocol;
442 /**< Security protocol to be configured */
446 struct rte_security_macsec_stats macsec;
447 struct rte_security_ipsec_stats ipsec;
452 * Get security session statistics
454 * @param instance security instance
455 * @param sess security session
456 * @param stats statistics
458 * - On success return 0
461 int __rte_experimental
462 rte_security_session_stats_get(struct rte_security_ctx *instance,
463 struct rte_security_session *sess,
464 struct rte_security_stats *stats);
467 * Security capability definition
469 struct rte_security_capability {
470 enum rte_security_session_action_type action;
471 /**< Security action type*/
472 enum rte_security_session_protocol protocol;
473 /**< Security protocol */
477 enum rte_security_ipsec_sa_protocol proto;
478 /**< IPsec SA protocol */
479 enum rte_security_ipsec_sa_mode mode;
480 /**< IPsec SA mode */
481 enum rte_security_ipsec_sa_direction direction;
482 /**< IPsec SA direction */
483 struct rte_security_ipsec_sa_options options;
484 /**< IPsec SA supported options */
486 /**< IPsec capability */
491 /**< MACsec capability */
494 const struct rte_cryptodev_capabilities *crypto_capabilities;
495 /**< Corresponding crypto capabilities for security capability */
498 /**< Device offload flags */
501 #define RTE_SECURITY_TX_OLOAD_NEED_MDATA 0x00000001
502 /**< HW needs metadata update, see rte_security_set_pkt_metadata().
505 #define RTE_SECURITY_TX_HW_TRAILER_OFFLOAD 0x00000002
506 /**< HW constructs trailer of packets
507 * Transmitted packets will have the trailer added to them
508 * by hardawre. The next protocol field will be based on
509 * the mbuf->inner_esp_next_proto field.
511 #define RTE_SECURITY_RX_HW_TRAILER_OFFLOAD 0x00010000
512 /**< HW removes trailer of packets
513 * Received packets have no trailer, the next protocol field
514 * is supplied in the mbuf->inner_esp_next_proto field.
515 * Inner packet is not modified.
519 * Security capability index used to query a security instance for a specific
520 * security capability
522 struct rte_security_capability_idx {
523 enum rte_security_session_action_type action;
524 enum rte_security_session_protocol protocol;
529 enum rte_security_ipsec_sa_protocol proto;
530 enum rte_security_ipsec_sa_mode mode;
531 enum rte_security_ipsec_sa_direction direction;
537 * Returns array of security instance capabilities
539 * @param instance Security instance.
542 * - Returns array of security capabilities.
543 * - Return NULL if no capabilities available.
545 const struct rte_security_capability * __rte_experimental
546 rte_security_capabilities_get(struct rte_security_ctx *instance);
549 * Query if a specific capability is available on security instance
551 * @param instance security instance.
552 * @param idx security capability index to match against
555 * - Returns pointer to security capability on match of capability
557 * - Return NULL if the capability not matched on security instance.
559 const struct rte_security_capability * __rte_experimental
560 rte_security_capability_get(struct rte_security_ctx *instance,
561 struct rte_security_capability_idx *idx);
567 #endif /* _RTE_SECURITY_H_ */