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;
379 /* NULL algorithm and combined algos do not
382 if (algo->key_not_req) {
387 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
388 if (status->status < 0)
391 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
392 status, "unrecognized input \"%s\", "
393 "expect \"auth_key\"", tokens[ti]);
394 if (status->status < 0)
397 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
398 if (status->status < 0)
401 key_len = parse_key_string(tokens[ti],
403 APP_CHECK(key_len == rule->auth_key_len, status,
404 "unrecognized input \"%s\"", tokens[ti]);
405 if (status->status < 0)
412 if (strcmp(tokens[ti], "aead_algo") == 0) {
413 const struct supported_aead_algo *algo;
416 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
418 if (status->status < 0)
421 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
422 if (status->status < 0)
425 algo = find_match_aead_algo(tokens[ti]);
427 APP_CHECK(algo != NULL, status, "unrecognized "
428 "input \"%s\"", tokens[ti]);
430 rule->aead_algo = algo->algo;
431 rule->cipher_key_len = algo->key_len;
432 rule->digest_len = algo->digest_len;
433 rule->aad_len = algo->aad_len;
434 rule->block_size = algo->block_size;
435 rule->iv_len = algo->iv_len;
437 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
438 if (status->status < 0)
441 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
442 status, "unrecognized input \"%s\", "
443 "expect \"aead_key\"", tokens[ti]);
444 if (status->status < 0)
447 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
448 if (status->status < 0)
451 key_len = parse_key_string(tokens[ti],
453 APP_CHECK(key_len == rule->cipher_key_len, status,
454 "unrecognized input \"%s\"", tokens[ti]);
455 if (status->status < 0)
459 rule->cipher_key_len = key_len;
461 &rule->cipher_key[key_len], 4);
467 if (strcmp(tokens[ti], "src") == 0) {
468 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
469 if (status->status < 0)
472 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
473 if (status->status < 0)
476 if (rule->flags == IP4_TUNNEL) {
479 APP_CHECK(parse_ipv4_addr(tokens[ti],
480 &ip, NULL) == 0, status,
481 "unrecognized input \"%s\", "
482 "expect valid ipv4 addr",
484 if (status->status < 0)
486 rule->src.ip.ip4 = rte_bswap32(
487 (uint32_t)ip.s_addr);
488 } else if (rule->flags == IP6_TUNNEL) {
491 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
493 "unrecognized input \"%s\", "
494 "expect valid ipv6 addr",
496 if (status->status < 0)
498 memcpy(rule->src.ip.ip6.ip6_b,
500 } else if (rule->flags == TRANSPORT) {
501 APP_CHECK(0, status, "unrecognized input "
502 "\"%s\"", tokens[ti]);
510 if (strcmp(tokens[ti], "dst") == 0) {
511 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
512 if (status->status < 0)
515 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
516 if (status->status < 0)
519 if (rule->flags == IP4_TUNNEL) {
522 APP_CHECK(parse_ipv4_addr(tokens[ti],
523 &ip, NULL) == 0, status,
524 "unrecognized input \"%s\", "
525 "expect valid ipv4 addr",
527 if (status->status < 0)
529 rule->dst.ip.ip4 = rte_bswap32(
530 (uint32_t)ip.s_addr);
531 } else if (rule->flags == IP6_TUNNEL) {
534 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
536 "unrecognized input \"%s\", "
537 "expect valid ipv6 addr",
539 if (status->status < 0)
541 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
542 } else if (rule->flags == TRANSPORT) {
543 APP_CHECK(0, status, "unrecognized "
544 "input \"%s\"", tokens[ti]);
552 /* unrecognizeable input */
553 APP_CHECK(0, status, "unrecognized input \"%s\"",
559 APP_CHECK(cipher_algo_p == 0, status,
560 "AEAD used, no need for cipher options");
561 if (status->status < 0)
564 APP_CHECK(auth_algo_p == 0, status,
565 "AEAD used, no need for auth options");
566 if (status->status < 0)
569 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
570 if (status->status < 0)
573 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
574 if (status->status < 0)
578 APP_CHECK(mode_p == 1, status, "missing mode option");
579 if (status->status < 0)
586 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
591 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
593 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
594 if (cipher_algos[i].algo == sa->cipher_algo) {
595 printf("%s ", cipher_algos[i].keyword);
600 for (i = 0; i < RTE_DIM(auth_algos); i++) {
601 if (auth_algos[i].algo == sa->auth_algo) {
602 printf("%s ", auth_algos[i].keyword);
607 for (i = 0; i < RTE_DIM(aead_algos); i++) {
608 if (aead_algos[i].algo == sa->aead_algo) {
609 printf("%s ", aead_algos[i].keyword);
618 printf("IP4Tunnel ");
619 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
620 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
621 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
622 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
625 printf("IP6Tunnel ");
626 for (i = 0; i < 16; i++) {
627 if (i % 2 && i != 15)
628 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
630 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
633 for (i = 0; i < 16; i++) {
634 if (i % 2 && i != 15)
635 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
637 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
648 struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
650 struct rte_crypto_sym_xform a;
651 struct rte_crypto_sym_xform b;
652 } xf[IPSEC_SA_MAX_ENTRIES];
655 static struct sa_ctx *
656 sa_create(const char *name, int32_t socket_id)
659 struct sa_ctx *sa_ctx;
661 const struct rte_memzone *mz;
663 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
665 /* Create SA array table */
666 printf("Creating SA context with %u maximum entries\n",
667 IPSEC_SA_MAX_ENTRIES);
669 mz_size = sizeof(struct sa_ctx);
670 mz = rte_memzone_reserve(s, mz_size, socket_id,
671 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
673 printf("Failed to allocate SA DB memory\n");
678 sa_ctx = (struct sa_ctx *)mz->addr;
684 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
685 uint32_t nb_entries, uint32_t inbound)
691 for (i = 0; i < nb_entries; i++) {
692 idx = SPI2IDX(entries[i].spi);
693 sa = &sa_ctx->sa[idx];
695 printf("Index %u already in use by SPI %u\n",
704 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
705 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
708 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
712 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
713 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
714 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
715 sa_ctx->xf[idx].a.aead.key.length =
717 sa_ctx->xf[idx].a.aead.op =
718 RTE_CRYPTO_AEAD_OP_DECRYPT;
719 sa_ctx->xf[idx].a.next = NULL;
720 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
721 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
722 sa_ctx->xf[idx].a.aead.aad_length =
724 sa_ctx->xf[idx].a.aead.digest_length =
726 } else { /* outbound */
727 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
728 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
729 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
730 sa_ctx->xf[idx].a.aead.key.length =
732 sa_ctx->xf[idx].a.aead.op =
733 RTE_CRYPTO_AEAD_OP_ENCRYPT;
734 sa_ctx->xf[idx].a.next = NULL;
735 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
736 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
737 sa_ctx->xf[idx].a.aead.aad_length =
739 sa_ctx->xf[idx].a.aead.digest_length =
743 sa->xforms = &sa_ctx->xf[idx].a;
745 print_one_sa_rule(sa, inbound);
747 switch (sa->cipher_algo) {
748 case RTE_CRYPTO_CIPHER_NULL:
749 case RTE_CRYPTO_CIPHER_AES_CBC:
750 iv_length = sa->iv_len;
752 case RTE_CRYPTO_CIPHER_AES_CTR:
756 RTE_LOG(ERR, IPSEC_ESP,
757 "unsupported cipher algorithm %u\n",
763 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
764 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
765 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
766 sa_ctx->xf[idx].b.cipher.key.length =
768 sa_ctx->xf[idx].b.cipher.op =
769 RTE_CRYPTO_CIPHER_OP_DECRYPT;
770 sa_ctx->xf[idx].b.next = NULL;
771 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
772 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
774 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
775 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
776 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
777 sa_ctx->xf[idx].a.auth.key.length =
779 sa_ctx->xf[idx].a.auth.digest_length =
781 sa_ctx->xf[idx].a.auth.op =
782 RTE_CRYPTO_AUTH_OP_VERIFY;
783 } else { /* outbound */
784 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
785 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
786 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
787 sa_ctx->xf[idx].a.cipher.key.length =
789 sa_ctx->xf[idx].a.cipher.op =
790 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
791 sa_ctx->xf[idx].a.next = NULL;
792 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
793 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
795 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
796 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
797 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
798 sa_ctx->xf[idx].b.auth.key.length =
800 sa_ctx->xf[idx].b.auth.digest_length =
802 sa_ctx->xf[idx].b.auth.op =
803 RTE_CRYPTO_AUTH_OP_GENERATE;
806 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
807 sa_ctx->xf[idx].b.next = NULL;
808 sa->xforms = &sa_ctx->xf[idx].a;
810 print_one_sa_rule(sa, inbound);
818 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
821 return sa_add_rules(sa_ctx, entries, nb_entries, 0);
825 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
828 return sa_add_rules(sa_ctx, entries, nb_entries, 1);
832 sa_init(struct socket_ctx *ctx, int32_t socket_id)
837 rte_exit(EXIT_FAILURE, "NULL context.\n");
839 if (ctx->sa_in != NULL)
840 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
841 "initialized\n", socket_id);
843 if (ctx->sa_out != NULL)
844 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
845 "initialized\n", socket_id);
849 ctx->sa_in = sa_create(name, socket_id);
850 if (ctx->sa_in == NULL)
851 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
852 "context %s in socket %d\n", rte_errno,
855 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
857 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
861 ctx->sa_out = sa_create(name, socket_id);
862 if (ctx->sa_out == NULL)
863 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
864 "context %s in socket %d\n", rte_errno,
867 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
869 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
874 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
876 struct ipsec_mbuf_metadata *priv;
878 priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
880 return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
884 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
885 struct ipsec_sa **sa_ret)
895 ip = rte_pktmbuf_mtod(pkt, struct ip *);
896 if (ip->ip_v == IPVERSION)
897 esp = (struct esp_hdr *)(ip + 1);
899 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
901 if (esp->spi == INVALID_SPI)
904 sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
905 if (rte_be_to_cpu_32(esp->spi) != sa->spi)
910 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
911 if ((ip->ip_v == IPVERSION) &&
912 (sa->src.ip.ip4 == *src4_addr) &&
913 (sa->dst.ip.ip4 == *(src4_addr + 1)))
917 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
918 if ((ip->ip_v == IP6_VERSION) &&
919 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
920 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
929 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
930 struct ipsec_sa *sa[], uint16_t nb_pkts)
934 for (i = 0; i < nb_pkts; i++)
935 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
939 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
940 struct ipsec_sa *sa[], uint16_t nb_pkts)
944 for (i = 0; i < nb_pkts; i++)
945 sa[i] = &sa_ctx->sa[sa_idx[i]];