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>
29 struct supported_cipher_algo {
31 enum rte_crypto_cipher_algorithm algo;
37 struct supported_auth_algo {
39 enum rte_crypto_auth_algorithm algo;
45 struct supported_aead_algo {
47 enum rte_crypto_aead_algorithm algo;
56 const struct supported_cipher_algo cipher_algos[] = {
59 .algo = RTE_CRYPTO_CIPHER_NULL,
65 .keyword = "aes-128-cbc",
66 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
72 .keyword = "aes-128-ctr",
73 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
75 .block_size = 16, /* XXX AESNI MB limition, should be 4 */
80 const struct supported_auth_algo auth_algos[] = {
83 .algo = RTE_CRYPTO_AUTH_NULL,
89 .keyword = "sha1-hmac",
90 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
95 .keyword = "sha256-hmac",
96 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
102 const struct supported_aead_algo aead_algos[] = {
104 .keyword = "aes-128-gcm",
105 .algo = RTE_CRYPTO_AEAD_AES_GCM,
114 struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
117 struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
120 static const struct supported_cipher_algo *
121 find_match_cipher_algo(const char *cipher_keyword)
125 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
126 const struct supported_cipher_algo *algo =
129 if (strcmp(cipher_keyword, algo->keyword) == 0)
136 static const struct supported_auth_algo *
137 find_match_auth_algo(const char *auth_keyword)
141 for (i = 0; i < RTE_DIM(auth_algos); i++) {
142 const struct supported_auth_algo *algo =
145 if (strcmp(auth_keyword, algo->keyword) == 0)
152 static const struct supported_aead_algo *
153 find_match_aead_algo(const char *aead_keyword)
157 for (i = 0; i < RTE_DIM(aead_algos); i++) {
158 const struct supported_aead_algo *algo =
161 if (strcmp(aead_keyword, algo->keyword) == 0)
169 * parse x:x:x:x.... hex number key string into uint8_t *key
171 * > 0: number of bytes parsed
175 parse_key_string(const char *key_str, uint8_t *key)
177 const char *pt_start = key_str, *pt_end = key_str;
178 uint32_t nb_bytes = 0;
180 while (pt_end != NULL) {
181 char sub_str[3] = {0};
183 pt_end = strchr(pt_start, ':');
185 if (pt_end == NULL) {
186 if (strlen(pt_start) > 2)
188 strncpy(sub_str, pt_start, 2);
190 if (pt_end - pt_start > 2)
193 strncpy(sub_str, pt_start, pt_end - pt_start);
194 pt_start = pt_end + 1;
197 key[nb_bytes++] = strtol(sub_str, NULL, 16);
204 parse_sa_tokens(char **tokens, uint32_t n_tokens,
205 struct parse_status *status)
207 struct ipsec_sa *rule = NULL;
208 uint32_t ti; /*token index*/
209 uint32_t *ri /*rule index*/;
210 uint32_t cipher_algo_p = 0;
211 uint32_t auth_algo_p = 0;
212 uint32_t aead_algo_p = 0;
217 uint32_t portid_p = 0;
219 if (strcmp(tokens[0], "in") == 0) {
222 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
223 "too many sa rules, abort insertion\n");
224 if (status->status < 0)
231 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
232 "too many sa rules, abort insertion\n");
233 if (status->status < 0)
240 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
241 if (status->status < 0)
243 rule->spi = atoi(tokens[1]);
245 for (ti = 2; ti < n_tokens; ti++) {
246 if (strcmp(tokens[ti], "mode") == 0) {
247 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
248 if (status->status < 0)
251 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
252 if (status->status < 0)
255 if (strcmp(tokens[ti], "ipv4-tunnel") == 0)
256 rule->flags = IP4_TUNNEL;
257 else if (strcmp(tokens[ti], "ipv6-tunnel") == 0)
258 rule->flags = IP6_TUNNEL;
259 else if (strcmp(tokens[ti], "transport") == 0)
260 rule->flags = TRANSPORT;
262 APP_CHECK(0, status, "unrecognized "
263 "input \"%s\"", tokens[ti]);
271 if (strcmp(tokens[ti], "cipher_algo") == 0) {
272 const struct supported_cipher_algo *algo;
275 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
277 if (status->status < 0)
280 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
281 if (status->status < 0)
284 algo = find_match_cipher_algo(tokens[ti]);
286 APP_CHECK(algo != NULL, status, "unrecognized "
287 "input \"%s\"", tokens[ti]);
289 rule->cipher_algo = algo->algo;
290 rule->block_size = algo->block_size;
291 rule->iv_len = algo->iv_len;
292 rule->cipher_key_len = algo->key_len;
294 /* for NULL algorithm, no cipher key required */
295 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
300 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
301 if (status->status < 0)
304 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
305 status, "unrecognized input \"%s\", "
306 "expect \"cipher_key\"", tokens[ti]);
307 if (status->status < 0)
310 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
311 if (status->status < 0)
314 key_len = parse_key_string(tokens[ti],
316 APP_CHECK(key_len == rule->cipher_key_len, status,
317 "unrecognized input \"%s\"", tokens[ti]);
318 if (status->status < 0)
321 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC)
322 rule->salt = (uint32_t)rte_rand();
324 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
326 rule->cipher_key_len = key_len;
328 &rule->cipher_key[key_len], 4);
335 if (strcmp(tokens[ti], "auth_algo") == 0) {
336 const struct supported_auth_algo *algo;
339 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
341 if (status->status < 0)
344 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
345 if (status->status < 0)
348 algo = find_match_auth_algo(tokens[ti]);
349 APP_CHECK(algo != NULL, status, "unrecognized "
350 "input \"%s\"", tokens[ti]);
352 rule->auth_algo = algo->algo;
353 rule->auth_key_len = algo->key_len;
354 rule->digest_len = algo->digest_len;
356 /* NULL algorithm and combined algos do not
359 if (algo->key_not_req) {
364 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
365 if (status->status < 0)
368 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
369 status, "unrecognized input \"%s\", "
370 "expect \"auth_key\"", tokens[ti]);
371 if (status->status < 0)
374 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
375 if (status->status < 0)
378 key_len = parse_key_string(tokens[ti],
380 APP_CHECK(key_len == rule->auth_key_len, status,
381 "unrecognized input \"%s\"", tokens[ti]);
382 if (status->status < 0)
389 if (strcmp(tokens[ti], "aead_algo") == 0) {
390 const struct supported_aead_algo *algo;
393 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
395 if (status->status < 0)
398 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
399 if (status->status < 0)
402 algo = find_match_aead_algo(tokens[ti]);
404 APP_CHECK(algo != NULL, status, "unrecognized "
405 "input \"%s\"", tokens[ti]);
407 rule->aead_algo = algo->algo;
408 rule->cipher_key_len = algo->key_len;
409 rule->digest_len = algo->digest_len;
410 rule->aad_len = algo->aad_len;
411 rule->block_size = algo->block_size;
412 rule->iv_len = algo->iv_len;
414 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
415 if (status->status < 0)
418 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
419 status, "unrecognized input \"%s\", "
420 "expect \"aead_key\"", tokens[ti]);
421 if (status->status < 0)
424 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
425 if (status->status < 0)
428 key_len = parse_key_string(tokens[ti],
430 APP_CHECK(key_len == rule->cipher_key_len, status,
431 "unrecognized input \"%s\"", tokens[ti]);
432 if (status->status < 0)
436 rule->cipher_key_len = key_len;
438 &rule->cipher_key[key_len], 4);
444 if (strcmp(tokens[ti], "src") == 0) {
445 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
446 if (status->status < 0)
449 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
450 if (status->status < 0)
453 if (rule->flags == IP4_TUNNEL) {
456 APP_CHECK(parse_ipv4_addr(tokens[ti],
457 &ip, NULL) == 0, status,
458 "unrecognized input \"%s\", "
459 "expect valid ipv4 addr",
461 if (status->status < 0)
463 rule->src.ip.ip4 = rte_bswap32(
464 (uint32_t)ip.s_addr);
465 } else if (rule->flags == IP6_TUNNEL) {
468 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
470 "unrecognized input \"%s\", "
471 "expect valid ipv6 addr",
473 if (status->status < 0)
475 memcpy(rule->src.ip.ip6.ip6_b,
477 } else if (rule->flags == TRANSPORT) {
478 APP_CHECK(0, status, "unrecognized input "
479 "\"%s\"", tokens[ti]);
487 if (strcmp(tokens[ti], "dst") == 0) {
488 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
489 if (status->status < 0)
492 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
493 if (status->status < 0)
496 if (rule->flags == IP4_TUNNEL) {
499 APP_CHECK(parse_ipv4_addr(tokens[ti],
500 &ip, NULL) == 0, status,
501 "unrecognized input \"%s\", "
502 "expect valid ipv4 addr",
504 if (status->status < 0)
506 rule->dst.ip.ip4 = rte_bswap32(
507 (uint32_t)ip.s_addr);
508 } else if (rule->flags == IP6_TUNNEL) {
511 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
513 "unrecognized input \"%s\", "
514 "expect valid ipv6 addr",
516 if (status->status < 0)
518 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
519 } else if (rule->flags == TRANSPORT) {
520 APP_CHECK(0, status, "unrecognized "
521 "input \"%s\"", tokens[ti]);
529 if (strcmp(tokens[ti], "type") == 0) {
530 APP_CHECK_PRESENCE(type_p, tokens[ti], status);
531 if (status->status < 0)
534 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
535 if (status->status < 0)
538 if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
540 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
541 else if (strcmp(tokens[ti],
542 "inline-protocol-offload") == 0)
544 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
545 else if (strcmp(tokens[ti],
546 "lookaside-protocol-offload") == 0)
548 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
549 else if (strcmp(tokens[ti], "no-offload") == 0)
550 rule->type = RTE_SECURITY_ACTION_TYPE_NONE;
552 APP_CHECK(0, status, "Invalid input \"%s\"",
561 if (strcmp(tokens[ti], "port_id") == 0) {
562 APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
563 if (status->status < 0)
565 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
566 if (status->status < 0)
568 rule->portid = atoi(tokens[ti]);
569 if (status->status < 0)
575 /* unrecognizeable input */
576 APP_CHECK(0, status, "unrecognized input \"%s\"",
582 APP_CHECK(cipher_algo_p == 0, status,
583 "AEAD used, no need for cipher options");
584 if (status->status < 0)
587 APP_CHECK(auth_algo_p == 0, status,
588 "AEAD used, no need for auth options");
589 if (status->status < 0)
592 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
593 if (status->status < 0)
596 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
597 if (status->status < 0)
601 APP_CHECK(mode_p == 1, status, "missing mode option");
602 if (status->status < 0)
605 if ((rule->type != RTE_SECURITY_ACTION_TYPE_NONE) && (portid_p == 0))
606 printf("Missing portid option, falling back to non-offload\n");
608 if (!type_p || !portid_p) {
609 rule->type = RTE_SECURITY_ACTION_TYPE_NONE;
617 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
622 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
624 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
625 if (cipher_algos[i].algo == sa->cipher_algo) {
626 printf("%s ", cipher_algos[i].keyword);
631 for (i = 0; i < RTE_DIM(auth_algos); i++) {
632 if (auth_algos[i].algo == sa->auth_algo) {
633 printf("%s ", auth_algos[i].keyword);
638 for (i = 0; i < RTE_DIM(aead_algos); i++) {
639 if (aead_algos[i].algo == sa->aead_algo) {
640 printf("%s ", aead_algos[i].keyword);
649 printf("IP4Tunnel ");
650 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
651 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
652 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
653 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
656 printf("IP6Tunnel ");
657 for (i = 0; i < 16; i++) {
658 if (i % 2 && i != 15)
659 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
661 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
664 for (i = 0; i < 16; i++) {
665 if (i % 2 && i != 15)
666 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
668 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
679 struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
682 struct rte_crypto_sym_xform a;
683 struct rte_crypto_sym_xform b;
685 } xf[IPSEC_SA_MAX_ENTRIES];
688 static struct sa_ctx *
689 sa_create(const char *name, int32_t socket_id)
692 struct sa_ctx *sa_ctx;
694 const struct rte_memzone *mz;
696 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
698 /* Create SA array table */
699 printf("Creating SA context with %u maximum entries\n",
700 IPSEC_SA_MAX_ENTRIES);
702 mz_size = sizeof(struct sa_ctx);
703 mz = rte_memzone_reserve(s, mz_size, socket_id,
704 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
706 printf("Failed to allocate SA DB memory\n");
711 sa_ctx = (struct sa_ctx *)mz->addr;
717 check_eth_dev_caps(uint16_t portid, uint32_t inbound)
719 struct rte_eth_dev_info dev_info;
721 rte_eth_dev_info_get(portid, &dev_info);
724 if ((dev_info.rx_offload_capa &
725 DEV_RX_OFFLOAD_SECURITY) == 0) {
726 RTE_LOG(WARNING, PORT,
727 "hardware RX IPSec offload is not supported\n");
731 } else { /* outbound */
732 if ((dev_info.tx_offload_capa &
733 DEV_TX_OFFLOAD_SECURITY) == 0) {
734 RTE_LOG(WARNING, PORT,
735 "hardware TX IPSec offload is not supported\n");
744 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
745 uint32_t nb_entries, uint32_t inbound)
751 for (i = 0; i < nb_entries; i++) {
752 idx = SPI2IDX(entries[i].spi);
753 sa = &sa_ctx->sa[idx];
755 printf("Index %u already in use by SPI %u\n",
762 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
763 sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
764 if (check_eth_dev_caps(sa->portid, inbound))
768 sa->direction = (inbound == 1) ?
769 RTE_SECURITY_IPSEC_SA_DIR_INGRESS :
770 RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
774 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
775 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
778 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
781 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
782 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
783 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
784 sa_ctx->xf[idx].a.aead.key.length =
786 sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
787 RTE_CRYPTO_AEAD_OP_DECRYPT :
788 RTE_CRYPTO_AEAD_OP_ENCRYPT;
789 sa_ctx->xf[idx].a.next = NULL;
790 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
791 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
792 sa_ctx->xf[idx].a.aead.aad_length =
794 sa_ctx->xf[idx].a.aead.digest_length =
797 sa->xforms = &sa_ctx->xf[idx].a;
799 print_one_sa_rule(sa, inbound);
801 switch (sa->cipher_algo) {
802 case RTE_CRYPTO_CIPHER_NULL:
803 case RTE_CRYPTO_CIPHER_AES_CBC:
804 iv_length = sa->iv_len;
806 case RTE_CRYPTO_CIPHER_AES_CTR:
810 RTE_LOG(ERR, IPSEC_ESP,
811 "unsupported cipher algorithm %u\n",
817 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
818 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
819 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
820 sa_ctx->xf[idx].b.cipher.key.length =
822 sa_ctx->xf[idx].b.cipher.op =
823 RTE_CRYPTO_CIPHER_OP_DECRYPT;
824 sa_ctx->xf[idx].b.next = NULL;
825 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
826 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
828 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
829 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
830 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
831 sa_ctx->xf[idx].a.auth.key.length =
833 sa_ctx->xf[idx].a.auth.digest_length =
835 sa_ctx->xf[idx].a.auth.op =
836 RTE_CRYPTO_AUTH_OP_VERIFY;
837 } else { /* outbound */
838 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
839 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
840 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
841 sa_ctx->xf[idx].a.cipher.key.length =
843 sa_ctx->xf[idx].a.cipher.op =
844 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
845 sa_ctx->xf[idx].a.next = NULL;
846 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
847 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
849 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
850 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
851 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
852 sa_ctx->xf[idx].b.auth.key.length =
854 sa_ctx->xf[idx].b.auth.digest_length =
856 sa_ctx->xf[idx].b.auth.op =
857 RTE_CRYPTO_AUTH_OP_GENERATE;
860 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
861 sa_ctx->xf[idx].b.next = NULL;
862 sa->xforms = &sa_ctx->xf[idx].a;
864 print_one_sa_rule(sa, inbound);
872 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
875 return sa_add_rules(sa_ctx, entries, nb_entries, 0);
879 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
882 return sa_add_rules(sa_ctx, entries, nb_entries, 1);
886 sa_init(struct socket_ctx *ctx, int32_t socket_id)
891 rte_exit(EXIT_FAILURE, "NULL context.\n");
893 if (ctx->sa_in != NULL)
894 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
895 "initialized\n", socket_id);
897 if (ctx->sa_out != NULL)
898 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
899 "initialized\n", socket_id);
903 ctx->sa_in = sa_create(name, socket_id);
904 if (ctx->sa_in == NULL)
905 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
906 "context %s in socket %d\n", rte_errno,
909 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
911 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
915 ctx->sa_out = sa_create(name, socket_id);
916 if (ctx->sa_out == NULL)
917 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
918 "context %s in socket %d\n", rte_errno,
921 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
923 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
928 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
930 struct ipsec_mbuf_metadata *priv;
932 priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
934 return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
938 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
939 struct ipsec_sa **sa_ret)
949 ip = rte_pktmbuf_mtod(pkt, struct ip *);
950 if (ip->ip_v == IPVERSION)
951 esp = (struct esp_hdr *)(ip + 1);
953 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
955 if (esp->spi == INVALID_SPI)
958 sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
959 if (rte_be_to_cpu_32(esp->spi) != sa->spi)
964 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
965 if ((ip->ip_v == IPVERSION) &&
966 (sa->src.ip.ip4 == *src4_addr) &&
967 (sa->dst.ip.ip4 == *(src4_addr + 1)))
971 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
972 if ((ip->ip_v == IP6_VERSION) &&
973 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
974 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
983 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
984 struct ipsec_sa *sa[], uint16_t nb_pkts)
988 for (i = 0; i < nb_pkts; i++)
989 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
993 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
994 struct ipsec_sa *sa[], uint16_t nb_pkts)
998 for (i = 0; i < nb_pkts; i++)
999 sa[i] = &sa_ctx->sa[sa_idx[i]];