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>
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 static struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
137 static uint32_t nb_sa_out;
138 static struct ipsec_sa_cnt sa_out_cnt;
140 static struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
141 static uint32_t nb_sa_in;
142 static struct ipsec_sa_cnt sa_in_cnt;
144 static const struct supported_cipher_algo *
145 find_match_cipher_algo(const char *cipher_keyword)
149 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
150 const struct supported_cipher_algo *algo =
153 if (strcmp(cipher_keyword, algo->keyword) == 0)
160 static const struct supported_auth_algo *
161 find_match_auth_algo(const char *auth_keyword)
165 for (i = 0; i < RTE_DIM(auth_algos); i++) {
166 const struct supported_auth_algo *algo =
169 if (strcmp(auth_keyword, algo->keyword) == 0)
176 static const struct supported_aead_algo *
177 find_match_aead_algo(const char *aead_keyword)
181 for (i = 0; i < RTE_DIM(aead_algos); i++) {
182 const struct supported_aead_algo *algo =
185 if (strcmp(aead_keyword, algo->keyword) == 0)
193 * parse x:x:x:x.... hex number key string into uint8_t *key
195 * > 0: number of bytes parsed
199 parse_key_string(const char *key_str, uint8_t *key)
201 const char *pt_start = key_str, *pt_end = key_str;
202 uint32_t nb_bytes = 0;
204 while (pt_end != NULL) {
205 char sub_str[3] = {0};
207 pt_end = strchr(pt_start, ':');
209 if (pt_end == NULL) {
210 if (strlen(pt_start) > 2)
212 strncpy(sub_str, pt_start, 2);
214 if (pt_end - pt_start > 2)
217 strncpy(sub_str, pt_start, pt_end - pt_start);
218 pt_start = pt_end + 1;
221 key[nb_bytes++] = strtol(sub_str, NULL, 16);
228 parse_sa_tokens(char **tokens, uint32_t n_tokens,
229 struct parse_status *status)
231 struct ipsec_sa *rule = NULL;
232 struct rte_ipsec_session *ips;
233 uint32_t ti; /*token index*/
234 uint32_t *ri /*rule index*/;
235 struct ipsec_sa_cnt *sa_cnt;
236 uint32_t cipher_algo_p = 0;
237 uint32_t auth_algo_p = 0;
238 uint32_t aead_algo_p = 0;
243 uint32_t portid_p = 0;
244 uint32_t fallback_p = 0;
246 if (strcmp(tokens[0], "in") == 0) {
250 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
251 "too many sa rules, abort insertion\n");
252 if (status->status < 0)
256 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
259 sa_cnt = &sa_out_cnt;
261 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
262 "too many sa rules, abort insertion\n");
263 if (status->status < 0)
267 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
271 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
272 if (status->status < 0)
274 if (atoi(tokens[1]) == INVALID_SPI)
276 rule->spi = atoi(tokens[1]);
277 ips = ipsec_get_primary_session(rule);
279 for (ti = 2; ti < n_tokens; ti++) {
280 if (strcmp(tokens[ti], "mode") == 0) {
281 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
282 if (status->status < 0)
285 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
286 if (status->status < 0)
289 if (strcmp(tokens[ti], "ipv4-tunnel") == 0) {
291 rule->flags = IP4_TUNNEL;
292 } else if (strcmp(tokens[ti], "ipv6-tunnel") == 0) {
294 rule->flags = IP6_TUNNEL;
295 } else if (strcmp(tokens[ti], "transport") == 0) {
298 rule->flags = TRANSPORT;
300 APP_CHECK(0, status, "unrecognized "
301 "input \"%s\"", tokens[ti]);
309 if (strcmp(tokens[ti], "cipher_algo") == 0) {
310 const struct supported_cipher_algo *algo;
313 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
315 if (status->status < 0)
318 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
319 if (status->status < 0)
322 algo = find_match_cipher_algo(tokens[ti]);
324 APP_CHECK(algo != NULL, status, "unrecognized "
325 "input \"%s\"", tokens[ti]);
327 if (status->status < 0)
330 rule->cipher_algo = algo->algo;
331 rule->block_size = algo->block_size;
332 rule->iv_len = algo->iv_len;
333 rule->cipher_key_len = algo->key_len;
335 /* for NULL algorithm, no cipher key required */
336 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
341 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
342 if (status->status < 0)
345 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
346 status, "unrecognized input \"%s\", "
347 "expect \"cipher_key\"", tokens[ti]);
348 if (status->status < 0)
351 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
352 if (status->status < 0)
355 key_len = parse_key_string(tokens[ti],
357 APP_CHECK(key_len == rule->cipher_key_len, status,
358 "unrecognized input \"%s\"", tokens[ti]);
359 if (status->status < 0)
362 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC ||
363 algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC)
364 rule->salt = (uint32_t)rte_rand();
366 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
368 rule->cipher_key_len = key_len;
370 &rule->cipher_key[key_len], 4);
377 if (strcmp(tokens[ti], "auth_algo") == 0) {
378 const struct supported_auth_algo *algo;
381 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
383 if (status->status < 0)
386 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
387 if (status->status < 0)
390 algo = find_match_auth_algo(tokens[ti]);
391 APP_CHECK(algo != NULL, status, "unrecognized "
392 "input \"%s\"", tokens[ti]);
394 if (status->status < 0)
397 rule->auth_algo = algo->algo;
398 rule->auth_key_len = algo->key_len;
399 rule->digest_len = algo->digest_len;
401 /* NULL algorithm and combined algos do not
404 if (algo->key_not_req) {
409 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
410 if (status->status < 0)
413 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
414 status, "unrecognized input \"%s\", "
415 "expect \"auth_key\"", tokens[ti]);
416 if (status->status < 0)
419 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
420 if (status->status < 0)
423 key_len = parse_key_string(tokens[ti],
425 APP_CHECK(key_len == rule->auth_key_len, status,
426 "unrecognized input \"%s\"", tokens[ti]);
427 if (status->status < 0)
434 if (strcmp(tokens[ti], "aead_algo") == 0) {
435 const struct supported_aead_algo *algo;
438 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
440 if (status->status < 0)
443 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
444 if (status->status < 0)
447 algo = find_match_aead_algo(tokens[ti]);
449 APP_CHECK(algo != NULL, status, "unrecognized "
450 "input \"%s\"", tokens[ti]);
452 if (status->status < 0)
455 rule->aead_algo = algo->algo;
456 rule->cipher_key_len = algo->key_len;
457 rule->digest_len = algo->digest_len;
458 rule->aad_len = algo->aad_len;
459 rule->block_size = algo->block_size;
460 rule->iv_len = algo->iv_len;
462 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
463 if (status->status < 0)
466 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
467 status, "unrecognized input \"%s\", "
468 "expect \"aead_key\"", tokens[ti]);
469 if (status->status < 0)
472 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
473 if (status->status < 0)
476 key_len = parse_key_string(tokens[ti],
478 APP_CHECK(key_len == rule->cipher_key_len, status,
479 "unrecognized input \"%s\"", tokens[ti]);
480 if (status->status < 0)
484 rule->cipher_key_len = key_len;
486 &rule->cipher_key[key_len], 4);
492 if (strcmp(tokens[ti], "src") == 0) {
493 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
494 if (status->status < 0)
497 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
498 if (status->status < 0)
501 if (IS_IP4_TUNNEL(rule->flags)) {
504 APP_CHECK(parse_ipv4_addr(tokens[ti],
505 &ip, NULL) == 0, status,
506 "unrecognized input \"%s\", "
507 "expect valid ipv4 addr",
509 if (status->status < 0)
511 rule->src.ip.ip4 = rte_bswap32(
512 (uint32_t)ip.s_addr);
513 } else if (IS_IP6_TUNNEL(rule->flags)) {
516 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
518 "unrecognized input \"%s\", "
519 "expect valid ipv6 addr",
521 if (status->status < 0)
523 memcpy(rule->src.ip.ip6.ip6_b,
525 } else if (IS_TRANSPORT(rule->flags)) {
526 APP_CHECK(0, status, "unrecognized input "
527 "\"%s\"", tokens[ti]);
535 if (strcmp(tokens[ti], "dst") == 0) {
536 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
537 if (status->status < 0)
540 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
541 if (status->status < 0)
544 if (IS_IP4_TUNNEL(rule->flags)) {
547 APP_CHECK(parse_ipv4_addr(tokens[ti],
548 &ip, NULL) == 0, status,
549 "unrecognized input \"%s\", "
550 "expect valid ipv4 addr",
552 if (status->status < 0)
554 rule->dst.ip.ip4 = rte_bswap32(
555 (uint32_t)ip.s_addr);
556 } else if (IS_IP6_TUNNEL(rule->flags)) {
559 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
561 "unrecognized input \"%s\", "
562 "expect valid ipv6 addr",
564 if (status->status < 0)
566 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
567 } else if (IS_TRANSPORT(rule->flags)) {
568 APP_CHECK(0, status, "unrecognized "
569 "input \"%s\"", tokens[ti]);
577 if (strcmp(tokens[ti], "type") == 0) {
578 APP_CHECK_PRESENCE(type_p, tokens[ti], status);
579 if (status->status < 0)
582 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
583 if (status->status < 0)
586 if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
588 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
589 else if (strcmp(tokens[ti],
590 "inline-protocol-offload") == 0)
592 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
593 else if (strcmp(tokens[ti],
594 "lookaside-protocol-offload") == 0)
596 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
597 else if (strcmp(tokens[ti], "no-offload") == 0)
598 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
600 APP_CHECK(0, status, "Invalid input \"%s\"",
609 if (strcmp(tokens[ti], "port_id") == 0) {
610 APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
611 if (status->status < 0)
613 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
614 if (status->status < 0)
616 rule->portid = atoi(tokens[ti]);
617 if (status->status < 0)
623 if (strcmp(tokens[ti], "fallback") == 0) {
624 struct rte_ipsec_session *fb;
626 APP_CHECK(app_sa_prm.enable, status, "Fallback session "
627 "not allowed for legacy mode.");
628 if (status->status < 0)
630 APP_CHECK(ips->type ==
631 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, status,
632 "Fallback session allowed if primary session "
633 "is of type inline-crypto-offload only.");
634 if (status->status < 0)
636 APP_CHECK(rule->direction ==
637 RTE_SECURITY_IPSEC_SA_DIR_INGRESS, status,
638 "Fallback session not allowed for egress "
640 if (status->status < 0)
642 APP_CHECK_PRESENCE(fallback_p, tokens[ti], status);
643 if (status->status < 0)
645 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
646 if (status->status < 0)
648 fb = ipsec_get_fallback_session(rule);
649 if (strcmp(tokens[ti], "lookaside-none") == 0) {
650 fb->type = RTE_SECURITY_ACTION_TYPE_NONE;
652 APP_CHECK(0, status, "unrecognized fallback "
653 "type %s.", tokens[ti]);
657 rule->fallback_sessions = 1;
662 /* unrecognizeable input */
663 APP_CHECK(0, status, "unrecognized input \"%s\"",
669 APP_CHECK(cipher_algo_p == 0, status,
670 "AEAD used, no need for cipher options");
671 if (status->status < 0)
674 APP_CHECK(auth_algo_p == 0, status,
675 "AEAD used, no need for auth options");
676 if (status->status < 0)
679 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
680 if (status->status < 0)
683 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
684 if (status->status < 0)
688 APP_CHECK(mode_p == 1, status, "missing mode option");
689 if (status->status < 0)
692 if ((ips->type != RTE_SECURITY_ACTION_TYPE_NONE) && (portid_p == 0))
693 printf("Missing portid option, falling back to non-offload\n");
695 if (!type_p || !portid_p) {
696 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
704 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
708 const struct rte_ipsec_session *ips;
709 const struct rte_ipsec_session *fallback_ips;
711 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
713 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
714 if (cipher_algos[i].algo == sa->cipher_algo &&
715 cipher_algos[i].key_len == sa->cipher_key_len) {
716 printf("%s ", cipher_algos[i].keyword);
721 for (i = 0; i < RTE_DIM(auth_algos); i++) {
722 if (auth_algos[i].algo == sa->auth_algo) {
723 printf("%s ", auth_algos[i].keyword);
728 for (i = 0; i < RTE_DIM(aead_algos); i++) {
729 if (aead_algos[i].algo == sa->aead_algo) {
730 printf("%s ", aead_algos[i].keyword);
737 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
739 printf("IP4Tunnel ");
740 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
741 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
742 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
743 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
746 printf("IP6Tunnel ");
747 for (i = 0; i < 16; i++) {
748 if (i % 2 && i != 15)
749 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
751 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
754 for (i = 0; i < 16; i++) {
755 if (i % 2 && i != 15)
756 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
758 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
762 printf("Transport ");
766 ips = &sa->sessions[IPSEC_SESSION_PRIMARY];
769 case RTE_SECURITY_ACTION_TYPE_NONE:
770 printf("no-offload ");
772 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
773 printf("inline-crypto-offload ");
775 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
776 printf("inline-protocol-offload ");
778 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
779 printf("lookaside-protocol-offload ");
783 fallback_ips = &sa->sessions[IPSEC_SESSION_FALLBACK];
784 if (fallback_ips != NULL && sa->fallback_sessions > 0) {
785 printf("inline fallback: ");
786 if (fallback_ips->type == RTE_SECURITY_ACTION_TYPE_NONE)
787 printf("lookaside-none");
795 struct rte_crypto_sym_xform a;
796 struct rte_crypto_sym_xform b;
800 void *satbl; /* pointer to array of rte_ipsec_sa objects*/
801 struct ipsec_sad sad;
804 struct ipsec_sa sa[];
807 static struct sa_ctx *
808 sa_create(const char *name, int32_t socket_id, uint32_t nb_sa)
811 struct sa_ctx *sa_ctx;
813 const struct rte_memzone *mz;
815 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
817 /* Create SA context */
818 printf("Creating SA context with %u maximum entries on socket %d\n",
821 mz_size = sizeof(struct ipsec_xf) * nb_sa;
822 mz = rte_memzone_reserve(s, mz_size, socket_id,
823 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
825 printf("Failed to allocate SA XFORM memory\n");
830 sa_ctx = rte_malloc(NULL, sizeof(struct sa_ctx) +
831 sizeof(struct ipsec_sa) * nb_sa, RTE_CACHE_LINE_SIZE);
833 if (sa_ctx == NULL) {
834 printf("Failed to allocate SA CTX memory\n");
836 rte_memzone_free(mz);
840 sa_ctx->xf = (struct ipsec_xf *)mz->addr;
841 sa_ctx->nb_sa = nb_sa;
847 check_eth_dev_caps(uint16_t portid, uint32_t inbound)
849 struct rte_eth_dev_info dev_info;
852 retval = rte_eth_dev_info_get(portid, &dev_info);
855 "Error during getting device (port %u) info: %s\n",
856 portid, strerror(-retval));
862 if ((dev_info.rx_offload_capa &
863 DEV_RX_OFFLOAD_SECURITY) == 0) {
864 RTE_LOG(WARNING, PORT,
865 "hardware RX IPSec offload is not supported\n");
869 } else { /* outbound */
870 if ((dev_info.tx_offload_capa &
871 DEV_TX_OFFLOAD_SECURITY) == 0) {
872 RTE_LOG(WARNING, PORT,
873 "hardware TX IPSec offload is not supported\n");
881 * Helper function, tries to determine next_proto for SPI
882 * by searching though SP rules.
885 get_spi_proto(uint32_t spi, enum rte_security_ipsec_sa_direction dir,
886 struct ip_addr ip_addr[2], uint32_t mask[2])
890 rc4 = sp4_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
892 rc6 = sp6_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
898 "%s: SPI %u used simultaeously by "
899 "IPv4(%d) and IPv6 (%d) SP rules\n",
900 __func__, spi, rc4, rc6);
904 } else if (rc6 < 0) {
906 "%s: SPI %u is not used by any SP rule\n",
914 * Helper function for getting source and destination IP addresses
915 * from SP. Needed for inline crypto transport mode, as addresses are not
916 * provided in config file for that mode. It checks if SP for current SA exists,
917 * and based on what type of protocol is returned, it stores appropriate
918 * addresses got from SP into SA.
921 sa_add_address_inline_crypto(struct ipsec_sa *sa)
924 struct ip_addr ip_addr[2];
927 protocol = get_spi_proto(sa->spi, sa->direction, ip_addr, mask);
930 else if (protocol == IPPROTO_IPIP) {
931 sa->flags |= IP4_TRANSPORT;
932 if (mask[0] == IP4_FULL_MASK &&
933 mask[1] == IP4_FULL_MASK &&
934 ip_addr[0].ip.ip4 != 0 &&
935 ip_addr[1].ip.ip4 != 0) {
937 sa->src.ip.ip4 = ip_addr[0].ip.ip4;
938 sa->dst.ip.ip4 = ip_addr[1].ip.ip4;
941 "%s: No valid address or mask entry in"
942 " IPv4 SP rule for SPI %u\n",
946 } else if (protocol == IPPROTO_IPV6) {
947 sa->flags |= IP6_TRANSPORT;
948 if (mask[0] == IP6_FULL_MASK &&
949 mask[1] == IP6_FULL_MASK &&
950 (ip_addr[0].ip.ip6.ip6[0] != 0 ||
951 ip_addr[0].ip.ip6.ip6[1] != 0) &&
952 (ip_addr[1].ip.ip6.ip6[0] != 0 ||
953 ip_addr[1].ip.ip6.ip6[1] != 0)) {
955 sa->src.ip.ip6 = ip_addr[0].ip.ip6;
956 sa->dst.ip.ip6 = ip_addr[1].ip.ip6;
959 "%s: No valid address or mask entry in"
960 " IPv6 SP rule for SPI %u\n",
969 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
970 uint32_t nb_entries, uint32_t inbound,
971 struct socket_ctx *skt_ctx)
975 uint16_t iv_length, aad_length;
978 struct rte_ipsec_session *ips;
980 /* for ESN upper 32 bits of SQN also need to be part of AAD */
981 aad_length = (app_sa_prm.enable_esn != 0) ? sizeof(uint32_t) : 0;
983 for (i = 0; i < nb_entries; i++) {
985 sa = &sa_ctx->sa[idx];
987 printf("Index %u already in use by SPI %u\n",
994 rc = ipsec_sad_add(&sa_ctx->sad, sa);
1000 ips = ipsec_get_primary_session(sa);
1002 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1003 ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1004 if (check_eth_dev_caps(sa->portid, inbound))
1009 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1011 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
1012 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
1016 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1018 sa_add_address_inline_crypto(sa);
1019 if (inline_status < 0)
1020 return inline_status;
1025 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
1026 struct rte_ipsec_session *ips;
1029 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1030 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
1031 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
1032 sa_ctx->xf[idx].a.aead.key.length =
1034 sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
1035 RTE_CRYPTO_AEAD_OP_DECRYPT :
1036 RTE_CRYPTO_AEAD_OP_ENCRYPT;
1037 sa_ctx->xf[idx].a.next = NULL;
1038 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
1039 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
1040 sa_ctx->xf[idx].a.aead.aad_length =
1041 sa->aad_len + aad_length;
1042 sa_ctx->xf[idx].a.aead.digest_length =
1045 sa->xforms = &sa_ctx->xf[idx].a;
1047 ips = ipsec_get_primary_session(sa);
1049 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1051 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1052 rc = create_inline_session(skt_ctx, sa, ips);
1054 RTE_LOG(ERR, IPSEC_ESP,
1055 "create_inline_session() failed\n");
1059 print_one_sa_rule(sa, inbound);
1061 switch (sa->cipher_algo) {
1062 case RTE_CRYPTO_CIPHER_NULL:
1063 case RTE_CRYPTO_CIPHER_3DES_CBC:
1064 case RTE_CRYPTO_CIPHER_AES_CBC:
1065 iv_length = sa->iv_len;
1067 case RTE_CRYPTO_CIPHER_AES_CTR:
1071 RTE_LOG(ERR, IPSEC_ESP,
1072 "unsupported cipher algorithm %u\n",
1078 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1079 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
1080 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
1081 sa_ctx->xf[idx].b.cipher.key.length =
1083 sa_ctx->xf[idx].b.cipher.op =
1084 RTE_CRYPTO_CIPHER_OP_DECRYPT;
1085 sa_ctx->xf[idx].b.next = NULL;
1086 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
1087 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
1089 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1090 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
1091 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
1092 sa_ctx->xf[idx].a.auth.key.length =
1094 sa_ctx->xf[idx].a.auth.digest_length =
1096 sa_ctx->xf[idx].a.auth.op =
1097 RTE_CRYPTO_AUTH_OP_VERIFY;
1098 } else { /* outbound */
1099 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1100 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
1101 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
1102 sa_ctx->xf[idx].a.cipher.key.length =
1104 sa_ctx->xf[idx].a.cipher.op =
1105 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1106 sa_ctx->xf[idx].a.next = NULL;
1107 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
1108 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
1110 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1111 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
1112 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
1113 sa_ctx->xf[idx].b.auth.key.length =
1115 sa_ctx->xf[idx].b.auth.digest_length =
1117 sa_ctx->xf[idx].b.auth.op =
1118 RTE_CRYPTO_AUTH_OP_GENERATE;
1121 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
1122 sa_ctx->xf[idx].b.next = NULL;
1123 sa->xforms = &sa_ctx->xf[idx].a;
1125 print_one_sa_rule(sa, inbound);
1133 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1134 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1136 return sa_add_rules(sa_ctx, entries, nb_entries, 0, skt_ctx);
1140 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1141 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1143 return sa_add_rules(sa_ctx, entries, nb_entries, 1, skt_ctx);
1147 * helper function, fills parameters that are identical for all SAs
1150 fill_ipsec_app_sa_prm(struct rte_ipsec_sa_prm *prm,
1151 const struct app_sa_prm *app_prm)
1153 memset(prm, 0, sizeof(*prm));
1155 prm->flags = app_prm->flags;
1156 prm->ipsec_xform.options.esn = app_prm->enable_esn;
1157 prm->ipsec_xform.replay_win_sz = app_prm->window_size;
1161 fill_ipsec_sa_prm(struct rte_ipsec_sa_prm *prm, const struct ipsec_sa *ss,
1162 const struct rte_ipv4_hdr *v4, struct rte_ipv6_hdr *v6)
1167 * Try to get SPI next proto by searching that SPI in SPD.
1168 * probably not the optimal way, but there seems nothing
1171 rc = get_spi_proto(ss->spi, ss->direction, NULL, NULL);
1175 fill_ipsec_app_sa_prm(prm, &app_sa_prm);
1176 prm->userdata = (uintptr_t)ss;
1178 /* setup ipsec xform */
1179 prm->ipsec_xform.spi = ss->spi;
1180 prm->ipsec_xform.salt = ss->salt;
1181 prm->ipsec_xform.direction = ss->direction;
1182 prm->ipsec_xform.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP;
1183 prm->ipsec_xform.mode = (IS_TRANSPORT(ss->flags)) ?
1184 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT :
1185 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
1186 prm->ipsec_xform.options.ecn = 1;
1187 prm->ipsec_xform.options.copy_dscp = 1;
1189 if (IS_IP4_TUNNEL(ss->flags)) {
1190 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV4;
1191 prm->tun.hdr_len = sizeof(*v4);
1192 prm->tun.next_proto = rc;
1194 } else if (IS_IP6_TUNNEL(ss->flags)) {
1195 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV6;
1196 prm->tun.hdr_len = sizeof(*v6);
1197 prm->tun.next_proto = rc;
1200 /* transport mode */
1201 prm->trs.proto = rc;
1204 /* setup crypto section */
1205 prm->crypto_xform = ss->xforms;
1210 fill_ipsec_session(struct rte_ipsec_session *ss, struct rte_ipsec_sa *sa)
1216 if (ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1217 ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
1218 if (ss->security.ses != NULL) {
1219 rc = rte_ipsec_session_prepare(ss);
1221 memset(ss, 0, sizeof(*ss));
1229 * Initialise related rte_ipsec_sa object.
1232 ipsec_sa_init(struct ipsec_sa *lsa, struct rte_ipsec_sa *sa, uint32_t sa_size)
1235 struct rte_ipsec_sa_prm prm;
1236 struct rte_ipsec_session *ips;
1237 struct rte_ipv4_hdr v4 = {
1238 .version_ihl = IPVERSION << 4 |
1239 sizeof(v4) / RTE_IPV4_IHL_MULTIPLIER,
1240 .time_to_live = IPDEFTTL,
1241 .next_proto_id = IPPROTO_ESP,
1242 .src_addr = lsa->src.ip.ip4,
1243 .dst_addr = lsa->dst.ip.ip4,
1245 struct rte_ipv6_hdr v6 = {
1246 .vtc_flow = htonl(IP6_VERSION << 28),
1247 .proto = IPPROTO_ESP,
1250 if (IS_IP6_TUNNEL(lsa->flags)) {
1251 memcpy(v6.src_addr, lsa->src.ip.ip6.ip6_b, sizeof(v6.src_addr));
1252 memcpy(v6.dst_addr, lsa->dst.ip.ip6.ip6_b, sizeof(v6.dst_addr));
1255 rc = fill_ipsec_sa_prm(&prm, lsa, &v4, &v6);
1257 rc = rte_ipsec_sa_init(sa, &prm, sa_size);
1261 /* init primary processing session */
1262 ips = ipsec_get_primary_session(lsa);
1263 rc = fill_ipsec_session(ips, sa);
1267 /* init inline fallback processing session */
1268 if (lsa->fallback_sessions == 1)
1269 rc = fill_ipsec_session(ipsec_get_fallback_session(lsa), sa);
1275 * Allocate space and init rte_ipsec_sa strcutures,
1279 ipsec_satbl_init(struct sa_ctx *ctx, uint32_t nb_ent, int32_t socket)
1284 struct rte_ipsec_sa *sa;
1285 struct ipsec_sa *lsa;
1286 struct rte_ipsec_sa_prm prm;
1288 /* determine SA size */
1290 fill_ipsec_sa_prm(&prm, ctx->sa + idx, NULL, NULL);
1291 sz = rte_ipsec_sa_size(&prm);
1293 RTE_LOG(ERR, IPSEC, "%s(%p, %u, %d): "
1294 "failed to determine SA size, error code: %d\n",
1295 __func__, ctx, nb_ent, socket, sz);
1301 ctx->satbl = rte_zmalloc_socket(NULL, tsz, RTE_CACHE_LINE_SIZE, socket);
1302 if (ctx->satbl == NULL) {
1304 "%s(%p, %u, %d): failed to allocate %zu bytes\n",
1305 __func__, ctx, nb_ent, socket, tsz);
1310 for (i = 0; i != nb_ent && rc == 0; i++) {
1314 sa = (struct rte_ipsec_sa *)((uintptr_t)ctx->satbl + sz * i);
1315 lsa = ctx->sa + idx;
1317 rc = ipsec_sa_init(lsa, sa, sz);
1324 * Walk through all SA rules to find an SA with given SPI
1327 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound)
1330 const struct ipsec_sa *sar;
1338 for (i = 0; i != num; i++) {
1339 if (sar[i].spi == spi)
1347 sa_init(struct socket_ctx *ctx, int32_t socket_id)
1353 rte_exit(EXIT_FAILURE, "NULL context.\n");
1355 if (ctx->sa_in != NULL)
1356 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
1357 "initialized\n", socket_id);
1359 if (ctx->sa_out != NULL)
1360 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
1361 "initialized\n", socket_id);
1365 ctx->sa_in = sa_create(name, socket_id, nb_sa_in);
1366 if (ctx->sa_in == NULL)
1367 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1368 "context %s in socket %d\n", rte_errno,
1371 rc = ipsec_sad_create(name, &ctx->sa_in->sad, socket_id,
1374 rte_exit(EXIT_FAILURE, "failed to init SAD\n");
1376 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in, ctx);
1378 if (app_sa_prm.enable != 0) {
1379 rc = ipsec_satbl_init(ctx->sa_in, nb_sa_in,
1382 rte_exit(EXIT_FAILURE,
1383 "failed to init inbound SAs\n");
1386 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
1388 if (nb_sa_out > 0) {
1390 ctx->sa_out = sa_create(name, socket_id, nb_sa_out);
1391 if (ctx->sa_out == NULL)
1392 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1393 "context %s in socket %d\n", rte_errno,
1396 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out, ctx);
1398 if (app_sa_prm.enable != 0) {
1399 rc = ipsec_satbl_init(ctx->sa_out, nb_sa_out,
1402 rte_exit(EXIT_FAILURE,
1403 "failed to init outbound SAs\n");
1406 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
1411 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
1413 struct ipsec_mbuf_metadata *priv;
1414 struct ipsec_sa *sa;
1419 return (sa_ctx->sa[sa_idx].spi == sa->spi);
1421 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
1426 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
1427 void *sa_arr[], uint16_t nb_pkts)
1431 uint32_t *src4_addr;
1434 struct ipsec_sa *sa;
1436 sad_lookup(&sa_ctx->sad, pkts, sa_arr, nb_pkts);
1439 * Mark need for inline offload fallback on the LSB of SA pointer.
1440 * Thanks to packet grouping mechanism which ipsec_process is using
1441 * packets marked for fallback processing will form separate group.
1443 * Because it is not safe to use SA pointer it is casted to generic
1444 * pointer to prevent from unintentional use. Use ipsec_mask_saptr
1445 * to get valid struct pointer.
1447 for (i = 0; i < nb_pkts; i++) {
1448 if (sa_arr[i] == NULL)
1451 result_sa = sa = sa_arr[i];
1452 if (MBUF_NO_SEC_OFFLOAD(pkts[i]) &&
1453 sa->fallback_sessions > 0) {
1454 uintptr_t intsa = (uintptr_t)sa;
1455 intsa |= IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
1456 result_sa = (void *)intsa;
1459 ip = rte_pktmbuf_mtod(pkts[i], struct ip *);
1460 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1462 src4_addr = RTE_PTR_ADD(ip,
1463 offsetof(struct ip, ip_src));
1464 if ((ip->ip_v == IPVERSION) &&
1465 (sa->src.ip.ip4 == *src4_addr) &&
1466 (sa->dst.ip.ip4 == *(src4_addr + 1)))
1467 sa_arr[i] = result_sa;
1472 src6_addr = RTE_PTR_ADD(ip,
1473 offsetof(struct ip6_hdr, ip6_src));
1474 if ((ip->ip_v == IP6_VERSION) &&
1475 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
1476 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
1477 sa_arr[i] = result_sa;
1482 sa_arr[i] = result_sa;
1488 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
1489 void *sa[], uint16_t nb_pkts)
1493 for (i = 0; i < nb_pkts; i++)
1494 sa[i] = &sa_ctx->sa[sa_idx[i]];
1498 * Select HW offloads to be used.
1501 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
1502 uint64_t *tx_offloads)
1504 struct ipsec_sa *rule;
1506 enum rte_security_session_action_type rule_type;
1511 /* Check for inbound rules that use offloads and use this port */
1512 for (idx_sa = 0; idx_sa < nb_sa_in; idx_sa++) {
1513 rule = &sa_in[idx_sa];
1514 rule_type = ipsec_get_action_type(rule);
1515 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1517 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1518 && rule->portid == port_id)
1519 *rx_offloads |= DEV_RX_OFFLOAD_SECURITY;
1522 /* Check for outbound rules that use offloads and use this port */
1523 for (idx_sa = 0; idx_sa < nb_sa_out; idx_sa++) {
1524 rule = &sa_out[idx_sa];
1525 rule_type = ipsec_get_action_type(rule);
1526 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1528 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1529 && rule->portid == port_id)
1530 *tx_offloads |= DEV_TX_OFFLOAD_SECURITY;