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;
174 uint32_t nb_bytes = 0;
176 while (pt_end != NULL) {
177 pt_end = strchr(pt_start, ':');
180 strncpy(sub_str, pt_start, strlen(pt_start));
182 if (pt_end - pt_start > 2)
185 strncpy(sub_str, pt_start, pt_end - pt_start);
186 pt_start = pt_end + 1;
189 key[nb_bytes++] = strtol(sub_str, NULL, 16);
196 parse_sa_tokens(char **tokens, uint32_t n_tokens,
197 struct parse_status *status)
199 struct ipsec_sa *rule = NULL;
200 uint32_t ti; /*token index*/
201 uint32_t *ri /*rule index*/;
202 uint32_t cipher_algo_p = 0;
203 uint32_t auth_algo_p = 0;
208 if (strcmp(tokens[0], "in") == 0) {
211 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
212 "too many sa rules, abort insertion\n");
213 if (status->status < 0)
220 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
221 "too many sa rules, abort insertion\n");
222 if (status->status < 0)
229 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
230 if (status->status < 0)
232 rule->spi = atoi(tokens[1]);
234 for (ti = 2; ti < n_tokens; ti++) {
235 if (strcmp(tokens[ti], "mode") == 0) {
236 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
237 if (status->status < 0)
240 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
241 if (status->status < 0)
244 if (strcmp(tokens[ti], "ipv4-tunnel") == 0)
245 rule->flags = IP4_TUNNEL;
246 else if (strcmp(tokens[ti], "ipv6-tunnel") == 0)
247 rule->flags = IP6_TUNNEL;
248 else if (strcmp(tokens[ti], "transport") == 0)
249 rule->flags = TRANSPORT;
251 APP_CHECK(0, status, "unrecognized "
252 "input \"%s\"", tokens[ti]);
260 if (strcmp(tokens[ti], "cipher_algo") == 0) {
261 const struct supported_cipher_algo *algo;
264 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
266 if (status->status < 0)
269 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
270 if (status->status < 0)
273 algo = find_match_cipher_algo(tokens[ti]);
275 APP_CHECK(algo != NULL, status, "unrecognized "
276 "input \"%s\"", tokens[ti]);
278 rule->cipher_algo = algo->algo;
279 rule->block_size = algo->block_size;
280 rule->iv_len = algo->iv_len;
281 rule->cipher_key_len = algo->key_len;
283 /* for NULL algorithm, no cipher key required */
284 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
289 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
290 if (status->status < 0)
293 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
294 status, "unrecognized input \"%s\", "
295 "expect \"cipher_key\"", tokens[ti]);
296 if (status->status < 0)
299 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
300 if (status->status < 0)
303 key_len = parse_key_string(tokens[ti],
305 APP_CHECK(key_len == rule->cipher_key_len, status,
306 "unrecognized input \"%s\"", tokens[ti]);
307 if (status->status < 0)
310 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC)
311 rule->salt = (uint32_t)rte_rand();
313 if ((algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) ||
314 (algo->algo == RTE_CRYPTO_CIPHER_AES_GCM)) {
316 rule->cipher_key_len = key_len;
318 &rule->cipher_key[key_len], 4);
325 if (strcmp(tokens[ti], "auth_algo") == 0) {
326 const struct supported_auth_algo *algo;
329 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
331 if (status->status < 0)
334 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
335 if (status->status < 0)
338 algo = find_match_auth_algo(tokens[ti]);
339 APP_CHECK(algo != NULL, status, "unrecognized "
340 "input \"%s\"", tokens[ti]);
342 rule->auth_algo = algo->algo;
343 rule->auth_key_len = algo->key_len;
344 rule->digest_len = algo->digest_len;
345 rule->aad_len = algo->key_len;
347 /* NULL algorithm and combined algos do not
350 if (algo->key_not_req) {
355 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
356 if (status->status < 0)
359 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
360 status, "unrecognized input \"%s\", "
361 "expect \"auth_key\"", tokens[ti]);
362 if (status->status < 0)
365 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
366 if (status->status < 0)
369 key_len = parse_key_string(tokens[ti],
371 APP_CHECK(key_len == rule->auth_key_len, status,
372 "unrecognized input \"%s\"", tokens[ti]);
373 if (status->status < 0)
380 if (strcmp(tokens[ti], "src") == 0) {
381 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
382 if (status->status < 0)
385 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
386 if (status->status < 0)
389 if (rule->flags == IP4_TUNNEL) {
392 APP_CHECK(parse_ipv4_addr(tokens[ti],
393 &ip, NULL) == 0, status,
394 "unrecognized input \"%s\", "
395 "expect valid ipv4 addr",
397 if (status->status < 0)
399 rule->src.ip.ip4 = rte_bswap32(
400 (uint32_t)ip.s_addr);
401 } else if (rule->flags == IP6_TUNNEL) {
404 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
406 "unrecognized input \"%s\", "
407 "expect valid ipv6 addr",
409 if (status->status < 0)
411 memcpy(rule->src.ip.ip6.ip6_b,
413 } else if (rule->flags == TRANSPORT) {
414 APP_CHECK(0, status, "unrecognized input "
415 "\"%s\"", tokens[ti]);
423 if (strcmp(tokens[ti], "dst") == 0) {
424 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
425 if (status->status < 0)
428 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
429 if (status->status < 0)
432 if (rule->flags == IP4_TUNNEL) {
435 APP_CHECK(parse_ipv4_addr(tokens[ti],
436 &ip, NULL) == 0, status,
437 "unrecognized input \"%s\", "
438 "expect valid ipv4 addr",
440 if (status->status < 0)
442 rule->dst.ip.ip4 = rte_bswap32(
443 (uint32_t)ip.s_addr);
444 } else if (rule->flags == IP6_TUNNEL) {
447 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
449 "unrecognized input \"%s\", "
450 "expect valid ipv6 addr",
452 if (status->status < 0)
454 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
455 } else if (rule->flags == TRANSPORT) {
456 APP_CHECK(0, status, "unrecognized "
457 "input \"%s\"", tokens[ti]);
465 /* unrecognizeable input */
466 APP_CHECK(0, status, "unrecognized input \"%s\"",
471 APP_CHECK(cipher_algo_p == 1, status, "missing cipher options");
472 if (status->status < 0)
475 APP_CHECK(auth_algo_p == 1, status, "missing auth options");
476 if (status->status < 0)
479 APP_CHECK(mode_p == 1, status, "missing mode option");
480 if (status->status < 0)
487 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
492 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
494 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
495 if (cipher_algos[i].algo == sa->cipher_algo) {
496 printf("%s ", cipher_algos[i].keyword);
501 for (i = 0; i < RTE_DIM(auth_algos); i++) {
502 if (auth_algos[i].algo == sa->auth_algo) {
503 printf("%s ", auth_algos[i].keyword);
512 printf("IP4Tunnel ");
513 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
514 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
515 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
516 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
519 printf("IP6Tunnel ");
520 for (i = 0; i < 16; i++) {
521 if (i % 2 && i != 15)
522 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
524 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
527 for (i = 0; i < 16; i++) {
528 if (i % 2 && i != 15)
529 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
531 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
542 struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
544 struct rte_crypto_sym_xform a;
545 struct rte_crypto_sym_xform b;
546 } xf[IPSEC_SA_MAX_ENTRIES];
549 static struct sa_ctx *
550 sa_create(const char *name, int32_t socket_id)
553 struct sa_ctx *sa_ctx;
555 const struct rte_memzone *mz;
557 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
559 /* Create SA array table */
560 printf("Creating SA context with %u maximum entries\n",
561 IPSEC_SA_MAX_ENTRIES);
563 mz_size = sizeof(struct sa_ctx);
564 mz = rte_memzone_reserve(s, mz_size, socket_id,
565 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
567 printf("Failed to allocate SA DB memory\n");
572 sa_ctx = (struct sa_ctx *)mz->addr;
578 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
579 uint32_t nb_entries, uint32_t inbound)
584 for (i = 0; i < nb_entries; i++) {
585 idx = SPI2IDX(entries[i].spi);
586 sa = &sa_ctx->sa[idx];
588 printf("Index %u already in use by SPI %u\n",
597 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
598 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
602 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
603 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
604 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
605 sa_ctx->xf[idx].b.cipher.key.length =
607 sa_ctx->xf[idx].b.cipher.op =
608 RTE_CRYPTO_CIPHER_OP_DECRYPT;
609 sa_ctx->xf[idx].b.next = NULL;
611 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
612 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
613 sa_ctx->xf[idx].a.auth.add_auth_data_length =
615 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
616 sa_ctx->xf[idx].a.auth.key.length =
618 sa_ctx->xf[idx].a.auth.digest_length =
620 sa_ctx->xf[idx].a.auth.op =
621 RTE_CRYPTO_AUTH_OP_VERIFY;
623 } else { /* outbound */
624 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
625 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
626 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
627 sa_ctx->xf[idx].a.cipher.key.length =
629 sa_ctx->xf[idx].a.cipher.op =
630 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
631 sa_ctx->xf[idx].a.next = NULL;
633 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
634 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
635 sa_ctx->xf[idx].b.auth.add_auth_data_length =
637 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
638 sa_ctx->xf[idx].b.auth.key.length =
640 sa_ctx->xf[idx].b.auth.digest_length =
642 sa_ctx->xf[idx].b.auth.op =
643 RTE_CRYPTO_AUTH_OP_GENERATE;
646 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
647 sa_ctx->xf[idx].b.next = NULL;
648 sa->xforms = &sa_ctx->xf[idx].a;
650 print_one_sa_rule(sa, inbound);
657 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
660 return sa_add_rules(sa_ctx, entries, nb_entries, 0);
664 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
667 return sa_add_rules(sa_ctx, entries, nb_entries, 1);
671 sa_init(struct socket_ctx *ctx, int32_t socket_id)
676 rte_exit(EXIT_FAILURE, "NULL context.\n");
678 if (ctx->sa_in != NULL)
679 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
680 "initialized\n", socket_id);
682 if (ctx->sa_out != NULL)
683 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
684 "initialized\n", socket_id);
688 ctx->sa_in = sa_create(name, socket_id);
689 if (ctx->sa_in == NULL)
690 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
691 "context %s in socket %d\n", rte_errno,
694 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
696 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
700 ctx->sa_out = sa_create(name, socket_id);
701 if (ctx->sa_out == NULL)
702 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
703 "context %s in socket %d\n", rte_errno,
706 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
708 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
713 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
715 struct ipsec_mbuf_metadata *priv;
717 priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
719 return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
723 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
724 struct ipsec_sa **sa_ret)
734 ip = rte_pktmbuf_mtod(pkt, struct ip *);
735 if (ip->ip_v == IPVERSION)
736 esp = (struct esp_hdr *)(ip + 1);
738 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
740 if (esp->spi == INVALID_SPI)
743 sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
744 if (rte_be_to_cpu_32(esp->spi) != sa->spi)
749 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
750 if ((ip->ip_v == IPVERSION) &&
751 (sa->src.ip.ip4 == *src4_addr) &&
752 (sa->dst.ip.ip4 == *(src4_addr + 1)))
756 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
757 if ((ip->ip_v == IP6_VERSION) &&
758 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
759 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
768 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
769 struct ipsec_sa *sa[], uint16_t nb_pkts)
773 for (i = 0; i < nb_pkts; i++)
774 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
778 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
779 struct ipsec_sa *sa[], uint16_t nb_pkts)
783 for (i = 0; i < nb_pkts; i++)
784 sa[i] = &sa_ctx->sa[sa_idx[i]];