4 * Copyright(c) 2016 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;
71 const struct supported_cipher_algo cipher_algos[] = {
74 .algo = RTE_CRYPTO_CIPHER_NULL,
80 .keyword = "aes-128-cbc",
81 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
87 .keyword = "aes-128-gcm",
88 .algo = RTE_CRYPTO_CIPHER_AES_GCM,
94 .keyword = "aes-128-ctr",
95 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
97 .block_size = 16, /* XXX AESNI MB limition, should be 4 */
102 const struct supported_auth_algo auth_algos[] = {
105 .algo = RTE_CRYPTO_AUTH_NULL,
111 .keyword = "sha1-hmac",
112 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
117 .keyword = "aes-128-gcm",
118 .algo = RTE_CRYPTO_AUTH_AES_GCM,
125 struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
128 struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
131 static const struct supported_cipher_algo *
132 find_match_cipher_algo(const char *cipher_keyword)
136 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
137 const struct supported_cipher_algo *algo =
140 if (strcmp(cipher_keyword, algo->keyword) == 0)
147 static const struct supported_auth_algo *
148 find_match_auth_algo(const char *auth_keyword)
152 for (i = 0; i < RTE_DIM(auth_algos); i++) {
153 const struct supported_auth_algo *algo =
156 if (strcmp(auth_keyword, algo->keyword) == 0)
164 * parse x:x:x:x.... hex number key string into uint8_t *key
166 * > 0: number of bytes parsed
170 parse_key_string(const char *key_str, uint8_t *key)
172 const char *pt_start = key_str, *pt_end = key_str;
173 uint32_t nb_bytes = 0;
175 while (pt_end != NULL) {
176 char sub_str[3] = {0};
178 pt_end = strchr(pt_start, ':');
180 if (pt_end == NULL) {
181 if (strlen(pt_start) > 2)
183 strncpy(sub_str, pt_start, 2);
185 if (pt_end - pt_start > 2)
188 strncpy(sub_str, pt_start, pt_end - pt_start);
189 pt_start = pt_end + 1;
192 key[nb_bytes++] = strtol(sub_str, NULL, 16);
199 parse_sa_tokens(char **tokens, uint32_t n_tokens,
200 struct parse_status *status)
202 struct ipsec_sa *rule = NULL;
203 uint32_t ti; /*token index*/
204 uint32_t *ri /*rule index*/;
205 uint32_t cipher_algo_p = 0;
206 uint32_t auth_algo_p = 0;
211 if (strcmp(tokens[0], "in") == 0) {
214 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
215 "too many sa rules, abort insertion\n");
216 if (status->status < 0)
223 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
224 "too many sa rules, abort insertion\n");
225 if (status->status < 0)
232 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
233 if (status->status < 0)
235 rule->spi = atoi(tokens[1]);
237 for (ti = 2; ti < n_tokens; ti++) {
238 if (strcmp(tokens[ti], "mode") == 0) {
239 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
240 if (status->status < 0)
243 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
244 if (status->status < 0)
247 if (strcmp(tokens[ti], "ipv4-tunnel") == 0)
248 rule->flags = IP4_TUNNEL;
249 else if (strcmp(tokens[ti], "ipv6-tunnel") == 0)
250 rule->flags = IP6_TUNNEL;
251 else if (strcmp(tokens[ti], "transport") == 0)
252 rule->flags = TRANSPORT;
254 APP_CHECK(0, status, "unrecognized "
255 "input \"%s\"", tokens[ti]);
263 if (strcmp(tokens[ti], "cipher_algo") == 0) {
264 const struct supported_cipher_algo *algo;
267 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
269 if (status->status < 0)
272 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
273 if (status->status < 0)
276 algo = find_match_cipher_algo(tokens[ti]);
278 APP_CHECK(algo != NULL, status, "unrecognized "
279 "input \"%s\"", tokens[ti]);
281 rule->cipher_algo = algo->algo;
282 rule->block_size = algo->block_size;
283 rule->iv_len = algo->iv_len;
284 rule->cipher_key_len = algo->key_len;
286 /* for NULL algorithm, no cipher key required */
287 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
292 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
293 if (status->status < 0)
296 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
297 status, "unrecognized input \"%s\", "
298 "expect \"cipher_key\"", tokens[ti]);
299 if (status->status < 0)
302 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
303 if (status->status < 0)
306 key_len = parse_key_string(tokens[ti],
308 APP_CHECK(key_len == rule->cipher_key_len, status,
309 "unrecognized input \"%s\"", tokens[ti]);
310 if (status->status < 0)
313 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC)
314 rule->salt = (uint32_t)rte_rand();
316 if ((algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) ||
317 (algo->algo == RTE_CRYPTO_CIPHER_AES_GCM)) {
319 rule->cipher_key_len = key_len;
321 &rule->cipher_key[key_len], 4);
328 if (strcmp(tokens[ti], "auth_algo") == 0) {
329 const struct supported_auth_algo *algo;
332 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
334 if (status->status < 0)
337 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
338 if (status->status < 0)
341 algo = find_match_auth_algo(tokens[ti]);
342 APP_CHECK(algo != NULL, status, "unrecognized "
343 "input \"%s\"", tokens[ti]);
345 rule->auth_algo = algo->algo;
346 rule->auth_key_len = algo->key_len;
347 rule->digest_len = algo->digest_len;
348 rule->aad_len = algo->key_len;
350 /* NULL algorithm and combined algos do not
353 if (algo->key_not_req) {
358 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
359 if (status->status < 0)
362 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
363 status, "unrecognized input \"%s\", "
364 "expect \"auth_key\"", tokens[ti]);
365 if (status->status < 0)
368 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
369 if (status->status < 0)
372 key_len = parse_key_string(tokens[ti],
374 APP_CHECK(key_len == rule->auth_key_len, status,
375 "unrecognized input \"%s\"", tokens[ti]);
376 if (status->status < 0)
383 if (strcmp(tokens[ti], "src") == 0) {
384 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
385 if (status->status < 0)
388 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
389 if (status->status < 0)
392 if (rule->flags == IP4_TUNNEL) {
395 APP_CHECK(parse_ipv4_addr(tokens[ti],
396 &ip, NULL) == 0, status,
397 "unrecognized input \"%s\", "
398 "expect valid ipv4 addr",
400 if (status->status < 0)
402 rule->src.ip.ip4 = rte_bswap32(
403 (uint32_t)ip.s_addr);
404 } else if (rule->flags == IP6_TUNNEL) {
407 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
409 "unrecognized input \"%s\", "
410 "expect valid ipv6 addr",
412 if (status->status < 0)
414 memcpy(rule->src.ip.ip6.ip6_b,
416 } else if (rule->flags == TRANSPORT) {
417 APP_CHECK(0, status, "unrecognized input "
418 "\"%s\"", tokens[ti]);
426 if (strcmp(tokens[ti], "dst") == 0) {
427 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
428 if (status->status < 0)
431 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
432 if (status->status < 0)
435 if (rule->flags == IP4_TUNNEL) {
438 APP_CHECK(parse_ipv4_addr(tokens[ti],
439 &ip, NULL) == 0, status,
440 "unrecognized input \"%s\", "
441 "expect valid ipv4 addr",
443 if (status->status < 0)
445 rule->dst.ip.ip4 = rte_bswap32(
446 (uint32_t)ip.s_addr);
447 } else if (rule->flags == IP6_TUNNEL) {
450 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
452 "unrecognized input \"%s\", "
453 "expect valid ipv6 addr",
455 if (status->status < 0)
457 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
458 } else if (rule->flags == TRANSPORT) {
459 APP_CHECK(0, status, "unrecognized "
460 "input \"%s\"", tokens[ti]);
468 /* unrecognizeable input */
469 APP_CHECK(0, status, "unrecognized input \"%s\"",
474 APP_CHECK(cipher_algo_p == 1, status, "missing cipher options");
475 if (status->status < 0)
478 APP_CHECK(auth_algo_p == 1, status, "missing auth options");
479 if (status->status < 0)
482 APP_CHECK(mode_p == 1, status, "missing mode option");
483 if (status->status < 0)
490 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
495 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
497 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
498 if (cipher_algos[i].algo == sa->cipher_algo) {
499 printf("%s ", cipher_algos[i].keyword);
504 for (i = 0; i < RTE_DIM(auth_algos); i++) {
505 if (auth_algos[i].algo == sa->auth_algo) {
506 printf("%s ", auth_algos[i].keyword);
515 printf("IP4Tunnel ");
516 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
517 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
518 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
519 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
522 printf("IP6Tunnel ");
523 for (i = 0; i < 16; i++) {
524 if (i % 2 && i != 15)
525 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
527 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
530 for (i = 0; i < 16; i++) {
531 if (i % 2 && i != 15)
532 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
534 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
545 struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
547 struct rte_crypto_sym_xform a;
548 struct rte_crypto_sym_xform b;
549 } xf[IPSEC_SA_MAX_ENTRIES];
552 static struct sa_ctx *
553 sa_create(const char *name, int32_t socket_id)
556 struct sa_ctx *sa_ctx;
558 const struct rte_memzone *mz;
560 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
562 /* Create SA array table */
563 printf("Creating SA context with %u maximum entries\n",
564 IPSEC_SA_MAX_ENTRIES);
566 mz_size = sizeof(struct sa_ctx);
567 mz = rte_memzone_reserve(s, mz_size, socket_id,
568 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
570 printf("Failed to allocate SA DB memory\n");
575 sa_ctx = (struct sa_ctx *)mz->addr;
581 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
582 uint32_t nb_entries, uint32_t inbound)
587 for (i = 0; i < nb_entries; i++) {
588 idx = SPI2IDX(entries[i].spi);
589 sa = &sa_ctx->sa[idx];
591 printf("Index %u already in use by SPI %u\n",
600 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
601 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
605 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
606 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
607 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
608 sa_ctx->xf[idx].b.cipher.key.length =
610 sa_ctx->xf[idx].b.cipher.op =
611 RTE_CRYPTO_CIPHER_OP_DECRYPT;
612 sa_ctx->xf[idx].b.next = NULL;
614 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
615 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
616 sa_ctx->xf[idx].a.auth.add_auth_data_length =
618 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
619 sa_ctx->xf[idx].a.auth.key.length =
621 sa_ctx->xf[idx].a.auth.digest_length =
623 sa_ctx->xf[idx].a.auth.op =
624 RTE_CRYPTO_AUTH_OP_VERIFY;
626 } else { /* outbound */
627 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
628 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
629 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
630 sa_ctx->xf[idx].a.cipher.key.length =
632 sa_ctx->xf[idx].a.cipher.op =
633 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
634 sa_ctx->xf[idx].a.next = NULL;
636 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
637 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
638 sa_ctx->xf[idx].b.auth.add_auth_data_length =
640 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
641 sa_ctx->xf[idx].b.auth.key.length =
643 sa_ctx->xf[idx].b.auth.digest_length =
645 sa_ctx->xf[idx].b.auth.op =
646 RTE_CRYPTO_AUTH_OP_GENERATE;
649 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
650 sa_ctx->xf[idx].b.next = NULL;
651 sa->xforms = &sa_ctx->xf[idx].a;
653 print_one_sa_rule(sa, inbound);
660 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
663 return sa_add_rules(sa_ctx, entries, nb_entries, 0);
667 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
670 return sa_add_rules(sa_ctx, entries, nb_entries, 1);
674 sa_init(struct socket_ctx *ctx, int32_t socket_id)
679 rte_exit(EXIT_FAILURE, "NULL context.\n");
681 if (ctx->sa_in != NULL)
682 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
683 "initialized\n", socket_id);
685 if (ctx->sa_out != NULL)
686 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
687 "initialized\n", socket_id);
691 ctx->sa_in = sa_create(name, socket_id);
692 if (ctx->sa_in == NULL)
693 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
694 "context %s in socket %d\n", rte_errno,
697 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
699 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
703 ctx->sa_out = sa_create(name, socket_id);
704 if (ctx->sa_out == NULL)
705 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
706 "context %s in socket %d\n", rte_errno,
709 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
711 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
716 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
718 struct ipsec_mbuf_metadata *priv;
720 priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
722 return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
726 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
727 struct ipsec_sa **sa_ret)
737 ip = rte_pktmbuf_mtod(pkt, struct ip *);
738 if (ip->ip_v == IPVERSION)
739 esp = (struct esp_hdr *)(ip + 1);
741 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
743 if (esp->spi == INVALID_SPI)
746 sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
747 if (rte_be_to_cpu_32(esp->spi) != sa->spi)
752 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
753 if ((ip->ip_v == IPVERSION) &&
754 (sa->src.ip.ip4 == *src4_addr) &&
755 (sa->dst.ip.ip4 == *(src4_addr + 1)))
759 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
760 if ((ip->ip_v == IP6_VERSION) &&
761 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
762 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
771 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
772 struct ipsec_sa *sa[], uint16_t nb_pkts)
776 for (i = 0; i < nb_pkts; i++)
777 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
781 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
782 struct ipsec_sa *sa[], uint16_t nb_pkts)
786 for (i = 0; i < nb_pkts; i++)
787 sa[i] = &sa_ctx->sa[sa_idx[i]];