4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Security Associations
37 #include <sys/types.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip6.h>
42 #include <rte_memzone.h>
43 #include <rte_crypto.h>
44 #include <rte_security.h>
45 #include <rte_cryptodev.h>
46 #include <rte_byteorder.h>
47 #include <rte_errno.h>
49 #include <rte_random.h>
50 #include <rte_ethdev.h>
58 struct supported_cipher_algo {
60 enum rte_crypto_cipher_algorithm algo;
66 struct supported_auth_algo {
68 enum rte_crypto_auth_algorithm algo;
74 struct supported_aead_algo {
76 enum rte_crypto_aead_algorithm algo;
85 const struct supported_cipher_algo cipher_algos[] = {
88 .algo = RTE_CRYPTO_CIPHER_NULL,
94 .keyword = "aes-128-cbc",
95 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
101 .keyword = "aes-128-ctr",
102 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
104 .block_size = 16, /* XXX AESNI MB limition, should be 4 */
109 const struct supported_auth_algo auth_algos[] = {
112 .algo = RTE_CRYPTO_AUTH_NULL,
118 .keyword = "sha1-hmac",
119 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
124 .keyword = "sha256-hmac",
125 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
131 const struct supported_aead_algo aead_algos[] = {
133 .keyword = "aes-128-gcm",
134 .algo = RTE_CRYPTO_AEAD_AES_GCM,
143 struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
146 struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
149 static const struct supported_cipher_algo *
150 find_match_cipher_algo(const char *cipher_keyword)
154 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
155 const struct supported_cipher_algo *algo =
158 if (strcmp(cipher_keyword, algo->keyword) == 0)
165 static const struct supported_auth_algo *
166 find_match_auth_algo(const char *auth_keyword)
170 for (i = 0; i < RTE_DIM(auth_algos); i++) {
171 const struct supported_auth_algo *algo =
174 if (strcmp(auth_keyword, algo->keyword) == 0)
181 static const struct supported_aead_algo *
182 find_match_aead_algo(const char *aead_keyword)
186 for (i = 0; i < RTE_DIM(aead_algos); i++) {
187 const struct supported_aead_algo *algo =
190 if (strcmp(aead_keyword, algo->keyword) == 0)
198 * parse x:x:x:x.... hex number key string into uint8_t *key
200 * > 0: number of bytes parsed
204 parse_key_string(const char *key_str, uint8_t *key)
206 const char *pt_start = key_str, *pt_end = key_str;
207 uint32_t nb_bytes = 0;
209 while (pt_end != NULL) {
210 char sub_str[3] = {0};
212 pt_end = strchr(pt_start, ':');
214 if (pt_end == NULL) {
215 if (strlen(pt_start) > 2)
217 strncpy(sub_str, pt_start, 2);
219 if (pt_end - pt_start > 2)
222 strncpy(sub_str, pt_start, pt_end - pt_start);
223 pt_start = pt_end + 1;
226 key[nb_bytes++] = strtol(sub_str, NULL, 16);
233 parse_sa_tokens(char **tokens, uint32_t n_tokens,
234 struct parse_status *status)
236 struct ipsec_sa *rule = NULL;
237 uint32_t ti; /*token index*/
238 uint32_t *ri /*rule index*/;
239 uint32_t cipher_algo_p = 0;
240 uint32_t auth_algo_p = 0;
241 uint32_t aead_algo_p = 0;
246 uint32_t portid_p = 0;
248 if (strcmp(tokens[0], "in") == 0) {
251 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
252 "too many sa rules, abort insertion\n");
253 if (status->status < 0)
260 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
261 "too many sa rules, abort insertion\n");
262 if (status->status < 0)
269 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
270 if (status->status < 0)
272 if (atoi(tokens[1]) == INVALID_SPI)
274 rule->spi = atoi(tokens[1]);
276 for (ti = 2; ti < n_tokens; ti++) {
277 if (strcmp(tokens[ti], "mode") == 0) {
278 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
279 if (status->status < 0)
282 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
283 if (status->status < 0)
286 if (strcmp(tokens[ti], "ipv4-tunnel") == 0)
287 rule->flags = IP4_TUNNEL;
288 else if (strcmp(tokens[ti], "ipv6-tunnel") == 0)
289 rule->flags = IP6_TUNNEL;
290 else if (strcmp(tokens[ti], "transport") == 0)
291 rule->flags = TRANSPORT;
293 APP_CHECK(0, status, "unrecognized "
294 "input \"%s\"", tokens[ti]);
302 if (strcmp(tokens[ti], "cipher_algo") == 0) {
303 const struct supported_cipher_algo *algo;
306 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
308 if (status->status < 0)
311 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
312 if (status->status < 0)
315 algo = find_match_cipher_algo(tokens[ti]);
317 APP_CHECK(algo != NULL, status, "unrecognized "
318 "input \"%s\"", tokens[ti]);
320 rule->cipher_algo = algo->algo;
321 rule->block_size = algo->block_size;
322 rule->iv_len = algo->iv_len;
323 rule->cipher_key_len = algo->key_len;
325 /* for NULL algorithm, no cipher key required */
326 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
331 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
332 if (status->status < 0)
335 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
336 status, "unrecognized input \"%s\", "
337 "expect \"cipher_key\"", tokens[ti]);
338 if (status->status < 0)
341 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
342 if (status->status < 0)
345 key_len = parse_key_string(tokens[ti],
347 APP_CHECK(key_len == rule->cipher_key_len, status,
348 "unrecognized input \"%s\"", tokens[ti]);
349 if (status->status < 0)
352 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC)
353 rule->salt = (uint32_t)rte_rand();
355 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
357 rule->cipher_key_len = key_len;
359 &rule->cipher_key[key_len], 4);
366 if (strcmp(tokens[ti], "auth_algo") == 0) {
367 const struct supported_auth_algo *algo;
370 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
372 if (status->status < 0)
375 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
376 if (status->status < 0)
379 algo = find_match_auth_algo(tokens[ti]);
380 APP_CHECK(algo != NULL, status, "unrecognized "
381 "input \"%s\"", tokens[ti]);
383 rule->auth_algo = algo->algo;
384 rule->auth_key_len = algo->key_len;
385 rule->digest_len = algo->digest_len;
387 /* NULL algorithm and combined algos do not
390 if (algo->key_not_req) {
395 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
396 if (status->status < 0)
399 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
400 status, "unrecognized input \"%s\", "
401 "expect \"auth_key\"", tokens[ti]);
402 if (status->status < 0)
405 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
406 if (status->status < 0)
409 key_len = parse_key_string(tokens[ti],
411 APP_CHECK(key_len == rule->auth_key_len, status,
412 "unrecognized input \"%s\"", tokens[ti]);
413 if (status->status < 0)
420 if (strcmp(tokens[ti], "aead_algo") == 0) {
421 const struct supported_aead_algo *algo;
424 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
426 if (status->status < 0)
429 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
430 if (status->status < 0)
433 algo = find_match_aead_algo(tokens[ti]);
435 APP_CHECK(algo != NULL, status, "unrecognized "
436 "input \"%s\"", tokens[ti]);
438 rule->aead_algo = algo->algo;
439 rule->cipher_key_len = algo->key_len;
440 rule->digest_len = algo->digest_len;
441 rule->aad_len = algo->aad_len;
442 rule->block_size = algo->block_size;
443 rule->iv_len = algo->iv_len;
445 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
446 if (status->status < 0)
449 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
450 status, "unrecognized input \"%s\", "
451 "expect \"aead_key\"", tokens[ti]);
452 if (status->status < 0)
455 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
456 if (status->status < 0)
459 key_len = parse_key_string(tokens[ti],
461 APP_CHECK(key_len == rule->cipher_key_len, status,
462 "unrecognized input \"%s\"", tokens[ti]);
463 if (status->status < 0)
467 rule->cipher_key_len = key_len;
469 &rule->cipher_key[key_len], 4);
475 if (strcmp(tokens[ti], "src") == 0) {
476 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
477 if (status->status < 0)
480 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
481 if (status->status < 0)
484 if (rule->flags == IP4_TUNNEL) {
487 APP_CHECK(parse_ipv4_addr(tokens[ti],
488 &ip, NULL) == 0, status,
489 "unrecognized input \"%s\", "
490 "expect valid ipv4 addr",
492 if (status->status < 0)
494 rule->src.ip.ip4 = rte_bswap32(
495 (uint32_t)ip.s_addr);
496 } else if (rule->flags == IP6_TUNNEL) {
499 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
501 "unrecognized input \"%s\", "
502 "expect valid ipv6 addr",
504 if (status->status < 0)
506 memcpy(rule->src.ip.ip6.ip6_b,
508 } else if (rule->flags == TRANSPORT) {
509 APP_CHECK(0, status, "unrecognized input "
510 "\"%s\"", tokens[ti]);
518 if (strcmp(tokens[ti], "dst") == 0) {
519 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
520 if (status->status < 0)
523 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
524 if (status->status < 0)
527 if (rule->flags == IP4_TUNNEL) {
530 APP_CHECK(parse_ipv4_addr(tokens[ti],
531 &ip, NULL) == 0, status,
532 "unrecognized input \"%s\", "
533 "expect valid ipv4 addr",
535 if (status->status < 0)
537 rule->dst.ip.ip4 = rte_bswap32(
538 (uint32_t)ip.s_addr);
539 } else if (rule->flags == IP6_TUNNEL) {
542 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
544 "unrecognized input \"%s\", "
545 "expect valid ipv6 addr",
547 if (status->status < 0)
549 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
550 } else if (rule->flags == TRANSPORT) {
551 APP_CHECK(0, status, "unrecognized "
552 "input \"%s\"", tokens[ti]);
560 if (strcmp(tokens[ti], "type") == 0) {
561 APP_CHECK_PRESENCE(type_p, tokens[ti], status);
562 if (status->status < 0)
565 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
566 if (status->status < 0)
569 if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
571 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
572 else if (strcmp(tokens[ti],
573 "inline-protocol-offload") == 0)
575 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
576 else if (strcmp(tokens[ti],
577 "lookaside-protocol-offload") == 0)
579 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
580 else if (strcmp(tokens[ti], "no-offload") == 0)
581 rule->type = RTE_SECURITY_ACTION_TYPE_NONE;
583 APP_CHECK(0, status, "Invalid input \"%s\"",
592 if (strcmp(tokens[ti], "port_id") == 0) {
593 APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
594 if (status->status < 0)
596 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
597 if (status->status < 0)
599 rule->portid = atoi(tokens[ti]);
600 if (status->status < 0)
606 /* unrecognizeable input */
607 APP_CHECK(0, status, "unrecognized input \"%s\"",
613 APP_CHECK(cipher_algo_p == 0, status,
614 "AEAD used, no need for cipher options");
615 if (status->status < 0)
618 APP_CHECK(auth_algo_p == 0, status,
619 "AEAD used, no need for auth options");
620 if (status->status < 0)
623 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
624 if (status->status < 0)
627 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
628 if (status->status < 0)
632 APP_CHECK(mode_p == 1, status, "missing mode option");
633 if (status->status < 0)
636 if ((rule->type != RTE_SECURITY_ACTION_TYPE_NONE) && (portid_p == 0))
637 printf("Missing portid option, falling back to non-offload\n");
639 if (!type_p || !portid_p) {
640 rule->type = RTE_SECURITY_ACTION_TYPE_NONE;
648 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
653 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
655 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
656 if (cipher_algos[i].algo == sa->cipher_algo) {
657 printf("%s ", cipher_algos[i].keyword);
662 for (i = 0; i < RTE_DIM(auth_algos); i++) {
663 if (auth_algos[i].algo == sa->auth_algo) {
664 printf("%s ", auth_algos[i].keyword);
669 for (i = 0; i < RTE_DIM(aead_algos); i++) {
670 if (aead_algos[i].algo == sa->aead_algo) {
671 printf("%s ", aead_algos[i].keyword);
680 printf("IP4Tunnel ");
681 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
682 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
683 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
684 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
687 printf("IP6Tunnel ");
688 for (i = 0; i < 16; i++) {
689 if (i % 2 && i != 15)
690 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
692 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
695 for (i = 0; i < 16; i++) {
696 if (i % 2 && i != 15)
697 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
699 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
710 struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
713 struct rte_crypto_sym_xform a;
714 struct rte_crypto_sym_xform b;
716 } xf[IPSEC_SA_MAX_ENTRIES];
719 static struct sa_ctx *
720 sa_create(const char *name, int32_t socket_id)
723 struct sa_ctx *sa_ctx;
725 const struct rte_memzone *mz;
727 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
729 /* Create SA array table */
730 printf("Creating SA context with %u maximum entries\n",
731 IPSEC_SA_MAX_ENTRIES);
733 mz_size = sizeof(struct sa_ctx);
734 mz = rte_memzone_reserve(s, mz_size, socket_id,
735 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
737 printf("Failed to allocate SA DB memory\n");
742 sa_ctx = (struct sa_ctx *)mz->addr;
748 check_eth_dev_caps(uint16_t portid, uint32_t inbound)
750 struct rte_eth_dev_info dev_info;
752 rte_eth_dev_info_get(portid, &dev_info);
755 if ((dev_info.rx_offload_capa &
756 DEV_RX_OFFLOAD_SECURITY) == 0) {
757 RTE_LOG(WARNING, PORT,
758 "hardware RX IPSec offload is not supported\n");
762 } else { /* outbound */
763 if ((dev_info.tx_offload_capa &
764 DEV_TX_OFFLOAD_SECURITY) == 0) {
765 RTE_LOG(WARNING, PORT,
766 "hardware TX IPSec offload is not supported\n");
775 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
776 uint32_t nb_entries, uint32_t inbound)
782 for (i = 0; i < nb_entries; i++) {
783 idx = SPI2IDX(entries[i].spi);
784 sa = &sa_ctx->sa[idx];
786 printf("Index %u already in use by SPI %u\n",
793 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
794 sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
795 if (check_eth_dev_caps(sa->portid, inbound))
799 sa->direction = (inbound == 1) ?
800 RTE_SECURITY_IPSEC_SA_DIR_INGRESS :
801 RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
805 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
806 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
809 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
812 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
813 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
814 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
815 sa_ctx->xf[idx].a.aead.key.length =
817 sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
818 RTE_CRYPTO_AEAD_OP_DECRYPT :
819 RTE_CRYPTO_AEAD_OP_ENCRYPT;
820 sa_ctx->xf[idx].a.next = NULL;
821 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
822 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
823 sa_ctx->xf[idx].a.aead.aad_length =
825 sa_ctx->xf[idx].a.aead.digest_length =
828 sa->xforms = &sa_ctx->xf[idx].a;
830 print_one_sa_rule(sa, inbound);
832 switch (sa->cipher_algo) {
833 case RTE_CRYPTO_CIPHER_NULL:
834 case RTE_CRYPTO_CIPHER_AES_CBC:
835 iv_length = sa->iv_len;
837 case RTE_CRYPTO_CIPHER_AES_CTR:
841 RTE_LOG(ERR, IPSEC_ESP,
842 "unsupported cipher algorithm %u\n",
848 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
849 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
850 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
851 sa_ctx->xf[idx].b.cipher.key.length =
853 sa_ctx->xf[idx].b.cipher.op =
854 RTE_CRYPTO_CIPHER_OP_DECRYPT;
855 sa_ctx->xf[idx].b.next = NULL;
856 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
857 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
859 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
860 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
861 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
862 sa_ctx->xf[idx].a.auth.key.length =
864 sa_ctx->xf[idx].a.auth.digest_length =
866 sa_ctx->xf[idx].a.auth.op =
867 RTE_CRYPTO_AUTH_OP_VERIFY;
868 } else { /* outbound */
869 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
870 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
871 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
872 sa_ctx->xf[idx].a.cipher.key.length =
874 sa_ctx->xf[idx].a.cipher.op =
875 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
876 sa_ctx->xf[idx].a.next = NULL;
877 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
878 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
880 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
881 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
882 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
883 sa_ctx->xf[idx].b.auth.key.length =
885 sa_ctx->xf[idx].b.auth.digest_length =
887 sa_ctx->xf[idx].b.auth.op =
888 RTE_CRYPTO_AUTH_OP_GENERATE;
891 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
892 sa_ctx->xf[idx].b.next = NULL;
893 sa->xforms = &sa_ctx->xf[idx].a;
895 print_one_sa_rule(sa, inbound);
903 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
906 return sa_add_rules(sa_ctx, entries, nb_entries, 0);
910 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
913 return sa_add_rules(sa_ctx, entries, nb_entries, 1);
917 sa_init(struct socket_ctx *ctx, int32_t socket_id)
922 rte_exit(EXIT_FAILURE, "NULL context.\n");
924 if (ctx->sa_in != NULL)
925 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
926 "initialized\n", socket_id);
928 if (ctx->sa_out != NULL)
929 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
930 "initialized\n", socket_id);
934 ctx->sa_in = sa_create(name, socket_id);
935 if (ctx->sa_in == NULL)
936 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
937 "context %s in socket %d\n", rte_errno,
940 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
942 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
946 ctx->sa_out = sa_create(name, socket_id);
947 if (ctx->sa_out == NULL)
948 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
949 "context %s in socket %d\n", rte_errno,
952 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
954 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
959 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
961 struct ipsec_mbuf_metadata *priv;
963 priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
965 return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
969 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
970 struct ipsec_sa **sa_ret)
980 ip = rte_pktmbuf_mtod(pkt, struct ip *);
981 if (ip->ip_v == IPVERSION)
982 esp = (struct esp_hdr *)(ip + 1);
984 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
986 if (esp->spi == INVALID_SPI)
989 sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
990 if (rte_be_to_cpu_32(esp->spi) != sa->spi)
995 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
996 if ((ip->ip_v == IPVERSION) &&
997 (sa->src.ip.ip4 == *src4_addr) &&
998 (sa->dst.ip.ip4 == *(src4_addr + 1)))
1002 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
1003 if ((ip->ip_v == IP6_VERSION) &&
1004 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
1005 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
1014 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
1015 struct ipsec_sa *sa[], uint16_t nb_pkts)
1019 for (i = 0; i < nb_pkts; i++)
1020 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
1024 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
1025 struct ipsec_sa *sa[], uint16_t nb_pkts)
1029 for (i = 0; i < nb_pkts; i++)
1030 sa[i] = &sa_ctx->sa[sa_idx[i]];