7be0e62840d2c9d52d869296f10c0a6342f4c05d
[deb_dpdk.git] / examples / ipsec-secgw / sa.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 /*
35  * Security Associations
36  */
37 #include <sys/types.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip6.h>
41
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>
47 #include <rte_ip.h>
48 #include <rte_random.h>
49
50 #include "ipsec.h"
51 #include "esp.h"
52 #include "parser.h"
53
54 struct supported_cipher_algo {
55         const char *keyword;
56         enum rte_crypto_cipher_algorithm algo;
57         uint16_t iv_len;
58         uint16_t block_size;
59         uint16_t key_len;
60 };
61
62 struct supported_auth_algo {
63         const char *keyword;
64         enum rte_crypto_auth_algorithm algo;
65         uint16_t digest_len;
66         uint16_t key_len;
67         uint8_t key_not_req;
68 };
69
70 struct supported_aead_algo {
71         const char *keyword;
72         enum rte_crypto_aead_algorithm algo;
73         uint16_t iv_len;
74         uint16_t block_size;
75         uint16_t digest_len;
76         uint16_t key_len;
77         uint8_t aad_len;
78 };
79
80
81 const struct supported_cipher_algo cipher_algos[] = {
82         {
83                 .keyword = "null",
84                 .algo = RTE_CRYPTO_CIPHER_NULL,
85                 .iv_len = 0,
86                 .block_size = 4,
87                 .key_len = 0
88         },
89         {
90                 .keyword = "aes-128-cbc",
91                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
92                 .iv_len = 16,
93                 .block_size = 16,
94                 .key_len = 16
95         },
96         {
97                 .keyword = "aes-128-ctr",
98                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
99                 .iv_len = 8,
100                 .block_size = 16, /* XXX AESNI MB limition, should be 4 */
101                 .key_len = 20
102         }
103 };
104
105 const struct supported_auth_algo auth_algos[] = {
106         {
107                 .keyword = "null",
108                 .algo = RTE_CRYPTO_AUTH_NULL,
109                 .digest_len = 0,
110                 .key_len = 0,
111                 .key_not_req = 1
112         },
113         {
114                 .keyword = "sha1-hmac",
115                 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
116                 .digest_len = 12,
117                 .key_len = 20
118         },
119         {
120                 .keyword = "sha256-hmac",
121                 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
122                 .digest_len = 12,
123                 .key_len = 32
124         }
125 };
126
127 const struct supported_aead_algo aead_algos[] = {
128         {
129                 .keyword = "aes-128-gcm",
130                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
131                 .iv_len = 8,
132                 .block_size = 4,
133                 .key_len = 20,
134                 .digest_len = 16,
135                 .aad_len = 8,
136         }
137 };
138
139 struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
140 uint32_t nb_sa_out;
141
142 struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
143 uint32_t nb_sa_in;
144
145 static const struct supported_cipher_algo *
146 find_match_cipher_algo(const char *cipher_keyword)
147 {
148         size_t i;
149
150         for (i = 0; i < RTE_DIM(cipher_algos); i++) {
151                 const struct supported_cipher_algo *algo =
152                         &cipher_algos[i];
153
154                 if (strcmp(cipher_keyword, algo->keyword) == 0)
155                         return algo;
156         }
157
158         return NULL;
159 }
160
161 static const struct supported_auth_algo *
162 find_match_auth_algo(const char *auth_keyword)
163 {
164         size_t i;
165
166         for (i = 0; i < RTE_DIM(auth_algos); i++) {
167                 const struct supported_auth_algo *algo =
168                         &auth_algos[i];
169
170                 if (strcmp(auth_keyword, algo->keyword) == 0)
171                         return algo;
172         }
173
174         return NULL;
175 }
176
177 static const struct supported_aead_algo *
178 find_match_aead_algo(const char *aead_keyword)
179 {
180         size_t i;
181
182         for (i = 0; i < RTE_DIM(aead_algos); i++) {
183                 const struct supported_aead_algo *algo =
184                         &aead_algos[i];
185
186                 if (strcmp(aead_keyword, algo->keyword) == 0)
187                         return algo;
188         }
189
190         return NULL;
191 }
192
193 /** parse_key_string
194  *  parse x:x:x:x.... hex number key string into uint8_t *key
195  *  return:
196  *  > 0: number of bytes parsed
197  *  0:   failed
198  */
199 static uint32_t
200 parse_key_string(const char *key_str, uint8_t *key)
201 {
202         const char *pt_start = key_str, *pt_end = key_str;
203         uint32_t nb_bytes = 0;
204
205         while (pt_end != NULL) {
206                 char sub_str[3] = {0};
207
208                 pt_end = strchr(pt_start, ':');
209
210                 if (pt_end == NULL) {
211                         if (strlen(pt_start) > 2)
212                                 return 0;
213                         strncpy(sub_str, pt_start, 2);
214                 } else {
215                         if (pt_end - pt_start > 2)
216                                 return 0;
217
218                         strncpy(sub_str, pt_start, pt_end - pt_start);
219                         pt_start = pt_end + 1;
220                 }
221
222                 key[nb_bytes++] = strtol(sub_str, NULL, 16);
223         }
224
225         return nb_bytes;
226 }
227
228 void
229 parse_sa_tokens(char **tokens, uint32_t n_tokens,
230         struct parse_status *status)
231 {
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;
238         uint32_t src_p = 0;
239         uint32_t dst_p = 0;
240         uint32_t mode_p = 0;
241
242         if (strcmp(tokens[0], "in") == 0) {
243                 ri = &nb_sa_in;
244
245                 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
246                         "too many sa rules, abort insertion\n");
247                 if (status->status < 0)
248                         return;
249
250                 rule = &sa_in[*ri];
251         } else {
252                 ri = &nb_sa_out;
253
254                 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
255                         "too many sa rules, abort insertion\n");
256                 if (status->status < 0)
257                         return;
258
259                 rule = &sa_out[*ri];
260         }
261
262         /* spi number */
263         APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
264         if (status->status < 0)
265                 return;
266         rule->spi = atoi(tokens[1]);
267
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)
272                                 return;
273
274                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
275                         if (status->status < 0)
276                                 return;
277
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;
284                         else {
285                                 APP_CHECK(0, status, "unrecognized "
286                                         "input \"%s\"", tokens[ti]);
287                                 return;
288                         }
289
290                         mode_p = 1;
291                         continue;
292                 }
293
294                 if (strcmp(tokens[ti], "cipher_algo") == 0) {
295                         const struct supported_cipher_algo *algo;
296                         uint32_t key_len;
297
298                         APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
299                                 status);
300                         if (status->status < 0)
301                                 return;
302
303                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
304                         if (status->status < 0)
305                                 return;
306
307                         algo = find_match_cipher_algo(tokens[ti]);
308
309                         APP_CHECK(algo != NULL, status, "unrecognized "
310                                 "input \"%s\"", tokens[ti]);
311
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;
316
317                         /* for NULL algorithm, no cipher key required */
318                         if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
319                                 cipher_algo_p = 1;
320                                 continue;
321                         }
322
323                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
324                         if (status->status < 0)
325                                 return;
326
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)
331                                 return;
332
333                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
334                         if (status->status < 0)
335                                 return;
336
337                         key_len = parse_key_string(tokens[ti],
338                                 rule->cipher_key);
339                         APP_CHECK(key_len == rule->cipher_key_len, status,
340                                 "unrecognized input \"%s\"", tokens[ti]);
341                         if (status->status < 0)
342                                 return;
343
344                         if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC)
345                                 rule->salt = (uint32_t)rte_rand();
346
347                         if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
348                                 key_len -= 4;
349                                 rule->cipher_key_len = key_len;
350                                 memcpy(&rule->salt,
351                                         &rule->cipher_key[key_len], 4);
352                         }
353
354                         cipher_algo_p = 1;
355                         continue;
356                 }
357
358                 if (strcmp(tokens[ti], "auth_algo") == 0) {
359                         const struct supported_auth_algo *algo;
360                         uint32_t key_len;
361
362                         APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
363                                 status);
364                         if (status->status < 0)
365                                 return;
366
367                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
368                         if (status->status < 0)
369                                 return;
370
371                         algo = find_match_auth_algo(tokens[ti]);
372                         APP_CHECK(algo != NULL, status, "unrecognized "
373                                 "input \"%s\"", tokens[ti]);
374
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;
379
380                         /* NULL algorithm and combined algos do not
381                          * require auth key
382                          */
383                         if (algo->key_not_req) {
384                                 auth_algo_p = 1;
385                                 continue;
386                         }
387
388                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
389                         if (status->status < 0)
390                                 return;
391
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)
396                                 return;
397
398                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
399                         if (status->status < 0)
400                                 return;
401
402                         key_len = parse_key_string(tokens[ti],
403                                 rule->auth_key);
404                         APP_CHECK(key_len == rule->auth_key_len, status,
405                                 "unrecognized input \"%s\"", tokens[ti]);
406                         if (status->status < 0)
407                                 return;
408
409                         auth_algo_p = 1;
410                         continue;
411                 }
412
413                 if (strcmp(tokens[ti], "aead_algo") == 0) {
414                         const struct supported_aead_algo *algo;
415                         uint32_t key_len;
416
417                         APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
418                                 status);
419                         if (status->status < 0)
420                                 return;
421
422                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
423                         if (status->status < 0)
424                                 return;
425
426                         algo = find_match_aead_algo(tokens[ti]);
427
428                         APP_CHECK(algo != NULL, status, "unrecognized "
429                                 "input \"%s\"", tokens[ti]);
430
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;
437
438                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
439                         if (status->status < 0)
440                                 return;
441
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)
446                                 return;
447
448                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
449                         if (status->status < 0)
450                                 return;
451
452                         key_len = parse_key_string(tokens[ti],
453                                 rule->cipher_key);
454                         APP_CHECK(key_len == rule->cipher_key_len, status,
455                                 "unrecognized input \"%s\"", tokens[ti]);
456                         if (status->status < 0)
457                                 return;
458
459                         key_len -= 4;
460                         rule->cipher_key_len = key_len;
461                         memcpy(&rule->salt,
462                                 &rule->cipher_key[key_len], 4);
463
464                         aead_algo_p = 1;
465                         continue;
466                 }
467
468                 if (strcmp(tokens[ti], "src") == 0) {
469                         APP_CHECK_PRESENCE(src_p, tokens[ti], status);
470                         if (status->status < 0)
471                                 return;
472
473                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
474                         if (status->status < 0)
475                                 return;
476
477                         if (rule->flags == IP4_TUNNEL) {
478                                 struct in_addr ip;
479
480                                 APP_CHECK(parse_ipv4_addr(tokens[ti],
481                                         &ip, NULL) == 0, status,
482                                         "unrecognized input \"%s\", "
483                                         "expect valid ipv4 addr",
484                                         tokens[ti]);
485                                 if (status->status < 0)
486                                         return;
487                                 rule->src.ip.ip4 = rte_bswap32(
488                                         (uint32_t)ip.s_addr);
489                         } else if (rule->flags == IP6_TUNNEL) {
490                                 struct in6_addr ip;
491
492                                 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
493                                         NULL) == 0, status,
494                                         "unrecognized input \"%s\", "
495                                         "expect valid ipv6 addr",
496                                         tokens[ti]);
497                                 if (status->status < 0)
498                                         return;
499                                 memcpy(rule->src.ip.ip6.ip6_b,
500                                         ip.s6_addr, 16);
501                         } else if (rule->flags == TRANSPORT) {
502                                 APP_CHECK(0, status, "unrecognized input "
503                                         "\"%s\"", tokens[ti]);
504                                 return;
505                         }
506
507                         src_p = 1;
508                         continue;
509                 }
510
511                 if (strcmp(tokens[ti], "dst") == 0) {
512                         APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
513                         if (status->status < 0)
514                                 return;
515
516                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
517                         if (status->status < 0)
518                                 return;
519
520                         if (rule->flags == IP4_TUNNEL) {
521                                 struct in_addr ip;
522
523                                 APP_CHECK(parse_ipv4_addr(tokens[ti],
524                                         &ip, NULL) == 0, status,
525                                         "unrecognized input \"%s\", "
526                                         "expect valid ipv4 addr",
527                                         tokens[ti]);
528                                 if (status->status < 0)
529                                         return;
530                                 rule->dst.ip.ip4 = rte_bswap32(
531                                         (uint32_t)ip.s_addr);
532                         } else if (rule->flags == IP6_TUNNEL) {
533                                 struct in6_addr ip;
534
535                                 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
536                                         NULL) == 0, status,
537                                         "unrecognized input \"%s\", "
538                                         "expect valid ipv6 addr",
539                                         tokens[ti]);
540                                 if (status->status < 0)
541                                         return;
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]);
546                                 return;
547                         }
548
549                         dst_p = 1;
550                         continue;
551                 }
552
553                 /* unrecognizeable input */
554                 APP_CHECK(0, status, "unrecognized input \"%s\"",
555                         tokens[ti]);
556                 return;
557         }
558
559         if (aead_algo_p) {
560                 APP_CHECK(cipher_algo_p == 0, status,
561                                 "AEAD used, no need for cipher options");
562                 if (status->status < 0)
563                         return;
564
565                 APP_CHECK(auth_algo_p == 0, status,
566                                 "AEAD used, no need for auth options");
567                 if (status->status < 0)
568                         return;
569         } else {
570                 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
571                 if (status->status < 0)
572                         return;
573
574                 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
575                 if (status->status < 0)
576                         return;
577         }
578
579         APP_CHECK(mode_p == 1, status, "missing mode option");
580         if (status->status < 0)
581                 return;
582
583         *ri = *ri + 1;
584 }
585
586 static inline void
587 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
588 {
589         uint32_t i;
590         uint8_t a, b, c, d;
591
592         printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
593
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);
597                         break;
598                 }
599         }
600
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);
604                         break;
605                 }
606         }
607
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);
611                         break;
612                 }
613         }
614
615         printf("mode:");
616
617         switch (sa->flags) {
618         case IP4_TUNNEL:
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);
624                 break;
625         case IP6_TUNNEL:
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]);
630                         else
631                                 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
632                 }
633                 printf(" ");
634                 for (i = 0; i < 16; i++) {
635                         if (i % 2 && i != 15)
636                                 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
637                         else
638                                 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
639                 }
640                 break;
641         case TRANSPORT:
642                 printf("Transport");
643                 break;
644         }
645         printf("\n");
646 }
647
648 struct sa_ctx {
649         struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
650         struct {
651                 struct rte_crypto_sym_xform a;
652                 struct rte_crypto_sym_xform b;
653         } xf[IPSEC_SA_MAX_ENTRIES];
654 };
655
656 static struct sa_ctx *
657 sa_create(const char *name, int32_t socket_id)
658 {
659         char s[PATH_MAX];
660         struct sa_ctx *sa_ctx;
661         uint32_t mz_size;
662         const struct rte_memzone *mz;
663
664         snprintf(s, sizeof(s), "%s_%u", name, socket_id);
665
666         /* Create SA array table */
667         printf("Creating SA context with %u maximum entries\n",
668                         IPSEC_SA_MAX_ENTRIES);
669
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);
673         if (mz == NULL) {
674                 printf("Failed to allocate SA DB memory\n");
675                 rte_errno = -ENOMEM;
676                 return NULL;
677         }
678
679         sa_ctx = (struct sa_ctx *)mz->addr;
680
681         return sa_ctx;
682 }
683
684 static int
685 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
686                 uint32_t nb_entries, uint32_t inbound)
687 {
688         struct ipsec_sa *sa;
689         uint32_t i, idx;
690         uint16_t iv_length;
691
692         for (i = 0; i < nb_entries; i++) {
693                 idx = SPI2IDX(entries[i].spi);
694                 sa = &sa_ctx->sa[idx];
695                 if (sa->spi != 0) {
696                         printf("Index %u already in use by SPI %u\n",
697                                         idx, sa->spi);
698                         return -EINVAL;
699                 }
700                 *sa = entries[i];
701                 sa->seq = 0;
702
703                 switch (sa->flags) {
704                 case IP4_TUNNEL:
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);
707                 }
708
709                 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
710                         iv_length = 16;
711
712                         if (inbound) {
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 =
717                                         sa->cipher_key_len;
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 =
724                                         sa->aad_len;
725                                 sa_ctx->xf[idx].a.aead.digest_length =
726                                         sa->digest_len;
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 =
732                                         sa->cipher_key_len;
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 =
739                                         sa->aad_len;
740                                 sa_ctx->xf[idx].a.aead.digest_length =
741                                         sa->digest_len;
742                         }
743
744                         sa->xforms = &sa_ctx->xf[idx].a;
745
746                         print_one_sa_rule(sa, inbound);
747                 } else {
748                         switch (sa->cipher_algo) {
749                         case RTE_CRYPTO_CIPHER_NULL:
750                         case RTE_CRYPTO_CIPHER_AES_CBC:
751                                 iv_length = sa->iv_len;
752                                 break;
753                         case RTE_CRYPTO_CIPHER_AES_CTR:
754                                 iv_length = 16;
755                                 break;
756                         default:
757                                 RTE_LOG(ERR, IPSEC_ESP,
758                                                 "unsupported cipher algorithm %u\n",
759                                                 sa->cipher_algo);
760                                 return -EINVAL;
761                         }
762
763                         if (inbound) {
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 =
768                                         sa->cipher_key_len;
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;
774
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 =
779                                         sa->auth_key_len;
780                                 sa_ctx->xf[idx].a.auth.digest_length =
781                                         sa->digest_len;
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 =
789                                         sa->cipher_key_len;
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;
795
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 =
800                                         sa->auth_key_len;
801                                 sa_ctx->xf[idx].b.auth.digest_length =
802                                         sa->digest_len;
803                                 sa_ctx->xf[idx].b.auth.op =
804                                         RTE_CRYPTO_AUTH_OP_GENERATE;
805                         }
806
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;
810
811                         print_one_sa_rule(sa, inbound);
812                 }
813         }
814
815         return 0;
816 }
817
818 static inline int
819 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
820                 uint32_t nb_entries)
821 {
822         return sa_add_rules(sa_ctx, entries, nb_entries, 0);
823 }
824
825 static inline int
826 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
827                 uint32_t nb_entries)
828 {
829         return sa_add_rules(sa_ctx, entries, nb_entries, 1);
830 }
831
832 void
833 sa_init(struct socket_ctx *ctx, int32_t socket_id)
834 {
835         const char *name;
836
837         if (ctx == NULL)
838                 rte_exit(EXIT_FAILURE, "NULL context.\n");
839
840         if (ctx->sa_in != NULL)
841                 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
842                                 "initialized\n", socket_id);
843
844         if (ctx->sa_out != NULL)
845                 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
846                                 "initialized\n", socket_id);
847
848         if (nb_sa_in > 0) {
849                 name = "sa_in";
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,
854                                 name, socket_id);
855
856                 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
857         } else
858                 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
859
860         if (nb_sa_out > 0) {
861                 name = "sa_out";
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,
866                                 name, socket_id);
867
868                 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
869         } else
870                 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
871                         "specified\n");
872 }
873
874 int
875 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
876 {
877         struct ipsec_mbuf_metadata *priv;
878
879         priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
880
881         return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
882 }
883
884 static inline void
885 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
886                 struct ipsec_sa **sa_ret)
887 {
888         struct esp_hdr *esp;
889         struct ip *ip;
890         uint32_t *src4_addr;
891         uint8_t *src6_addr;
892         struct ipsec_sa *sa;
893
894         *sa_ret = NULL;
895
896         ip = rte_pktmbuf_mtod(pkt, struct ip *);
897         if (ip->ip_v == IPVERSION)
898                 esp = (struct esp_hdr *)(ip + 1);
899         else
900                 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
901
902         if (esp->spi == INVALID_SPI)
903                 return;
904
905         sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
906         if (rte_be_to_cpu_32(esp->spi) != sa->spi)
907                 return;
908
909         switch (sa->flags) {
910         case IP4_TUNNEL:
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)))
915                         *sa_ret = sa;
916                 break;
917         case IP6_TUNNEL:
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))
922                         *sa_ret = sa;
923                 break;
924         case TRANSPORT:
925                 *sa_ret = sa;
926         }
927 }
928
929 void
930 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
931                 struct ipsec_sa *sa[], uint16_t nb_pkts)
932 {
933         uint32_t i;
934
935         for (i = 0; i < nb_pkts; i++)
936                 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
937 }
938
939 void
940 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
941                 struct ipsec_sa *sa[], uint16_t nb_pkts)
942 {
943         uint32_t i;
944
945         for (i = 0; i < nb_pkts; i++)
946                 sa[i] = &sa_ctx->sa[sa_idx[i]];
947 }