examples/ipsec-secgw: add AES-CTR
[dpdk.git] / examples / ipsec-secgw / sa.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 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
49 #include "ipsec.h"
50 #include "esp.h"
51 #include "parser.h"
52
53 struct supported_cipher_algo {
54         const char *keyword;
55         enum rte_crypto_cipher_algorithm algo;
56         uint16_t iv_len;
57         uint16_t block_size;
58         uint16_t key_len;
59 };
60
61 struct supported_auth_algo {
62         const char *keyword;
63         enum rte_crypto_auth_algorithm algo;
64         uint16_t digest_len;
65         uint16_t key_len;
66         uint8_t aad_len;
67         uint8_t key_not_req;
68 };
69
70 const struct supported_cipher_algo cipher_algos[] = {
71         {
72                 .keyword = "null",
73                 .algo = RTE_CRYPTO_CIPHER_NULL,
74                 .iv_len = 0,
75                 .block_size = 4,
76                 .key_len = 0
77         },
78         {
79                 .keyword = "aes-128-cbc",
80                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
81                 .iv_len = 16,
82                 .block_size = 16,
83                 .key_len = 16
84         },
85         {
86                 .keyword = "aes-128-gcm",
87                 .algo = RTE_CRYPTO_CIPHER_AES_GCM,
88                 .iv_len = 8,
89                 .block_size = 4,
90                 .key_len = 16
91         },
92         {
93                 .keyword = "aes-128-ctr",
94                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
95                 .iv_len = 8,
96                 .block_size = 16, /* XXX AESNI MB limition, should be 4 */
97                 .key_len = 16
98         }
99 };
100
101 const struct supported_auth_algo auth_algos[] = {
102         {
103                 .keyword = "null",
104                 .algo = RTE_CRYPTO_AUTH_NULL,
105                 .digest_len = 0,
106                 .key_len = 0,
107                 .key_not_req = 1
108         },
109         {
110                 .keyword = "sha1-hmac",
111                 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
112                 .digest_len = 12,
113                 .key_len = 20
114         },
115         {
116                 .keyword = "aes-128-gcm",
117                 .algo = RTE_CRYPTO_AUTH_AES_GCM,
118                 .digest_len = 16,
119                 .key_len = 16,
120                 .aad_len = 8,
121                 .key_not_req = 1
122         }
123 };
124
125 struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
126 uint32_t nb_sa_out;
127
128 struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
129 uint32_t nb_sa_in;
130
131 static const struct supported_cipher_algo *
132 find_match_cipher_algo(const char *cipher_keyword)
133 {
134         size_t i;
135
136         for (i = 0; i < RTE_DIM(cipher_algos); i++) {
137                 const struct supported_cipher_algo *algo =
138                         &cipher_algos[i];
139
140                 if (strcmp(cipher_keyword, algo->keyword) == 0)
141                         return algo;
142         }
143
144         return NULL;
145 }
146
147 static const struct supported_auth_algo *
148 find_match_auth_algo(const char *auth_keyword)
149 {
150         size_t i;
151
152         for (i = 0; i < RTE_DIM(auth_algos); i++) {
153                 const struct supported_auth_algo *algo =
154                         &auth_algos[i];
155
156                 if (strcmp(auth_keyword, algo->keyword) == 0)
157                         return algo;
158         }
159
160         return NULL;
161 }
162
163 /** parse_key_string
164  *  parse x:x:x:x.... hex number key string into uint8_t *key
165  *  return:
166  *  > 0: number of bytes parsed
167  *  0:   failed
168  */
169 static uint32_t
170 parse_key_string(const char *key_str, uint8_t *key)
171 {
172         const char *pt_start = key_str, *pt_end = key_str;
173         char sub_str[3];
174         uint32_t nb_bytes = 0;
175
176         while (pt_end != NULL) {
177                 pt_end = strchr(pt_start, ':');
178
179                 if (pt_end == NULL)
180                         strncpy(sub_str, pt_start, strlen(pt_start));
181                 else {
182                         if (pt_end - pt_start > 2)
183                                 return 0;
184
185                         strncpy(sub_str, pt_start, pt_end - pt_start);
186                         pt_start = pt_end + 1;
187                 }
188
189                 key[nb_bytes++] = strtol(sub_str, NULL, 16);
190         }
191
192         return nb_bytes;
193 }
194
195 void
196 parse_sa_tokens(char **tokens, uint32_t n_tokens,
197         struct parse_status *status)
198 {
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;
204         uint32_t src_p = 0;
205         uint32_t dst_p = 0;
206         uint32_t mode_p = 0;
207
208         if (strcmp(tokens[0], "in") == 0) {
209                 ri = &nb_sa_in;
210
211                 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
212                         "too many sa rules, abort insertion\n");
213                 if (status->status < 0)
214                         return;
215
216                 rule = &sa_in[*ri];
217         } else {
218                 ri = &nb_sa_out;
219
220                 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
221                         "too many sa rules, abort insertion\n");
222                 if (status->status < 0)
223                         return;
224
225                 rule = &sa_out[*ri];
226         }
227
228         /* spi number */
229         APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
230         if (status->status < 0)
231                 return;
232         rule->spi = atoi(tokens[1]);
233
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)
238                                 return;
239
240                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
241                         if (status->status < 0)
242                                 return;
243
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;
250                         else {
251                                 APP_CHECK(0, status, "unrecognized "
252                                         "input \"%s\"", tokens[ti]);
253                                 return;
254                         }
255
256                         mode_p = 1;
257                         continue;
258                 }
259
260                 if (strcmp(tokens[ti], "cipher_algo") == 0) {
261                         const struct supported_cipher_algo *algo;
262                         uint32_t key_len;
263
264                         APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
265                                 status);
266                         if (status->status < 0)
267                                 return;
268
269                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
270                         if (status->status < 0)
271                                 return;
272
273                         algo = find_match_cipher_algo(tokens[ti]);
274
275                         APP_CHECK(algo != NULL, status, "unrecognized "
276                                 "input \"%s\"", tokens[ti]);
277
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;
282
283                         /* for NULL algorithm, no cipher key required */
284                         if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
285                                 cipher_algo_p = 1;
286                                 continue;
287                         }
288
289                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
290                         if (status->status < 0)
291                                 return;
292
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)
297                                 return;
298
299                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
300                         if (status->status < 0)
301                                 return;
302
303                         key_len = parse_key_string(tokens[ti],
304                                 rule->cipher_key);
305                         APP_CHECK(key_len == rule->cipher_key_len, status,
306                                 "unrecognized input \"%s\"", tokens[ti]);
307                         if (status->status < 0)
308                                 return;
309
310                         cipher_algo_p = 1;
311                         continue;
312                 }
313
314                 if (strcmp(tokens[ti], "auth_algo") == 0) {
315                         const struct supported_auth_algo *algo;
316                         uint32_t key_len;
317
318                         APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
319                                 status);
320                         if (status->status < 0)
321                                 return;
322
323                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
324                         if (status->status < 0)
325                                 return;
326
327                         algo = find_match_auth_algo(tokens[ti]);
328                         APP_CHECK(algo != NULL, status, "unrecognized "
329                                 "input \"%s\"", tokens[ti]);
330
331                         rule->auth_algo = algo->algo;
332                         rule->auth_key_len = algo->key_len;
333                         rule->digest_len = algo->digest_len;
334                         rule->aad_len = algo->key_len;
335
336                         /* NULL algorithm and combined algos do not
337                          * require auth key
338                          */
339                         if (algo->key_not_req) {
340                                 auth_algo_p = 1;
341                                 continue;
342                         }
343
344                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
345                         if (status->status < 0)
346                                 return;
347
348                         APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
349                                 status, "unrecognized input \"%s\", "
350                                 "expect \"auth_key\"", tokens[ti]);
351                         if (status->status < 0)
352                                 return;
353
354                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
355                         if (status->status < 0)
356                                 return;
357
358                         key_len = parse_key_string(tokens[ti],
359                                 rule->auth_key);
360                         APP_CHECK(key_len == rule->auth_key_len, status,
361                                 "unrecognized input \"%s\"", tokens[ti]);
362                         if (status->status < 0)
363                                 return;
364
365                         auth_algo_p = 1;
366                         continue;
367                 }
368
369                 if (strcmp(tokens[ti], "src") == 0) {
370                         APP_CHECK_PRESENCE(src_p, tokens[ti], status);
371                         if (status->status < 0)
372                                 return;
373
374                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
375                         if (status->status < 0)
376                                 return;
377
378                         if (rule->flags == IP4_TUNNEL) {
379                                 struct in_addr ip;
380
381                                 APP_CHECK(parse_ipv4_addr(tokens[ti],
382                                         &ip, NULL) == 0, status,
383                                         "unrecognized input \"%s\", "
384                                         "expect valid ipv4 addr",
385                                         tokens[ti]);
386                                 if (status->status < 0)
387                                         return;
388                                 rule->src.ip.ip4 = rte_bswap32(
389                                         (uint32_t)ip.s_addr);
390                         } else if (rule->flags == IP6_TUNNEL) {
391                                 struct in6_addr ip;
392
393                                 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
394                                         NULL) == 0, status,
395                                         "unrecognized input \"%s\", "
396                                         "expect valid ipv6 addr",
397                                         tokens[ti]);
398                                 if (status->status < 0)
399                                         return;
400                                 memcpy(rule->src.ip.ip6.ip6_b,
401                                         ip.s6_addr, 16);
402                         } else if (rule->flags == TRANSPORT) {
403                                 APP_CHECK(0, status, "unrecognized input "
404                                         "\"%s\"", tokens[ti]);
405                                 return;
406                         }
407
408                         src_p = 1;
409                         continue;
410                 }
411
412                 if (strcmp(tokens[ti], "dst") == 0) {
413                         APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
414                         if (status->status < 0)
415                                 return;
416
417                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
418                         if (status->status < 0)
419                                 return;
420
421                         if (rule->flags == IP4_TUNNEL) {
422                                 struct in_addr ip;
423
424                                 APP_CHECK(parse_ipv4_addr(tokens[ti],
425                                         &ip, NULL) == 0, status,
426                                         "unrecognized input \"%s\", "
427                                         "expect valid ipv4 addr",
428                                         tokens[ti]);
429                                 if (status->status < 0)
430                                         return;
431                                 rule->dst.ip.ip4 = rte_bswap32(
432                                         (uint32_t)ip.s_addr);
433                         } else if (rule->flags == IP6_TUNNEL) {
434                                 struct in6_addr ip;
435
436                                 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
437                                         NULL) == 0, status,
438                                         "unrecognized input \"%s\", "
439                                         "expect valid ipv6 addr",
440                                         tokens[ti]);
441                                 if (status->status < 0)
442                                         return;
443                                 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
444                         } else if (rule->flags == TRANSPORT) {
445                                 APP_CHECK(0, status, "unrecognized "
446                                         "input \"%s\"", tokens[ti]);
447                                 return;
448                         }
449
450                         dst_p = 1;
451                         continue;
452                 }
453
454                 /* unrecognizeable input */
455                 APP_CHECK(0, status, "unrecognized input \"%s\"",
456                         tokens[ti]);
457                 return;
458         }
459
460         APP_CHECK(cipher_algo_p == 1, status, "missing cipher options");
461         if (status->status < 0)
462                 return;
463
464         APP_CHECK(auth_algo_p == 1, status, "missing auth options");
465         if (status->status < 0)
466                 return;
467
468         APP_CHECK(mode_p == 1, status, "missing mode option");
469         if (status->status < 0)
470                 return;
471
472         *ri = *ri + 1;
473 }
474
475 static inline void
476 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
477 {
478         uint32_t i;
479         uint8_t a, b, c, d;
480
481         printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
482
483         for (i = 0; i < RTE_DIM(cipher_algos); i++) {
484                 if (cipher_algos[i].algo == sa->cipher_algo) {
485                         printf("%s ", cipher_algos[i].keyword);
486                         break;
487                 }
488         }
489
490         for (i = 0; i < RTE_DIM(auth_algos); i++) {
491                 if (auth_algos[i].algo == sa->auth_algo) {
492                         printf("%s ", auth_algos[i].keyword);
493                         break;
494                 }
495         }
496
497         printf("mode:");
498
499         switch (sa->flags) {
500         case IP4_TUNNEL:
501                 printf("IP4Tunnel ");
502                 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
503                 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
504                 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
505                 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
506                 break;
507         case IP6_TUNNEL:
508                 printf("IP6Tunnel ");
509                 for (i = 0; i < 16; i++) {
510                         if (i % 2 && i != 15)
511                                 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
512                         else
513                                 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
514                 }
515                 printf(" ");
516                 for (i = 0; i < 16; i++) {
517                         if (i % 2 && i != 15)
518                                 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
519                         else
520                                 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
521                 }
522                 break;
523         case TRANSPORT:
524                 printf("Transport");
525                 break;
526         }
527         printf("\n");
528 }
529
530 struct sa_ctx {
531         struct ipsec_sa sa[IPSEC_SA_MAX_ENTRIES];
532         struct {
533                 struct rte_crypto_sym_xform a;
534                 struct rte_crypto_sym_xform b;
535         } xf[IPSEC_SA_MAX_ENTRIES];
536 };
537
538 static struct sa_ctx *
539 sa_create(const char *name, int32_t socket_id)
540 {
541         char s[PATH_MAX];
542         struct sa_ctx *sa_ctx;
543         uint32_t mz_size;
544         const struct rte_memzone *mz;
545
546         snprintf(s, sizeof(s), "%s_%u", name, socket_id);
547
548         /* Create SA array table */
549         printf("Creating SA context with %u maximum entries\n",
550                         IPSEC_SA_MAX_ENTRIES);
551
552         mz_size = sizeof(struct sa_ctx);
553         mz = rte_memzone_reserve(s, mz_size, socket_id,
554                         RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
555         if (mz == NULL) {
556                 printf("Failed to allocate SA DB memory\n");
557                 rte_errno = -ENOMEM;
558                 return NULL;
559         }
560
561         sa_ctx = (struct sa_ctx *)mz->addr;
562
563         return sa_ctx;
564 }
565
566 static int
567 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
568                 uint32_t nb_entries, uint32_t inbound)
569 {
570         struct ipsec_sa *sa;
571         uint32_t i, idx;
572
573         for (i = 0; i < nb_entries; i++) {
574                 idx = SPI2IDX(entries[i].spi);
575                 sa = &sa_ctx->sa[idx];
576                 if (sa->spi != 0) {
577                         printf("Index %u already in use by SPI %u\n",
578                                         idx, sa->spi);
579                         return -EINVAL;
580                 }
581                 *sa = entries[i];
582                 sa->seq = 0;
583
584                 switch (sa->flags) {
585                 case IP4_TUNNEL:
586                         sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
587                         sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
588                 }
589
590                 if (inbound) {
591                         sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
592                         sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
593                         sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
594                         sa_ctx->xf[idx].b.cipher.key.length =
595                                 sa->cipher_key_len;
596                         sa_ctx->xf[idx].b.cipher.op =
597                                 RTE_CRYPTO_CIPHER_OP_DECRYPT;
598                         sa_ctx->xf[idx].b.next = NULL;
599
600                         sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
601                         sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
602                         sa_ctx->xf[idx].a.auth.add_auth_data_length =
603                                 sa->aad_len;
604                         sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
605                         sa_ctx->xf[idx].a.auth.key.length =
606                                 sa->auth_key_len;
607                         sa_ctx->xf[idx].a.auth.digest_length =
608                                 sa->digest_len;
609                         sa_ctx->xf[idx].a.auth.op =
610                                 RTE_CRYPTO_AUTH_OP_VERIFY;
611
612                 } else { /* outbound */
613                         sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
614                         sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
615                         sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
616                         sa_ctx->xf[idx].a.cipher.key.length =
617                                 sa->cipher_key_len;
618                         sa_ctx->xf[idx].a.cipher.op =
619                                 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
620                         sa_ctx->xf[idx].a.next = NULL;
621
622                         sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
623                         sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
624                         sa_ctx->xf[idx].b.auth.add_auth_data_length =
625                                 sa->aad_len;
626                         sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
627                         sa_ctx->xf[idx].b.auth.key.length =
628                                 sa->auth_key_len;
629                         sa_ctx->xf[idx].b.auth.digest_length =
630                                 sa->digest_len;
631                         sa_ctx->xf[idx].b.auth.op =
632                                 RTE_CRYPTO_AUTH_OP_GENERATE;
633                 }
634
635                 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
636                 sa_ctx->xf[idx].b.next = NULL;
637                 sa->xforms = &sa_ctx->xf[idx].a;
638
639                 print_one_sa_rule(sa, inbound);
640         }
641
642         return 0;
643 }
644
645 static inline int
646 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
647                 uint32_t nb_entries)
648 {
649         return sa_add_rules(sa_ctx, entries, nb_entries, 0);
650 }
651
652 static inline int
653 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
654                 uint32_t nb_entries)
655 {
656         return sa_add_rules(sa_ctx, entries, nb_entries, 1);
657 }
658
659 void
660 sa_init(struct socket_ctx *ctx, int32_t socket_id)
661 {
662         const char *name;
663
664         if (ctx == NULL)
665                 rte_exit(EXIT_FAILURE, "NULL context.\n");
666
667         if (ctx->sa_in != NULL)
668                 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
669                                 "initialized\n", socket_id);
670
671         if (ctx->sa_out != NULL)
672                 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
673                                 "initialized\n", socket_id);
674
675         if (nb_sa_in > 0) {
676                 name = "sa_in";
677                 ctx->sa_in = sa_create(name, socket_id);
678                 if (ctx->sa_in == NULL)
679                         rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
680                                 "context %s in socket %d\n", rte_errno,
681                                 name, socket_id);
682
683                 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in);
684         } else
685                 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
686
687         if (nb_sa_out > 0) {
688                 name = "sa_out";
689                 ctx->sa_out = sa_create(name, socket_id);
690                 if (ctx->sa_out == NULL)
691                         rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
692                                 "context %s in socket %d\n", rte_errno,
693                                 name, socket_id);
694
695                 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out);
696         } else
697                 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
698                         "specified\n");
699 }
700
701 int
702 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
703 {
704         struct ipsec_mbuf_metadata *priv;
705
706         priv = RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
707
708         return (sa_ctx->sa[sa_idx].spi == priv->sa->spi);
709 }
710
711 static inline void
712 single_inbound_lookup(struct ipsec_sa *sadb, struct rte_mbuf *pkt,
713                 struct ipsec_sa **sa_ret)
714 {
715         struct esp_hdr *esp;
716         struct ip *ip;
717         uint32_t *src4_addr;
718         uint8_t *src6_addr;
719         struct ipsec_sa *sa;
720
721         *sa_ret = NULL;
722
723         ip = rte_pktmbuf_mtod(pkt, struct ip *);
724         if (ip->ip_v == IPVERSION)
725                 esp = (struct esp_hdr *)(ip + 1);
726         else
727                 esp = (struct esp_hdr *)(((struct ip6_hdr *)ip) + 1);
728
729         if (esp->spi == INVALID_SPI)
730                 return;
731
732         sa = &sadb[SPI2IDX(rte_be_to_cpu_32(esp->spi))];
733         if (rte_be_to_cpu_32(esp->spi) != sa->spi)
734                 return;
735
736         switch (sa->flags) {
737         case IP4_TUNNEL:
738                 src4_addr = RTE_PTR_ADD(ip, offsetof(struct ip, ip_src));
739                 if ((ip->ip_v == IPVERSION) &&
740                                 (sa->src.ip.ip4 == *src4_addr) &&
741                                 (sa->dst.ip.ip4 == *(src4_addr + 1)))
742                         *sa_ret = sa;
743                 break;
744         case IP6_TUNNEL:
745                 src6_addr = RTE_PTR_ADD(ip, offsetof(struct ip6_hdr, ip6_src));
746                 if ((ip->ip_v == IP6_VERSION) &&
747                                 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
748                                 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
749                         *sa_ret = sa;
750                 break;
751         case TRANSPORT:
752                 *sa_ret = sa;
753         }
754 }
755
756 void
757 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
758                 struct ipsec_sa *sa[], uint16_t nb_pkts)
759 {
760         uint32_t i;
761
762         for (i = 0; i < nb_pkts; i++)
763                 single_inbound_lookup(sa_ctx->sa, pkts[i], &sa[i]);
764 }
765
766 void
767 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
768                 struct ipsec_sa *sa[], uint16_t nb_pkts)
769 {
770         uint32_t i;
771
772         for (i = 0; i < nb_pkts; i++)
773                 sa[i] = &sa_ctx->sa[sa_idx[i]];
774 }