1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
6 * Security Associations
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
11 #include <netinet/ip6.h>
13 #include <rte_memzone.h>
14 #include <rte_crypto.h>
15 #include <rte_security.h>
16 #include <rte_cryptodev.h>
17 #include <rte_byteorder.h>
18 #include <rte_errno.h>
20 #include <rte_random.h>
21 #include <rte_ethdev.h>
22 #include <rte_malloc.h>
30 #define IP4_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip4) * CHAR_BIT)
32 #define IP6_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip6.ip6) * CHAR_BIT)
34 #define MBUF_NO_SEC_OFFLOAD(m) ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)
36 struct supported_cipher_algo {
38 enum rte_crypto_cipher_algorithm algo;
44 struct supported_auth_algo {
46 enum rte_crypto_auth_algorithm algo;
52 struct supported_aead_algo {
54 enum rte_crypto_aead_algorithm algo;
63 const struct supported_cipher_algo cipher_algos[] = {
66 .algo = RTE_CRYPTO_CIPHER_NULL,
72 .keyword = "aes-128-cbc",
73 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
79 .keyword = "aes-256-cbc",
80 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
86 .keyword = "aes-128-ctr",
87 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
93 .keyword = "3des-cbc",
94 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
101 const struct supported_auth_algo auth_algos[] = {
104 .algo = RTE_CRYPTO_AUTH_NULL,
110 .keyword = "sha1-hmac",
111 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
116 .keyword = "sha256-hmac",
117 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
123 const struct supported_aead_algo aead_algos[] = {
125 .keyword = "aes-128-gcm",
126 .algo = RTE_CRYPTO_AEAD_AES_GCM,
135 static struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
136 static uint32_t nb_sa_out;
138 static struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
139 static uint32_t nb_sa_in;
141 static const struct supported_cipher_algo *
142 find_match_cipher_algo(const char *cipher_keyword)
146 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
147 const struct supported_cipher_algo *algo =
150 if (strcmp(cipher_keyword, algo->keyword) == 0)
157 static const struct supported_auth_algo *
158 find_match_auth_algo(const char *auth_keyword)
162 for (i = 0; i < RTE_DIM(auth_algos); i++) {
163 const struct supported_auth_algo *algo =
166 if (strcmp(auth_keyword, algo->keyword) == 0)
173 static const struct supported_aead_algo *
174 find_match_aead_algo(const char *aead_keyword)
178 for (i = 0; i < RTE_DIM(aead_algos); i++) {
179 const struct supported_aead_algo *algo =
182 if (strcmp(aead_keyword, algo->keyword) == 0)
190 * parse x:x:x:x.... hex number key string into uint8_t *key
192 * > 0: number of bytes parsed
196 parse_key_string(const char *key_str, uint8_t *key)
198 const char *pt_start = key_str, *pt_end = key_str;
199 uint32_t nb_bytes = 0;
201 while (pt_end != NULL) {
202 char sub_str[3] = {0};
204 pt_end = strchr(pt_start, ':');
206 if (pt_end == NULL) {
207 if (strlen(pt_start) > 2)
209 strncpy(sub_str, pt_start, 2);
211 if (pt_end - pt_start > 2)
214 strncpy(sub_str, pt_start, pt_end - pt_start);
215 pt_start = pt_end + 1;
218 key[nb_bytes++] = strtol(sub_str, NULL, 16);
225 parse_sa_tokens(char **tokens, uint32_t n_tokens,
226 struct parse_status *status)
228 struct ipsec_sa *rule = NULL;
229 struct rte_ipsec_session *ips;
230 uint32_t ti; /*token index*/
231 uint32_t *ri /*rule index*/;
232 uint32_t cipher_algo_p = 0;
233 uint32_t auth_algo_p = 0;
234 uint32_t aead_algo_p = 0;
239 uint32_t portid_p = 0;
240 uint32_t fallback_p = 0;
242 if (strcmp(tokens[0], "in") == 0) {
245 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
246 "too many sa rules, abort insertion\n");
247 if (status->status < 0)
251 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
255 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
256 "too many sa rules, abort insertion\n");
257 if (status->status < 0)
261 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
265 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
266 if (status->status < 0)
268 if (atoi(tokens[1]) == INVALID_SPI)
270 rule->spi = atoi(tokens[1]);
271 ips = ipsec_get_primary_session(rule);
273 for (ti = 2; ti < n_tokens; ti++) {
274 if (strcmp(tokens[ti], "mode") == 0) {
275 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
276 if (status->status < 0)
279 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
280 if (status->status < 0)
283 if (strcmp(tokens[ti], "ipv4-tunnel") == 0)
284 rule->flags = IP4_TUNNEL;
285 else if (strcmp(tokens[ti], "ipv6-tunnel") == 0)
286 rule->flags = IP6_TUNNEL;
287 else if (strcmp(tokens[ti], "transport") == 0)
288 rule->flags = TRANSPORT;
290 APP_CHECK(0, status, "unrecognized "
291 "input \"%s\"", tokens[ti]);
299 if (strcmp(tokens[ti], "cipher_algo") == 0) {
300 const struct supported_cipher_algo *algo;
303 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
305 if (status->status < 0)
308 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
309 if (status->status < 0)
312 algo = find_match_cipher_algo(tokens[ti]);
314 APP_CHECK(algo != NULL, status, "unrecognized "
315 "input \"%s\"", tokens[ti]);
317 rule->cipher_algo = algo->algo;
318 rule->block_size = algo->block_size;
319 rule->iv_len = algo->iv_len;
320 rule->cipher_key_len = algo->key_len;
322 /* for NULL algorithm, no cipher key required */
323 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
328 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
329 if (status->status < 0)
332 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
333 status, "unrecognized input \"%s\", "
334 "expect \"cipher_key\"", tokens[ti]);
335 if (status->status < 0)
338 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
339 if (status->status < 0)
342 key_len = parse_key_string(tokens[ti],
344 APP_CHECK(key_len == rule->cipher_key_len, status,
345 "unrecognized input \"%s\"", tokens[ti]);
346 if (status->status < 0)
349 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC ||
350 algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC)
351 rule->salt = (uint32_t)rte_rand();
353 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
355 rule->cipher_key_len = key_len;
357 &rule->cipher_key[key_len], 4);
364 if (strcmp(tokens[ti], "auth_algo") == 0) {
365 const struct supported_auth_algo *algo;
368 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
370 if (status->status < 0)
373 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
374 if (status->status < 0)
377 algo = find_match_auth_algo(tokens[ti]);
378 APP_CHECK(algo != NULL, status, "unrecognized "
379 "input \"%s\"", tokens[ti]);
381 rule->auth_algo = algo->algo;
382 rule->auth_key_len = algo->key_len;
383 rule->digest_len = algo->digest_len;
385 /* NULL algorithm and combined algos do not
388 if (algo->key_not_req) {
393 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
394 if (status->status < 0)
397 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
398 status, "unrecognized input \"%s\", "
399 "expect \"auth_key\"", tokens[ti]);
400 if (status->status < 0)
403 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
404 if (status->status < 0)
407 key_len = parse_key_string(tokens[ti],
409 APP_CHECK(key_len == rule->auth_key_len, status,
410 "unrecognized input \"%s\"", tokens[ti]);
411 if (status->status < 0)
418 if (strcmp(tokens[ti], "aead_algo") == 0) {
419 const struct supported_aead_algo *algo;
422 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
424 if (status->status < 0)
427 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
428 if (status->status < 0)
431 algo = find_match_aead_algo(tokens[ti]);
433 APP_CHECK(algo != NULL, status, "unrecognized "
434 "input \"%s\"", tokens[ti]);
436 rule->aead_algo = algo->algo;
437 rule->cipher_key_len = algo->key_len;
438 rule->digest_len = algo->digest_len;
439 rule->aad_len = algo->aad_len;
440 rule->block_size = algo->block_size;
441 rule->iv_len = algo->iv_len;
443 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
444 if (status->status < 0)
447 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
448 status, "unrecognized input \"%s\", "
449 "expect \"aead_key\"", tokens[ti]);
450 if (status->status < 0)
453 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
454 if (status->status < 0)
457 key_len = parse_key_string(tokens[ti],
459 APP_CHECK(key_len == rule->cipher_key_len, status,
460 "unrecognized input \"%s\"", tokens[ti]);
461 if (status->status < 0)
465 rule->cipher_key_len = key_len;
467 &rule->cipher_key[key_len], 4);
473 if (strcmp(tokens[ti], "src") == 0) {
474 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
475 if (status->status < 0)
478 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
479 if (status->status < 0)
482 if (IS_IP4_TUNNEL(rule->flags)) {
485 APP_CHECK(parse_ipv4_addr(tokens[ti],
486 &ip, NULL) == 0, status,
487 "unrecognized input \"%s\", "
488 "expect valid ipv4 addr",
490 if (status->status < 0)
492 rule->src.ip.ip4 = rte_bswap32(
493 (uint32_t)ip.s_addr);
494 } else if (IS_IP6_TUNNEL(rule->flags)) {
497 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
499 "unrecognized input \"%s\", "
500 "expect valid ipv6 addr",
502 if (status->status < 0)
504 memcpy(rule->src.ip.ip6.ip6_b,
506 } else if (IS_TRANSPORT(rule->flags)) {
507 APP_CHECK(0, status, "unrecognized input "
508 "\"%s\"", tokens[ti]);
516 if (strcmp(tokens[ti], "dst") == 0) {
517 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
518 if (status->status < 0)
521 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
522 if (status->status < 0)
525 if (IS_IP4_TUNNEL(rule->flags)) {
528 APP_CHECK(parse_ipv4_addr(tokens[ti],
529 &ip, NULL) == 0, status,
530 "unrecognized input \"%s\", "
531 "expect valid ipv4 addr",
533 if (status->status < 0)
535 rule->dst.ip.ip4 = rte_bswap32(
536 (uint32_t)ip.s_addr);
537 } else if (IS_IP6_TUNNEL(rule->flags)) {
540 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
542 "unrecognized input \"%s\", "
543 "expect valid ipv6 addr",
545 if (status->status < 0)
547 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
548 } else if (IS_TRANSPORT(rule->flags)) {
549 APP_CHECK(0, status, "unrecognized "
550 "input \"%s\"", tokens[ti]);
558 if (strcmp(tokens[ti], "type") == 0) {
559 APP_CHECK_PRESENCE(type_p, tokens[ti], status);
560 if (status->status < 0)
563 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
564 if (status->status < 0)
567 if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
569 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
570 else if (strcmp(tokens[ti],
571 "inline-protocol-offload") == 0)
573 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
574 else if (strcmp(tokens[ti],
575 "lookaside-protocol-offload") == 0)
577 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
578 else if (strcmp(tokens[ti], "no-offload") == 0)
579 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
581 APP_CHECK(0, status, "Invalid input \"%s\"",
590 if (strcmp(tokens[ti], "port_id") == 0) {
591 APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
592 if (status->status < 0)
594 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
595 if (status->status < 0)
597 rule->portid = atoi(tokens[ti]);
598 if (status->status < 0)
604 if (strcmp(tokens[ti], "fallback") == 0) {
605 struct rte_ipsec_session *fb;
607 APP_CHECK(app_sa_prm.enable, status, "Fallback session "
608 "not allowed for legacy mode.");
609 if (status->status < 0)
611 APP_CHECK(ips->type ==
612 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, status,
613 "Fallback session allowed if primary session "
614 "is of type inline-crypto-offload only.");
615 if (status->status < 0)
617 APP_CHECK(rule->direction ==
618 RTE_SECURITY_IPSEC_SA_DIR_INGRESS, status,
619 "Fallback session not allowed for egress "
621 if (status->status < 0)
623 APP_CHECK_PRESENCE(fallback_p, tokens[ti], status);
624 if (status->status < 0)
626 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
627 if (status->status < 0)
629 fb = ipsec_get_fallback_session(rule);
630 if (strcmp(tokens[ti], "lookaside-none") == 0) {
631 fb->type = RTE_SECURITY_ACTION_TYPE_NONE;
633 APP_CHECK(0, status, "unrecognized fallback "
634 "type %s.", tokens[ti]);
638 rule->fallback_sessions = 1;
643 /* unrecognizeable input */
644 APP_CHECK(0, status, "unrecognized input \"%s\"",
650 APP_CHECK(cipher_algo_p == 0, status,
651 "AEAD used, no need for cipher options");
652 if (status->status < 0)
655 APP_CHECK(auth_algo_p == 0, status,
656 "AEAD used, no need for auth options");
657 if (status->status < 0)
660 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
661 if (status->status < 0)
664 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
665 if (status->status < 0)
669 APP_CHECK(mode_p == 1, status, "missing mode option");
670 if (status->status < 0)
673 if ((ips->type != RTE_SECURITY_ACTION_TYPE_NONE) && (portid_p == 0))
674 printf("Missing portid option, falling back to non-offload\n");
676 if (!type_p || !portid_p) {
677 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
685 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
689 const struct rte_ipsec_session *ips;
690 const struct rte_ipsec_session *fallback_ips;
692 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
694 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
695 if (cipher_algos[i].algo == sa->cipher_algo &&
696 cipher_algos[i].key_len == sa->cipher_key_len) {
697 printf("%s ", cipher_algos[i].keyword);
702 for (i = 0; i < RTE_DIM(auth_algos); i++) {
703 if (auth_algos[i].algo == sa->auth_algo) {
704 printf("%s ", auth_algos[i].keyword);
709 for (i = 0; i < RTE_DIM(aead_algos); i++) {
710 if (aead_algos[i].algo == sa->aead_algo) {
711 printf("%s ", aead_algos[i].keyword);
718 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
720 printf("IP4Tunnel ");
721 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
722 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
723 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
724 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
727 printf("IP6Tunnel ");
728 for (i = 0; i < 16; i++) {
729 if (i % 2 && i != 15)
730 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
732 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
735 for (i = 0; i < 16; i++) {
736 if (i % 2 && i != 15)
737 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
739 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
743 printf("Transport ");
747 ips = &sa->sessions[IPSEC_SESSION_PRIMARY];
750 case RTE_SECURITY_ACTION_TYPE_NONE:
751 printf("no-offload ");
753 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
754 printf("inline-crypto-offload ");
756 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
757 printf("inline-protocol-offload ");
759 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
760 printf("lookaside-protocol-offload ");
764 fallback_ips = &sa->sessions[IPSEC_SESSION_FALLBACK];
765 if (fallback_ips != NULL && sa->fallback_sessions > 0) {
766 printf("inline fallback: ");
767 if (fallback_ips->type == RTE_SECURITY_ACTION_TYPE_NONE)
768 printf("lookaside-none");
776 void *satbl; /* pointer to array of rte_ipsec_sa objects*/
777 struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
780 struct rte_crypto_sym_xform a;
781 struct rte_crypto_sym_xform b;
783 } xf[IPSEC_SA_MAX_ENTRIES];
786 static struct sa_ctx *
787 sa_create(const char *name, int32_t socket_id)
790 struct sa_ctx *sa_ctx;
792 const struct rte_memzone *mz;
794 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
796 /* Create SA array table */
797 printf("Creating SA context with %u maximum entries on socket %d\n",
798 IPSEC_SA_MAX_ENTRIES, socket_id);
800 mz_size = sizeof(struct sa_ctx);
801 mz = rte_memzone_reserve(s, mz_size, socket_id,
802 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
804 printf("Failed to allocate SA DB memory\n");
809 sa_ctx = (struct sa_ctx *)mz->addr;
815 check_eth_dev_caps(uint16_t portid, uint32_t inbound)
817 struct rte_eth_dev_info dev_info;
820 retval = rte_eth_dev_info_get(portid, &dev_info);
823 "Error during getting device (port %u) info: %s\n",
824 portid, strerror(-retval));
830 if ((dev_info.rx_offload_capa &
831 DEV_RX_OFFLOAD_SECURITY) == 0) {
832 RTE_LOG(WARNING, PORT,
833 "hardware RX IPSec offload is not supported\n");
837 } else { /* outbound */
838 if ((dev_info.tx_offload_capa &
839 DEV_TX_OFFLOAD_SECURITY) == 0) {
840 RTE_LOG(WARNING, PORT,
841 "hardware TX IPSec offload is not supported\n");
849 * Helper function, tries to determine next_proto for SPI
850 * by searching though SP rules.
853 get_spi_proto(uint32_t spi, enum rte_security_ipsec_sa_direction dir,
854 struct ip_addr ip_addr[2], uint32_t mask[2])
858 rc4 = sp4_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
860 rc6 = sp6_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
866 "%s: SPI %u used simultaeously by "
867 "IPv4(%d) and IPv6 (%d) SP rules\n",
868 __func__, spi, rc4, rc6);
872 } else if (rc6 < 0) {
874 "%s: SPI %u is not used by any SP rule\n",
882 * Helper function for getting source and destination IP addresses
883 * from SP. Needed for inline crypto transport mode, as addresses are not
884 * provided in config file for that mode. It checks if SP for current SA exists,
885 * and based on what type of protocol is returned, it stores appropriate
886 * addresses got from SP into SA.
889 sa_add_address_inline_crypto(struct ipsec_sa *sa)
892 struct ip_addr ip_addr[2];
895 protocol = get_spi_proto(sa->spi, sa->direction, ip_addr, mask);
898 else if (protocol == IPPROTO_IPIP) {
899 sa->flags |= IP4_TRANSPORT;
900 if (mask[0] == IP4_FULL_MASK &&
901 mask[1] == IP4_FULL_MASK &&
902 ip_addr[0].ip.ip4 != 0 &&
903 ip_addr[1].ip.ip4 != 0) {
905 sa->src.ip.ip4 = ip_addr[0].ip.ip4;
906 sa->dst.ip.ip4 = ip_addr[1].ip.ip4;
909 "%s: No valid address or mask entry in"
910 " IPv4 SP rule for SPI %u\n",
914 } else if (protocol == IPPROTO_IPV6) {
915 sa->flags |= IP6_TRANSPORT;
916 if (mask[0] == IP6_FULL_MASK &&
917 mask[1] == IP6_FULL_MASK &&
918 (ip_addr[0].ip.ip6.ip6[0] != 0 ||
919 ip_addr[0].ip.ip6.ip6[1] != 0) &&
920 (ip_addr[1].ip.ip6.ip6[0] != 0 ||
921 ip_addr[1].ip.ip6.ip6[1] != 0)) {
923 sa->src.ip.ip6 = ip_addr[0].ip.ip6;
924 sa->dst.ip.ip6 = ip_addr[1].ip.ip6;
927 "%s: No valid address or mask entry in"
928 " IPv6 SP rule for SPI %u\n",
937 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
938 uint32_t nb_entries, uint32_t inbound,
939 struct socket_ctx *skt_ctx)
943 uint16_t iv_length, aad_length;
946 struct rte_ipsec_session *ips;
948 /* for ESN upper 32 bits of SQN also need to be part of AAD */
949 aad_length = (app_sa_prm.enable_esn != 0) ? sizeof(uint32_t) : 0;
951 for (i = 0; i < nb_entries; i++) {
952 idx = SPI2IDX(entries[i].spi);
953 sa = &sa_ctx->sa[idx];
955 printf("Index %u already in use by SPI %u\n",
961 ips = ipsec_get_primary_session(sa);
963 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
964 ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
965 if (check_eth_dev_caps(sa->portid, inbound))
970 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
972 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
973 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
977 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
979 sa_add_address_inline_crypto(sa);
980 if (inline_status < 0)
981 return inline_status;
986 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
987 struct rte_ipsec_session *ips;
990 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
991 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
992 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
993 sa_ctx->xf[idx].a.aead.key.length =
995 sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
996 RTE_CRYPTO_AEAD_OP_DECRYPT :
997 RTE_CRYPTO_AEAD_OP_ENCRYPT;
998 sa_ctx->xf[idx].a.next = NULL;
999 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
1000 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
1001 sa_ctx->xf[idx].a.aead.aad_length =
1002 sa->aad_len + aad_length;
1003 sa_ctx->xf[idx].a.aead.digest_length =
1006 sa->xforms = &sa_ctx->xf[idx].a;
1008 ips = ipsec_get_primary_session(sa);
1010 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1012 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1013 rc = create_inline_session(skt_ctx, sa, ips);
1015 RTE_LOG(ERR, IPSEC_ESP,
1016 "create_inline_session() failed\n");
1020 print_one_sa_rule(sa, inbound);
1022 switch (sa->cipher_algo) {
1023 case RTE_CRYPTO_CIPHER_NULL:
1024 case RTE_CRYPTO_CIPHER_3DES_CBC:
1025 case RTE_CRYPTO_CIPHER_AES_CBC:
1026 iv_length = sa->iv_len;
1028 case RTE_CRYPTO_CIPHER_AES_CTR:
1032 RTE_LOG(ERR, IPSEC_ESP,
1033 "unsupported cipher algorithm %u\n",
1039 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1040 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
1041 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
1042 sa_ctx->xf[idx].b.cipher.key.length =
1044 sa_ctx->xf[idx].b.cipher.op =
1045 RTE_CRYPTO_CIPHER_OP_DECRYPT;
1046 sa_ctx->xf[idx].b.next = NULL;
1047 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
1048 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
1050 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1051 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
1052 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
1053 sa_ctx->xf[idx].a.auth.key.length =
1055 sa_ctx->xf[idx].a.auth.digest_length =
1057 sa_ctx->xf[idx].a.auth.op =
1058 RTE_CRYPTO_AUTH_OP_VERIFY;
1059 } else { /* outbound */
1060 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1061 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
1062 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
1063 sa_ctx->xf[idx].a.cipher.key.length =
1065 sa_ctx->xf[idx].a.cipher.op =
1066 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1067 sa_ctx->xf[idx].a.next = NULL;
1068 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
1069 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
1071 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1072 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
1073 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
1074 sa_ctx->xf[idx].b.auth.key.length =
1076 sa_ctx->xf[idx].b.auth.digest_length =
1078 sa_ctx->xf[idx].b.auth.op =
1079 RTE_CRYPTO_AUTH_OP_GENERATE;
1082 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
1083 sa_ctx->xf[idx].b.next = NULL;
1084 sa->xforms = &sa_ctx->xf[idx].a;
1086 print_one_sa_rule(sa, inbound);
1094 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1095 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1097 return sa_add_rules(sa_ctx, entries, nb_entries, 0, skt_ctx);
1101 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1102 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1104 return sa_add_rules(sa_ctx, entries, nb_entries, 1, skt_ctx);
1108 * helper function, fills parameters that are identical for all SAs
1111 fill_ipsec_app_sa_prm(struct rte_ipsec_sa_prm *prm,
1112 const struct app_sa_prm *app_prm)
1114 memset(prm, 0, sizeof(*prm));
1116 prm->flags = app_prm->flags;
1117 prm->ipsec_xform.options.esn = app_prm->enable_esn;
1118 prm->ipsec_xform.replay_win_sz = app_prm->window_size;
1122 fill_ipsec_sa_prm(struct rte_ipsec_sa_prm *prm, const struct ipsec_sa *ss,
1123 const struct rte_ipv4_hdr *v4, struct rte_ipv6_hdr *v6)
1128 * Try to get SPI next proto by searching that SPI in SPD.
1129 * probably not the optimal way, but there seems nothing
1132 rc = get_spi_proto(ss->spi, ss->direction, NULL, NULL);
1136 fill_ipsec_app_sa_prm(prm, &app_sa_prm);
1137 prm->userdata = (uintptr_t)ss;
1139 /* setup ipsec xform */
1140 prm->ipsec_xform.spi = ss->spi;
1141 prm->ipsec_xform.salt = ss->salt;
1142 prm->ipsec_xform.direction = ss->direction;
1143 prm->ipsec_xform.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP;
1144 prm->ipsec_xform.mode = (IS_TRANSPORT(ss->flags)) ?
1145 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT :
1146 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
1147 prm->ipsec_xform.options.ecn = 1;
1148 prm->ipsec_xform.options.copy_dscp = 1;
1150 if (IS_IP4_TUNNEL(ss->flags)) {
1151 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV4;
1152 prm->tun.hdr_len = sizeof(*v4);
1153 prm->tun.next_proto = rc;
1155 } else if (IS_IP6_TUNNEL(ss->flags)) {
1156 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV6;
1157 prm->tun.hdr_len = sizeof(*v6);
1158 prm->tun.next_proto = rc;
1161 /* transport mode */
1162 prm->trs.proto = rc;
1165 /* setup crypto section */
1166 prm->crypto_xform = ss->xforms;
1171 fill_ipsec_session(struct rte_ipsec_session *ss, struct rte_ipsec_sa *sa)
1177 if (ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1178 ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
1179 if (ss->security.ses != NULL) {
1180 rc = rte_ipsec_session_prepare(ss);
1182 memset(ss, 0, sizeof(*ss));
1190 * Initialise related rte_ipsec_sa object.
1193 ipsec_sa_init(struct ipsec_sa *lsa, struct rte_ipsec_sa *sa, uint32_t sa_size)
1196 struct rte_ipsec_sa_prm prm;
1197 struct rte_ipsec_session *ips;
1198 struct rte_ipv4_hdr v4 = {
1199 .version_ihl = IPVERSION << 4 |
1200 sizeof(v4) / RTE_IPV4_IHL_MULTIPLIER,
1201 .time_to_live = IPDEFTTL,
1202 .next_proto_id = IPPROTO_ESP,
1203 .src_addr = lsa->src.ip.ip4,
1204 .dst_addr = lsa->dst.ip.ip4,
1206 struct rte_ipv6_hdr v6 = {
1207 .vtc_flow = htonl(IP6_VERSION << 28),
1208 .proto = IPPROTO_ESP,
1211 if (IS_IP6_TUNNEL(lsa->flags)) {
1212 memcpy(v6.src_addr, lsa->src.ip.ip6.ip6_b, sizeof(v6.src_addr));
1213 memcpy(v6.dst_addr, lsa->dst.ip.ip6.ip6_b, sizeof(v6.dst_addr));
1216 rc = fill_ipsec_sa_prm(&prm, lsa, &v4, &v6);
1218 rc = rte_ipsec_sa_init(sa, &prm, sa_size);
1222 /* init primary processing session */
1223 ips = ipsec_get_primary_session(lsa);
1224 rc = fill_ipsec_session(ips, sa);
1228 /* init inline fallback processing session */
1229 if (lsa->fallback_sessions == 1)
1230 rc = fill_ipsec_session(ipsec_get_fallback_session(lsa), sa);
1236 * Allocate space and init rte_ipsec_sa strcutures,
1240 ipsec_satbl_init(struct sa_ctx *ctx, const struct ipsec_sa *ent,
1241 uint32_t nb_ent, int32_t socket)
1246 struct rte_ipsec_sa *sa;
1247 struct ipsec_sa *lsa;
1248 struct rte_ipsec_sa_prm prm;
1250 /* determine SA size */
1251 idx = SPI2IDX(ent[0].spi);
1252 fill_ipsec_sa_prm(&prm, ctx->sa + idx, NULL, NULL);
1253 sz = rte_ipsec_sa_size(&prm);
1255 RTE_LOG(ERR, IPSEC, "%s(%p, %u, %d): "
1256 "failed to determine SA size, error code: %d\n",
1257 __func__, ctx, nb_ent, socket, sz);
1263 ctx->satbl = rte_zmalloc_socket(NULL, tsz, RTE_CACHE_LINE_SIZE, socket);
1264 if (ctx->satbl == NULL) {
1266 "%s(%p, %u, %d): failed to allocate %zu bytes\n",
1267 __func__, ctx, nb_ent, socket, tsz);
1272 for (i = 0; i != nb_ent && rc == 0; i++) {
1274 idx = SPI2IDX(ent[i].spi);
1276 sa = (struct rte_ipsec_sa *)((uintptr_t)ctx->satbl + sz * i);
1277 lsa = ctx->sa + idx;
1279 rc = ipsec_sa_init(lsa, sa, sz);
1286 * Walk through all SA rules to find an SA with given SPI
1289 sa_spi_present(uint32_t spi, int inbound)
1292 const struct ipsec_sa *sar;
1302 for (i = 0; i != num; i++) {
1303 if (sar[i].spi == spi)
1311 sa_init(struct socket_ctx *ctx, int32_t socket_id)
1317 rte_exit(EXIT_FAILURE, "NULL context.\n");
1319 if (ctx->sa_in != NULL)
1320 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
1321 "initialized\n", socket_id);
1323 if (ctx->sa_out != NULL)
1324 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
1325 "initialized\n", socket_id);
1329 ctx->sa_in = sa_create(name, socket_id);
1330 if (ctx->sa_in == NULL)
1331 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1332 "context %s in socket %d\n", rte_errno,
1335 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in, ctx);
1337 if (app_sa_prm.enable != 0) {
1338 rc = ipsec_satbl_init(ctx->sa_in, sa_in, nb_sa_in,
1341 rte_exit(EXIT_FAILURE,
1342 "failed to init inbound SAs\n");
1345 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
1347 if (nb_sa_out > 0) {
1349 ctx->sa_out = sa_create(name, socket_id);
1350 if (ctx->sa_out == NULL)
1351 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1352 "context %s in socket %d\n", rte_errno,
1355 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out, ctx);
1357 if (app_sa_prm.enable != 0) {
1358 rc = ipsec_satbl_init(ctx->sa_out, sa_out, nb_sa_out,
1361 rte_exit(EXIT_FAILURE,
1362 "failed to init outbound SAs\n");
1365 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
1370 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
1372 struct ipsec_mbuf_metadata *priv;
1373 struct ipsec_sa *sa;
1378 return (sa_ctx->sa[sa_idx].spi == sa->spi);
1380 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
1385 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
1388 struct rte_esp_hdr *esp;
1390 uint32_t *src4_addr;
1392 struct ipsec_sa *sa;
1397 ip = rte_pktmbuf_mtod(pkt, struct ip *);
1398 esp = rte_pktmbuf_mtod_offset(pkt, struct rte_esp_hdr *, pkt->l3_len);
1400 if (esp->spi == INVALID_SPI)
1403 result_sa = sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
1404 if (rte_be_to_cpu_32(esp->spi) != sa->spi)
1408 * Mark need for inline offload fallback on the LSB of SA pointer.
1409 * Thanks to packet grouping mechanism which ipsec_process is using
1410 * packets marked for fallback processing will form separate group.
1412 * Because it is not safe to use SA pointer it is casted to generic
1413 * pointer to prevent from unintentional use. Use ipsec_mask_saptr
1414 * to get valid struct pointer.
1416 if (MBUF_NO_SEC_OFFLOAD(pkt) && sa->fallback_sessions > 0) {
1417 uintptr_t intsa = (uintptr_t)sa;
1418 intsa |= IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
1419 result_sa = (void *)intsa;
1422 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1424 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
1425 if ((ip->ip_v == IPVERSION) &&
1426 (sa->src.ip.ip4 == *src4_addr) &&
1427 (sa->dst.ip.ip4 == *(src4_addr + 1)))
1428 *sa_ret = result_sa;
1431 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
1432 if ((ip->ip_v == IP6_VERSION) &&
1433 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
1434 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
1435 *sa_ret = result_sa;
1438 *sa_ret = result_sa;
1443 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
1444 void *sa[], uint16_t nb_pkts)
1448 for (i = 0; i < nb_pkts; i++)
1449 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
1453 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
1454 void *sa[], uint16_t nb_pkts)
1458 for (i = 0; i < nb_pkts; i++)
1459 sa[i] = &sa_ctx->sa[sa_idx[i]];
1463 * Select HW offloads to be used.
1466 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
1467 uint64_t *tx_offloads)
1469 struct ipsec_sa *rule;
1471 enum rte_security_session_action_type rule_type;
1476 /* Check for inbound rules that use offloads and use this port */
1477 for (idx_sa = 0; idx_sa < nb_sa_in; idx_sa++) {
1478 rule = &sa_in[idx_sa];
1479 rule_type = ipsec_get_action_type(rule);
1480 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1482 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1483 && rule->portid == port_id)
1484 *rx_offloads |= DEV_RX_OFFLOAD_SECURITY;
1487 /* Check for outbound rules that use offloads and use this port */
1488 for (idx_sa = 0; idx_sa < nb_sa_out; idx_sa++) {
1489 rule = &sa_out[idx_sa];
1490 rule_type = ipsec_get_action_type(rule);
1491 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1493 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1494 && rule->portid == port_id)
1495 *tx_offloads |= DEV_TX_OFFLOAD_SECURITY;