examples/ipsec-secgw: fix AAD length setting
[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
379                         /* NULL algorithm and combined algos do not
380                          * require auth key
381                          */
382                         if (algo->key_not_req) {
383                                 auth_algo_p = 1;
384                                 continue;
385                         }
386
387                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
388                         if (status->status < 0)
389                                 return;
390
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)
395                                 return;
396
397                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
398                         if (status->status < 0)
399                                 return;
400
401                         key_len = parse_key_string(tokens[ti],
402                                 rule->auth_key);
403                         APP_CHECK(key_len == rule->auth_key_len, status,
404                                 "unrecognized input \"%s\"", tokens[ti]);
405                         if (status->status < 0)
406                                 return;
407
408                         auth_algo_p = 1;
409                         continue;
410                 }
411
412                 if (strcmp(tokens[ti], "aead_algo") == 0) {
413                         const struct supported_aead_algo *algo;
414                         uint32_t key_len;
415
416                         APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
417                                 status);
418                         if (status->status < 0)
419                                 return;
420
421                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
422                         if (status->status < 0)
423                                 return;
424
425                         algo = find_match_aead_algo(tokens[ti]);
426
427                         APP_CHECK(algo != NULL, status, "unrecognized "
428                                 "input \"%s\"", tokens[ti]);
429
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;
436
437                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
438                         if (status->status < 0)
439                                 return;
440
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)
445                                 return;
446
447                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
448                         if (status->status < 0)
449                                 return;
450
451                         key_len = parse_key_string(tokens[ti],
452                                 rule->cipher_key);
453                         APP_CHECK(key_len == rule->cipher_key_len, status,
454                                 "unrecognized input \"%s\"", tokens[ti]);
455                         if (status->status < 0)
456                                 return;
457
458                         key_len -= 4;
459                         rule->cipher_key_len = key_len;
460                         memcpy(&rule->salt,
461                                 &rule->cipher_key[key_len], 4);
462
463                         aead_algo_p = 1;
464                         continue;
465                 }
466
467                 if (strcmp(tokens[ti], "src") == 0) {
468                         APP_CHECK_PRESENCE(src_p, tokens[ti], status);
469                         if (status->status < 0)
470                                 return;
471
472                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
473                         if (status->status < 0)
474                                 return;
475
476                         if (rule->flags == IP4_TUNNEL) {
477                                 struct in_addr ip;
478
479                                 APP_CHECK(parse_ipv4_addr(tokens[ti],
480                                         &ip, NULL) == 0, status,
481                                         "unrecognized input \"%s\", "
482                                         "expect valid ipv4 addr",
483                                         tokens[ti]);
484                                 if (status->status < 0)
485                                         return;
486                                 rule->src.ip.ip4 = rte_bswap32(
487                                         (uint32_t)ip.s_addr);
488                         } else if (rule->flags == IP6_TUNNEL) {
489                                 struct in6_addr ip;
490
491                                 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
492                                         NULL) == 0, status,
493                                         "unrecognized input \"%s\", "
494                                         "expect valid ipv6 addr",
495                                         tokens[ti]);
496                                 if (status->status < 0)
497                                         return;
498                                 memcpy(rule->src.ip.ip6.ip6_b,
499                                         ip.s6_addr, 16);
500                         } else if (rule->flags == TRANSPORT) {
501                                 APP_CHECK(0, status, "unrecognized input "
502                                         "\"%s\"", tokens[ti]);
503                                 return;
504                         }
505
506                         src_p = 1;
507                         continue;
508                 }
509
510                 if (strcmp(tokens[ti], "dst") == 0) {
511                         APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
512                         if (status->status < 0)
513                                 return;
514
515                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
516                         if (status->status < 0)
517                                 return;
518
519                         if (rule->flags == IP4_TUNNEL) {
520                                 struct in_addr ip;
521
522                                 APP_CHECK(parse_ipv4_addr(tokens[ti],
523                                         &ip, NULL) == 0, status,
524                                         "unrecognized input \"%s\", "
525                                         "expect valid ipv4 addr",
526                                         tokens[ti]);
527                                 if (status->status < 0)
528                                         return;
529                                 rule->dst.ip.ip4 = rte_bswap32(
530                                         (uint32_t)ip.s_addr);
531                         } else if (rule->flags == IP6_TUNNEL) {
532                                 struct in6_addr ip;
533
534                                 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
535                                         NULL) == 0, status,
536                                         "unrecognized input \"%s\", "
537                                         "expect valid ipv6 addr",
538                                         tokens[ti]);
539                                 if (status->status < 0)
540                                         return;
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]);
545                                 return;
546                         }
547
548                         dst_p = 1;
549                         continue;
550                 }
551
552                 /* unrecognizeable input */
553                 APP_CHECK(0, status, "unrecognized input \"%s\"",
554                         tokens[ti]);
555                 return;
556         }
557
558         if (aead_algo_p) {
559                 APP_CHECK(cipher_algo_p == 0, status,
560                                 "AEAD used, no need for cipher options");
561                 if (status->status < 0)
562                         return;
563
564                 APP_CHECK(auth_algo_p == 0, status,
565                                 "AEAD used, no need for auth options");
566                 if (status->status < 0)
567                         return;
568         } else {
569                 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
570                 if (status->status < 0)
571                         return;
572
573                 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
574                 if (status->status < 0)
575                         return;
576         }
577
578         APP_CHECK(mode_p == 1, status, "missing mode option");
579         if (status->status < 0)
580                 return;
581
582         *ri = *ri + 1;
583 }
584
585 static inline void
586 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
587 {
588         uint32_t i;
589         uint8_t a, b, c, d;
590
591         printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
592
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);
596                         break;
597                 }
598         }
599
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);
603                         break;
604                 }
605         }
606
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);
610                         break;
611                 }
612         }
613
614         printf("mode:");
615
616         switch (sa->flags) {
617         case IP4_TUNNEL:
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);
623                 break;
624         case IP6_TUNNEL:
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]);
629                         else
630                                 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
631                 }
632                 printf(" ");
633                 for (i = 0; i < 16; i++) {
634                         if (i % 2 && i != 15)
635                                 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
636                         else
637                                 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
638                 }
639                 break;
640         case TRANSPORT:
641                 printf("Transport");
642                 break;
643         }
644         printf("\n");
645 }
646
647 struct sa_ctx {
648         struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
649         struct {
650                 struct rte_crypto_sym_xform a;
651                 struct rte_crypto_sym_xform b;
652         } xf[IPSEC_SA_MAX_ENTRIES];
653 };
654
655 static struct sa_ctx *
656 sa_create(const char *name, int32_t socket_id)
657 {
658         char s[PATH_MAX];
659         struct sa_ctx *sa_ctx;
660         uint32_t mz_size;
661         const struct rte_memzone *mz;
662
663         snprintf(s, sizeof(s), "%s_%u", name, socket_id);
664
665         /* Create SA array table */
666         printf("Creating SA context with %u maximum entries\n",
667                         IPSEC_SA_MAX_ENTRIES);
668
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);
672         if (mz == NULL) {
673                 printf("Failed to allocate SA DB memory\n");
674                 rte_errno = -ENOMEM;
675                 return NULL;
676         }
677
678         sa_ctx = (struct sa_ctx *)mz->addr;
679
680         return sa_ctx;
681 }
682
683 static int
684 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
685                 uint32_t nb_entries, uint32_t inbound)
686 {
687         struct ipsec_sa *sa;
688         uint32_t i, idx;
689         uint16_t iv_length;
690
691         for (i = 0; i < nb_entries; i++) {
692                 idx = SPI2IDX(entries[i].spi);
693                 sa = &sa_ctx->sa[idx];
694                 if (sa->spi != 0) {
695                         printf("Index %u already in use by SPI %u\n",
696                                         idx, sa->spi);
697                         return -EINVAL;
698                 }
699                 *sa = entries[i];
700                 sa->seq = 0;
701
702                 switch (sa->flags) {
703                 case IP4_TUNNEL:
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);
706                 }
707
708                 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
709                         iv_length = 16;
710
711                         if (inbound) {
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 =
716                                         sa->cipher_key_len;
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 =
723                                         sa->aad_len;
724                                 sa_ctx->xf[idx].a.aead.digest_length =
725                                         sa->digest_len;
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 =
731                                         sa->cipher_key_len;
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 =
738                                         sa->aad_len;
739                                 sa_ctx->xf[idx].a.aead.digest_length =
740                                         sa->digest_len;
741                         }
742
743                         sa->xforms = &sa_ctx->xf[idx].a;
744
745                         print_one_sa_rule(sa, inbound);
746                 } else {
747                         switch (sa->cipher_algo) {
748                         case RTE_CRYPTO_CIPHER_NULL:
749                         case RTE_CRYPTO_CIPHER_AES_CBC:
750                                 iv_length = sa->iv_len;
751                                 break;
752                         case RTE_CRYPTO_CIPHER_AES_CTR:
753                                 iv_length = 16;
754                                 break;
755                         default:
756                                 RTE_LOG(ERR, IPSEC_ESP,
757                                                 "unsupported cipher algorithm %u\n",
758                                                 sa->cipher_algo);
759                                 return -EINVAL;
760                         }
761
762                         if (inbound) {
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 =
767                                         sa->cipher_key_len;
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;
773
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 =
778                                         sa->auth_key_len;
779                                 sa_ctx->xf[idx].a.auth.digest_length =
780                                         sa->digest_len;
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 =
788                                         sa->cipher_key_len;
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;
794
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 =
799                                         sa->auth_key_len;
800                                 sa_ctx->xf[idx].b.auth.digest_length =
801                                         sa->digest_len;
802                                 sa_ctx->xf[idx].b.auth.op =
803                                         RTE_CRYPTO_AUTH_OP_GENERATE;
804                         }
805
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;
809
810                         print_one_sa_rule(sa, inbound);
811                 }
812         }
813
814         return 0;
815 }
816
817 static inline int
818 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
819                 uint32_t nb_entries)
820 {
821         return sa_add_rules(sa_ctx, entries, nb_entries, 0);
822 }
823
824 static inline int
825 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
826                 uint32_t nb_entries)
827 {
828         return sa_add_rules(sa_ctx, entries, nb_entries, 1);
829 }
830
831 void
832 sa_init(struct socket_ctx *ctx, int32_t socket_id)
833 {
834         const char *name;
835
836         if (ctx == NULL)
837                 rte_exit(EXIT_FAILURE, "NULL context.\n");
838
839         if (ctx->sa_in != NULL)
840                 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
841                                 "initialized\n", socket_id);
842
843         if (ctx->sa_out != NULL)
844                 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
845                                 "initialized\n", socket_id);
846
847         if (nb_sa_in > 0) {
848                 name = "sa_in";
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,
853                                 name, socket_id);
854
855                 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
856         } else
857                 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
858
859         if (nb_sa_out > 0) {
860                 name = "sa_out";
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,
865                                 name, socket_id);
866
867                 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
868         } else
869                 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
870                         "specified\n");
871 }
872
873 int
874 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
875 {
876         struct ipsec_mbuf_metadata *priv;
877
878         priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
879
880         return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
881 }
882
883 static inline void
884 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
885                 struct ipsec_sa **sa_ret)
886 {
887         struct esp_hdr *esp;
888         struct ip *ip;
889         uint32_t *src4_addr;
890         uint8_t *src6_addr;
891         struct ipsec_sa *sa;
892
893         *sa_ret = NULL;
894
895         ip = rte_pktmbuf_mtod(pkt, struct ip *);
896         if (ip->ip_v == IPVERSION)
897                 esp = (struct esp_hdr *)(ip + 1);
898         else
899                 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
900
901         if (esp->spi == INVALID_SPI)
902                 return;
903
904         sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
905         if (rte_be_to_cpu_32(esp->spi) != sa->spi)
906                 return;
907
908         switch (sa->flags) {
909         case IP4_TUNNEL:
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)))
914                         *sa_ret = sa;
915                 break;
916         case IP6_TUNNEL:
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))
921                         *sa_ret = sa;
922                 break;
923         case TRANSPORT:
924                 *sa_ret = sa;
925         }
926 }
927
928 void
929 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
930                 struct ipsec_sa *sa[], uint16_t nb_pkts)
931 {
932         uint32_t i;
933
934         for (i = 0; i < nb_pkts; i++)
935                 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
936 }
937
938 void
939 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
940                 struct ipsec_sa *sa[], uint16_t nb_pkts)
941 {
942         uint32_t i;
943
944         for (i = 0; i < nb_pkts; i++)
945                 sa[i] = &sa_ctx->sa[sa_idx[i]];
946 }