New upstream version 18.02
[deb_dpdk.git] / lib / librte_security / rte_security.h
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 #ifndef _RTE_SECURITY_H_
35 #define _RTE_SECURITY_H_
36
37 /**
38  * @file rte_security.h
39  * @b EXPERIMENTAL: this API may change without prior notice
40  *
41  * RTE Security Common Definitions
42  *
43  */
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #include <sys/types.h>
50
51 #include <netinet/in.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip6.h>
54
55 #include <rte_compat.h>
56 #include <rte_common.h>
57 #include <rte_crypto.h>
58 #include <rte_mbuf.h>
59 #include <rte_memory.h>
60 #include <rte_mempool.h>
61
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 */
68 };
69
70 /** IPSec Protocol */
71 enum rte_security_ipsec_sa_protocol {
72         RTE_SECURITY_IPSEC_SA_PROTO_AH = 1,
73         /**< AH protocol */
74         RTE_SECURITY_IPSEC_SA_PROTO_ESP,
75         /**< ESP protocol */
76 };
77
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 */
84 };
85
86 /**
87  * Security context for crypto/eth devices
88  *
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.
94  */
95 struct rte_security_ctx {
96         void *device;
97         /**< Crypto/ethernet device attached */
98         const struct rte_security_ops *ops;
99         /**< Pointer to security ops for the device */
100         uint16_t sess_cnt;
101         /**< Number of sessions attached to this context */
102 };
103
104 /**
105  * IPSEC tunnel parameters
106  *
107  * These parameters are used to build outbound tunnel headers.
108  */
109 struct rte_security_ipsec_tunnel_param {
110         enum rte_security_ipsec_tunnel_type type;
111         /**< Tunnel type: IPv4 or IPv6 */
112         RTE_STD_C11
113         union {
114                 struct {
115                         struct in_addr src_ip;
116                         /**< IPv4 source address */
117                         struct in_addr dst_ip;
118                         /**< IPv4 destination address */
119                         uint8_t dscp;
120                         /**< IPv4 Differentiated Services Code Point */
121                         uint8_t df;
122                         /**< IPv4 Don't Fragment bit */
123                         uint8_t ttl;
124                         /**< IPv4 Time To Live */
125                 } ipv4;
126                 /**< IPv4 header parameters */
127                 struct {
128                         struct in6_addr src_addr;
129                         /**< IPv6 source address */
130                         struct in6_addr dst_addr;
131                         /**< IPv6 destination address */
132                         uint8_t dscp;
133                         /**< IPv6 Differentiated Services Code Point */
134                         uint32_t flabel;
135                         /**< IPv6 flow label */
136                         uint8_t hlimit;
137                         /**< IPv6 hop limit */
138                 } ipv6;
139                 /**< IPv6 header parameters */
140         };
141 };
142
143 /**
144  * IPsec Security Association option flags
145  */
146 struct rte_security_ipsec_sa_options {
147         /**< Extended Sequence Numbers (ESN)
148          *
149          * * 1: Use extended (64 bit) sequence numbers
150          * * 0: Use normal sequence numbers
151          */
152         uint32_t esn : 1;
153
154         /**< UDP encapsulation
155          *
156          * * 1: Do UDP encapsulation/decapsulation so that IPSEC packets can
157          *      traverse through NAT boxes.
158          * * 0: No UDP encapsulation
159          */
160         uint32_t udp_encap : 1;
161
162         /**< Copy DSCP bits
163          *
164          * * 1: Copy IPv4 or IPv6 DSCP bits from inner IP header to
165          *      the outer IP header in encapsulation, and vice versa in
166          *      decapsulation.
167          * * 0: Do not change DSCP field.
168          */
169         uint32_t copy_dscp : 1;
170
171         /**< Copy IPv6 Flow Label
172          *
173          * * 1: Copy IPv6 flow label from inner IPv6 header to the
174          *      outer IPv6 header.
175          * * 0: Outer header is not modified.
176          */
177         uint32_t copy_flabel : 1;
178
179         /**< Copy IPv4 Don't Fragment bit
180          *
181          * * 1: Copy the DF bit from the inner IPv4 header to the outer
182          *      IPv4 header.
183          * * 0: Outer header is not modified.
184          */
185         uint32_t copy_df : 1;
186
187         /**< Decrement inner packet Time To Live (TTL) field
188          *
189          * * 1: In tunnel mode, decrement inner packet IPv4 TTL or
190          *      IPv6 Hop Limit after tunnel decapsulation, or before tunnel
191          *      encapsulation.
192          * * 0: Inner packet is not modified.
193          */
194         uint32_t dec_ttl : 1;
195 };
196
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 */
203 };
204
205 /**
206  * IPsec security association configuration data.
207  *
208  * This structure contains data required to create an IPsec SA security session.
209  */
210 struct rte_security_ipsec_xform {
211         uint32_t spi;
212         /**< SA security parameter index */
213         uint32_t salt;
214         /**< SA salt */
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 */
225 };
226
227 /**
228  * MACsec security session configuration
229  */
230 struct rte_security_macsec_xform {
231         /** To be Filled */
232         int dummy;
233 };
234
235 /**
236  * Security session action type.
237  */
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
244          */
245         RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
246         /**< All security protocol processing is performed inline during
247          * transmission
248          */
249         RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL
250         /**< All security protocol processing including crypto is performed
251          * on a lookaside accelerator
252          */
253 };
254
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 */
261 };
262
263 /**
264  * Security session configuration
265  */
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 */
271         RTE_STD_C11
272         union {
273                 struct rte_security_ipsec_xform ipsec;
274                 struct rte_security_macsec_xform macsec;
275         };
276         /**< Configuration parameters for security session */
277         struct rte_crypto_sym_xform *crypto_xform;
278         /**< Security Session Crypto Transformations */
279         void *userdata;
280         /**< Application specific userdata to be saved with session */
281 };
282
283 struct rte_security_session {
284         void *sess_private_data;
285         /**< Private session material */
286 };
287
288 /**
289  * Create security session as specified by the session configuration
290  *
291  * @param   instance    security instance
292  * @param   conf        session configuration parameters
293  * @param   mp          mempool to allocate session objects from
294  * @return
295  *  - On success, pointer to session
296  *  - On failure, NULL
297  */
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);
302
303 /**
304  * Update security session as specified by the session configuration
305  *
306  * @param   instance    security instance
307  * @param   sess        session to update parameters
308  * @param   conf        update configuration parameters
309  * @return
310  *  - On success returns 0
311  *  - On failure return errno
312  */
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);
317
318 /**
319  * Get the size of the security session data for a device.
320  *
321  * @param   instance    security instance.
322  *
323  * @return
324  *   - Size of the private data, if successful
325  *   - 0 if device is invalid or does not support the operation.
326  */
327 unsigned int __rte_experimental
328 rte_security_session_get_size(struct rte_security_ctx *instance);
329
330 /**
331  * Free security session header and the session private data and
332  * return it to its original mempool.
333  *
334  * @param   instance    security instance
335  * @param   sess        security session to freed
336  *
337  * @return
338  *  - 0 if successful.
339  *  - -EINVAL if session is NULL.
340  *  - -EBUSY if not all device private data has been freed.
341  */
342 int __rte_experimental
343 rte_security_session_destroy(struct rte_security_ctx *instance,
344                              struct rte_security_session *sess);
345
346 /**
347  *  Updates the buffer with device-specific defined metadata
348  *
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
354  *
355  * @return
356  *  - On success, zero.
357  *  - On failure, a negative value.
358  */
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);
363
364 /**
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.
369  *
370  * This is valid only for inline processed ingress packets.
371  *
372  * @param   instance    security instance
373  * @param   md          device-specific metadata set in mbuf
374  *
375  * @return
376  *  - On success, userdata
377  *  - On failure, NULL
378  */
379 void * __rte_experimental
380 rte_security_get_userdata(struct rte_security_ctx *instance, uint64_t md);
381
382 /**
383  * Attach a session to a symmetric crypto operation
384  *
385  * @param       sym_op  crypto operation
386  * @param       sess    security session
387  */
388 static inline int __rte_experimental
389 __rte_security_attach_session(struct rte_crypto_sym_op *sym_op,
390                               struct rte_security_session *sess)
391 {
392         sym_op->sec_session = sess;
393
394         return 0;
395 }
396
397 static inline void * __rte_experimental
398 get_sec_session_private_data(const struct rte_security_session *sess)
399 {
400         return sess->sess_private_data;
401 }
402
403 static inline void __rte_experimental
404 set_sec_session_private_data(struct rte_security_session *sess,
405                              void *private_data)
406 {
407         sess->sess_private_data = private_data;
408 }
409
410 /**
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.
415  *
416  * @param       op      crypto operation
417  * @param       sess    security session
418  */
419 static inline int __rte_experimental
420 rte_security_attach_session(struct rte_crypto_op *op,
421                             struct rte_security_session *sess)
422 {
423         if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
424                 return -EINVAL;
425
426         op->sess_type =  RTE_CRYPTO_OP_SECURITY_SESSION;
427
428         return __rte_security_attach_session(op->sym, sess);
429 }
430
431 struct rte_security_macsec_stats {
432         uint64_t reserved;
433 };
434
435 struct rte_security_ipsec_stats {
436         uint64_t reserved;
437
438 };
439
440 struct rte_security_stats {
441         enum rte_security_session_protocol protocol;
442         /**< Security protocol to be configured */
443
444         RTE_STD_C11
445         union {
446                 struct rte_security_macsec_stats macsec;
447                 struct rte_security_ipsec_stats ipsec;
448         };
449 };
450
451 /**
452  * Get security session statistics
453  *
454  * @param       instance        security instance
455  * @param       sess            security session
456  * @param       stats           statistics
457  * @return
458  *  - On success return 0
459  *  - On failure errno
460  */
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);
465
466 /**
467  * Security capability definition
468  */
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 */
474         RTE_STD_C11
475         union {
476                 struct {
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 */
485                 } ipsec;
486                 /**< IPsec capability */
487                 struct {
488                         /* To be Filled */
489                         int dummy;
490                 } macsec;
491                 /**< MACsec capability */
492         };
493
494         const struct rte_cryptodev_capabilities *crypto_capabilities;
495         /**< Corresponding crypto capabilities for security capability  */
496
497         uint32_t ol_flags;
498         /**< Device offload flags */
499 };
500
501 #define RTE_SECURITY_TX_OLOAD_NEED_MDATA        0x00000001
502 /**< HW needs metadata update, see rte_security_set_pkt_metadata().
503  */
504
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.
510  */
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.
516  */
517
518 /**
519  * Security capability index used to query a security instance for a specific
520  * security capability
521  */
522 struct rte_security_capability_idx {
523         enum rte_security_session_action_type action;
524         enum rte_security_session_protocol protocol;
525
526         RTE_STD_C11
527         union {
528                 struct {
529                         enum rte_security_ipsec_sa_protocol proto;
530                         enum rte_security_ipsec_sa_mode mode;
531                         enum rte_security_ipsec_sa_direction direction;
532                 } ipsec;
533         };
534 };
535
536 /**
537  *  Returns array of security instance capabilities
538  *
539  * @param       instance        Security instance.
540  *
541  * @return
542  *   - Returns array of security capabilities.
543  *   - Return NULL if no capabilities available.
544  */
545 const struct rte_security_capability * __rte_experimental
546 rte_security_capabilities_get(struct rte_security_ctx *instance);
547
548 /**
549  * Query if a specific capability is available on security instance
550  *
551  * @param       instance        security instance.
552  * @param       idx             security capability index to match against
553  *
554  * @return
555  *   - Returns pointer to security capability on match of capability
556  *     index criteria.
557  *   - Return NULL if the capability not matched on security instance.
558  */
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);
562
563 #ifdef __cplusplus
564 }
565 #endif
566
567 #endif /* _RTE_SECURITY_H_ */