net/virtio: fix incorrect cast of void *
[dpdk.git] / librte_security / rte_security.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 NXP.
3  * Copyright(c) 2017 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
177 /** IPSec security association direction */
178 enum rte_security_ipsec_sa_direction {
179         RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
180         /**< Encrypt and generate digest */
181         RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
182         /**< Verify digest and decrypt */
183 };
184
185 /**
186  * IPsec security association configuration data.
187  *
188  * This structure contains data required to create an IPsec SA security session.
189  */
190 struct rte_security_ipsec_xform {
191         uint32_t spi;
192         /**< SA security parameter index */
193         uint32_t salt;
194         /**< SA salt */
195         struct rte_security_ipsec_sa_options options;
196         /**< various SA options */
197         enum rte_security_ipsec_sa_direction direction;
198         /**< IPSec SA Direction - Egress/Ingress */
199         enum rte_security_ipsec_sa_protocol proto;
200         /**< IPsec SA Protocol - AH/ESP */
201         enum rte_security_ipsec_sa_mode mode;
202         /**< IPsec SA Mode - transport/tunnel */
203         struct rte_security_ipsec_tunnel_param tunnel;
204         /**< Tunnel parameters, NULL for transport mode */
205         uint64_t esn_soft_limit;
206         /**< ESN for which the overflow event need to be raised */
207 };
208
209 /**
210  * MACsec security session configuration
211  */
212 struct rte_security_macsec_xform {
213         /** To be Filled */
214         int dummy;
215 };
216
217 /**
218  * PDCP Mode of session
219  */
220 enum rte_security_pdcp_domain {
221         RTE_SECURITY_PDCP_MODE_CONTROL, /**< PDCP control plane */
222         RTE_SECURITY_PDCP_MODE_DATA,    /**< PDCP data plane */
223 };
224
225 /** PDCP Frame direction */
226 enum rte_security_pdcp_direction {
227         RTE_SECURITY_PDCP_UPLINK,       /**< Uplink */
228         RTE_SECURITY_PDCP_DOWNLINK,     /**< Downlink */
229 };
230
231 /** PDCP Sequence Number Size selectors */
232 enum rte_security_pdcp_sn_size {
233         /** PDCP_SN_SIZE_5: 5bit sequence number */
234         RTE_SECURITY_PDCP_SN_SIZE_5 = 5,
235         /** PDCP_SN_SIZE_7: 7bit sequence number */
236         RTE_SECURITY_PDCP_SN_SIZE_7 = 7,
237         /** PDCP_SN_SIZE_12: 12bit sequence number */
238         RTE_SECURITY_PDCP_SN_SIZE_12 = 12,
239         /** PDCP_SN_SIZE_15: 15bit sequence number */
240         RTE_SECURITY_PDCP_SN_SIZE_15 = 15,
241         /** PDCP_SN_SIZE_18: 18bit sequence number */
242         RTE_SECURITY_PDCP_SN_SIZE_18 = 18
243 };
244
245 /**
246  * PDCP security association configuration data.
247  *
248  * This structure contains data required to create a PDCP security session.
249  */
250 struct rte_security_pdcp_xform {
251         int8_t bearer;  /**< PDCP bearer ID */
252         /** Enable in order delivery, this field shall be set only if
253          * driver/HW is capable. See RTE_SECURITY_PDCP_ORDERING_CAP.
254          */
255         uint8_t en_ordering;
256         /** Notify driver/HW to detect and remove duplicate packets.
257          * This field should be set only when driver/hw is capable.
258          * See RTE_SECURITY_PDCP_DUP_DETECT_CAP.
259          */
260         uint8_t remove_duplicates;
261         /** PDCP mode of operation: Control or data */
262         enum rte_security_pdcp_domain domain;
263         /** PDCP Frame Direction 0:UL 1:DL */
264         enum rte_security_pdcp_direction pkt_dir;
265         /** Sequence number size, 5/7/12/15/18 */
266         enum rte_security_pdcp_sn_size sn_size;
267         /** Starting Hyper Frame Number to be used together with the SN
268          * from the PDCP frames
269          */
270         uint32_t hfn;
271         /** HFN Threshold for key renegotiation */
272         uint32_t hfn_threshold;
273 };
274
275 /**
276  * Security session action type.
277  */
278 enum rte_security_session_action_type {
279         RTE_SECURITY_ACTION_TYPE_NONE,
280         /**< No security actions */
281         RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
282         /**< Crypto processing for security protocol is processed inline
283          * during transmission
284          */
285         RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
286         /**< All security protocol processing is performed inline during
287          * transmission
288          */
289         RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL
290         /**< All security protocol processing including crypto is performed
291          * on a lookaside accelerator
292          */
293 };
294
295 /** Security session protocol definition */
296 enum rte_security_session_protocol {
297         RTE_SECURITY_PROTOCOL_IPSEC = 1,
298         /**< IPsec Protocol */
299         RTE_SECURITY_PROTOCOL_MACSEC,
300         /**< MACSec Protocol */
301         RTE_SECURITY_PROTOCOL_PDCP,
302         /**< PDCP Protocol */
303 };
304
305 /**
306  * Security session configuration
307  */
308 struct rte_security_session_conf {
309         enum rte_security_session_action_type action_type;
310         /**< Type of action to be performed on the session */
311         enum rte_security_session_protocol protocol;
312         /**< Security protocol to be configured */
313         RTE_STD_C11
314         union {
315                 struct rte_security_ipsec_xform ipsec;
316                 struct rte_security_macsec_xform macsec;
317                 struct rte_security_pdcp_xform pdcp;
318         };
319         /**< Configuration parameters for security session */
320         struct rte_crypto_sym_xform *crypto_xform;
321         /**< Security Session Crypto Transformations */
322         void *userdata;
323         /**< Application specific userdata to be saved with session */
324 };
325
326 struct rte_security_session {
327         void *sess_private_data;
328         /**< Private session material */
329         uint64_t opaque_data;
330         /**< Opaque user defined data */
331 };
332
333 /**
334  * Create security session as specified by the session configuration
335  *
336  * @param   instance    security instance
337  * @param   conf        session configuration parameters
338  * @param   mp          mempool to allocate session objects from
339  * @return
340  *  - On success, pointer to session
341  *  - On failure, NULL
342  */
343 struct rte_security_session *
344 rte_security_session_create(struct rte_security_ctx *instance,
345                             struct rte_security_session_conf *conf,
346                             struct rte_mempool *mp);
347
348 /**
349  * Update security session as specified by the session configuration
350  *
351  * @param   instance    security instance
352  * @param   sess        session to update parameters
353  * @param   conf        update configuration parameters
354  * @return
355  *  - On success returns 0
356  *  - On failure return errno
357  */
358 __rte_experimental
359 int
360 rte_security_session_update(struct rte_security_ctx *instance,
361                             struct rte_security_session *sess,
362                             struct rte_security_session_conf *conf);
363
364 /**
365  * Get the size of the security session data for a device.
366  *
367  * @param   instance    security instance.
368  *
369  * @return
370  *   - Size of the private data, if successful
371  *   - 0 if device is invalid or does not support the operation.
372  */
373 unsigned int
374 rte_security_session_get_size(struct rte_security_ctx *instance);
375
376 /**
377  * Free security session header and the session private data and
378  * return it to its original mempool.
379  *
380  * @param   instance    security instance
381  * @param   sess        security session to freed
382  *
383  * @return
384  *  - 0 if successful.
385  *  - -EINVAL if session is NULL.
386  *  - -EBUSY if not all device private data has been freed.
387  */
388 int
389 rte_security_session_destroy(struct rte_security_ctx *instance,
390                              struct rte_security_session *sess);
391
392 /**
393  *  Updates the buffer with device-specific defined metadata
394  *
395  * @param       instance        security instance
396  * @param       sess            security session
397  * @param       mb              packet mbuf to set metadata on.
398  * @param       params          device-specific defined parameters
399  *                              required for metadata
400  *
401  * @return
402  *  - On success, zero.
403  *  - On failure, a negative value.
404  */
405 int
406 rte_security_set_pkt_metadata(struct rte_security_ctx *instance,
407                               struct rte_security_session *sess,
408                               struct rte_mbuf *mb, void *params);
409
410 /**
411  * Get userdata associated with the security session. Device specific metadata
412  * provided would be used to uniquely identify the security session being
413  * referred to. This userdata would be registered while creating the session,
414  * and application can use this to identify the SA etc.
415  *
416  * Device specific metadata would be set in mbuf for inline processed inbound
417  * packets. In addition, the same metadata would be set for IPsec events
418  * reported by rte_eth_event framework.
419  *
420  * @param   instance    security instance
421  * @param   md          device-specific metadata
422  *
423  * @return
424  *  - On success, userdata
425  *  - On failure, NULL
426  */
427 __rte_experimental
428 void *
429 rte_security_get_userdata(struct rte_security_ctx *instance, uint64_t md);
430
431 /**
432  * Attach a session to a symmetric crypto operation
433  *
434  * @param       sym_op  crypto operation
435  * @param       sess    security session
436  */
437 static inline int
438 __rte_security_attach_session(struct rte_crypto_sym_op *sym_op,
439                               struct rte_security_session *sess)
440 {
441         sym_op->sec_session = sess;
442
443         return 0;
444 }
445
446 static inline void *
447 get_sec_session_private_data(const struct rte_security_session *sess)
448 {
449         return sess->sess_private_data;
450 }
451
452 static inline void
453 set_sec_session_private_data(struct rte_security_session *sess,
454                              void *private_data)
455 {
456         sess->sess_private_data = private_data;
457 }
458
459 /**
460  * Attach a session to a crypto operation.
461  * This API is needed only in case of RTE_SECURITY_SESS_CRYPTO_PROTO_OFFLOAD
462  * For other rte_security_session_action_type, ol_flags in rte_mbuf may be
463  * defined to perform security operations.
464  *
465  * @param       op      crypto operation
466  * @param       sess    security session
467  */
468 static inline int
469 rte_security_attach_session(struct rte_crypto_op *op,
470                             struct rte_security_session *sess)
471 {
472         if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
473                 return -EINVAL;
474
475         op->sess_type =  RTE_CRYPTO_OP_SECURITY_SESSION;
476
477         return __rte_security_attach_session(op->sym, sess);
478 }
479
480 struct rte_security_macsec_stats {
481         uint64_t reserved;
482 };
483
484 struct rte_security_ipsec_stats {
485         uint64_t reserved;
486
487 };
488
489 struct rte_security_pdcp_stats {
490         uint64_t reserved;
491 };
492
493 struct rte_security_stats {
494         enum rte_security_session_protocol protocol;
495         /**< Security protocol to be configured */
496
497         RTE_STD_C11
498         union {
499                 struct rte_security_macsec_stats macsec;
500                 struct rte_security_ipsec_stats ipsec;
501                 struct rte_security_pdcp_stats pdcp;
502         };
503 };
504
505 /**
506  * Get security session statistics
507  *
508  * @param       instance        security instance
509  * @param       sess            security session
510  * @param       stats           statistics
511  * @return
512  *  - On success return 0
513  *  - On failure errno
514  */
515 __rte_experimental
516 int
517 rte_security_session_stats_get(struct rte_security_ctx *instance,
518                                struct rte_security_session *sess,
519                                struct rte_security_stats *stats);
520
521 /**
522  * Security capability definition
523  */
524 struct rte_security_capability {
525         enum rte_security_session_action_type action;
526         /**< Security action type*/
527         enum rte_security_session_protocol protocol;
528         /**< Security protocol */
529         RTE_STD_C11
530         union {
531                 struct {
532                         enum rte_security_ipsec_sa_protocol proto;
533                         /**< IPsec SA protocol */
534                         enum rte_security_ipsec_sa_mode mode;
535                         /**< IPsec SA mode */
536                         enum rte_security_ipsec_sa_direction direction;
537                         /**< IPsec SA direction */
538                         struct rte_security_ipsec_sa_options options;
539                         /**< IPsec SA supported options */
540                 } ipsec;
541                 /**< IPsec capability */
542                 struct {
543                         /* To be Filled */
544                         int dummy;
545                 } macsec;
546                 /**< MACsec capability */
547                 struct {
548                         enum rte_security_pdcp_domain domain;
549                         /**< PDCP mode of operation: Control or data */
550                         uint32_t capa_flags;
551                         /**< Capability flags, see RTE_SECURITY_PDCP_* */
552                 } pdcp;
553                 /**< PDCP capability */
554         };
555
556         const struct rte_cryptodev_capabilities *crypto_capabilities;
557         /**< Corresponding crypto capabilities for security capability  */
558
559         uint32_t ol_flags;
560         /**< Device offload flags */
561 };
562
563 /** Underlying Hardware/driver which support PDCP may or may not support
564  * packet ordering. Set RTE_SECURITY_PDCP_ORDERING_CAP if it support.
565  * If it is not set, driver/HW assumes packets received are in order
566  * and it will be application's responsibility to maintain ordering.
567  */
568 #define RTE_SECURITY_PDCP_ORDERING_CAP          0x00000001
569
570 /** Underlying Hardware/driver which support PDCP may or may not detect
571  * duplicate packet. Set RTE_SECURITY_PDCP_DUP_DETECT_CAP if it support.
572  * If it is not set, driver/HW assumes there is no duplicate packet received.
573  */
574 #define RTE_SECURITY_PDCP_DUP_DETECT_CAP        0x00000002
575
576 #define RTE_SECURITY_TX_OLOAD_NEED_MDATA        0x00000001
577 /**< HW needs metadata update, see rte_security_set_pkt_metadata().
578  */
579
580 #define RTE_SECURITY_TX_HW_TRAILER_OFFLOAD      0x00000002
581 /**< HW constructs trailer of packets
582  * Transmitted packets will have the trailer added to them
583  * by hardware. The next protocol field will be based on
584  * the mbuf->inner_esp_next_proto field.
585  */
586 #define RTE_SECURITY_RX_HW_TRAILER_OFFLOAD      0x00010000
587 /**< HW removes trailer of packets
588  * Received packets have no trailer, the next protocol field
589  * is supplied in the mbuf->inner_esp_next_proto field.
590  * Inner packet is not modified.
591  */
592
593 /**
594  * Security capability index used to query a security instance for a specific
595  * security capability
596  */
597 struct rte_security_capability_idx {
598         enum rte_security_session_action_type action;
599         enum rte_security_session_protocol protocol;
600
601         RTE_STD_C11
602         union {
603                 struct {
604                         enum rte_security_ipsec_sa_protocol proto;
605                         enum rte_security_ipsec_sa_mode mode;
606                         enum rte_security_ipsec_sa_direction direction;
607                 } ipsec;
608                 struct {
609                         enum rte_security_pdcp_domain domain;
610                         uint32_t capa_flags;
611                 } pdcp;
612         };
613 };
614
615 /**
616  *  Returns array of security instance capabilities
617  *
618  * @param       instance        Security instance.
619  *
620  * @return
621  *   - Returns array of security capabilities.
622  *   - Return NULL if no capabilities available.
623  */
624 const struct rte_security_capability *
625 rte_security_capabilities_get(struct rte_security_ctx *instance);
626
627 /**
628  * Query if a specific capability is available on security instance
629  *
630  * @param       instance        security instance.
631  * @param       idx             security capability index to match against
632  *
633  * @return
634  *   - Returns pointer to security capability on match of capability
635  *     index criteria.
636  *   - Return NULL if the capability not matched on security instance.
637  */
638 const struct rte_security_capability *
639 rte_security_capability_get(struct rte_security_ctx *instance,
640                             struct rte_security_capability_idx *idx);
641
642 #ifdef __cplusplus
643 }
644 #endif
645
646 #endif /* _RTE_SECURITY_H_ */