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