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 struct supported_cipher_algo {
36 enum rte_crypto_cipher_algorithm algo;
42 struct supported_auth_algo {
44 enum rte_crypto_auth_algorithm algo;
50 struct supported_aead_algo {
52 enum rte_crypto_aead_algorithm algo;
61 const struct supported_cipher_algo cipher_algos[] = {
64 .algo = RTE_CRYPTO_CIPHER_NULL,
70 .keyword = "aes-128-cbc",
71 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
77 .keyword = "aes-256-cbc",
78 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
84 .keyword = "aes-128-ctr",
85 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
91 .keyword = "3des-cbc",
92 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
99 const struct supported_auth_algo auth_algos[] = {
102 .algo = RTE_CRYPTO_AUTH_NULL,
108 .keyword = "sha1-hmac",
109 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
114 .keyword = "sha256-hmac",
115 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
121 const struct supported_aead_algo aead_algos[] = {
123 .keyword = "aes-128-gcm",
124 .algo = RTE_CRYPTO_AEAD_AES_GCM,
133 static struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
134 static uint32_t nb_sa_out;
136 static struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
137 static uint32_t nb_sa_in;
139 static const struct supported_cipher_algo *
140 find_match_cipher_algo(const char *cipher_keyword)
144 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
145 const struct supported_cipher_algo *algo =
148 if (strcmp(cipher_keyword, algo->keyword) == 0)
155 static const struct supported_auth_algo *
156 find_match_auth_algo(const char *auth_keyword)
160 for (i = 0; i < RTE_DIM(auth_algos); i++) {
161 const struct supported_auth_algo *algo =
164 if (strcmp(auth_keyword, algo->keyword) == 0)
171 static const struct supported_aead_algo *
172 find_match_aead_algo(const char *aead_keyword)
176 for (i = 0; i < RTE_DIM(aead_algos); i++) {
177 const struct supported_aead_algo *algo =
180 if (strcmp(aead_keyword, algo->keyword) == 0)
188 * parse x:x:x:x.... hex number key string into uint8_t *key
190 * > 0: number of bytes parsed
194 parse_key_string(const char *key_str, uint8_t *key)
196 const char *pt_start = key_str, *pt_end = key_str;
197 uint32_t nb_bytes = 0;
199 while (pt_end != NULL) {
200 char sub_str[3] = {0};
202 pt_end = strchr(pt_start, ':');
204 if (pt_end == NULL) {
205 if (strlen(pt_start) > 2)
207 strncpy(sub_str, pt_start, 2);
209 if (pt_end - pt_start > 2)
212 strncpy(sub_str, pt_start, pt_end - pt_start);
213 pt_start = pt_end + 1;
216 key[nb_bytes++] = strtol(sub_str, NULL, 16);
223 parse_sa_tokens(char **tokens, uint32_t n_tokens,
224 struct parse_status *status)
226 struct ipsec_sa *rule = NULL;
227 uint32_t ti; /*token index*/
228 uint32_t *ri /*rule index*/;
229 uint32_t cipher_algo_p = 0;
230 uint32_t auth_algo_p = 0;
231 uint32_t aead_algo_p = 0;
236 uint32_t portid_p = 0;
238 if (strcmp(tokens[0], "in") == 0) {
241 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
242 "too many sa rules, abort insertion\n");
243 if (status->status < 0)
250 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
251 "too many sa rules, abort insertion\n");
252 if (status->status < 0)
259 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
260 if (status->status < 0)
262 if (atoi(tokens[1]) == INVALID_SPI)
264 rule->spi = atoi(tokens[1]);
266 for (ti = 2; ti < n_tokens; ti++) {
267 if (strcmp(tokens[ti], "mode") == 0) {
268 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
269 if (status->status < 0)
272 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
273 if (status->status < 0)
276 if (strcmp(tokens[ti], "ipv4-tunnel") == 0)
277 rule->flags = IP4_TUNNEL;
278 else if (strcmp(tokens[ti], "ipv6-tunnel") == 0)
279 rule->flags = IP6_TUNNEL;
280 else if (strcmp(tokens[ti], "transport") == 0)
281 rule->flags = TRANSPORT;
283 APP_CHECK(0, status, "unrecognized "
284 "input \"%s\"", tokens[ti]);
292 if (strcmp(tokens[ti], "cipher_algo") == 0) {
293 const struct supported_cipher_algo *algo;
296 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
298 if (status->status < 0)
301 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
302 if (status->status < 0)
305 algo = find_match_cipher_algo(tokens[ti]);
307 APP_CHECK(algo != NULL, status, "unrecognized "
308 "input \"%s\"", tokens[ti]);
310 rule->cipher_algo = algo->algo;
311 rule->block_size = algo->block_size;
312 rule->iv_len = algo->iv_len;
313 rule->cipher_key_len = algo->key_len;
315 /* for NULL algorithm, no cipher key required */
316 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
321 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
322 if (status->status < 0)
325 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
326 status, "unrecognized input \"%s\", "
327 "expect \"cipher_key\"", tokens[ti]);
328 if (status->status < 0)
331 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
332 if (status->status < 0)
335 key_len = parse_key_string(tokens[ti],
337 APP_CHECK(key_len == rule->cipher_key_len, status,
338 "unrecognized input \"%s\"", tokens[ti]);
339 if (status->status < 0)
342 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC ||
343 algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC)
344 rule->salt = (uint32_t)rte_rand();
346 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
348 rule->cipher_key_len = key_len;
350 &rule->cipher_key[key_len], 4);
357 if (strcmp(tokens[ti], "auth_algo") == 0) {
358 const struct supported_auth_algo *algo;
361 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
363 if (status->status < 0)
366 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
367 if (status->status < 0)
370 algo = find_match_auth_algo(tokens[ti]);
371 APP_CHECK(algo != NULL, status, "unrecognized "
372 "input \"%s\"", tokens[ti]);
374 rule->auth_algo = algo->algo;
375 rule->auth_key_len = algo->key_len;
376 rule->digest_len = algo->digest_len;
378 /* NULL algorithm and combined algos do not
381 if (algo->key_not_req) {
386 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
387 if (status->status < 0)
390 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
391 status, "unrecognized input \"%s\", "
392 "expect \"auth_key\"", tokens[ti]);
393 if (status->status < 0)
396 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
397 if (status->status < 0)
400 key_len = parse_key_string(tokens[ti],
402 APP_CHECK(key_len == rule->auth_key_len, status,
403 "unrecognized input \"%s\"", tokens[ti]);
404 if (status->status < 0)
411 if (strcmp(tokens[ti], "aead_algo") == 0) {
412 const struct supported_aead_algo *algo;
415 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
417 if (status->status < 0)
420 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
421 if (status->status < 0)
424 algo = find_match_aead_algo(tokens[ti]);
426 APP_CHECK(algo != NULL, status, "unrecognized "
427 "input \"%s\"", tokens[ti]);
429 rule->aead_algo = algo->algo;
430 rule->cipher_key_len = algo->key_len;
431 rule->digest_len = algo->digest_len;
432 rule->aad_len = algo->aad_len;
433 rule->block_size = algo->block_size;
434 rule->iv_len = algo->iv_len;
436 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
437 if (status->status < 0)
440 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
441 status, "unrecognized input \"%s\", "
442 "expect \"aead_key\"", tokens[ti]);
443 if (status->status < 0)
446 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
447 if (status->status < 0)
450 key_len = parse_key_string(tokens[ti],
452 APP_CHECK(key_len == rule->cipher_key_len, status,
453 "unrecognized input \"%s\"", tokens[ti]);
454 if (status->status < 0)
458 rule->cipher_key_len = key_len;
460 &rule->cipher_key[key_len], 4);
466 if (strcmp(tokens[ti], "src") == 0) {
467 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
468 if (status->status < 0)
471 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
472 if (status->status < 0)
475 if (IS_IP4_TUNNEL(rule->flags)) {
478 APP_CHECK(parse_ipv4_addr(tokens[ti],
479 &ip, NULL) == 0, status,
480 "unrecognized input \"%s\", "
481 "expect valid ipv4 addr",
483 if (status->status < 0)
485 rule->src.ip.ip4 = rte_bswap32(
486 (uint32_t)ip.s_addr);
487 } else if (IS_IP6_TUNNEL(rule->flags)) {
490 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
492 "unrecognized input \"%s\", "
493 "expect valid ipv6 addr",
495 if (status->status < 0)
497 memcpy(rule->src.ip.ip6.ip6_b,
499 } else if (IS_TRANSPORT(rule->flags)) {
500 APP_CHECK(0, status, "unrecognized input "
501 "\"%s\"", tokens[ti]);
509 if (strcmp(tokens[ti], "dst") == 0) {
510 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
511 if (status->status < 0)
514 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
515 if (status->status < 0)
518 if (IS_IP4_TUNNEL(rule->flags)) {
521 APP_CHECK(parse_ipv4_addr(tokens[ti],
522 &ip, NULL) == 0, status,
523 "unrecognized input \"%s\", "
524 "expect valid ipv4 addr",
526 if (status->status < 0)
528 rule->dst.ip.ip4 = rte_bswap32(
529 (uint32_t)ip.s_addr);
530 } else if (IS_IP6_TUNNEL(rule->flags)) {
533 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
535 "unrecognized input \"%s\", "
536 "expect valid ipv6 addr",
538 if (status->status < 0)
540 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
541 } else if (IS_TRANSPORT(rule->flags)) {
542 APP_CHECK(0, status, "unrecognized "
543 "input \"%s\"", tokens[ti]);
551 if (strcmp(tokens[ti], "type") == 0) {
552 APP_CHECK_PRESENCE(type_p, tokens[ti], status);
553 if (status->status < 0)
556 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
557 if (status->status < 0)
560 if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
562 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
563 else if (strcmp(tokens[ti],
564 "inline-protocol-offload") == 0)
566 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
567 else if (strcmp(tokens[ti],
568 "lookaside-protocol-offload") == 0)
570 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
571 else if (strcmp(tokens[ti], "no-offload") == 0)
572 rule->type = RTE_SECURITY_ACTION_TYPE_NONE;
574 APP_CHECK(0, status, "Invalid input \"%s\"",
583 if (strcmp(tokens[ti], "port_id") == 0) {
584 APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
585 if (status->status < 0)
587 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
588 if (status->status < 0)
590 rule->portid = atoi(tokens[ti]);
591 if (status->status < 0)
597 /* unrecognizeable input */
598 APP_CHECK(0, status, "unrecognized input \"%s\"",
604 APP_CHECK(cipher_algo_p == 0, status,
605 "AEAD used, no need for cipher options");
606 if (status->status < 0)
609 APP_CHECK(auth_algo_p == 0, status,
610 "AEAD used, no need for auth options");
611 if (status->status < 0)
614 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
615 if (status->status < 0)
618 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
619 if (status->status < 0)
623 APP_CHECK(mode_p == 1, status, "missing mode option");
624 if (status->status < 0)
627 if ((rule->type != RTE_SECURITY_ACTION_TYPE_NONE) && (portid_p == 0))
628 printf("Missing portid option, falling back to non-offload\n");
630 if (!type_p || !portid_p) {
631 rule->type = RTE_SECURITY_ACTION_TYPE_NONE;
639 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
644 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
646 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
647 if (cipher_algos[i].algo == sa->cipher_algo &&
648 cipher_algos[i].key_len == sa->cipher_key_len) {
649 printf("%s ", cipher_algos[i].keyword);
654 for (i = 0; i < RTE_DIM(auth_algos); i++) {
655 if (auth_algos[i].algo == sa->auth_algo) {
656 printf("%s ", auth_algos[i].keyword);
661 for (i = 0; i < RTE_DIM(aead_algos); i++) {
662 if (aead_algos[i].algo == sa->aead_algo) {
663 printf("%s ", aead_algos[i].keyword);
670 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
672 printf("IP4Tunnel ");
673 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
674 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
675 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
676 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
679 printf("IP6Tunnel ");
680 for (i = 0; i < 16; i++) {
681 if (i % 2 && i != 15)
682 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
684 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
687 for (i = 0; i < 16; i++) {
688 if (i % 2 && i != 15)
689 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
691 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
695 printf("Transport ");
700 case RTE_SECURITY_ACTION_TYPE_NONE:
701 printf("no-offload ");
703 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
704 printf("inline-crypto-offload ");
706 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
707 printf("inline-protocol-offload ");
709 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
710 printf("lookaside-protocol-offload ");
717 void *satbl; /* pointer to array of rte_ipsec_sa objects*/
718 struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
721 struct rte_crypto_sym_xform a;
722 struct rte_crypto_sym_xform b;
724 } xf[IPSEC_SA_MAX_ENTRIES];
727 static struct sa_ctx *
728 sa_create(const char *name, int32_t socket_id)
731 struct sa_ctx *sa_ctx;
733 const struct rte_memzone *mz;
735 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
737 /* Create SA array table */
738 printf("Creating SA context with %u maximum entries on socket %d\n",
739 IPSEC_SA_MAX_ENTRIES, socket_id);
741 mz_size = sizeof(struct sa_ctx);
742 mz = rte_memzone_reserve(s, mz_size, socket_id,
743 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
745 printf("Failed to allocate SA DB memory\n");
750 sa_ctx = (struct sa_ctx *)mz->addr;
756 check_eth_dev_caps(uint16_t portid, uint32_t inbound)
758 struct rte_eth_dev_info dev_info;
760 rte_eth_dev_info_get(portid, &dev_info);
763 if ((dev_info.rx_offload_capa &
764 DEV_RX_OFFLOAD_SECURITY) == 0) {
765 RTE_LOG(WARNING, PORT,
766 "hardware RX IPSec offload is not supported\n");
770 } else { /* outbound */
771 if ((dev_info.tx_offload_capa &
772 DEV_TX_OFFLOAD_SECURITY) == 0) {
773 RTE_LOG(WARNING, PORT,
774 "hardware TX IPSec offload is not supported\n");
782 * Helper function, tries to determine next_proto for SPI
783 * by searching though SP rules.
786 get_spi_proto(uint32_t spi, enum rte_security_ipsec_sa_direction dir,
787 struct ip_addr ip_addr[2], uint32_t mask[2])
791 rc4 = sp4_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
793 rc6 = sp6_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
799 "%s: SPI %u used simultaeously by "
800 "IPv4(%d) and IPv6 (%d) SP rules\n",
801 __func__, spi, rc4, rc6);
805 } else if (rc6 < 0) {
807 "%s: SPI %u is not used by any SP rule\n",
815 * Helper function for getting source and destination IP addresses
816 * from SP. Needed for inline crypto transport mode, as addresses are not
817 * provided in config file for that mode. It checks if SP for current SA exists,
818 * and based on what type of protocol is returned, it stores appropriate
819 * addresses got from SP into SA.
822 sa_add_address_inline_crypto(struct ipsec_sa *sa)
825 struct ip_addr ip_addr[2];
828 protocol = get_spi_proto(sa->spi, sa->direction, ip_addr, mask);
831 else if (protocol == IPPROTO_IPIP) {
832 sa->flags |= IP4_TRANSPORT;
833 if (mask[0] == IP4_FULL_MASK &&
834 mask[1] == IP4_FULL_MASK &&
835 ip_addr[0].ip.ip4 != 0 &&
836 ip_addr[1].ip.ip4 != 0) {
838 sa->src.ip.ip4 = ip_addr[0].ip.ip4;
839 sa->dst.ip.ip4 = ip_addr[1].ip.ip4;
842 "%s: No valid address or mask entry in"
843 " IPv4 SP rule for SPI %u\n",
847 } else if (protocol == IPPROTO_IPV6) {
848 sa->flags |= IP6_TRANSPORT;
849 if (mask[0] == IP6_FULL_MASK &&
850 mask[1] == IP6_FULL_MASK &&
851 (ip_addr[0].ip.ip6.ip6[0] != 0 ||
852 ip_addr[0].ip.ip6.ip6[1] != 0) &&
853 (ip_addr[1].ip.ip6.ip6[0] != 0 ||
854 ip_addr[1].ip.ip6.ip6[1] != 0)) {
856 sa->src.ip.ip6 = ip_addr[0].ip.ip6;
857 sa->dst.ip.ip6 = ip_addr[1].ip.ip6;
860 "%s: No valid address or mask entry in"
861 " IPv6 SP rule for SPI %u\n",
870 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
871 uint32_t nb_entries, uint32_t inbound)
875 uint16_t iv_length, aad_length;
878 /* for ESN upper 32 bits of SQN also need to be part of AAD */
879 aad_length = (app_sa_prm.enable_esn != 0) ? sizeof(uint32_t) : 0;
881 for (i = 0; i < nb_entries; i++) {
882 idx = SPI2IDX(entries[i].spi);
883 sa = &sa_ctx->sa[idx];
885 printf("Index %u already in use by SPI %u\n",
892 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
893 sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
894 if (check_eth_dev_caps(sa->portid, inbound))
898 sa->direction = (inbound == 1) ?
899 RTE_SECURITY_IPSEC_SA_DIR_INGRESS :
900 RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
902 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
904 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
905 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
909 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
911 sa_add_address_inline_crypto(sa);
912 if (inline_status < 0)
913 return inline_status;
918 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
921 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
922 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
923 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
924 sa_ctx->xf[idx].a.aead.key.length =
926 sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
927 RTE_CRYPTO_AEAD_OP_DECRYPT :
928 RTE_CRYPTO_AEAD_OP_ENCRYPT;
929 sa_ctx->xf[idx].a.next = NULL;
930 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
931 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
932 sa_ctx->xf[idx].a.aead.aad_length =
933 sa->aad_len + aad_length;
934 sa_ctx->xf[idx].a.aead.digest_length =
937 sa->xforms = &sa_ctx->xf[idx].a;
939 print_one_sa_rule(sa, inbound);
941 switch (sa->cipher_algo) {
942 case RTE_CRYPTO_CIPHER_NULL:
943 case RTE_CRYPTO_CIPHER_3DES_CBC:
944 case RTE_CRYPTO_CIPHER_AES_CBC:
945 iv_length = sa->iv_len;
947 case RTE_CRYPTO_CIPHER_AES_CTR:
951 RTE_LOG(ERR, IPSEC_ESP,
952 "unsupported cipher algorithm %u\n",
958 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
959 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
960 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
961 sa_ctx->xf[idx].b.cipher.key.length =
963 sa_ctx->xf[idx].b.cipher.op =
964 RTE_CRYPTO_CIPHER_OP_DECRYPT;
965 sa_ctx->xf[idx].b.next = NULL;
966 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
967 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
969 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
970 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
971 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
972 sa_ctx->xf[idx].a.auth.key.length =
974 sa_ctx->xf[idx].a.auth.digest_length =
976 sa_ctx->xf[idx].a.auth.op =
977 RTE_CRYPTO_AUTH_OP_VERIFY;
978 } else { /* outbound */
979 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
980 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
981 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
982 sa_ctx->xf[idx].a.cipher.key.length =
984 sa_ctx->xf[idx].a.cipher.op =
985 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
986 sa_ctx->xf[idx].a.next = NULL;
987 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
988 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
990 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
991 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
992 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
993 sa_ctx->xf[idx].b.auth.key.length =
995 sa_ctx->xf[idx].b.auth.digest_length =
997 sa_ctx->xf[idx].b.auth.op =
998 RTE_CRYPTO_AUTH_OP_GENERATE;
1001 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
1002 sa_ctx->xf[idx].b.next = NULL;
1003 sa->xforms = &sa_ctx->xf[idx].a;
1005 print_one_sa_rule(sa, inbound);
1013 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1014 uint32_t nb_entries)
1016 return sa_add_rules(sa_ctx, entries, nb_entries, 0);
1020 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1021 uint32_t nb_entries)
1023 return sa_add_rules(sa_ctx, entries, nb_entries, 1);
1027 * helper function, fills parameters that are identical for all SAs
1030 fill_ipsec_app_sa_prm(struct rte_ipsec_sa_prm *prm,
1031 const struct app_sa_prm *app_prm)
1033 memset(prm, 0, sizeof(*prm));
1035 prm->flags = app_prm->flags;
1036 prm->ipsec_xform.options.esn = app_prm->enable_esn;
1037 prm->replay_win_sz = app_prm->window_size;
1041 fill_ipsec_sa_prm(struct rte_ipsec_sa_prm *prm, const struct ipsec_sa *ss,
1042 const struct rte_ipv4_hdr *v4, struct rte_ipv6_hdr *v6)
1047 * Try to get SPI next proto by searching that SPI in SPD.
1048 * probably not the optimal way, but there seems nothing
1051 rc = get_spi_proto(ss->spi, ss->direction, NULL, NULL);
1055 fill_ipsec_app_sa_prm(prm, &app_sa_prm);
1056 prm->userdata = (uintptr_t)ss;
1058 /* setup ipsec xform */
1059 prm->ipsec_xform.spi = ss->spi;
1060 prm->ipsec_xform.salt = ss->salt;
1061 prm->ipsec_xform.direction = ss->direction;
1062 prm->ipsec_xform.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP;
1063 prm->ipsec_xform.mode = (IS_TRANSPORT(ss->flags)) ?
1064 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT :
1065 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
1066 prm->ipsec_xform.options.ecn = 1;
1067 prm->ipsec_xform.options.copy_dscp = 1;
1069 if (IS_IP4_TUNNEL(ss->flags)) {
1070 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV4;
1071 prm->tun.hdr_len = sizeof(*v4);
1072 prm->tun.next_proto = rc;
1074 } else if (IS_IP6_TUNNEL(ss->flags)) {
1075 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV6;
1076 prm->tun.hdr_len = sizeof(*v6);
1077 prm->tun.next_proto = rc;
1080 /* transport mode */
1081 prm->trs.proto = rc;
1084 /* setup crypto section */
1085 prm->crypto_xform = ss->xforms;
1090 fill_ipsec_session(struct rte_ipsec_session *ss, struct rte_ipsec_sa *sa,
1091 const struct ipsec_sa *lsa)
1094 ss->type = lsa->type;
1096 /* setup crypto section */
1097 if (ss->type == RTE_SECURITY_ACTION_TYPE_NONE) {
1098 ss->crypto.ses = lsa->crypto_session;
1099 /* setup session action type */
1101 ss->security.ses = lsa->sec_session;
1102 ss->security.ctx = lsa->security_ctx;
1103 ss->security.ol_flags = lsa->ol_flags;
1108 * Initialise related rte_ipsec_sa object.
1111 ipsec_sa_init(struct ipsec_sa *lsa, struct rte_ipsec_sa *sa, uint32_t sa_size)
1114 struct rte_ipsec_sa_prm prm;
1115 struct rte_ipv4_hdr v4 = {
1116 .version_ihl = IPVERSION << 4 |
1117 sizeof(v4) / RTE_IPV4_IHL_MULTIPLIER,
1118 .time_to_live = IPDEFTTL,
1119 .next_proto_id = IPPROTO_ESP,
1120 .src_addr = lsa->src.ip.ip4,
1121 .dst_addr = lsa->dst.ip.ip4,
1123 struct rte_ipv6_hdr v6 = {
1124 .vtc_flow = htonl(IP6_VERSION << 28),
1125 .proto = IPPROTO_ESP,
1128 if (IS_IP6_TUNNEL(lsa->flags)) {
1129 memcpy(v6.src_addr, lsa->src.ip.ip6.ip6_b, sizeof(v6.src_addr));
1130 memcpy(v6.dst_addr, lsa->dst.ip.ip6.ip6_b, sizeof(v6.dst_addr));
1133 rc = fill_ipsec_sa_prm(&prm, lsa, &v4, &v6);
1135 rc = rte_ipsec_sa_init(sa, &prm, sa_size);
1139 fill_ipsec_session(&lsa->ips, sa, lsa);
1144 * Allocate space and init rte_ipsec_sa strcutures,
1148 ipsec_satbl_init(struct sa_ctx *ctx, const struct ipsec_sa *ent,
1149 uint32_t nb_ent, int32_t socket)
1154 struct rte_ipsec_sa *sa;
1155 struct ipsec_sa *lsa;
1156 struct rte_ipsec_sa_prm prm;
1158 /* determine SA size */
1159 idx = SPI2IDX(ent[0].spi);
1160 fill_ipsec_sa_prm(&prm, ctx->sa + idx, NULL, NULL);
1161 sz = rte_ipsec_sa_size(&prm);
1163 RTE_LOG(ERR, IPSEC, "%s(%p, %u, %d): "
1164 "failed to determine SA size, error code: %d\n",
1165 __func__, ctx, nb_ent, socket, sz);
1171 ctx->satbl = rte_zmalloc_socket(NULL, tsz, RTE_CACHE_LINE_SIZE, socket);
1172 if (ctx->satbl == NULL) {
1174 "%s(%p, %u, %d): failed to allocate %zu bytes\n",
1175 __func__, ctx, nb_ent, socket, tsz);
1180 for (i = 0; i != nb_ent && rc == 0; i++) {
1182 idx = SPI2IDX(ent[i].spi);
1184 sa = (struct rte_ipsec_sa *)((uintptr_t)ctx->satbl + sz * i);
1185 lsa = ctx->sa + idx;
1187 rc = ipsec_sa_init(lsa, sa, sz);
1194 * Walk through all SA rules to find an SA with given SPI
1197 sa_spi_present(uint32_t spi, int inbound)
1200 const struct ipsec_sa *sar;
1210 for (i = 0; i != num; i++) {
1211 if (sar[i].spi == spi)
1219 sa_init(struct socket_ctx *ctx, int32_t socket_id)
1225 rte_exit(EXIT_FAILURE, "NULL context.\n");
1227 if (ctx->sa_in != NULL)
1228 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
1229 "initialized\n", socket_id);
1231 if (ctx->sa_out != NULL)
1232 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
1233 "initialized\n", socket_id);
1237 ctx->sa_in = sa_create(name, socket_id);
1238 if (ctx->sa_in == NULL)
1239 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1240 "context %s in socket %d\n", rte_errno,
1243 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
1245 if (app_sa_prm.enable != 0) {
1246 rc = ipsec_satbl_init(ctx->sa_in, sa_in, nb_sa_in,
1249 rte_exit(EXIT_FAILURE,
1250 "failed to init inbound SAs\n");
1253 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
1255 if (nb_sa_out > 0) {
1257 ctx->sa_out = sa_create(name, socket_id);
1258 if (ctx->sa_out == NULL)
1259 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1260 "context %s in socket %d\n", rte_errno,
1263 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
1265 if (app_sa_prm.enable != 0) {
1266 rc = ipsec_satbl_init(ctx->sa_out, sa_out, nb_sa_out,
1269 rte_exit(EXIT_FAILURE,
1270 "failed to init outbound SAs\n");
1273 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
1278 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
1280 struct ipsec_mbuf_metadata *priv;
1281 struct ipsec_sa *sa;
1286 return (sa_ctx->sa[sa_idx].spi == sa->spi);
1288 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
1293 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
1294 struct ipsec_sa **sa_ret)
1296 struct rte_esp_hdr *esp;
1298 uint32_t *src4_addr;
1300 struct ipsec_sa *sa;
1304 ip = rte_pktmbuf_mtod(pkt, struct ip *);
1305 esp = rte_pktmbuf_mtod_offset(pkt, struct rte_esp_hdr *, pkt->l3_len);
1307 if (esp->spi == INVALID_SPI)
1310 sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
1311 if (rte_be_to_cpu_32(esp->spi) != sa->spi)
1314 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1316 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
1317 if ((ip->ip_v == IPVERSION) &&
1318 (sa->src.ip.ip4 == *src4_addr) &&
1319 (sa->dst.ip.ip4 == *(src4_addr + 1)))
1323 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
1324 if ((ip->ip_v == IP6_VERSION) &&
1325 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
1326 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
1335 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
1336 struct ipsec_sa *sa[], uint16_t nb_pkts)
1340 for (i = 0; i < nb_pkts; i++)
1341 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
1345 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
1346 struct ipsec_sa *sa[], uint16_t nb_pkts)
1350 for (i = 0; i < nb_pkts; i++)
1351 sa[i] = &sa_ctx->sa[sa_idx[i]];
1355 * Select HW offloads to be used.
1358 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
1359 uint64_t *tx_offloads)
1361 struct ipsec_sa *rule;
1367 /* Check for inbound rules that use offloads and use this port */
1368 for (idx_sa = 0; idx_sa < nb_sa_in; idx_sa++) {
1369 rule = &sa_in[idx_sa];
1370 if ((rule->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1372 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1373 && rule->portid == port_id)
1374 *rx_offloads |= DEV_RX_OFFLOAD_SECURITY;
1377 /* Check for outbound rules that use offloads and use this port */
1378 for (idx_sa = 0; idx_sa < nb_sa_out; idx_sa++) {
1379 rule = &sa_out[idx_sa];
1380 if ((rule->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1382 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1383 && rule->portid == port_id)
1384 *tx_offloads |= DEV_TX_OFFLOAD_SECURITY;