security: fix return types in documentation
[dpdk.git] / lib / librte_security / rte_security.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017,2019 NXP
3  * Copyright(c) 2017-2020 Intel Corporation.
4  */
5
6 #ifndef _RTE_SECURITY_H_
7 #define _RTE_SECURITY_H_
8
9 /**
10  * @file rte_security.h
11  *
12  * RTE Security Common Definitions
13  *
14  */
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <sys/types.h>
21
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <netinet/ip6.h>
25
26 #include <rte_compat.h>
27 #include <rte_common.h>
28 #include <rte_crypto.h>
29 #include <rte_mbuf.h>
30 #include <rte_memory.h>
31 #include <rte_mempool.h>
32
33 /** IPSec protocol mode */
34 enum rte_security_ipsec_sa_mode {
35         RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT = 1,
36         /**< IPSec Transport mode */
37         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
38         /**< IPSec Tunnel mode */
39 };
40
41 /** IPSec Protocol */
42 enum rte_security_ipsec_sa_protocol {
43         RTE_SECURITY_IPSEC_SA_PROTO_AH = 1,
44         /**< AH protocol */
45         RTE_SECURITY_IPSEC_SA_PROTO_ESP,
46         /**< ESP protocol */
47 };
48
49 /** IPSEC tunnel type */
50 enum rte_security_ipsec_tunnel_type {
51         RTE_SECURITY_IPSEC_TUNNEL_IPV4 = 1,
52         /**< Outer header is IPv4 */
53         RTE_SECURITY_IPSEC_TUNNEL_IPV6,
54         /**< Outer header is IPv6 */
55 };
56
57 /**
58  * Security context for crypto/eth devices
59  *
60  * Security instance for each driver to register security operations.
61  * The application can get the security context from the crypto/eth device id
62  * using the APIs rte_cryptodev_get_sec_ctx()/rte_eth_dev_get_sec_ctx()
63  * This structure is used to identify the device(crypto/eth) for which the
64  * security operations need to be performed.
65  */
66 struct rte_security_ctx {
67         void *device;
68         /**< Crypto/ethernet device attached */
69         const struct rte_security_ops *ops;
70         /**< Pointer to security ops for the device */
71         uint16_t sess_cnt;
72         /**< Number of sessions attached to this context */
73 };
74
75 /**
76  * IPSEC tunnel parameters
77  *
78  * These parameters are used to build outbound tunnel headers.
79  */
80 struct rte_security_ipsec_tunnel_param {
81         enum rte_security_ipsec_tunnel_type type;
82         /**< Tunnel type: IPv4 or IPv6 */
83         RTE_STD_C11
84         union {
85                 struct {
86                         struct in_addr src_ip;
87                         /**< IPv4 source address */
88                         struct in_addr dst_ip;
89                         /**< IPv4 destination address */
90                         uint8_t dscp;
91                         /**< IPv4 Differentiated Services Code Point */
92                         uint8_t df;
93                         /**< IPv4 Don't Fragment bit */
94                         uint8_t ttl;
95                         /**< IPv4 Time To Live */
96                 } ipv4;
97                 /**< IPv4 header parameters */
98                 struct {
99                         struct in6_addr src_addr;
100                         /**< IPv6 source address */
101                         struct in6_addr dst_addr;
102                         /**< IPv6 destination address */
103                         uint8_t dscp;
104                         /**< IPv6 Differentiated Services Code Point */
105                         uint32_t flabel;
106                         /**< IPv6 flow label */
107                         uint8_t hlimit;
108                         /**< IPv6 hop limit */
109                 } ipv6;
110                 /**< IPv6 header parameters */
111         };
112 };
113
114 /**
115  * IPsec Security Association option flags
116  */
117 struct rte_security_ipsec_sa_options {
118         /** Extended Sequence Numbers (ESN)
119          *
120          * * 1: Use extended (64 bit) sequence numbers
121          * * 0: Use normal sequence numbers
122          */
123         uint32_t esn : 1;
124
125         /** UDP encapsulation
126          *
127          * * 1: Do UDP encapsulation/decapsulation so that IPSEC packets can
128          *      traverse through NAT boxes.
129          * * 0: No UDP encapsulation
130          */
131         uint32_t udp_encap : 1;
132
133         /** Copy DSCP bits
134          *
135          * * 1: Copy IPv4 or IPv6 DSCP bits from inner IP header to
136          *      the outer IP header in encapsulation, and vice versa in
137          *      decapsulation.
138          * * 0: Do not change DSCP field.
139          */
140         uint32_t copy_dscp : 1;
141
142         /** Copy IPv6 Flow Label
143          *
144          * * 1: Copy IPv6 flow label from inner IPv6 header to the
145          *      outer IPv6 header.
146          * * 0: Outer header is not modified.
147          */
148         uint32_t copy_flabel : 1;
149
150         /** Copy IPv4 Don't Fragment bit
151          *
152          * * 1: Copy the DF bit from the inner IPv4 header to the outer
153          *      IPv4 header.
154          * * 0: Outer header is not modified.
155          */
156         uint32_t copy_df : 1;
157
158         /** Decrement inner packet Time To Live (TTL) field
159          *
160          * * 1: In tunnel mode, decrement inner packet IPv4 TTL or
161          *      IPv6 Hop Limit after tunnel decapsulation, or before tunnel
162          *      encapsulation.
163          * * 0: Inner packet is not modified.
164          */
165         uint32_t dec_ttl : 1;
166
167         /** Explicit Congestion Notification (ECN)
168          *
169          * * 1: In tunnel mode, enable outer header ECN Field copied from
170          *      inner header in tunnel encapsulation, or inner header ECN
171          *      field construction in decapsulation.
172          * * 0: Inner/outer header are not modified.
173          */
174         uint32_t ecn : 1;
175
176         /** Security statistics
177          *
178          * * 1: Enable per session security statistics collection for
179          *      this SA, if supported by the driver.
180          * * 0: Disable per session security statistics collection for this SA.
181          */
182         uint32_t stats : 1;
183 };
184
185 /** IPSec security association direction */
186 enum rte_security_ipsec_sa_direction {
187         RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
188         /**< Encrypt and generate digest */
189         RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
190         /**< Verify digest and decrypt */
191 };
192
193 /**
194  * IPsec security association configuration data.
195  *
196  * This structure contains data required to create an IPsec SA security session.
197  */
198 struct rte_security_ipsec_xform {
199         uint32_t spi;
200         /**< SA security parameter index */
201         uint32_t salt;
202         /**< SA salt */
203         struct rte_security_ipsec_sa_options options;
204         /**< various SA options */
205         enum rte_security_ipsec_sa_direction direction;
206         /**< IPSec SA Direction - Egress/Ingress */
207         enum rte_security_ipsec_sa_protocol proto;
208         /**< IPsec SA Protocol - AH/ESP */
209         enum rte_security_ipsec_sa_mode mode;
210         /**< IPsec SA Mode - transport/tunnel */
211         struct rte_security_ipsec_tunnel_param tunnel;
212         /**< Tunnel parameters, NULL for transport mode */
213         uint64_t esn_soft_limit;
214         /**< ESN for which the overflow event need to be raised */
215         uint32_t replay_win_sz;
216         /**< Anti replay window size to enable sequence replay attack handling.
217          * replay checking is disabled if the window size is 0.
218          */
219 };
220
221 /**
222  * MACsec security session configuration
223  */
224 struct rte_security_macsec_xform {
225         /** To be Filled */
226         int dummy;
227 };
228
229 /**
230  * PDCP Mode of session
231  */
232 enum rte_security_pdcp_domain {
233         RTE_SECURITY_PDCP_MODE_CONTROL, /**< PDCP control plane */
234         RTE_SECURITY_PDCP_MODE_DATA,    /**< PDCP data plane */
235 };
236
237 /** PDCP Frame direction */
238 enum rte_security_pdcp_direction {
239         RTE_SECURITY_PDCP_UPLINK,       /**< Uplink */
240         RTE_SECURITY_PDCP_DOWNLINK,     /**< Downlink */
241 };
242
243 /** PDCP Sequence Number Size selectors */
244 enum rte_security_pdcp_sn_size {
245         /** PDCP_SN_SIZE_5: 5bit sequence number */
246         RTE_SECURITY_PDCP_SN_SIZE_5 = 5,
247         /** PDCP_SN_SIZE_7: 7bit sequence number */
248         RTE_SECURITY_PDCP_SN_SIZE_7 = 7,
249         /** PDCP_SN_SIZE_12: 12bit sequence number */
250         RTE_SECURITY_PDCP_SN_SIZE_12 = 12,
251         /** PDCP_SN_SIZE_15: 15bit sequence number */
252         RTE_SECURITY_PDCP_SN_SIZE_15 = 15,
253         /** PDCP_SN_SIZE_18: 18bit sequence number */
254         RTE_SECURITY_PDCP_SN_SIZE_18 = 18
255 };
256
257 /**
258  * PDCP security association configuration data.
259  *
260  * This structure contains data required to create a PDCP security session.
261  */
262 struct rte_security_pdcp_xform {
263         int8_t bearer;  /**< PDCP bearer ID */
264         /** Enable in order delivery, this field shall be set only if
265          * driver/HW is capable. See RTE_SECURITY_PDCP_ORDERING_CAP.
266          */
267         uint8_t en_ordering;
268         /** Notify driver/HW to detect and remove duplicate packets.
269          * This field should be set only when driver/hw is capable.
270          * See RTE_SECURITY_PDCP_DUP_DETECT_CAP.
271          */
272         uint8_t remove_duplicates;
273         /** PDCP mode of operation: Control or data */
274         enum rte_security_pdcp_domain domain;
275         /** PDCP Frame Direction 0:UL 1:DL */
276         enum rte_security_pdcp_direction pkt_dir;
277         /** Sequence number size, 5/7/12/15/18 */
278         enum rte_security_pdcp_sn_size sn_size;
279         /** Starting Hyper Frame Number to be used together with the SN
280          * from the PDCP frames
281          */
282         uint32_t hfn;
283         /** HFN Threshold for key renegotiation */
284         uint32_t hfn_threshold;
285         /** HFN can be given as a per packet value also.
286          * As we do not have IV in case of PDCP, and HFN is
287          * used to generate IV. IV field can be used to get the
288          * per packet HFN while enq/deq.
289          * If hfn_ovrd field is set, user is expected to set the
290          * per packet HFN in place of IV. PMDs will extract the HFN
291          * and perform operations accordingly.
292          */
293         uint32_t hfn_ovrd;
294 };
295
296 /**
297  * Security session action type.
298  */
299 enum rte_security_session_action_type {
300         RTE_SECURITY_ACTION_TYPE_NONE,
301         /**< No security actions */
302         RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
303         /**< Crypto processing for security protocol is processed inline
304          * during transmission
305          */
306         RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
307         /**< All security protocol processing is performed inline during
308          * transmission
309          */
310         RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
311         /**< All security protocol processing including crypto is performed
312          * on a lookaside accelerator
313          */
314         RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO
315         /**< Similar to ACTION_TYPE_NONE but crypto processing for security
316          * protocol is processed synchronously by a CPU.
317          */
318 };
319
320 /** Security session protocol definition */
321 enum rte_security_session_protocol {
322         RTE_SECURITY_PROTOCOL_IPSEC = 1,
323         /**< IPsec Protocol */
324         RTE_SECURITY_PROTOCOL_MACSEC,
325         /**< MACSec Protocol */
326         RTE_SECURITY_PROTOCOL_PDCP,
327         /**< PDCP Protocol */
328 };
329
330 /**
331  * Security session configuration
332  */
333 struct rte_security_session_conf {
334         enum rte_security_session_action_type action_type;
335         /**< Type of action to be performed on the session */
336         enum rte_security_session_protocol protocol;
337         /**< Security protocol to be configured */
338         RTE_STD_C11
339         union {
340                 struct rte_security_ipsec_xform ipsec;
341                 struct rte_security_macsec_xform macsec;
342                 struct rte_security_pdcp_xform pdcp;
343         };
344         /**< Configuration parameters for security session */
345         struct rte_crypto_sym_xform *crypto_xform;
346         /**< Security Session Crypto Transformations */
347         void *userdata;
348         /**< Application specific userdata to be saved with session */
349 };
350
351 struct rte_security_session {
352         void *sess_private_data;
353         /**< Private session material */
354         uint64_t opaque_data;
355         /**< Opaque user defined data */
356 };
357
358 /**
359  * Create security session as specified by the session configuration
360  *
361  * @param   instance    security instance
362  * @param   conf        session configuration parameters
363  * @param   mp          mempool to allocate session objects from
364  * @return
365  *  - On success, pointer to session
366  *  - On failure, NULL
367  */
368 struct rte_security_session *
369 rte_security_session_create(struct rte_security_ctx *instance,
370                             struct rte_security_session_conf *conf,
371                             struct rte_mempool *mp);
372
373 /**
374  * Update security session as specified by the session configuration
375  *
376  * @param   instance    security instance
377  * @param   sess        session to update parameters
378  * @param   conf        update configuration parameters
379  * @return
380  *  - On success returns 0
381  *  - On failure returns a negative errno value.
382  */
383 __rte_experimental
384 int
385 rte_security_session_update(struct rte_security_ctx *instance,
386                             struct rte_security_session *sess,
387                             struct rte_security_session_conf *conf);
388
389 /**
390  * Get the size of the security session data for a device.
391  *
392  * @param   instance    security instance.
393  *
394  * @return
395  *   - Size of the private data, if successful
396  *   - 0 if device is invalid or does not support the operation.
397  */
398 unsigned int
399 rte_security_session_get_size(struct rte_security_ctx *instance);
400
401 /**
402  * Free security session header and the session private data and
403  * return it to its original mempool.
404  *
405  * @param   instance    security instance
406  * @param   sess        security session to be freed
407  *
408  * @return
409  *  - 0 if successful.
410  *  - -EINVAL if session or context instance is NULL.
411  *  - -EBUSY if not all device private data has been freed.
412  *  - -ENOTSUP if destroying private data is not supported.
413  *  - other negative values in case of freeing private data errors.
414  */
415 int
416 rte_security_session_destroy(struct rte_security_ctx *instance,
417                              struct rte_security_session *sess);
418
419 /**
420  *  Updates the buffer with device-specific defined metadata
421  *
422  * @param       instance        security instance
423  * @param       sess            security session
424  * @param       mb              packet mbuf to set metadata on.
425  * @param       params          device-specific defined parameters
426  *                              required for metadata
427  *
428  * @return
429  *  - On success, zero.
430  *  - On failure, a negative value.
431  */
432 int
433 rte_security_set_pkt_metadata(struct rte_security_ctx *instance,
434                               struct rte_security_session *sess,
435                               struct rte_mbuf *mb, void *params);
436
437 /**
438  * Get userdata associated with the security session. Device specific metadata
439  * provided would be used to uniquely identify the security session being
440  * referred to. This userdata would be registered while creating the session,
441  * and application can use this to identify the SA etc.
442  *
443  * Device specific metadata would be set in mbuf for inline processed inbound
444  * packets. In addition, the same metadata would be set for IPsec events
445  * reported by rte_eth_event framework.
446  *
447  * @param   instance    security instance
448  * @param   md          device-specific metadata
449  *
450  * @return
451  *  - On success, userdata
452  *  - On failure, NULL
453  */
454 __rte_experimental
455 void *
456 rte_security_get_userdata(struct rte_security_ctx *instance, uint64_t md);
457
458 /**
459  * Attach a session to a symmetric crypto operation
460  *
461  * @param       sym_op  crypto operation
462  * @param       sess    security session
463  */
464 static inline int
465 __rte_security_attach_session(struct rte_crypto_sym_op *sym_op,
466                               struct rte_security_session *sess)
467 {
468         sym_op->sec_session = sess;
469
470         return 0;
471 }
472
473 static inline void *
474 get_sec_session_private_data(const struct rte_security_session *sess)
475 {
476         return sess->sess_private_data;
477 }
478
479 static inline void
480 set_sec_session_private_data(struct rte_security_session *sess,
481                              void *private_data)
482 {
483         sess->sess_private_data = private_data;
484 }
485
486 /**
487  * Attach a session to a crypto operation.
488  * This API is needed only in case of RTE_SECURITY_SESS_CRYPTO_PROTO_OFFLOAD
489  * For other rte_security_session_action_type, ol_flags in rte_mbuf may be
490  * defined to perform security operations.
491  *
492  * @param       op      crypto operation
493  * @param       sess    security session
494  */
495 static inline int
496 rte_security_attach_session(struct rte_crypto_op *op,
497                             struct rte_security_session *sess)
498 {
499         if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
500                 return -EINVAL;
501
502         op->sess_type =  RTE_CRYPTO_OP_SECURITY_SESSION;
503
504         return __rte_security_attach_session(op->sym, sess);
505 }
506
507 struct rte_security_macsec_stats {
508         uint64_t reserved;
509 };
510
511 struct rte_security_ipsec_stats {
512         uint64_t ipackets;  /**< Successfully received IPsec packets. */
513         uint64_t opackets;  /**< Successfully transmitted IPsec packets.*/
514         uint64_t ibytes;    /**< Successfully received IPsec bytes. */
515         uint64_t obytes;    /**< Successfully transmitted IPsec bytes. */
516         uint64_t ierrors;   /**< IPsec packets receive/decrypt errors. */
517         uint64_t oerrors;   /**< IPsec packets transmit/encrypt errors. */
518         uint64_t reserved1; /**< Reserved for future use. */
519         uint64_t reserved2; /**< Reserved for future use. */
520 };
521
522 struct rte_security_pdcp_stats {
523         uint64_t reserved;
524 };
525
526 struct rte_security_stats {
527         enum rte_security_session_protocol protocol;
528         /**< Security protocol to be configured */
529
530         RTE_STD_C11
531         union {
532                 struct rte_security_macsec_stats macsec;
533                 struct rte_security_ipsec_stats ipsec;
534                 struct rte_security_pdcp_stats pdcp;
535         };
536 };
537
538 /**
539  * Get security session statistics
540  *
541  * @param       instance        security instance
542  * @param       sess            security session
543  * If security session is NULL then global (per security instance) statistics
544  * will be retrieved, if supported. Global statistics collection is not
545  * dependent on the per session statistics configuration.
546  * @param       stats           statistics
547  * @return
548  *  - On success, return 0
549  *  - On failure, a negative value
550  */
551 __rte_experimental
552 int
553 rte_security_session_stats_get(struct rte_security_ctx *instance,
554                                struct rte_security_session *sess,
555                                struct rte_security_stats *stats);
556
557 /**
558  * Security capability definition
559  */
560 struct rte_security_capability {
561         enum rte_security_session_action_type action;
562         /**< Security action type*/
563         enum rte_security_session_protocol protocol;
564         /**< Security protocol */
565         RTE_STD_C11
566         union {
567                 struct {
568                         enum rte_security_ipsec_sa_protocol proto;
569                         /**< IPsec SA protocol */
570                         enum rte_security_ipsec_sa_mode mode;
571                         /**< IPsec SA mode */
572                         enum rte_security_ipsec_sa_direction direction;
573                         /**< IPsec SA direction */
574                         struct rte_security_ipsec_sa_options options;
575                         /**< IPsec SA supported options */
576                         uint32_t replay_win_sz_max;
577                         /**< IPsec Anti Replay Window Size. A '0' value
578                          * indicates that Anti Replay is not supported.
579                          */
580                 } ipsec;
581                 /**< IPsec capability */
582                 struct {
583                         /* To be Filled */
584                         int dummy;
585                 } macsec;
586                 /**< MACsec capability */
587                 struct {
588                         enum rte_security_pdcp_domain domain;
589                         /**< PDCP mode of operation: Control or data */
590                         uint32_t capa_flags;
591                         /**< Capability flags, see RTE_SECURITY_PDCP_* */
592                 } pdcp;
593                 /**< PDCP capability */
594         };
595
596         const struct rte_cryptodev_capabilities *crypto_capabilities;
597         /**< Corresponding crypto capabilities for security capability  */
598
599         uint32_t ol_flags;
600         /**< Device offload flags */
601 };
602
603 /** Underlying Hardware/driver which support PDCP may or may not support
604  * packet ordering. Set RTE_SECURITY_PDCP_ORDERING_CAP if it support.
605  * If it is not set, driver/HW assumes packets received are in order
606  * and it will be application's responsibility to maintain ordering.
607  */
608 #define RTE_SECURITY_PDCP_ORDERING_CAP          0x00000001
609
610 /** Underlying Hardware/driver which support PDCP may or may not detect
611  * duplicate packet. Set RTE_SECURITY_PDCP_DUP_DETECT_CAP if it support.
612  * If it is not set, driver/HW assumes there is no duplicate packet received.
613  */
614 #define RTE_SECURITY_PDCP_DUP_DETECT_CAP        0x00000002
615
616 #define RTE_SECURITY_TX_OLOAD_NEED_MDATA        0x00000001
617 /**< HW needs metadata update, see rte_security_set_pkt_metadata().
618  */
619
620 #define RTE_SECURITY_TX_HW_TRAILER_OFFLOAD      0x00000002
621 /**< HW constructs trailer of packets
622  * Transmitted packets will have the trailer added to them
623  * by hardware. The next protocol field will be based on
624  * the mbuf->inner_esp_next_proto field.
625  */
626 #define RTE_SECURITY_RX_HW_TRAILER_OFFLOAD      0x00010000
627 /**< HW removes trailer of packets
628  * Received packets have no trailer, the next protocol field
629  * is supplied in the mbuf->inner_esp_next_proto field.
630  * Inner packet is not modified.
631  */
632
633 /**
634  * Security capability index used to query a security instance for a specific
635  * security capability
636  */
637 struct rte_security_capability_idx {
638         enum rte_security_session_action_type action;
639         enum rte_security_session_protocol protocol;
640
641         RTE_STD_C11
642         union {
643                 struct {
644                         enum rte_security_ipsec_sa_protocol proto;
645                         enum rte_security_ipsec_sa_mode mode;
646                         enum rte_security_ipsec_sa_direction direction;
647                 } ipsec;
648                 struct {
649                         enum rte_security_pdcp_domain domain;
650                         uint32_t capa_flags;
651                 } pdcp;
652         };
653 };
654
655 /**
656  *  Returns array of security instance capabilities
657  *
658  * @param       instance        Security instance.
659  *
660  * @return
661  *   - Returns array of security capabilities.
662  *   - Return NULL if no capabilities available.
663  */
664 const struct rte_security_capability *
665 rte_security_capabilities_get(struct rte_security_ctx *instance);
666
667 /**
668  * Query if a specific capability is available on security instance
669  *
670  * @param       instance        security instance.
671  * @param       idx             security capability index to match against
672  *
673  * @return
674  *   - Returns pointer to security capability on match of capability
675  *     index criteria.
676  *   - Return NULL if the capability not matched on security instance.
677  */
678 const struct rte_security_capability *
679 rte_security_capability_get(struct rte_security_ctx *instance,
680                             struct rte_security_capability_idx *idx);
681
682 #ifdef __cplusplus
683 }
684 #endif
685
686 #endif /* _RTE_SECURITY_H_ */