1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2020 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>
31 #define IP4_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip4) * CHAR_BIT)
33 #define IP6_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip6.ip6) * CHAR_BIT)
35 #define MBUF_NO_SEC_OFFLOAD(m) ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)
37 struct supported_cipher_algo {
39 enum rte_crypto_cipher_algorithm algo;
45 struct supported_auth_algo {
47 enum rte_crypto_auth_algorithm algo;
53 struct supported_aead_algo {
55 enum rte_crypto_aead_algorithm algo;
64 const struct supported_cipher_algo cipher_algos[] = {
67 .algo = RTE_CRYPTO_CIPHER_NULL,
73 .keyword = "aes-128-cbc",
74 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
80 .keyword = "aes-256-cbc",
81 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
87 .keyword = "aes-128-ctr",
88 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
94 .keyword = "3des-cbc",
95 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
102 const struct supported_auth_algo auth_algos[] = {
105 .algo = RTE_CRYPTO_AUTH_NULL,
111 .keyword = "sha1-hmac",
112 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
117 .keyword = "sha256-hmac",
118 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
124 const struct supported_aead_algo aead_algos[] = {
126 .keyword = "aes-128-gcm",
127 .algo = RTE_CRYPTO_AEAD_AES_GCM,
136 #define SA_INIT_NB 128
138 struct ipsec_sa *sa_out;
140 static uint32_t sa_out_sz;
141 static struct ipsec_sa_cnt sa_out_cnt;
143 struct ipsec_sa *sa_in;
145 static uint32_t sa_in_sz;
146 static struct ipsec_sa_cnt sa_in_cnt;
148 static const struct supported_cipher_algo *
149 find_match_cipher_algo(const char *cipher_keyword)
153 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
154 const struct supported_cipher_algo *algo =
157 if (strcmp(cipher_keyword, algo->keyword) == 0)
164 static const struct supported_auth_algo *
165 find_match_auth_algo(const char *auth_keyword)
169 for (i = 0; i < RTE_DIM(auth_algos); i++) {
170 const struct supported_auth_algo *algo =
173 if (strcmp(auth_keyword, algo->keyword) == 0)
180 static const struct supported_aead_algo *
181 find_match_aead_algo(const char *aead_keyword)
185 for (i = 0; i < RTE_DIM(aead_algos); i++) {
186 const struct supported_aead_algo *algo =
189 if (strcmp(aead_keyword, algo->keyword) == 0)
197 * parse x:x:x:x.... hex number key string into uint8_t *key
199 * > 0: number of bytes parsed
203 parse_key_string(const char *key_str, uint8_t *key)
205 const char *pt_start = key_str, *pt_end = key_str;
206 uint32_t nb_bytes = 0;
208 while (pt_end != NULL) {
209 char sub_str[3] = {0};
211 pt_end = strchr(pt_start, ':');
213 if (pt_end == NULL) {
214 if (strlen(pt_start) > 2)
216 strncpy(sub_str, pt_start, 2);
218 if (pt_end - pt_start > 2)
221 strncpy(sub_str, pt_start, pt_end - pt_start);
222 pt_start = pt_end + 1;
225 key[nb_bytes++] = strtol(sub_str, NULL, 16);
232 extend_sa_arr(struct ipsec_sa **sa_tbl, uint32_t cur_cnt, uint32_t *cur_sz)
234 if (*sa_tbl == NULL) {
235 *sa_tbl = calloc(SA_INIT_NB, sizeof(struct ipsec_sa));
238 *cur_sz = SA_INIT_NB;
242 if (cur_cnt >= *cur_sz) {
243 *sa_tbl = realloc(*sa_tbl,
244 *cur_sz * sizeof(struct ipsec_sa) * 2);
247 /* clean reallocated extra space */
248 memset(&(*sa_tbl)[*cur_sz], 0,
249 *cur_sz * sizeof(struct ipsec_sa));
257 parse_sa_tokens(char **tokens, uint32_t n_tokens,
258 struct parse_status *status)
260 struct ipsec_sa *rule = NULL;
261 struct rte_ipsec_session *ips;
262 uint32_t ti; /*token index*/
263 uint32_t *ri /*rule index*/;
264 struct ipsec_sa_cnt *sa_cnt;
265 uint32_t cipher_algo_p = 0;
266 uint32_t auth_algo_p = 0;
267 uint32_t aead_algo_p = 0;
272 uint32_t portid_p = 0;
273 uint32_t fallback_p = 0;
275 if (strcmp(tokens[0], "in") == 0) {
278 if (extend_sa_arr(&sa_in, nb_sa_in, &sa_in_sz) < 0)
281 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
284 sa_cnt = &sa_out_cnt;
285 if (extend_sa_arr(&sa_out, nb_sa_out, &sa_out_sz) < 0)
288 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
292 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
293 if (status->status < 0)
295 if (atoi(tokens[1]) == INVALID_SPI)
297 rule->spi = atoi(tokens[1]);
298 ips = ipsec_get_primary_session(rule);
300 for (ti = 2; ti < n_tokens; ti++) {
301 if (strcmp(tokens[ti], "mode") == 0) {
302 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
303 if (status->status < 0)
306 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
307 if (status->status < 0)
310 if (strcmp(tokens[ti], "ipv4-tunnel") == 0) {
312 rule->flags = IP4_TUNNEL;
313 } else if (strcmp(tokens[ti], "ipv6-tunnel") == 0) {
315 rule->flags = IP6_TUNNEL;
316 } else if (strcmp(tokens[ti], "transport") == 0) {
319 rule->flags = TRANSPORT;
321 APP_CHECK(0, status, "unrecognized "
322 "input \"%s\"", tokens[ti]);
330 if (strcmp(tokens[ti], "cipher_algo") == 0) {
331 const struct supported_cipher_algo *algo;
334 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
336 if (status->status < 0)
339 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
340 if (status->status < 0)
343 algo = find_match_cipher_algo(tokens[ti]);
345 APP_CHECK(algo != NULL, status, "unrecognized "
346 "input \"%s\"", tokens[ti]);
348 if (status->status < 0)
351 rule->cipher_algo = algo->algo;
352 rule->block_size = algo->block_size;
353 rule->iv_len = algo->iv_len;
354 rule->cipher_key_len = algo->key_len;
356 /* for NULL algorithm, no cipher key required */
357 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
362 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
363 if (status->status < 0)
366 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
367 status, "unrecognized input \"%s\", "
368 "expect \"cipher_key\"", tokens[ti]);
369 if (status->status < 0)
372 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
373 if (status->status < 0)
376 key_len = parse_key_string(tokens[ti],
378 APP_CHECK(key_len == rule->cipher_key_len, status,
379 "unrecognized input \"%s\"", tokens[ti]);
380 if (status->status < 0)
383 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC ||
384 algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC)
385 rule->salt = (uint32_t)rte_rand();
387 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
389 rule->cipher_key_len = key_len;
391 &rule->cipher_key[key_len], 4);
398 if (strcmp(tokens[ti], "auth_algo") == 0) {
399 const struct supported_auth_algo *algo;
402 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
404 if (status->status < 0)
407 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
408 if (status->status < 0)
411 algo = find_match_auth_algo(tokens[ti]);
412 APP_CHECK(algo != NULL, status, "unrecognized "
413 "input \"%s\"", tokens[ti]);
415 if (status->status < 0)
418 rule->auth_algo = algo->algo;
419 rule->auth_key_len = algo->key_len;
420 rule->digest_len = algo->digest_len;
422 /* NULL algorithm and combined algos do not
425 if (algo->key_not_req) {
430 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
431 if (status->status < 0)
434 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
435 status, "unrecognized input \"%s\", "
436 "expect \"auth_key\"", tokens[ti]);
437 if (status->status < 0)
440 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
441 if (status->status < 0)
444 key_len = parse_key_string(tokens[ti],
446 APP_CHECK(key_len == rule->auth_key_len, status,
447 "unrecognized input \"%s\"", tokens[ti]);
448 if (status->status < 0)
455 if (strcmp(tokens[ti], "aead_algo") == 0) {
456 const struct supported_aead_algo *algo;
459 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
461 if (status->status < 0)
464 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
465 if (status->status < 0)
468 algo = find_match_aead_algo(tokens[ti]);
470 APP_CHECK(algo != NULL, status, "unrecognized "
471 "input \"%s\"", tokens[ti]);
473 if (status->status < 0)
476 rule->aead_algo = algo->algo;
477 rule->cipher_key_len = algo->key_len;
478 rule->digest_len = algo->digest_len;
479 rule->aad_len = algo->aad_len;
480 rule->block_size = algo->block_size;
481 rule->iv_len = algo->iv_len;
483 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
484 if (status->status < 0)
487 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
488 status, "unrecognized input \"%s\", "
489 "expect \"aead_key\"", tokens[ti]);
490 if (status->status < 0)
493 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
494 if (status->status < 0)
497 key_len = parse_key_string(tokens[ti],
499 APP_CHECK(key_len == rule->cipher_key_len, status,
500 "unrecognized input \"%s\"", tokens[ti]);
501 if (status->status < 0)
505 rule->cipher_key_len = key_len;
507 &rule->cipher_key[key_len], 4);
513 if (strcmp(tokens[ti], "src") == 0) {
514 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
515 if (status->status < 0)
518 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
519 if (status->status < 0)
522 if (IS_IP4_TUNNEL(rule->flags)) {
525 APP_CHECK(parse_ipv4_addr(tokens[ti],
526 &ip, NULL) == 0, status,
527 "unrecognized input \"%s\", "
528 "expect valid ipv4 addr",
530 if (status->status < 0)
532 rule->src.ip.ip4 = rte_bswap32(
533 (uint32_t)ip.s_addr);
534 } else if (IS_IP6_TUNNEL(rule->flags)) {
537 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
539 "unrecognized input \"%s\", "
540 "expect valid ipv6 addr",
542 if (status->status < 0)
544 memcpy(rule->src.ip.ip6.ip6_b,
546 } else if (IS_TRANSPORT(rule->flags)) {
547 APP_CHECK(0, status, "unrecognized input "
548 "\"%s\"", tokens[ti]);
556 if (strcmp(tokens[ti], "dst") == 0) {
557 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
558 if (status->status < 0)
561 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
562 if (status->status < 0)
565 if (IS_IP4_TUNNEL(rule->flags)) {
568 APP_CHECK(parse_ipv4_addr(tokens[ti],
569 &ip, NULL) == 0, status,
570 "unrecognized input \"%s\", "
571 "expect valid ipv4 addr",
573 if (status->status < 0)
575 rule->dst.ip.ip4 = rte_bswap32(
576 (uint32_t)ip.s_addr);
577 } else if (IS_IP6_TUNNEL(rule->flags)) {
580 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
582 "unrecognized input \"%s\", "
583 "expect valid ipv6 addr",
585 if (status->status < 0)
587 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
588 } else if (IS_TRANSPORT(rule->flags)) {
589 APP_CHECK(0, status, "unrecognized "
590 "input \"%s\"", tokens[ti]);
598 if (strcmp(tokens[ti], "type") == 0) {
599 APP_CHECK_PRESENCE(type_p, tokens[ti], status);
600 if (status->status < 0)
603 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
604 if (status->status < 0)
607 if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
609 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
610 else if (strcmp(tokens[ti],
611 "inline-protocol-offload") == 0)
613 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
614 else if (strcmp(tokens[ti],
615 "lookaside-protocol-offload") == 0)
617 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
618 else if (strcmp(tokens[ti], "no-offload") == 0)
619 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
620 else if (strcmp(tokens[ti], "cpu-crypto") == 0)
621 ips->type = RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO;
623 APP_CHECK(0, status, "Invalid input \"%s\"",
632 if (strcmp(tokens[ti], "port_id") == 0) {
633 APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
634 if (status->status < 0)
636 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
637 if (status->status < 0)
639 rule->portid = atoi(tokens[ti]);
640 if (status->status < 0)
646 if (strcmp(tokens[ti], "fallback") == 0) {
647 struct rte_ipsec_session *fb;
649 APP_CHECK(app_sa_prm.enable, status, "Fallback session "
650 "not allowed for legacy mode.");
651 if (status->status < 0)
653 APP_CHECK(ips->type ==
654 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, status,
655 "Fallback session allowed if primary session "
656 "is of type inline-crypto-offload only.");
657 if (status->status < 0)
659 APP_CHECK(rule->direction ==
660 RTE_SECURITY_IPSEC_SA_DIR_INGRESS, status,
661 "Fallback session not allowed for egress "
663 if (status->status < 0)
665 APP_CHECK_PRESENCE(fallback_p, tokens[ti], status);
666 if (status->status < 0)
668 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
669 if (status->status < 0)
671 fb = ipsec_get_fallback_session(rule);
672 if (strcmp(tokens[ti], "lookaside-none") == 0)
673 fb->type = RTE_SECURITY_ACTION_TYPE_NONE;
674 else if (strcmp(tokens[ti], "cpu-crypto") == 0)
675 fb->type = RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO;
677 APP_CHECK(0, status, "unrecognized fallback "
678 "type %s.", tokens[ti]);
682 rule->fallback_sessions = 1;
687 /* unrecognizeable input */
688 APP_CHECK(0, status, "unrecognized input \"%s\"",
694 APP_CHECK(cipher_algo_p == 0, status,
695 "AEAD used, no need for cipher options");
696 if (status->status < 0)
699 APP_CHECK(auth_algo_p == 0, status,
700 "AEAD used, no need for auth options");
701 if (status->status < 0)
704 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
705 if (status->status < 0)
708 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
709 if (status->status < 0)
713 APP_CHECK(mode_p == 1, status, "missing mode option");
714 if (status->status < 0)
717 if ((ips->type != RTE_SECURITY_ACTION_TYPE_NONE && ips->type !=
718 RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) && (portid_p == 0))
719 printf("Missing portid option, falling back to non-offload\n");
721 if (!type_p || (!portid_p && ips->type !=
722 RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO)) {
723 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
731 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
735 const struct rte_ipsec_session *ips;
736 const struct rte_ipsec_session *fallback_ips;
738 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
740 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
741 if (cipher_algos[i].algo == sa->cipher_algo &&
742 cipher_algos[i].key_len == sa->cipher_key_len) {
743 printf("%s ", cipher_algos[i].keyword);
748 for (i = 0; i < RTE_DIM(auth_algos); i++) {
749 if (auth_algos[i].algo == sa->auth_algo) {
750 printf("%s ", auth_algos[i].keyword);
755 for (i = 0; i < RTE_DIM(aead_algos); i++) {
756 if (aead_algos[i].algo == sa->aead_algo) {
757 printf("%s ", aead_algos[i].keyword);
764 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
766 printf("IP4Tunnel ");
767 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
768 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
769 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
770 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
773 printf("IP6Tunnel ");
774 for (i = 0; i < 16; i++) {
775 if (i % 2 && i != 15)
776 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
778 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
781 for (i = 0; i < 16; i++) {
782 if (i % 2 && i != 15)
783 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
785 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
789 printf("Transport ");
793 ips = &sa->sessions[IPSEC_SESSION_PRIMARY];
796 case RTE_SECURITY_ACTION_TYPE_NONE:
797 printf("no-offload ");
799 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
800 printf("inline-crypto-offload ");
802 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
803 printf("inline-protocol-offload ");
805 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
806 printf("lookaside-protocol-offload ");
808 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
809 printf("cpu-crypto-accelerated");
813 fallback_ips = &sa->sessions[IPSEC_SESSION_FALLBACK];
814 if (fallback_ips != NULL && sa->fallback_sessions > 0) {
815 printf("inline fallback: ");
816 switch (fallback_ips->type) {
817 case RTE_SECURITY_ACTION_TYPE_NONE:
818 printf("lookaside-none");
820 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
821 printf("cpu-crypto-accelerated");
831 static struct sa_ctx *
832 sa_create(const char *name, int32_t socket_id, uint32_t nb_sa)
835 struct sa_ctx *sa_ctx;
837 const struct rte_memzone *mz;
839 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
841 /* Create SA context */
842 printf("Creating SA context with %u maximum entries on socket %d\n",
845 mz_size = sizeof(struct ipsec_xf) * nb_sa;
846 mz = rte_memzone_reserve(s, mz_size, socket_id,
847 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
849 printf("Failed to allocate SA XFORM memory\n");
854 sa_ctx = rte_zmalloc(NULL, sizeof(struct sa_ctx) +
855 sizeof(struct ipsec_sa) * nb_sa, RTE_CACHE_LINE_SIZE);
857 if (sa_ctx == NULL) {
858 printf("Failed to allocate SA CTX memory\n");
860 rte_memzone_free(mz);
864 sa_ctx->xf = (struct ipsec_xf *)mz->addr;
865 sa_ctx->nb_sa = nb_sa;
871 check_eth_dev_caps(uint16_t portid, uint32_t inbound)
873 struct rte_eth_dev_info dev_info;
876 retval = rte_eth_dev_info_get(portid, &dev_info);
879 "Error during getting device (port %u) info: %s\n",
880 portid, strerror(-retval));
886 if ((dev_info.rx_offload_capa &
887 DEV_RX_OFFLOAD_SECURITY) == 0) {
888 RTE_LOG(WARNING, PORT,
889 "hardware RX IPSec offload is not supported\n");
893 } else { /* outbound */
894 if ((dev_info.tx_offload_capa &
895 DEV_TX_OFFLOAD_SECURITY) == 0) {
896 RTE_LOG(WARNING, PORT,
897 "hardware TX IPSec offload is not supported\n");
905 * Helper function, tries to determine next_proto for SPI
906 * by searching though SP rules.
909 get_spi_proto(uint32_t spi, enum rte_security_ipsec_sa_direction dir,
910 struct ip_addr ip_addr[2], uint32_t mask[2])
914 rc4 = sp4_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
916 rc6 = sp6_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
922 "%s: SPI %u used simultaeously by "
923 "IPv4(%d) and IPv6 (%d) SP rules\n",
924 __func__, spi, rc4, rc6);
928 } else if (rc6 < 0) {
930 "%s: SPI %u is not used by any SP rule\n",
938 * Helper function for getting source and destination IP addresses
939 * from SP. Needed for inline crypto transport mode, as addresses are not
940 * provided in config file for that mode. It checks if SP for current SA exists,
941 * and based on what type of protocol is returned, it stores appropriate
942 * addresses got from SP into SA.
945 sa_add_address_inline_crypto(struct ipsec_sa *sa)
948 struct ip_addr ip_addr[2];
951 protocol = get_spi_proto(sa->spi, sa->direction, ip_addr, mask);
954 else if (protocol == IPPROTO_IPIP) {
955 sa->flags |= IP4_TRANSPORT;
956 if (mask[0] == IP4_FULL_MASK &&
957 mask[1] == IP4_FULL_MASK &&
958 ip_addr[0].ip.ip4 != 0 &&
959 ip_addr[1].ip.ip4 != 0) {
961 sa->src.ip.ip4 = ip_addr[0].ip.ip4;
962 sa->dst.ip.ip4 = ip_addr[1].ip.ip4;
965 "%s: No valid address or mask entry in"
966 " IPv4 SP rule for SPI %u\n",
970 } else if (protocol == IPPROTO_IPV6) {
971 sa->flags |= IP6_TRANSPORT;
972 if (mask[0] == IP6_FULL_MASK &&
973 mask[1] == IP6_FULL_MASK &&
974 (ip_addr[0].ip.ip6.ip6[0] != 0 ||
975 ip_addr[0].ip.ip6.ip6[1] != 0) &&
976 (ip_addr[1].ip.ip6.ip6[0] != 0 ||
977 ip_addr[1].ip.ip6.ip6[1] != 0)) {
979 sa->src.ip.ip6 = ip_addr[0].ip.ip6;
980 sa->dst.ip.ip6 = ip_addr[1].ip.ip6;
983 "%s: No valid address or mask entry in"
984 " IPv6 SP rule for SPI %u\n",
993 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
994 uint32_t nb_entries, uint32_t inbound,
995 struct socket_ctx *skt_ctx)
999 uint16_t iv_length, aad_length;
1002 struct rte_ipsec_session *ips;
1004 /* for ESN upper 32 bits of SQN also need to be part of AAD */
1005 aad_length = (app_sa_prm.enable_esn != 0) ? sizeof(uint32_t) : 0;
1007 for (i = 0; i < nb_entries; i++) {
1009 sa = &sa_ctx->sa[idx];
1011 printf("Index %u already in use by SPI %u\n",
1018 rc = ipsec_sad_add(&sa_ctx->sad, sa);
1024 ips = ipsec_get_primary_session(sa);
1026 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1027 ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1028 if (check_eth_dev_caps(sa->portid, inbound))
1032 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1034 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
1035 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
1039 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1041 sa_add_address_inline_crypto(sa);
1042 if (inline_status < 0)
1043 return inline_status;
1048 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
1051 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1052 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
1053 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
1054 sa_ctx->xf[idx].a.aead.key.length =
1056 sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
1057 RTE_CRYPTO_AEAD_OP_DECRYPT :
1058 RTE_CRYPTO_AEAD_OP_ENCRYPT;
1059 sa_ctx->xf[idx].a.next = NULL;
1060 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
1061 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
1062 sa_ctx->xf[idx].a.aead.aad_length =
1063 sa->aad_len + aad_length;
1064 sa_ctx->xf[idx].a.aead.digest_length =
1067 sa->xforms = &sa_ctx->xf[idx].a;
1069 switch (sa->cipher_algo) {
1070 case RTE_CRYPTO_CIPHER_NULL:
1071 case RTE_CRYPTO_CIPHER_3DES_CBC:
1072 case RTE_CRYPTO_CIPHER_AES_CBC:
1073 iv_length = sa->iv_len;
1075 case RTE_CRYPTO_CIPHER_AES_CTR:
1079 RTE_LOG(ERR, IPSEC_ESP,
1080 "unsupported cipher algorithm %u\n",
1086 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1087 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
1088 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
1089 sa_ctx->xf[idx].b.cipher.key.length =
1091 sa_ctx->xf[idx].b.cipher.op =
1092 RTE_CRYPTO_CIPHER_OP_DECRYPT;
1093 sa_ctx->xf[idx].b.next = NULL;
1094 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
1095 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
1097 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1098 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
1099 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
1100 sa_ctx->xf[idx].a.auth.key.length =
1102 sa_ctx->xf[idx].a.auth.digest_length =
1104 sa_ctx->xf[idx].a.auth.op =
1105 RTE_CRYPTO_AUTH_OP_VERIFY;
1106 } else { /* outbound */
1107 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1108 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
1109 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
1110 sa_ctx->xf[idx].a.cipher.key.length =
1112 sa_ctx->xf[idx].a.cipher.op =
1113 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1114 sa_ctx->xf[idx].a.next = NULL;
1115 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
1116 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
1118 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1119 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
1120 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
1121 sa_ctx->xf[idx].b.auth.key.length =
1123 sa_ctx->xf[idx].b.auth.digest_length =
1125 sa_ctx->xf[idx].b.auth.op =
1126 RTE_CRYPTO_AUTH_OP_GENERATE;
1129 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
1130 sa_ctx->xf[idx].b.next = NULL;
1131 sa->xforms = &sa_ctx->xf[idx].a;
1135 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1137 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1138 rc = create_inline_session(skt_ctx, sa, ips);
1140 RTE_LOG(ERR, IPSEC_ESP,
1141 "create_inline_session() failed\n");
1146 print_one_sa_rule(sa, inbound);
1153 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1154 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1156 return sa_add_rules(sa_ctx, entries, nb_entries, 0, skt_ctx);
1160 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1161 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1163 return sa_add_rules(sa_ctx, entries, nb_entries, 1, skt_ctx);
1167 * helper function, fills parameters that are identical for all SAs
1170 fill_ipsec_app_sa_prm(struct rte_ipsec_sa_prm *prm,
1171 const struct app_sa_prm *app_prm)
1173 memset(prm, 0, sizeof(*prm));
1175 prm->flags = app_prm->flags;
1176 prm->ipsec_xform.options.esn = app_prm->enable_esn;
1177 prm->ipsec_xform.replay_win_sz = app_prm->window_size;
1181 fill_ipsec_sa_prm(struct rte_ipsec_sa_prm *prm, const struct ipsec_sa *ss,
1182 const struct rte_ipv4_hdr *v4, struct rte_ipv6_hdr *v6)
1187 * Try to get SPI next proto by searching that SPI in SPD.
1188 * probably not the optimal way, but there seems nothing
1191 rc = get_spi_proto(ss->spi, ss->direction, NULL, NULL);
1195 fill_ipsec_app_sa_prm(prm, &app_sa_prm);
1196 prm->userdata = (uintptr_t)ss;
1198 /* setup ipsec xform */
1199 prm->ipsec_xform.spi = ss->spi;
1200 prm->ipsec_xform.salt = ss->salt;
1201 prm->ipsec_xform.direction = ss->direction;
1202 prm->ipsec_xform.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP;
1203 prm->ipsec_xform.mode = (IS_TRANSPORT(ss->flags)) ?
1204 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT :
1205 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
1206 prm->ipsec_xform.options.ecn = 1;
1207 prm->ipsec_xform.options.copy_dscp = 1;
1209 if (IS_IP4_TUNNEL(ss->flags)) {
1210 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV4;
1211 prm->tun.hdr_len = sizeof(*v4);
1212 prm->tun.next_proto = rc;
1214 } else if (IS_IP6_TUNNEL(ss->flags)) {
1215 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV6;
1216 prm->tun.hdr_len = sizeof(*v6);
1217 prm->tun.next_proto = rc;
1220 /* transport mode */
1221 prm->trs.proto = rc;
1224 /* setup crypto section */
1225 prm->crypto_xform = ss->xforms;
1230 fill_ipsec_session(struct rte_ipsec_session *ss, struct rte_ipsec_sa *sa)
1236 if (ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1237 ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
1238 if (ss->security.ses != NULL) {
1239 rc = rte_ipsec_session_prepare(ss);
1241 memset(ss, 0, sizeof(*ss));
1249 * Initialise related rte_ipsec_sa object.
1252 ipsec_sa_init(struct ipsec_sa *lsa, struct rte_ipsec_sa *sa, uint32_t sa_size)
1255 struct rte_ipsec_sa_prm prm;
1256 struct rte_ipsec_session *ips;
1257 struct rte_ipv4_hdr v4 = {
1258 .version_ihl = IPVERSION << 4 |
1259 sizeof(v4) / RTE_IPV4_IHL_MULTIPLIER,
1260 .time_to_live = IPDEFTTL,
1261 .next_proto_id = IPPROTO_ESP,
1262 .src_addr = lsa->src.ip.ip4,
1263 .dst_addr = lsa->dst.ip.ip4,
1265 struct rte_ipv6_hdr v6 = {
1266 .vtc_flow = htonl(IP6_VERSION << 28),
1267 .proto = IPPROTO_ESP,
1270 if (IS_IP6_TUNNEL(lsa->flags)) {
1271 memcpy(v6.src_addr, lsa->src.ip.ip6.ip6_b, sizeof(v6.src_addr));
1272 memcpy(v6.dst_addr, lsa->dst.ip.ip6.ip6_b, sizeof(v6.dst_addr));
1275 rc = fill_ipsec_sa_prm(&prm, lsa, &v4, &v6);
1277 rc = rte_ipsec_sa_init(sa, &prm, sa_size);
1281 /* init primary processing session */
1282 ips = ipsec_get_primary_session(lsa);
1283 rc = fill_ipsec_session(ips, sa);
1287 /* init inline fallback processing session */
1288 if (lsa->fallback_sessions == 1)
1289 rc = fill_ipsec_session(ipsec_get_fallback_session(lsa), sa);
1295 * Allocate space and init rte_ipsec_sa strcutures,
1299 ipsec_satbl_init(struct sa_ctx *ctx, uint32_t nb_ent, int32_t socket)
1304 struct rte_ipsec_sa *sa;
1305 struct ipsec_sa *lsa;
1306 struct rte_ipsec_sa_prm prm;
1308 /* determine SA size */
1310 fill_ipsec_sa_prm(&prm, ctx->sa + idx, NULL, NULL);
1311 sz = rte_ipsec_sa_size(&prm);
1313 RTE_LOG(ERR, IPSEC, "%s(%p, %u, %d): "
1314 "failed to determine SA size, error code: %d\n",
1315 __func__, ctx, nb_ent, socket, sz);
1321 ctx->satbl = rte_zmalloc_socket(NULL, tsz, RTE_CACHE_LINE_SIZE, socket);
1322 if (ctx->satbl == NULL) {
1324 "%s(%p, %u, %d): failed to allocate %zu bytes\n",
1325 __func__, ctx, nb_ent, socket, tsz);
1330 for (i = 0; i != nb_ent && rc == 0; i++) {
1334 sa = (struct rte_ipsec_sa *)((uintptr_t)ctx->satbl + sz * i);
1335 lsa = ctx->sa + idx;
1337 rc = ipsec_sa_init(lsa, sa, sz);
1344 sa_cmp(const void *p, const void *q)
1346 uint32_t spi1 = ((const struct ipsec_sa *)p)->spi;
1347 uint32_t spi2 = ((const struct ipsec_sa *)q)->spi;
1349 return (int)(spi1 - spi2);
1353 * Walk through all SA rules to find an SA with given SPI
1356 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound)
1359 struct ipsec_sa *sa;
1360 struct ipsec_sa tmpl;
1361 const struct ipsec_sa *sar;
1371 sa = bsearch(&tmpl, sar, num, sizeof(struct ipsec_sa), sa_cmp);
1373 return RTE_PTR_DIFF(sa, sar) / sizeof(struct ipsec_sa);
1379 sa_init(struct socket_ctx *ctx, int32_t socket_id)
1385 rte_exit(EXIT_FAILURE, "NULL context.\n");
1387 if (ctx->sa_in != NULL)
1388 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
1389 "initialized\n", socket_id);
1391 if (ctx->sa_out != NULL)
1392 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
1393 "initialized\n", socket_id);
1397 ctx->sa_in = sa_create(name, socket_id, nb_sa_in);
1398 if (ctx->sa_in == NULL)
1399 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1400 "context %s in socket %d\n", rte_errno,
1403 rc = ipsec_sad_create(name, &ctx->sa_in->sad, socket_id,
1406 rte_exit(EXIT_FAILURE, "failed to init SAD\n");
1408 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in, ctx);
1410 if (app_sa_prm.enable != 0) {
1411 rc = ipsec_satbl_init(ctx->sa_in, nb_sa_in,
1414 rte_exit(EXIT_FAILURE,
1415 "failed to init inbound SAs\n");
1418 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
1420 if (nb_sa_out > 0) {
1422 ctx->sa_out = sa_create(name, socket_id, nb_sa_out);
1423 if (ctx->sa_out == NULL)
1424 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1425 "context %s in socket %d\n", rte_errno,
1428 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out, ctx);
1430 if (app_sa_prm.enable != 0) {
1431 rc = ipsec_satbl_init(ctx->sa_out, nb_sa_out,
1434 rte_exit(EXIT_FAILURE,
1435 "failed to init outbound SAs\n");
1438 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
1443 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
1445 struct ipsec_mbuf_metadata *priv;
1446 struct ipsec_sa *sa;
1451 return (sa_ctx->sa[sa_idx].spi == sa->spi);
1453 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
1458 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
1459 void *sa_arr[], uint16_t nb_pkts)
1463 struct ipsec_sa *sa;
1465 sad_lookup(&sa_ctx->sad, pkts, sa_arr, nb_pkts);
1468 * Mark need for inline offload fallback on the LSB of SA pointer.
1469 * Thanks to packet grouping mechanism which ipsec_process is using
1470 * packets marked for fallback processing will form separate group.
1472 * Because it is not safe to use SA pointer it is casted to generic
1473 * pointer to prevent from unintentional use. Use ipsec_mask_saptr
1474 * to get valid struct pointer.
1476 for (i = 0; i < nb_pkts; i++) {
1477 if (sa_arr[i] == NULL)
1480 result_sa = sa = sa_arr[i];
1481 if (MBUF_NO_SEC_OFFLOAD(pkts[i]) &&
1482 sa->fallback_sessions > 0) {
1483 uintptr_t intsa = (uintptr_t)sa;
1484 intsa |= IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
1485 result_sa = (void *)intsa;
1487 sa_arr[i] = result_sa;
1492 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
1493 void *sa[], uint16_t nb_pkts)
1497 for (i = 0; i < nb_pkts; i++)
1498 sa[i] = &sa_ctx->sa[sa_idx[i]];
1502 * Select HW offloads to be used.
1505 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
1506 uint64_t *tx_offloads)
1508 struct ipsec_sa *rule;
1510 enum rte_security_session_action_type rule_type;
1515 /* Check for inbound rules that use offloads and use this port */
1516 for (idx_sa = 0; idx_sa < nb_sa_in; idx_sa++) {
1517 rule = &sa_in[idx_sa];
1518 rule_type = ipsec_get_action_type(rule);
1519 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1521 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1522 && rule->portid == port_id)
1523 *rx_offloads |= DEV_RX_OFFLOAD_SECURITY;
1526 /* Check for outbound rules that use offloads and use this port */
1527 for (idx_sa = 0; idx_sa < nb_sa_out; idx_sa++) {
1528 rule = &sa_out[idx_sa];
1529 rule_type = ipsec_get_action_type(rule);
1530 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1532 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1533 && rule->portid == port_id)
1534 *tx_offloads |= DEV_TX_OFFLOAD_SECURITY;
1542 qsort(sa_in, nb_sa_in, sizeof(struct ipsec_sa), sa_cmp);
1543 qsort(sa_out, nb_sa_out, sizeof(struct ipsec_sa), sa_cmp);