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_cryptodev.h>
45 #include <rte_byteorder.h>
46 #include <rte_errno.h>
48 #include <rte_random.h>
54 struct supported_cipher_algo {
56 enum rte_crypto_cipher_algorithm algo;
62 struct supported_auth_algo {
64 enum rte_crypto_auth_algorithm algo;
70 struct supported_aead_algo {
72 enum rte_crypto_aead_algorithm algo;
81 const struct supported_cipher_algo cipher_algos[] = {
84 .algo = RTE_CRYPTO_CIPHER_NULL,
90 .keyword = "aes-128-cbc",
91 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
97 .keyword = "aes-128-ctr",
98 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
100 .block_size = 16, /* XXX AESNI MB limition, should be 4 */
105 const struct supported_auth_algo auth_algos[] = {
108 .algo = RTE_CRYPTO_AUTH_NULL,
114 .keyword = "sha1-hmac",
115 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
120 .keyword = "sha256-hmac",
121 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
127 const struct supported_aead_algo aead_algos[] = {
129 .keyword = "aes-128-gcm",
130 .algo = RTE_CRYPTO_AEAD_AES_GCM,
139 struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
142 struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
145 static const struct supported_cipher_algo *
146 find_match_cipher_algo(const char *cipher_keyword)
150 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
151 const struct supported_cipher_algo *algo =
154 if (strcmp(cipher_keyword, algo->keyword) == 0)
161 static const struct supported_auth_algo *
162 find_match_auth_algo(const char *auth_keyword)
166 for (i = 0; i < RTE_DIM(auth_algos); i++) {
167 const struct supported_auth_algo *algo =
170 if (strcmp(auth_keyword, algo->keyword) == 0)
177 static const struct supported_aead_algo *
178 find_match_aead_algo(const char *aead_keyword)
182 for (i = 0; i < RTE_DIM(aead_algos); i++) {
183 const struct supported_aead_algo *algo =
186 if (strcmp(aead_keyword, algo->keyword) == 0)
194 * parse x:x:x:x.... hex number key string into uint8_t *key
196 * > 0: number of bytes parsed
200 parse_key_string(const char *key_str, uint8_t *key)
202 const char *pt_start = key_str, *pt_end = key_str;
203 uint32_t nb_bytes = 0;
205 while (pt_end != NULL) {
206 char sub_str[3] = {0};
208 pt_end = strchr(pt_start, ':');
210 if (pt_end == NULL) {
211 if (strlen(pt_start) > 2)
213 strncpy(sub_str, pt_start, 2);
215 if (pt_end - pt_start > 2)
218 strncpy(sub_str, pt_start, pt_end - pt_start);
219 pt_start = pt_end + 1;
222 key[nb_bytes++] = strtol(sub_str, NULL, 16);
229 parse_sa_tokens(char **tokens, uint32_t n_tokens,
230 struct parse_status *status)
232 struct ipsec_sa *rule = NULL;
233 uint32_t ti; /*token index*/
234 uint32_t *ri /*rule index*/;
235 uint32_t cipher_algo_p = 0;
236 uint32_t auth_algo_p = 0;
237 uint32_t aead_algo_p = 0;
242 if (strcmp(tokens[0], "in") == 0) {
245 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
246 "too many sa rules, abort insertion\n");
247 if (status->status < 0)
254 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
255 "too many sa rules, abort insertion\n");
256 if (status->status < 0)
263 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
264 if (status->status < 0)
266 rule->spi = atoi(tokens[1]);
268 for (ti = 2; ti < n_tokens; ti++) {
269 if (strcmp(tokens[ti], "mode") == 0) {
270 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
271 if (status->status < 0)
274 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
275 if (status->status < 0)
278 if (strcmp(tokens[ti], "ipv4-tunnel") == 0)
279 rule->flags = IP4_TUNNEL;
280 else if (strcmp(tokens[ti], "ipv6-tunnel") == 0)
281 rule->flags = IP6_TUNNEL;
282 else if (strcmp(tokens[ti], "transport") == 0)
283 rule->flags = TRANSPORT;
285 APP_CHECK(0, status, "unrecognized "
286 "input \"%s\"", tokens[ti]);
294 if (strcmp(tokens[ti], "cipher_algo") == 0) {
295 const struct supported_cipher_algo *algo;
298 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
300 if (status->status < 0)
303 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
304 if (status->status < 0)
307 algo = find_match_cipher_algo(tokens[ti]);
309 APP_CHECK(algo != NULL, status, "unrecognized "
310 "input \"%s\"", tokens[ti]);
312 rule->cipher_algo = algo->algo;
313 rule->block_size = algo->block_size;
314 rule->iv_len = algo->iv_len;
315 rule->cipher_key_len = algo->key_len;
317 /* for NULL algorithm, no cipher key required */
318 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
323 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
324 if (status->status < 0)
327 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
328 status, "unrecognized input \"%s\", "
329 "expect \"cipher_key\"", tokens[ti]);
330 if (status->status < 0)
333 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
334 if (status->status < 0)
337 key_len = parse_key_string(tokens[ti],
339 APP_CHECK(key_len == rule->cipher_key_len, status,
340 "unrecognized input \"%s\"", tokens[ti]);
341 if (status->status < 0)
344 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC)
345 rule->salt = (uint32_t)rte_rand();
347 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
349 rule->cipher_key_len = key_len;
351 &rule->cipher_key[key_len], 4);
358 if (strcmp(tokens[ti], "auth_algo") == 0) {
359 const struct supported_auth_algo *algo;
362 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
364 if (status->status < 0)
367 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
368 if (status->status < 0)
371 algo = find_match_auth_algo(tokens[ti]);
372 APP_CHECK(algo != NULL, status, "unrecognized "
373 "input \"%s\"", tokens[ti]);
375 rule->auth_algo = algo->algo;
376 rule->auth_key_len = algo->key_len;
377 rule->digest_len = algo->digest_len;
378 rule->aad_len = algo->key_len;
380 /* NULL algorithm and combined algos do not
383 if (algo->key_not_req) {
388 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
389 if (status->status < 0)
392 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
393 status, "unrecognized input \"%s\", "
394 "expect \"auth_key\"", tokens[ti]);
395 if (status->status < 0)
398 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
399 if (status->status < 0)
402 key_len = parse_key_string(tokens[ti],
404 APP_CHECK(key_len == rule->auth_key_len, status,
405 "unrecognized input \"%s\"", tokens[ti]);
406 if (status->status < 0)
413 if (strcmp(tokens[ti], "aead_algo") == 0) {
414 const struct supported_aead_algo *algo;
417 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
419 if (status->status < 0)
422 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
423 if (status->status < 0)
426 algo = find_match_aead_algo(tokens[ti]);
428 APP_CHECK(algo != NULL, status, "unrecognized "
429 "input \"%s\"", tokens[ti]);
431 rule->aead_algo = algo->algo;
432 rule->cipher_key_len = algo->key_len;
433 rule->digest_len = algo->digest_len;
434 rule->aad_len = algo->key_len;
435 rule->block_size = algo->block_size;
436 rule->iv_len = algo->iv_len;
438 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
439 if (status->status < 0)
442 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
443 status, "unrecognized input \"%s\", "
444 "expect \"aead_key\"", tokens[ti]);
445 if (status->status < 0)
448 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
449 if (status->status < 0)
452 key_len = parse_key_string(tokens[ti],
454 APP_CHECK(key_len == rule->cipher_key_len, status,
455 "unrecognized input \"%s\"", tokens[ti]);
456 if (status->status < 0)
460 rule->cipher_key_len = key_len;
462 &rule->cipher_key[key_len], 4);
468 if (strcmp(tokens[ti], "src") == 0) {
469 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
470 if (status->status < 0)
473 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
474 if (status->status < 0)
477 if (rule->flags == IP4_TUNNEL) {
480 APP_CHECK(parse_ipv4_addr(tokens[ti],
481 &ip, NULL) == 0, status,
482 "unrecognized input \"%s\", "
483 "expect valid ipv4 addr",
485 if (status->status < 0)
487 rule->src.ip.ip4 = rte_bswap32(
488 (uint32_t)ip.s_addr);
489 } else if (rule->flags == IP6_TUNNEL) {
492 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
494 "unrecognized input \"%s\", "
495 "expect valid ipv6 addr",
497 if (status->status < 0)
499 memcpy(rule->src.ip.ip6.ip6_b,
501 } else if (rule->flags == TRANSPORT) {
502 APP_CHECK(0, status, "unrecognized input "
503 "\"%s\"", tokens[ti]);
511 if (strcmp(tokens[ti], "dst") == 0) {
512 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
513 if (status->status < 0)
516 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
517 if (status->status < 0)
520 if (rule->flags == IP4_TUNNEL) {
523 APP_CHECK(parse_ipv4_addr(tokens[ti],
524 &ip, NULL) == 0, status,
525 "unrecognized input \"%s\", "
526 "expect valid ipv4 addr",
528 if (status->status < 0)
530 rule->dst.ip.ip4 = rte_bswap32(
531 (uint32_t)ip.s_addr);
532 } else if (rule->flags == IP6_TUNNEL) {
535 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
537 "unrecognized input \"%s\", "
538 "expect valid ipv6 addr",
540 if (status->status < 0)
542 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
543 } else if (rule->flags == TRANSPORT) {
544 APP_CHECK(0, status, "unrecognized "
545 "input \"%s\"", tokens[ti]);
553 /* unrecognizeable input */
554 APP_CHECK(0, status, "unrecognized input \"%s\"",
560 APP_CHECK(cipher_algo_p == 0, status,
561 "AEAD used, no need for cipher options");
562 if (status->status < 0)
565 APP_CHECK(auth_algo_p == 0, status,
566 "AEAD used, no need for auth options");
567 if (status->status < 0)
570 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
571 if (status->status < 0)
574 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
575 if (status->status < 0)
579 APP_CHECK(mode_p == 1, status, "missing mode option");
580 if (status->status < 0)
587 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
592 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
594 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
595 if (cipher_algos[i].algo == sa->cipher_algo) {
596 printf("%s ", cipher_algos[i].keyword);
601 for (i = 0; i < RTE_DIM(auth_algos); i++) {
602 if (auth_algos[i].algo == sa->auth_algo) {
603 printf("%s ", auth_algos[i].keyword);
608 for (i = 0; i < RTE_DIM(aead_algos); i++) {
609 if (aead_algos[i].algo == sa->aead_algo) {
610 printf("%s ", aead_algos[i].keyword);
619 printf("IP4Tunnel ");
620 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
621 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
622 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
623 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
626 printf("IP6Tunnel ");
627 for (i = 0; i < 16; i++) {
628 if (i % 2 && i != 15)
629 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
631 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
634 for (i = 0; i < 16; i++) {
635 if (i % 2 && i != 15)
636 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
638 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
649 struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
651 struct rte_crypto_sym_xform a;
652 struct rte_crypto_sym_xform b;
653 } xf[IPSEC_SA_MAX_ENTRIES];
656 static struct sa_ctx *
657 sa_create(const char *name, int32_t socket_id)
660 struct sa_ctx *sa_ctx;
662 const struct rte_memzone *mz;
664 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
666 /* Create SA array table */
667 printf("Creating SA context with %u maximum entries\n",
668 IPSEC_SA_MAX_ENTRIES);
670 mz_size = sizeof(struct sa_ctx);
671 mz = rte_memzone_reserve(s, mz_size, socket_id,
672 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
674 printf("Failed to allocate SA DB memory\n");
679 sa_ctx = (struct sa_ctx *)mz->addr;
685 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
686 uint32_t nb_entries, uint32_t inbound)
692 for (i = 0; i < nb_entries; i++) {
693 idx = SPI2IDX(entries[i].spi);
694 sa = &sa_ctx->sa[idx];
696 printf("Index %u already in use by SPI %u\n",
705 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
706 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
709 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
713 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
714 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
715 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
716 sa_ctx->xf[idx].a.aead.key.length =
718 sa_ctx->xf[idx].a.aead.op =
719 RTE_CRYPTO_AEAD_OP_DECRYPT;
720 sa_ctx->xf[idx].a.next = NULL;
721 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
722 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
723 sa_ctx->xf[idx].a.aead.aad_length =
725 sa_ctx->xf[idx].a.aead.digest_length =
727 } else { /* outbound */
728 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
729 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
730 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
731 sa_ctx->xf[idx].a.aead.key.length =
733 sa_ctx->xf[idx].a.aead.op =
734 RTE_CRYPTO_AEAD_OP_ENCRYPT;
735 sa_ctx->xf[idx].a.next = NULL;
736 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
737 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
738 sa_ctx->xf[idx].a.aead.aad_length =
740 sa_ctx->xf[idx].a.aead.digest_length =
744 sa->xforms = &sa_ctx->xf[idx].a;
746 print_one_sa_rule(sa, inbound);
748 switch (sa->cipher_algo) {
749 case RTE_CRYPTO_CIPHER_NULL:
750 case RTE_CRYPTO_CIPHER_AES_CBC:
751 iv_length = sa->iv_len;
753 case RTE_CRYPTO_CIPHER_AES_CTR:
757 RTE_LOG(ERR, IPSEC_ESP,
758 "unsupported cipher algorithm %u\n",
764 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
765 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
766 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
767 sa_ctx->xf[idx].b.cipher.key.length =
769 sa_ctx->xf[idx].b.cipher.op =
770 RTE_CRYPTO_CIPHER_OP_DECRYPT;
771 sa_ctx->xf[idx].b.next = NULL;
772 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
773 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
775 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
776 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
777 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
778 sa_ctx->xf[idx].a.auth.key.length =
780 sa_ctx->xf[idx].a.auth.digest_length =
782 sa_ctx->xf[idx].a.auth.op =
783 RTE_CRYPTO_AUTH_OP_VERIFY;
784 } else { /* outbound */
785 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
786 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
787 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
788 sa_ctx->xf[idx].a.cipher.key.length =
790 sa_ctx->xf[idx].a.cipher.op =
791 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
792 sa_ctx->xf[idx].a.next = NULL;
793 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
794 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
796 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
797 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
798 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
799 sa_ctx->xf[idx].b.auth.key.length =
801 sa_ctx->xf[idx].b.auth.digest_length =
803 sa_ctx->xf[idx].b.auth.op =
804 RTE_CRYPTO_AUTH_OP_GENERATE;
807 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
808 sa_ctx->xf[idx].b.next = NULL;
809 sa->xforms = &sa_ctx->xf[idx].a;
811 print_one_sa_rule(sa, inbound);
819 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
822 return sa_add_rules(sa_ctx, entries, nb_entries, 0);
826 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
829 return sa_add_rules(sa_ctx, entries, nb_entries, 1);
833 sa_init(struct socket_ctx *ctx, int32_t socket_id)
838 rte_exit(EXIT_FAILURE, "NULL context.\n");
840 if (ctx->sa_in != NULL)
841 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
842 "initialized\n", socket_id);
844 if (ctx->sa_out != NULL)
845 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
846 "initialized\n", socket_id);
850 ctx->sa_in = sa_create(name, socket_id);
851 if (ctx->sa_in == NULL)
852 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
853 "context %s in socket %d\n", rte_errno,
856 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
858 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
862 ctx->sa_out = sa_create(name, socket_id);
863 if (ctx->sa_out == NULL)
864 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
865 "context %s in socket %d\n", rte_errno,
868 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
870 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
875 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
877 struct ipsec_mbuf_metadata *priv;
879 priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
881 return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
885 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
886 struct ipsec_sa **sa_ret)
896 ip = rte_pktmbuf_mtod(pkt, struct ip *);
897 if (ip->ip_v == IPVERSION)
898 esp = (struct esp_hdr *)(ip + 1);
900 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
902 if (esp->spi == INVALID_SPI)
905 sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
906 if (rte_be_to_cpu_32(esp->spi) != sa->spi)
911 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
912 if ((ip->ip_v == IPVERSION) &&
913 (sa->src.ip.ip4 == *src4_addr) &&
914 (sa->dst.ip.ip4 == *(src4_addr + 1)))
918 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
919 if ((ip->ip_v == IP6_VERSION) &&
920 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
921 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
930 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
931 struct ipsec_sa *sa[], uint16_t nb_pkts)
935 for (i = 0; i < nb_pkts; i++)
936 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
940 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
941 struct ipsec_sa *sa[], uint16_t nb_pkts)
945 for (i = 0; i < nb_pkts; i++)
946 sa[i] = &sa_ctx->sa[sa_idx[i]];