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>
21 #include <rte_random.h>
22 #include <rte_ethdev.h>
23 #include <rte_malloc.h>
32 #define IP4_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip4) * CHAR_BIT)
34 #define IP6_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip6.ip6) * CHAR_BIT)
36 #define MBUF_NO_SEC_OFFLOAD(m) ((m->ol_flags & RTE_MBUF_F_RX_SEC_OFFLOAD) == 0)
38 struct supported_cipher_algo {
40 enum rte_crypto_cipher_algorithm algo;
46 struct supported_auth_algo {
48 enum rte_crypto_auth_algorithm algo;
54 struct supported_aead_algo {
56 enum rte_crypto_aead_algorithm algo;
65 const struct supported_cipher_algo cipher_algos[] = {
68 .algo = RTE_CRYPTO_CIPHER_NULL,
74 .keyword = "aes-128-cbc",
75 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
81 .keyword = "aes-192-cbc",
82 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
88 .keyword = "aes-256-cbc",
89 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
95 .keyword = "aes-128-ctr",
96 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
102 .keyword = "3des-cbc",
103 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
110 const struct supported_auth_algo auth_algos[] = {
113 .algo = RTE_CRYPTO_AUTH_NULL,
119 .keyword = "sha1-hmac",
120 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
125 .keyword = "sha256-hmac",
126 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
132 const struct supported_aead_algo aead_algos[] = {
134 .keyword = "aes-128-gcm",
135 .algo = RTE_CRYPTO_AEAD_AES_GCM,
143 .keyword = "aes-192-gcm",
144 .algo = RTE_CRYPTO_AEAD_AES_GCM,
152 .keyword = "aes-256-gcm",
153 .algo = RTE_CRYPTO_AEAD_AES_GCM,
162 #define SA_INIT_NB 128
164 static uint32_t nb_crypto_sessions;
165 struct ipsec_sa *sa_out;
167 static uint32_t sa_out_sz;
168 static struct ipsec_sa_cnt sa_out_cnt;
170 struct ipsec_sa *sa_in;
172 static uint32_t sa_in_sz;
173 static struct ipsec_sa_cnt sa_in_cnt;
175 static const struct supported_cipher_algo *
176 find_match_cipher_algo(const char *cipher_keyword)
180 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
181 const struct supported_cipher_algo *algo =
184 if (strcmp(cipher_keyword, algo->keyword) == 0)
191 static const struct supported_auth_algo *
192 find_match_auth_algo(const char *auth_keyword)
196 for (i = 0; i < RTE_DIM(auth_algos); i++) {
197 const struct supported_auth_algo *algo =
200 if (strcmp(auth_keyword, algo->keyword) == 0)
207 static const struct supported_aead_algo *
208 find_match_aead_algo(const char *aead_keyword)
212 for (i = 0; i < RTE_DIM(aead_algos); i++) {
213 const struct supported_aead_algo *algo =
216 if (strcmp(aead_keyword, algo->keyword) == 0)
224 * parse x:x:x:x.... hex number key string into uint8_t *key
226 * > 0: number of bytes parsed
230 parse_key_string(const char *key_str, uint8_t *key)
232 const char *pt_start = key_str, *pt_end = key_str;
233 uint32_t nb_bytes = 0;
235 while (pt_end != NULL) {
236 char sub_str[3] = {0};
238 pt_end = strchr(pt_start, ':');
240 if (pt_end == NULL) {
241 if (strlen(pt_start) > 2)
243 strncpy(sub_str, pt_start, 2);
245 if (pt_end - pt_start > 2)
248 strncpy(sub_str, pt_start, pt_end - pt_start);
249 pt_start = pt_end + 1;
252 key[nb_bytes++] = strtol(sub_str, NULL, 16);
259 extend_sa_arr(struct ipsec_sa **sa_tbl, uint32_t cur_cnt, uint32_t *cur_sz)
261 if (*sa_tbl == NULL) {
262 *sa_tbl = calloc(SA_INIT_NB, sizeof(struct ipsec_sa));
265 *cur_sz = SA_INIT_NB;
269 if (cur_cnt >= *cur_sz) {
270 *sa_tbl = realloc(*sa_tbl,
271 *cur_sz * sizeof(struct ipsec_sa) * 2);
274 /* clean reallocated extra space */
275 memset(&(*sa_tbl)[*cur_sz], 0,
276 *cur_sz * sizeof(struct ipsec_sa));
284 parse_sa_tokens(char **tokens, uint32_t n_tokens,
285 struct parse_status *status)
287 struct ipsec_sa *rule = NULL;
288 struct rte_ipsec_session *ips;
289 uint32_t ti; /*token index*/
290 uint32_t *ri /*rule index*/;
291 struct ipsec_sa_cnt *sa_cnt;
292 uint32_t cipher_algo_p = 0;
293 uint32_t auth_algo_p = 0;
294 uint32_t aead_algo_p = 0;
299 uint32_t portid_p = 0;
300 uint32_t fallback_p = 0;
301 int16_t status_p = 0;
302 uint16_t udp_encap_p = 0;
304 if (strcmp(tokens[0], "in") == 0) {
307 if (extend_sa_arr(&sa_in, nb_sa_in, &sa_in_sz) < 0)
310 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
313 sa_cnt = &sa_out_cnt;
314 if (extend_sa_arr(&sa_out, nb_sa_out, &sa_out_sz) < 0)
317 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
321 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
322 if (status->status < 0)
324 if (atoi(tokens[1]) == INVALID_SPI)
326 rule->spi = atoi(tokens[1]);
327 rule->portid = UINT16_MAX;
328 ips = ipsec_get_primary_session(rule);
330 for (ti = 2; ti < n_tokens; ti++) {
331 if (strcmp(tokens[ti], "mode") == 0) {
332 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
333 if (status->status < 0)
336 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
337 if (status->status < 0)
340 if (strcmp(tokens[ti], "ipv4-tunnel") == 0) {
342 rule->flags = IP4_TUNNEL;
343 } else if (strcmp(tokens[ti], "ipv6-tunnel") == 0) {
345 rule->flags = IP6_TUNNEL;
346 } else if (strcmp(tokens[ti], "transport") == 0) {
349 rule->flags = TRANSPORT;
351 APP_CHECK(0, status, "unrecognized "
352 "input \"%s\"", tokens[ti]);
360 if (strcmp(tokens[ti], "cipher_algo") == 0) {
361 const struct supported_cipher_algo *algo;
364 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
366 if (status->status < 0)
369 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
370 if (status->status < 0)
373 algo = find_match_cipher_algo(tokens[ti]);
375 APP_CHECK(algo != NULL, status, "unrecognized "
376 "input \"%s\"", tokens[ti]);
378 if (status->status < 0)
381 rule->cipher_algo = algo->algo;
382 rule->block_size = algo->block_size;
383 rule->iv_len = algo->iv_len;
384 rule->cipher_key_len = algo->key_len;
386 /* for NULL algorithm, no cipher key required */
387 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
392 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
393 if (status->status < 0)
396 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
397 status, "unrecognized input \"%s\", "
398 "expect \"cipher_key\"", tokens[ti]);
399 if (status->status < 0)
402 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
403 if (status->status < 0)
406 key_len = parse_key_string(tokens[ti],
408 APP_CHECK(key_len == rule->cipher_key_len, status,
409 "unrecognized input \"%s\"", tokens[ti]);
410 if (status->status < 0)
413 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC ||
414 algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC)
415 rule->salt = (uint32_t)rte_rand();
417 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
419 rule->cipher_key_len = key_len;
421 &rule->cipher_key[key_len], 4);
428 if (strcmp(tokens[ti], "auth_algo") == 0) {
429 const struct supported_auth_algo *algo;
432 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
434 if (status->status < 0)
437 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
438 if (status->status < 0)
441 algo = find_match_auth_algo(tokens[ti]);
442 APP_CHECK(algo != NULL, status, "unrecognized "
443 "input \"%s\"", tokens[ti]);
445 if (status->status < 0)
448 rule->auth_algo = algo->algo;
449 rule->auth_key_len = algo->key_len;
450 rule->digest_len = algo->digest_len;
452 /* NULL algorithm and combined algos do not
455 if (algo->key_not_req) {
460 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
461 if (status->status < 0)
464 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
465 status, "unrecognized input \"%s\", "
466 "expect \"auth_key\"", tokens[ti]);
467 if (status->status < 0)
470 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
471 if (status->status < 0)
474 key_len = parse_key_string(tokens[ti],
476 APP_CHECK(key_len == rule->auth_key_len, status,
477 "unrecognized input \"%s\"", tokens[ti]);
478 if (status->status < 0)
485 if (strcmp(tokens[ti], "aead_algo") == 0) {
486 const struct supported_aead_algo *algo;
489 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
491 if (status->status < 0)
494 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
495 if (status->status < 0)
498 algo = find_match_aead_algo(tokens[ti]);
500 APP_CHECK(algo != NULL, status, "unrecognized "
501 "input \"%s\"", tokens[ti]);
503 if (status->status < 0)
506 rule->aead_algo = algo->algo;
507 rule->cipher_key_len = algo->key_len;
508 rule->digest_len = algo->digest_len;
509 rule->aad_len = algo->aad_len;
510 rule->block_size = algo->block_size;
511 rule->iv_len = algo->iv_len;
513 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
514 if (status->status < 0)
517 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
518 status, "unrecognized input \"%s\", "
519 "expect \"aead_key\"", tokens[ti]);
520 if (status->status < 0)
523 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
524 if (status->status < 0)
527 key_len = parse_key_string(tokens[ti],
529 APP_CHECK(key_len == rule->cipher_key_len, status,
530 "unrecognized input \"%s\"", tokens[ti]);
531 if (status->status < 0)
535 rule->cipher_key_len = key_len;
537 &rule->cipher_key[key_len], 4);
543 if (strcmp(tokens[ti], "src") == 0) {
544 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
545 if (status->status < 0)
548 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
549 if (status->status < 0)
552 if (IS_IP4_TUNNEL(rule->flags)) {
555 APP_CHECK(parse_ipv4_addr(tokens[ti],
556 &ip, NULL) == 0, status,
557 "unrecognized input \"%s\", "
558 "expect valid ipv4 addr",
560 if (status->status < 0)
562 rule->src.ip.ip4 = rte_bswap32(
563 (uint32_t)ip.s_addr);
564 } else if (IS_IP6_TUNNEL(rule->flags)) {
567 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
569 "unrecognized input \"%s\", "
570 "expect valid ipv6 addr",
572 if (status->status < 0)
574 memcpy(rule->src.ip.ip6.ip6_b,
576 } else if (IS_TRANSPORT(rule->flags)) {
577 APP_CHECK(0, status, "unrecognized input "
578 "\"%s\"", tokens[ti]);
586 if (strcmp(tokens[ti], "dst") == 0) {
587 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
588 if (status->status < 0)
591 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
592 if (status->status < 0)
595 if (IS_IP4_TUNNEL(rule->flags)) {
598 APP_CHECK(parse_ipv4_addr(tokens[ti],
599 &ip, NULL) == 0, status,
600 "unrecognized input \"%s\", "
601 "expect valid ipv4 addr",
603 if (status->status < 0)
605 rule->dst.ip.ip4 = rte_bswap32(
606 (uint32_t)ip.s_addr);
607 } else if (IS_IP6_TUNNEL(rule->flags)) {
610 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
612 "unrecognized input \"%s\", "
613 "expect valid ipv6 addr",
615 if (status->status < 0)
617 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
618 } else if (IS_TRANSPORT(rule->flags)) {
619 APP_CHECK(0, status, "unrecognized "
620 "input \"%s\"", tokens[ti]);
628 if (strcmp(tokens[ti], "type") == 0) {
629 APP_CHECK_PRESENCE(type_p, tokens[ti], status);
630 if (status->status < 0)
633 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
634 if (status->status < 0)
637 if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
639 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
640 else if (strcmp(tokens[ti],
641 "inline-protocol-offload") == 0)
643 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
644 else if (strcmp(tokens[ti],
645 "lookaside-protocol-offload") == 0)
647 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
648 else if (strcmp(tokens[ti], "no-offload") == 0)
649 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
650 else if (strcmp(tokens[ti], "cpu-crypto") == 0)
651 ips->type = RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO;
653 APP_CHECK(0, status, "Invalid input \"%s\"",
662 if (strcmp(tokens[ti], "port_id") == 0) {
663 APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
664 if (status->status < 0)
666 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
667 if (status->status < 0)
669 if (rule->portid == UINT16_MAX)
670 rule->portid = atoi(tokens[ti]);
671 else if (rule->portid != atoi(tokens[ti])) {
673 "portid %s not matching with already assigned portid %u",
674 tokens[ti], rule->portid);
681 if (strcmp(tokens[ti], "mss") == 0) {
682 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
683 if (status->status < 0)
685 rule->mss = atoi(tokens[ti]);
686 if (status->status < 0)
691 if (strcmp(tokens[ti], "fallback") == 0) {
692 struct rte_ipsec_session *fb;
694 APP_CHECK(app_sa_prm.enable, status, "Fallback session "
695 "not allowed for legacy mode.");
696 if (status->status < 0)
698 APP_CHECK(ips->type ==
699 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, status,
700 "Fallback session allowed if primary session "
701 "is of type inline-crypto-offload only.");
702 if (status->status < 0)
704 APP_CHECK(rule->direction ==
705 RTE_SECURITY_IPSEC_SA_DIR_INGRESS, status,
706 "Fallback session not allowed for egress "
708 if (status->status < 0)
710 APP_CHECK_PRESENCE(fallback_p, tokens[ti], status);
711 if (status->status < 0)
713 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
714 if (status->status < 0)
716 fb = ipsec_get_fallback_session(rule);
717 if (strcmp(tokens[ti], "lookaside-none") == 0)
718 fb->type = RTE_SECURITY_ACTION_TYPE_NONE;
719 else if (strcmp(tokens[ti], "cpu-crypto") == 0)
720 fb->type = RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO;
722 APP_CHECK(0, status, "unrecognized fallback "
723 "type %s.", tokens[ti]);
727 rule->fallback_sessions = 1;
728 nb_crypto_sessions++;
732 if (strcmp(tokens[ti], "flow-direction") == 0) {
734 case RTE_SECURITY_ACTION_TYPE_NONE:
735 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
737 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
738 if (status->status < 0)
740 if (rule->portid == UINT16_MAX)
741 rule->portid = atoi(tokens[ti]);
742 else if (rule->portid != atoi(tokens[ti])) {
744 "portid %s not matching with already assigned portid %u",
745 tokens[ti], rule->portid);
748 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
749 if (status->status < 0)
751 rule->fdir_qid = atoi(tokens[ti]);
752 /* validating portid and queueid */
753 status_p = check_flow_params(rule->portid,
756 printf("port id %u / queue id %u is "
757 "not valid\n", rule->portid,
761 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
762 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
763 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
766 "flow director not supported for security session type %d",
772 if (strcmp(tokens[ti], "udp-encap") == 0) {
774 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
775 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
776 APP_CHECK_PRESENCE(udp_encap_p, tokens[ti],
778 if (status->status < 0)
782 app_sa_prm.udp_encap = 1;
785 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
788 rule->udp.dport = 4500;
792 "UDP encapsulation not supported for "
793 "security session type %d",
800 /* unrecognizeable input */
801 APP_CHECK(0, status, "unrecognized input \"%s\"",
807 APP_CHECK(cipher_algo_p == 0, status,
808 "AEAD used, no need for cipher options");
809 if (status->status < 0)
812 APP_CHECK(auth_algo_p == 0, status,
813 "AEAD used, no need for auth options");
814 if (status->status < 0)
817 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
818 if (status->status < 0)
821 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
822 if (status->status < 0)
826 APP_CHECK(mode_p == 1, status, "missing mode option");
827 if (status->status < 0)
830 if ((ips->type != RTE_SECURITY_ACTION_TYPE_NONE && ips->type !=
831 RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) && (portid_p == 0))
832 printf("Missing portid option, falling back to non-offload\n");
834 if (!type_p || (!portid_p && ips->type !=
835 RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO)) {
836 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
839 nb_crypto_sessions++;
844 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
848 const struct rte_ipsec_session *ips;
849 const struct rte_ipsec_session *fallback_ips;
851 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
853 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
854 if (cipher_algos[i].algo == sa->cipher_algo &&
855 cipher_algos[i].key_len == sa->cipher_key_len) {
856 printf("%s ", cipher_algos[i].keyword);
861 for (i = 0; i < RTE_DIM(auth_algos); i++) {
862 if (auth_algos[i].algo == sa->auth_algo) {
863 printf("%s ", auth_algos[i].keyword);
868 for (i = 0; i < RTE_DIM(aead_algos); i++) {
869 if (aead_algos[i].algo == sa->aead_algo &&
870 aead_algos[i].key_len-4 == sa->cipher_key_len) {
871 printf("%s ", aead_algos[i].keyword);
878 printf("UDP encapsulated ");
880 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
882 printf("IP4Tunnel ");
883 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
884 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
885 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
886 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
889 printf("IP6Tunnel ");
890 for (i = 0; i < 16; i++) {
891 if (i % 2 && i != 15)
892 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
894 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
897 for (i = 0; i < 16; i++) {
898 if (i % 2 && i != 15)
899 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
901 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
905 printf("Transport ");
909 ips = &sa->sessions[IPSEC_SESSION_PRIMARY];
912 case RTE_SECURITY_ACTION_TYPE_NONE:
913 printf("no-offload ");
915 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
916 printf("inline-crypto-offload ");
918 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
919 printf("inline-protocol-offload ");
921 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
922 printf("lookaside-protocol-offload ");
924 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
925 printf("cpu-crypto-accelerated ");
929 fallback_ips = &sa->sessions[IPSEC_SESSION_FALLBACK];
930 if (fallback_ips != NULL && sa->fallback_sessions > 0) {
931 printf("inline fallback: ");
932 switch (fallback_ips->type) {
933 case RTE_SECURITY_ACTION_TYPE_NONE:
934 printf("lookaside-none");
936 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
937 printf("cpu-crypto-accelerated");
944 if (sa->fdir_flag == 1)
945 printf("flow-direction port %d queue %d", sa->portid,
951 static struct sa_ctx *
952 sa_create(const char *name, int32_t socket_id, uint32_t nb_sa)
955 struct sa_ctx *sa_ctx;
957 const struct rte_memzone *mz;
959 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
961 /* Create SA context */
962 printf("Creating SA context with %u maximum entries on socket %d\n",
965 mz_size = sizeof(struct ipsec_xf) * nb_sa;
966 mz = rte_memzone_reserve(s, mz_size, socket_id,
967 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
969 printf("Failed to allocate SA XFORM memory\n");
974 sa_ctx = rte_zmalloc(NULL, sizeof(struct sa_ctx) +
975 sizeof(struct ipsec_sa) * nb_sa, RTE_CACHE_LINE_SIZE);
977 if (sa_ctx == NULL) {
978 printf("Failed to allocate SA CTX memory\n");
980 rte_memzone_free(mz);
984 sa_ctx->xf = (struct ipsec_xf *)mz->addr;
985 sa_ctx->nb_sa = nb_sa;
991 check_eth_dev_caps(uint16_t portid, uint32_t inbound, uint32_t tso)
993 struct rte_eth_dev_info dev_info;
996 retval = rte_eth_dev_info_get(portid, &dev_info);
999 "Error during getting device (port %u) info: %s\n",
1000 portid, strerror(-retval));
1006 if ((dev_info.rx_offload_capa &
1007 RTE_ETH_RX_OFFLOAD_SECURITY) == 0) {
1008 RTE_LOG(WARNING, PORT,
1009 "hardware RX IPSec offload is not supported\n");
1013 } else { /* outbound */
1014 if ((dev_info.tx_offload_capa &
1015 RTE_ETH_TX_OFFLOAD_SECURITY) == 0) {
1016 RTE_LOG(WARNING, PORT,
1017 "hardware TX IPSec offload is not supported\n");
1020 if (tso && (dev_info.tx_offload_capa &
1021 RTE_ETH_TX_OFFLOAD_TCP_TSO) == 0) {
1022 RTE_LOG(WARNING, PORT,
1023 "hardware TCP TSO offload is not supported\n");
1031 * Helper function, tries to determine next_proto for SPI
1032 * by searching though SP rules.
1035 get_spi_proto(uint32_t spi, enum rte_security_ipsec_sa_direction dir,
1036 struct ip_addr ip_addr[2], uint32_t mask[2])
1040 rc4 = sp4_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
1042 rc6 = sp6_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
1048 "%s: SPI %u used simultaeously by "
1049 "IPv4(%d) and IPv6 (%d) SP rules\n",
1050 __func__, spi, rc4, rc6);
1053 return IPPROTO_IPIP;
1054 } else if (rc6 < 0) {
1056 "%s: SPI %u is not used by any SP rule\n",
1060 return IPPROTO_IPV6;
1064 * Helper function for getting source and destination IP addresses
1065 * from SP. Needed for inline crypto transport mode, as addresses are not
1066 * provided in config file for that mode. It checks if SP for current SA exists,
1067 * and based on what type of protocol is returned, it stores appropriate
1068 * addresses got from SP into SA.
1071 sa_add_address_inline_crypto(struct ipsec_sa *sa)
1074 struct ip_addr ip_addr[2];
1077 protocol = get_spi_proto(sa->spi, sa->direction, ip_addr, mask);
1080 else if (protocol == IPPROTO_IPIP) {
1081 sa->flags |= IP4_TRANSPORT;
1082 if (mask[0] == IP4_FULL_MASK &&
1083 mask[1] == IP4_FULL_MASK &&
1084 ip_addr[0].ip.ip4 != 0 &&
1085 ip_addr[1].ip.ip4 != 0) {
1087 sa->src.ip.ip4 = ip_addr[0].ip.ip4;
1088 sa->dst.ip.ip4 = ip_addr[1].ip.ip4;
1091 "%s: No valid address or mask entry in"
1092 " IPv4 SP rule for SPI %u\n",
1096 } else if (protocol == IPPROTO_IPV6) {
1097 sa->flags |= IP6_TRANSPORT;
1098 if (mask[0] == IP6_FULL_MASK &&
1099 mask[1] == IP6_FULL_MASK &&
1100 (ip_addr[0].ip.ip6.ip6[0] != 0 ||
1101 ip_addr[0].ip.ip6.ip6[1] != 0) &&
1102 (ip_addr[1].ip.ip6.ip6[0] != 0 ||
1103 ip_addr[1].ip.ip6.ip6[1] != 0)) {
1105 sa->src.ip.ip6 = ip_addr[0].ip.ip6;
1106 sa->dst.ip.ip6 = ip_addr[1].ip.ip6;
1109 "%s: No valid address or mask entry in"
1110 " IPv6 SP rule for SPI %u\n",
1119 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1120 uint32_t nb_entries, uint32_t inbound,
1121 struct socket_ctx *skt_ctx)
1123 struct ipsec_sa *sa;
1125 uint16_t iv_length, aad_length;
1128 struct rte_ipsec_session *ips;
1130 /* for ESN upper 32 bits of SQN also need to be part of AAD */
1131 aad_length = (app_sa_prm.enable_esn != 0) ? sizeof(uint32_t) : 0;
1133 for (i = 0; i < nb_entries; i++) {
1135 sa = &sa_ctx->sa[idx];
1137 printf("Index %u already in use by SPI %u\n",
1144 rc = ipsec_sad_add(&sa_ctx->sad, sa);
1150 ips = ipsec_get_primary_session(sa);
1152 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1153 ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1154 if (check_eth_dev_caps(sa->portid, inbound, sa->mss))
1158 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1160 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
1161 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
1165 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1167 sa_add_address_inline_crypto(sa);
1168 if (inline_status < 0)
1169 return inline_status;
1174 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
1177 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1178 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
1179 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
1180 sa_ctx->xf[idx].a.aead.key.length =
1182 sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
1183 RTE_CRYPTO_AEAD_OP_DECRYPT :
1184 RTE_CRYPTO_AEAD_OP_ENCRYPT;
1185 sa_ctx->xf[idx].a.next = NULL;
1186 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
1187 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
1188 sa_ctx->xf[idx].a.aead.aad_length =
1189 sa->aad_len + aad_length;
1190 sa_ctx->xf[idx].a.aead.digest_length =
1193 sa->xforms = &sa_ctx->xf[idx].a;
1195 switch (sa->cipher_algo) {
1196 case RTE_CRYPTO_CIPHER_NULL:
1197 case RTE_CRYPTO_CIPHER_3DES_CBC:
1198 case RTE_CRYPTO_CIPHER_AES_CBC:
1199 iv_length = sa->iv_len;
1201 case RTE_CRYPTO_CIPHER_AES_CTR:
1205 RTE_LOG(ERR, IPSEC_ESP,
1206 "unsupported cipher algorithm %u\n",
1212 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1213 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
1214 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
1215 sa_ctx->xf[idx].b.cipher.key.length =
1217 sa_ctx->xf[idx].b.cipher.op =
1218 RTE_CRYPTO_CIPHER_OP_DECRYPT;
1219 sa_ctx->xf[idx].b.next = NULL;
1220 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
1221 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
1223 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1224 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
1225 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
1226 sa_ctx->xf[idx].a.auth.key.length =
1228 sa_ctx->xf[idx].a.auth.digest_length =
1230 sa_ctx->xf[idx].a.auth.op =
1231 RTE_CRYPTO_AUTH_OP_VERIFY;
1232 } else { /* outbound */
1233 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1234 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
1235 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
1236 sa_ctx->xf[idx].a.cipher.key.length =
1238 sa_ctx->xf[idx].a.cipher.op =
1239 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1240 sa_ctx->xf[idx].a.next = NULL;
1241 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
1242 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
1244 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1245 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
1246 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
1247 sa_ctx->xf[idx].b.auth.key.length =
1249 sa_ctx->xf[idx].b.auth.digest_length =
1251 sa_ctx->xf[idx].b.auth.op =
1252 RTE_CRYPTO_AUTH_OP_GENERATE;
1255 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
1256 sa_ctx->xf[idx].b.next = NULL;
1257 sa->xforms = &sa_ctx->xf[idx].a;
1261 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1263 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1264 rc = create_inline_session(skt_ctx, sa, ips);
1266 RTE_LOG(ERR, IPSEC_ESP,
1267 "create_inline_session() failed\n");
1272 if (sa->fdir_flag && inbound) {
1273 rc = create_ipsec_esp_flow(sa);
1275 RTE_LOG(ERR, IPSEC_ESP,
1276 "create_ipsec_esp_flow() failed\n");
1278 print_one_sa_rule(sa, inbound);
1285 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1286 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1288 return sa_add_rules(sa_ctx, entries, nb_entries, 0, skt_ctx);
1292 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1293 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1295 return sa_add_rules(sa_ctx, entries, nb_entries, 1, skt_ctx);
1299 * helper function, fills parameters that are identical for all SAs
1302 fill_ipsec_app_sa_prm(struct rte_ipsec_sa_prm *prm,
1303 const struct app_sa_prm *app_prm)
1305 memset(prm, 0, sizeof(*prm));
1307 prm->flags = app_prm->flags;
1308 prm->ipsec_xform.options.esn = app_prm->enable_esn;
1309 prm->ipsec_xform.replay_win_sz = app_prm->window_size;
1313 fill_ipsec_sa_prm(struct rte_ipsec_sa_prm *prm, const struct ipsec_sa *ss,
1314 const struct rte_ipv4_hdr *v4, struct rte_ipv6_hdr *v6)
1319 * Try to get SPI next proto by searching that SPI in SPD.
1320 * probably not the optimal way, but there seems nothing
1323 rc = get_spi_proto(ss->spi, ss->direction, NULL, NULL);
1327 fill_ipsec_app_sa_prm(prm, &app_sa_prm);
1328 prm->userdata = (uintptr_t)ss;
1330 /* setup ipsec xform */
1331 prm->ipsec_xform.spi = ss->spi;
1332 prm->ipsec_xform.salt = ss->salt;
1333 prm->ipsec_xform.direction = ss->direction;
1334 prm->ipsec_xform.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP;
1335 prm->ipsec_xform.mode = (IS_TRANSPORT(ss->flags)) ?
1336 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT :
1337 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
1338 prm->ipsec_xform.options.udp_encap = ss->udp_encap;
1339 prm->ipsec_xform.options.ecn = 1;
1340 prm->ipsec_xform.options.copy_dscp = 1;
1342 if (IS_IP4_TUNNEL(ss->flags)) {
1343 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV4;
1344 prm->tun.hdr_len = sizeof(*v4);
1345 prm->tun.next_proto = rc;
1347 } else if (IS_IP6_TUNNEL(ss->flags)) {
1348 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV6;
1349 prm->tun.hdr_len = sizeof(*v6);
1350 prm->tun.next_proto = rc;
1353 /* transport mode */
1354 prm->trs.proto = rc;
1357 /* setup crypto section */
1358 prm->crypto_xform = ss->xforms;
1363 fill_ipsec_session(struct rte_ipsec_session *ss, struct rte_ipsec_sa *sa)
1369 if (ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1370 ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
1371 if (ss->security.ses != NULL) {
1372 rc = rte_ipsec_session_prepare(ss);
1374 memset(ss, 0, sizeof(*ss));
1382 * Initialise related rte_ipsec_sa object.
1385 ipsec_sa_init(struct ipsec_sa *lsa, struct rte_ipsec_sa *sa, uint32_t sa_size)
1388 struct rte_ipsec_sa_prm prm;
1389 struct rte_ipsec_session *ips;
1390 struct rte_ipv4_hdr v4 = {
1391 .version_ihl = IPVERSION << 4 |
1392 sizeof(v4) / RTE_IPV4_IHL_MULTIPLIER,
1393 .time_to_live = IPDEFTTL,
1394 .next_proto_id = IPPROTO_ESP,
1395 .src_addr = lsa->src.ip.ip4,
1396 .dst_addr = lsa->dst.ip.ip4,
1398 struct rte_ipv6_hdr v6 = {
1399 .vtc_flow = htonl(IP6_VERSION << 28),
1400 .proto = IPPROTO_ESP,
1403 if (IS_IP6_TUNNEL(lsa->flags)) {
1404 memcpy(v6.src_addr, lsa->src.ip.ip6.ip6_b, sizeof(v6.src_addr));
1405 memcpy(v6.dst_addr, lsa->dst.ip.ip6.ip6_b, sizeof(v6.dst_addr));
1408 rc = fill_ipsec_sa_prm(&prm, lsa, &v4, &v6);
1410 rc = rte_ipsec_sa_init(sa, &prm, sa_size);
1414 /* init primary processing session */
1415 ips = ipsec_get_primary_session(lsa);
1416 rc = fill_ipsec_session(ips, sa);
1420 /* init inline fallback processing session */
1421 if (lsa->fallback_sessions == 1)
1422 rc = fill_ipsec_session(ipsec_get_fallback_session(lsa), sa);
1428 * Allocate space and init rte_ipsec_sa strcutures,
1432 ipsec_satbl_init(struct sa_ctx *ctx, uint32_t nb_ent, int32_t socket)
1437 struct rte_ipsec_sa *sa;
1438 struct ipsec_sa *lsa;
1439 struct rte_ipsec_sa_prm prm;
1441 /* determine SA size */
1443 fill_ipsec_sa_prm(&prm, ctx->sa + idx, NULL, NULL);
1444 sz = rte_ipsec_sa_size(&prm);
1446 RTE_LOG(ERR, IPSEC, "%s(%p, %u, %d): "
1447 "failed to determine SA size, error code: %d\n",
1448 __func__, ctx, nb_ent, socket, sz);
1454 ctx->satbl = rte_zmalloc_socket(NULL, tsz, RTE_CACHE_LINE_SIZE, socket);
1455 if (ctx->satbl == NULL) {
1457 "%s(%p, %u, %d): failed to allocate %zu bytes\n",
1458 __func__, ctx, nb_ent, socket, tsz);
1463 for (i = 0; i != nb_ent && rc == 0; i++) {
1467 sa = (struct rte_ipsec_sa *)((uintptr_t)ctx->satbl + sz * i);
1468 lsa = ctx->sa + idx;
1470 rc = ipsec_sa_init(lsa, sa, sz);
1477 sa_cmp(const void *p, const void *q)
1479 uint32_t spi1 = ((const struct ipsec_sa *)p)->spi;
1480 uint32_t spi2 = ((const struct ipsec_sa *)q)->spi;
1482 return (int)(spi1 - spi2);
1486 * Walk through all SA rules to find an SA with given SPI
1489 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound)
1492 struct ipsec_sa *sa;
1493 struct ipsec_sa tmpl;
1494 const struct ipsec_sa *sar;
1504 sa = bsearch(&tmpl, sar, num, sizeof(struct ipsec_sa), sa_cmp);
1506 return RTE_PTR_DIFF(sa, sar) / sizeof(struct ipsec_sa);
1512 sa_init(struct socket_ctx *ctx, int32_t socket_id)
1518 rte_exit(EXIT_FAILURE, "NULL context.\n");
1520 if (ctx->sa_in != NULL)
1521 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
1522 "initialized\n", socket_id);
1524 if (ctx->sa_out != NULL)
1525 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
1526 "initialized\n", socket_id);
1530 ctx->sa_in = sa_create(name, socket_id, nb_sa_in);
1531 if (ctx->sa_in == NULL)
1532 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1533 "context %s in socket %d\n", rte_errno,
1536 rc = ipsec_sad_create(name, &ctx->sa_in->sad, socket_id,
1539 rte_exit(EXIT_FAILURE, "failed to init SAD\n");
1541 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in, ctx);
1543 if (app_sa_prm.enable != 0) {
1544 rc = ipsec_satbl_init(ctx->sa_in, nb_sa_in,
1547 rte_exit(EXIT_FAILURE,
1548 "failed to init inbound SAs\n");
1551 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
1553 if (nb_sa_out > 0) {
1555 ctx->sa_out = sa_create(name, socket_id, nb_sa_out);
1556 if (ctx->sa_out == NULL)
1557 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1558 "context %s in socket %d\n", rte_errno,
1561 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out, ctx);
1563 if (app_sa_prm.enable != 0) {
1564 rc = ipsec_satbl_init(ctx->sa_out, nb_sa_out,
1567 rte_exit(EXIT_FAILURE,
1568 "failed to init outbound SAs\n");
1571 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
1576 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
1578 struct ipsec_mbuf_metadata *priv;
1579 struct ipsec_sa *sa;
1584 return (sa_ctx->sa[sa_idx].spi == sa->spi);
1586 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
1591 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
1592 void *sa_arr[], uint16_t nb_pkts)
1596 struct ipsec_sa *sa;
1598 sad_lookup(&sa_ctx->sad, pkts, sa_arr, nb_pkts);
1601 * Mark need for inline offload fallback on the LSB of SA pointer.
1602 * Thanks to packet grouping mechanism which ipsec_process is using
1603 * packets marked for fallback processing will form separate group.
1605 * Because it is not safe to use SA pointer it is casted to generic
1606 * pointer to prevent from unintentional use. Use ipsec_mask_saptr
1607 * to get valid struct pointer.
1609 for (i = 0; i < nb_pkts; i++) {
1610 if (sa_arr[i] == NULL)
1613 result_sa = sa = sa_arr[i];
1614 if (MBUF_NO_SEC_OFFLOAD(pkts[i]) &&
1615 sa->fallback_sessions > 0) {
1616 uintptr_t intsa = (uintptr_t)sa;
1617 intsa |= IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
1618 result_sa = (void *)intsa;
1620 sa_arr[i] = result_sa;
1625 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
1626 void *sa[], uint16_t nb_pkts)
1630 for (i = 0; i < nb_pkts; i++)
1631 sa[i] = &sa_ctx->sa[sa_idx[i]];
1635 * Select HW offloads to be used.
1638 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
1639 uint64_t *tx_offloads)
1641 struct ipsec_sa *rule;
1643 enum rte_security_session_action_type rule_type;
1648 /* Check for inbound rules that use offloads and use this port */
1649 for (idx_sa = 0; idx_sa < nb_sa_in; idx_sa++) {
1650 rule = &sa_in[idx_sa];
1651 rule_type = ipsec_get_action_type(rule);
1652 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1654 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1655 && rule->portid == port_id)
1656 *rx_offloads |= RTE_ETH_RX_OFFLOAD_SECURITY;
1659 /* Check for outbound rules that use offloads and use this port */
1660 for (idx_sa = 0; idx_sa < nb_sa_out; idx_sa++) {
1661 rule = &sa_out[idx_sa];
1662 rule_type = ipsec_get_action_type(rule);
1663 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1665 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1666 && rule->portid == port_id) {
1667 *tx_offloads |= RTE_ETH_TX_OFFLOAD_SECURITY;
1669 *tx_offloads |= RTE_ETH_TX_OFFLOAD_TCP_TSO;
1678 qsort(sa_in, nb_sa_in, sizeof(struct ipsec_sa), sa_cmp);
1679 qsort(sa_out, nb_sa_out, sizeof(struct ipsec_sa), sa_cmp);
1683 get_nb_crypto_sessions(void)
1685 return nb_crypto_sessions;