examples/ipsec-secgw: integrate inbound SAD
[dpdk.git] / examples / ipsec-secgw / sa.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4
5 /*
6  * Security Associations
7  */
8 #include <sys/types.h>
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
11 #include <netinet/ip6.h>
12
13 #include <rte_memzone.h>
14 #include <rte_crypto.h>
15 #include <rte_security.h>
16 #include <rte_cryptodev.h>
17 #include <rte_byteorder.h>
18 #include <rte_errno.h>
19 #include <rte_ip.h>
20 #include <rte_random.h>
21 #include <rte_ethdev.h>
22 #include <rte_malloc.h>
23
24 #include "ipsec.h"
25 #include "esp.h"
26 #include "parser.h"
27 #include "sad.h"
28
29 #define IPDEFTTL 64
30
31 #define IP4_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip4) * CHAR_BIT)
32
33 #define IP6_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip6.ip6) * CHAR_BIT)
34
35 #define MBUF_NO_SEC_OFFLOAD(m) ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)
36
37 struct supported_cipher_algo {
38         const char *keyword;
39         enum rte_crypto_cipher_algorithm algo;
40         uint16_t iv_len;
41         uint16_t block_size;
42         uint16_t key_len;
43 };
44
45 struct supported_auth_algo {
46         const char *keyword;
47         enum rte_crypto_auth_algorithm algo;
48         uint16_t digest_len;
49         uint16_t key_len;
50         uint8_t key_not_req;
51 };
52
53 struct supported_aead_algo {
54         const char *keyword;
55         enum rte_crypto_aead_algorithm algo;
56         uint16_t iv_len;
57         uint16_t block_size;
58         uint16_t digest_len;
59         uint16_t key_len;
60         uint8_t aad_len;
61 };
62
63
64 const struct supported_cipher_algo cipher_algos[] = {
65         {
66                 .keyword = "null",
67                 .algo = RTE_CRYPTO_CIPHER_NULL,
68                 .iv_len = 0,
69                 .block_size = 4,
70                 .key_len = 0
71         },
72         {
73                 .keyword = "aes-128-cbc",
74                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
75                 .iv_len = 16,
76                 .block_size = 16,
77                 .key_len = 16
78         },
79         {
80                 .keyword = "aes-256-cbc",
81                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
82                 .iv_len = 16,
83                 .block_size = 16,
84                 .key_len = 32
85         },
86         {
87                 .keyword = "aes-128-ctr",
88                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
89                 .iv_len = 8,
90                 .block_size = 4,
91                 .key_len = 20
92         },
93         {
94                 .keyword = "3des-cbc",
95                 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
96                 .iv_len = 8,
97                 .block_size = 8,
98                 .key_len = 24
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 = "sha256-hmac",
118                 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
119                 .digest_len = 16,
120                 .key_len = 32
121         }
122 };
123
124 const struct supported_aead_algo aead_algos[] = {
125         {
126                 .keyword = "aes-128-gcm",
127                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
128                 .iv_len = 8,
129                 .block_size = 4,
130                 .key_len = 20,
131                 .digest_len = 16,
132                 .aad_len = 8,
133         }
134 };
135
136 static struct ipsec_sa sa_out[IPSEC_SA_MAX_ENTRIES];
137 static uint32_t nb_sa_out;
138 static struct ipsec_sa_cnt sa_out_cnt;
139
140 static struct ipsec_sa sa_in[IPSEC_SA_MAX_ENTRIES];
141 static uint32_t nb_sa_in;
142 static struct ipsec_sa_cnt sa_in_cnt;
143
144 static const struct supported_cipher_algo *
145 find_match_cipher_algo(const char *cipher_keyword)
146 {
147         size_t i;
148
149         for (i = 0; i < RTE_DIM(cipher_algos); i++) {
150                 const struct supported_cipher_algo *algo =
151                         &cipher_algos[i];
152
153                 if (strcmp(cipher_keyword, algo->keyword) == 0)
154                         return algo;
155         }
156
157         return NULL;
158 }
159
160 static const struct supported_auth_algo *
161 find_match_auth_algo(const char *auth_keyword)
162 {
163         size_t i;
164
165         for (i = 0; i < RTE_DIM(auth_algos); i++) {
166                 const struct supported_auth_algo *algo =
167                         &auth_algos[i];
168
169                 if (strcmp(auth_keyword, algo->keyword) == 0)
170                         return algo;
171         }
172
173         return NULL;
174 }
175
176 static const struct supported_aead_algo *
177 find_match_aead_algo(const char *aead_keyword)
178 {
179         size_t i;
180
181         for (i = 0; i < RTE_DIM(aead_algos); i++) {
182                 const struct supported_aead_algo *algo =
183                         &aead_algos[i];
184
185                 if (strcmp(aead_keyword, algo->keyword) == 0)
186                         return algo;
187         }
188
189         return NULL;
190 }
191
192 /** parse_key_string
193  *  parse x:x:x:x.... hex number key string into uint8_t *key
194  *  return:
195  *  > 0: number of bytes parsed
196  *  0:   failed
197  */
198 static uint32_t
199 parse_key_string(const char *key_str, uint8_t *key)
200 {
201         const char *pt_start = key_str, *pt_end = key_str;
202         uint32_t nb_bytes = 0;
203
204         while (pt_end != NULL) {
205                 char sub_str[3] = {0};
206
207                 pt_end = strchr(pt_start, ':');
208
209                 if (pt_end == NULL) {
210                         if (strlen(pt_start) > 2)
211                                 return 0;
212                         strncpy(sub_str, pt_start, 2);
213                 } else {
214                         if (pt_end - pt_start > 2)
215                                 return 0;
216
217                         strncpy(sub_str, pt_start, pt_end - pt_start);
218                         pt_start = pt_end + 1;
219                 }
220
221                 key[nb_bytes++] = strtol(sub_str, NULL, 16);
222         }
223
224         return nb_bytes;
225 }
226
227 void
228 parse_sa_tokens(char **tokens, uint32_t n_tokens,
229         struct parse_status *status)
230 {
231         struct ipsec_sa *rule = NULL;
232         struct rte_ipsec_session *ips;
233         uint32_t ti; /*token index*/
234         uint32_t *ri /*rule index*/;
235         struct ipsec_sa_cnt *sa_cnt;
236         uint32_t cipher_algo_p = 0;
237         uint32_t auth_algo_p = 0;
238         uint32_t aead_algo_p = 0;
239         uint32_t src_p = 0;
240         uint32_t dst_p = 0;
241         uint32_t mode_p = 0;
242         uint32_t type_p = 0;
243         uint32_t portid_p = 0;
244         uint32_t fallback_p = 0;
245
246         if (strcmp(tokens[0], "in") == 0) {
247                 ri = &nb_sa_in;
248                 sa_cnt = &sa_in_cnt;
249
250                 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
251                         "too many sa rules, abort insertion\n");
252                 if (status->status < 0)
253                         return;
254
255                 rule = &sa_in[*ri];
256                 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
257         } else {
258                 ri = &nb_sa_out;
259                 sa_cnt = &sa_out_cnt;
260
261                 APP_CHECK(*ri <= IPSEC_SA_MAX_ENTRIES - 1, status,
262                         "too many sa rules, abort insertion\n");
263                 if (status->status < 0)
264                         return;
265
266                 rule = &sa_out[*ri];
267                 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
268         }
269
270         /* spi number */
271         APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
272         if (status->status < 0)
273                 return;
274         if (atoi(tokens[1]) == INVALID_SPI)
275                 return;
276         rule->spi = atoi(tokens[1]);
277         ips = ipsec_get_primary_session(rule);
278
279         for (ti = 2; ti < n_tokens; ti++) {
280                 if (strcmp(tokens[ti], "mode") == 0) {
281                         APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
282                         if (status->status < 0)
283                                 return;
284
285                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
286                         if (status->status < 0)
287                                 return;
288
289                         if (strcmp(tokens[ti], "ipv4-tunnel") == 0) {
290                                 sa_cnt->nb_v4++;
291                                 rule->flags = IP4_TUNNEL;
292                         } else if (strcmp(tokens[ti], "ipv6-tunnel") == 0) {
293                                 sa_cnt->nb_v6++;
294                                 rule->flags = IP6_TUNNEL;
295                         } else if (strcmp(tokens[ti], "transport") == 0) {
296                                 sa_cnt->nb_v4++;
297                                 sa_cnt->nb_v6++;
298                                 rule->flags = TRANSPORT;
299                         } else {
300                                 APP_CHECK(0, status, "unrecognized "
301                                         "input \"%s\"", tokens[ti]);
302                                 return;
303                         }
304
305                         mode_p = 1;
306                         continue;
307                 }
308
309                 if (strcmp(tokens[ti], "cipher_algo") == 0) {
310                         const struct supported_cipher_algo *algo;
311                         uint32_t key_len;
312
313                         APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
314                                 status);
315                         if (status->status < 0)
316                                 return;
317
318                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
319                         if (status->status < 0)
320                                 return;
321
322                         algo = find_match_cipher_algo(tokens[ti]);
323
324                         APP_CHECK(algo != NULL, status, "unrecognized "
325                                 "input \"%s\"", tokens[ti]);
326
327                         if (status->status < 0)
328                                 return;
329
330                         rule->cipher_algo = algo->algo;
331                         rule->block_size = algo->block_size;
332                         rule->iv_len = algo->iv_len;
333                         rule->cipher_key_len = algo->key_len;
334
335                         /* for NULL algorithm, no cipher key required */
336                         if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
337                                 cipher_algo_p = 1;
338                                 continue;
339                         }
340
341                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
342                         if (status->status < 0)
343                                 return;
344
345                         APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
346                                 status, "unrecognized input \"%s\", "
347                                 "expect \"cipher_key\"", tokens[ti]);
348                         if (status->status < 0)
349                                 return;
350
351                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
352                         if (status->status < 0)
353                                 return;
354
355                         key_len = parse_key_string(tokens[ti],
356                                 rule->cipher_key);
357                         APP_CHECK(key_len == rule->cipher_key_len, status,
358                                 "unrecognized input \"%s\"", tokens[ti]);
359                         if (status->status < 0)
360                                 return;
361
362                         if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC ||
363                                 algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC)
364                                 rule->salt = (uint32_t)rte_rand();
365
366                         if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
367                                 key_len -= 4;
368                                 rule->cipher_key_len = key_len;
369                                 memcpy(&rule->salt,
370                                         &rule->cipher_key[key_len], 4);
371                         }
372
373                         cipher_algo_p = 1;
374                         continue;
375                 }
376
377                 if (strcmp(tokens[ti], "auth_algo") == 0) {
378                         const struct supported_auth_algo *algo;
379                         uint32_t key_len;
380
381                         APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
382                                 status);
383                         if (status->status < 0)
384                                 return;
385
386                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
387                         if (status->status < 0)
388                                 return;
389
390                         algo = find_match_auth_algo(tokens[ti]);
391                         APP_CHECK(algo != NULL, status, "unrecognized "
392                                 "input \"%s\"", tokens[ti]);
393
394                         if (status->status < 0)
395                                 return;
396
397                         rule->auth_algo = algo->algo;
398                         rule->auth_key_len = algo->key_len;
399                         rule->digest_len = algo->digest_len;
400
401                         /* NULL algorithm and combined algos do not
402                          * require auth key
403                          */
404                         if (algo->key_not_req) {
405                                 auth_algo_p = 1;
406                                 continue;
407                         }
408
409                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
410                         if (status->status < 0)
411                                 return;
412
413                         APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
414                                 status, "unrecognized input \"%s\", "
415                                 "expect \"auth_key\"", tokens[ti]);
416                         if (status->status < 0)
417                                 return;
418
419                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
420                         if (status->status < 0)
421                                 return;
422
423                         key_len = parse_key_string(tokens[ti],
424                                 rule->auth_key);
425                         APP_CHECK(key_len == rule->auth_key_len, status,
426                                 "unrecognized input \"%s\"", tokens[ti]);
427                         if (status->status < 0)
428                                 return;
429
430                         auth_algo_p = 1;
431                         continue;
432                 }
433
434                 if (strcmp(tokens[ti], "aead_algo") == 0) {
435                         const struct supported_aead_algo *algo;
436                         uint32_t key_len;
437
438                         APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
439                                 status);
440                         if (status->status < 0)
441                                 return;
442
443                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
444                         if (status->status < 0)
445                                 return;
446
447                         algo = find_match_aead_algo(tokens[ti]);
448
449                         APP_CHECK(algo != NULL, status, "unrecognized "
450                                 "input \"%s\"", tokens[ti]);
451
452                         if (status->status < 0)
453                                 return;
454
455                         rule->aead_algo = algo->algo;
456                         rule->cipher_key_len = algo->key_len;
457                         rule->digest_len = algo->digest_len;
458                         rule->aad_len = algo->aad_len;
459                         rule->block_size = algo->block_size;
460                         rule->iv_len = algo->iv_len;
461
462                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
463                         if (status->status < 0)
464                                 return;
465
466                         APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
467                                 status, "unrecognized input \"%s\", "
468                                 "expect \"aead_key\"", tokens[ti]);
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                         key_len = parse_key_string(tokens[ti],
477                                 rule->cipher_key);
478                         APP_CHECK(key_len == rule->cipher_key_len, status,
479                                 "unrecognized input \"%s\"", tokens[ti]);
480                         if (status->status < 0)
481                                 return;
482
483                         key_len -= 4;
484                         rule->cipher_key_len = key_len;
485                         memcpy(&rule->salt,
486                                 &rule->cipher_key[key_len], 4);
487
488                         aead_algo_p = 1;
489                         continue;
490                 }
491
492                 if (strcmp(tokens[ti], "src") == 0) {
493                         APP_CHECK_PRESENCE(src_p, tokens[ti], status);
494                         if (status->status < 0)
495                                 return;
496
497                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
498                         if (status->status < 0)
499                                 return;
500
501                         if (IS_IP4_TUNNEL(rule->flags)) {
502                                 struct in_addr ip;
503
504                                 APP_CHECK(parse_ipv4_addr(tokens[ti],
505                                         &ip, NULL) == 0, status,
506                                         "unrecognized input \"%s\", "
507                                         "expect valid ipv4 addr",
508                                         tokens[ti]);
509                                 if (status->status < 0)
510                                         return;
511                                 rule->src.ip.ip4 = rte_bswap32(
512                                         (uint32_t)ip.s_addr);
513                         } else if (IS_IP6_TUNNEL(rule->flags)) {
514                                 struct in6_addr ip;
515
516                                 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
517                                         NULL) == 0, status,
518                                         "unrecognized input \"%s\", "
519                                         "expect valid ipv6 addr",
520                                         tokens[ti]);
521                                 if (status->status < 0)
522                                         return;
523                                 memcpy(rule->src.ip.ip6.ip6_b,
524                                         ip.s6_addr, 16);
525                         } else if (IS_TRANSPORT(rule->flags)) {
526                                 APP_CHECK(0, status, "unrecognized input "
527                                         "\"%s\"", tokens[ti]);
528                                 return;
529                         }
530
531                         src_p = 1;
532                         continue;
533                 }
534
535                 if (strcmp(tokens[ti], "dst") == 0) {
536                         APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
537                         if (status->status < 0)
538                                 return;
539
540                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
541                         if (status->status < 0)
542                                 return;
543
544                         if (IS_IP4_TUNNEL(rule->flags)) {
545                                 struct in_addr ip;
546
547                                 APP_CHECK(parse_ipv4_addr(tokens[ti],
548                                         &ip, NULL) == 0, status,
549                                         "unrecognized input \"%s\", "
550                                         "expect valid ipv4 addr",
551                                         tokens[ti]);
552                                 if (status->status < 0)
553                                         return;
554                                 rule->dst.ip.ip4 = rte_bswap32(
555                                         (uint32_t)ip.s_addr);
556                         } else if (IS_IP6_TUNNEL(rule->flags)) {
557                                 struct in6_addr ip;
558
559                                 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
560                                         NULL) == 0, status,
561                                         "unrecognized input \"%s\", "
562                                         "expect valid ipv6 addr",
563                                         tokens[ti]);
564                                 if (status->status < 0)
565                                         return;
566                                 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
567                         } else if (IS_TRANSPORT(rule->flags)) {
568                                 APP_CHECK(0, status, "unrecognized "
569                                         "input \"%s\"", tokens[ti]);
570                                 return;
571                         }
572
573                         dst_p = 1;
574                         continue;
575                 }
576
577                 if (strcmp(tokens[ti], "type") == 0) {
578                         APP_CHECK_PRESENCE(type_p, tokens[ti], status);
579                         if (status->status < 0)
580                                 return;
581
582                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
583                         if (status->status < 0)
584                                 return;
585
586                         if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
587                                 ips->type =
588                                         RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
589                         else if (strcmp(tokens[ti],
590                                         "inline-protocol-offload") == 0)
591                                 ips->type =
592                                 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
593                         else if (strcmp(tokens[ti],
594                                         "lookaside-protocol-offload") == 0)
595                                 ips->type =
596                                 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
597                         else if (strcmp(tokens[ti], "no-offload") == 0)
598                                 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
599                         else {
600                                 APP_CHECK(0, status, "Invalid input \"%s\"",
601                                                 tokens[ti]);
602                                 return;
603                         }
604
605                         type_p = 1;
606                         continue;
607                 }
608
609                 if (strcmp(tokens[ti], "port_id") == 0) {
610                         APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
611                         if (status->status < 0)
612                                 return;
613                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
614                         if (status->status < 0)
615                                 return;
616                         rule->portid = atoi(tokens[ti]);
617                         if (status->status < 0)
618                                 return;
619                         portid_p = 1;
620                         continue;
621                 }
622
623                 if (strcmp(tokens[ti], "fallback") == 0) {
624                         struct rte_ipsec_session *fb;
625
626                         APP_CHECK(app_sa_prm.enable, status, "Fallback session "
627                                 "not allowed for legacy mode.");
628                         if (status->status < 0)
629                                 return;
630                         APP_CHECK(ips->type ==
631                                 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, status,
632                                 "Fallback session allowed if primary session "
633                                 "is of type inline-crypto-offload only.");
634                         if (status->status < 0)
635                                 return;
636                         APP_CHECK(rule->direction ==
637                                 RTE_SECURITY_IPSEC_SA_DIR_INGRESS, status,
638                                 "Fallback session not allowed for egress "
639                                 "rule");
640                         if (status->status < 0)
641                                 return;
642                         APP_CHECK_PRESENCE(fallback_p, tokens[ti], status);
643                         if (status->status < 0)
644                                 return;
645                         INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
646                         if (status->status < 0)
647                                 return;
648                         fb = ipsec_get_fallback_session(rule);
649                         if (strcmp(tokens[ti], "lookaside-none") == 0) {
650                                 fb->type = RTE_SECURITY_ACTION_TYPE_NONE;
651                         } else {
652                                 APP_CHECK(0, status, "unrecognized fallback "
653                                         "type %s.", tokens[ti]);
654                                 return;
655                         }
656
657                         rule->fallback_sessions = 1;
658                         fallback_p = 1;
659                         continue;
660                 }
661
662                 /* unrecognizeable input */
663                 APP_CHECK(0, status, "unrecognized input \"%s\"",
664                         tokens[ti]);
665                 return;
666         }
667
668         if (aead_algo_p) {
669                 APP_CHECK(cipher_algo_p == 0, status,
670                                 "AEAD used, no need for cipher options");
671                 if (status->status < 0)
672                         return;
673
674                 APP_CHECK(auth_algo_p == 0, status,
675                                 "AEAD used, no need for auth options");
676                 if (status->status < 0)
677                         return;
678         } else {
679                 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
680                 if (status->status < 0)
681                         return;
682
683                 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
684                 if (status->status < 0)
685                         return;
686         }
687
688         APP_CHECK(mode_p == 1, status, "missing mode option");
689         if (status->status < 0)
690                 return;
691
692         if ((ips->type != RTE_SECURITY_ACTION_TYPE_NONE) && (portid_p == 0))
693                 printf("Missing portid option, falling back to non-offload\n");
694
695         if (!type_p || !portid_p) {
696                 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
697                 rule->portid = -1;
698         }
699
700         *ri = *ri + 1;
701 }
702
703 static void
704 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
705 {
706         uint32_t i;
707         uint8_t a, b, c, d;
708         const struct rte_ipsec_session *ips;
709         const struct rte_ipsec_session *fallback_ips;
710
711         printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
712
713         for (i = 0; i < RTE_DIM(cipher_algos); i++) {
714                 if (cipher_algos[i].algo == sa->cipher_algo &&
715                                 cipher_algos[i].key_len == sa->cipher_key_len) {
716                         printf("%s ", cipher_algos[i].keyword);
717                         break;
718                 }
719         }
720
721         for (i = 0; i < RTE_DIM(auth_algos); i++) {
722                 if (auth_algos[i].algo == sa->auth_algo) {
723                         printf("%s ", auth_algos[i].keyword);
724                         break;
725                 }
726         }
727
728         for (i = 0; i < RTE_DIM(aead_algos); i++) {
729                 if (aead_algos[i].algo == sa->aead_algo) {
730                         printf("%s ", aead_algos[i].keyword);
731                         break;
732                 }
733         }
734
735         printf("mode:");
736
737         switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
738         case IP4_TUNNEL:
739                 printf("IP4Tunnel ");
740                 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
741                 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
742                 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
743                 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
744                 break;
745         case IP6_TUNNEL:
746                 printf("IP6Tunnel ");
747                 for (i = 0; i < 16; i++) {
748                         if (i % 2 && i != 15)
749                                 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
750                         else
751                                 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
752                 }
753                 printf(" ");
754                 for (i = 0; i < 16; i++) {
755                         if (i % 2 && i != 15)
756                                 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
757                         else
758                                 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
759                 }
760                 break;
761         case TRANSPORT:
762                 printf("Transport ");
763                 break;
764         }
765
766         ips = &sa->sessions[IPSEC_SESSION_PRIMARY];
767         printf(" type:");
768         switch (ips->type) {
769         case RTE_SECURITY_ACTION_TYPE_NONE:
770                 printf("no-offload ");
771                 break;
772         case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
773                 printf("inline-crypto-offload ");
774                 break;
775         case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
776                 printf("inline-protocol-offload ");
777                 break;
778         case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
779                 printf("lookaside-protocol-offload ");
780                 break;
781         }
782
783         fallback_ips = &sa->sessions[IPSEC_SESSION_FALLBACK];
784         if (fallback_ips != NULL && sa->fallback_sessions > 0) {
785                 printf("inline fallback: ");
786                 if (fallback_ips->type == RTE_SECURITY_ACTION_TYPE_NONE)
787                         printf("lookaside-none");
788                 else
789                         printf("invalid");
790         }
791         printf("\n");
792 }
793
794 struct ipsec_xf {
795         struct rte_crypto_sym_xform a;
796         struct rte_crypto_sym_xform b;
797 };
798
799 struct sa_ctx {
800         void *satbl; /* pointer to array of rte_ipsec_sa objects*/
801         struct ipsec_sad sad;
802         struct ipsec_xf *xf;
803         uint32_t nb_sa;
804         struct ipsec_sa sa[];
805 };
806
807 static struct sa_ctx *
808 sa_create(const char *name, int32_t socket_id, uint32_t nb_sa)
809 {
810         char s[PATH_MAX];
811         struct sa_ctx *sa_ctx;
812         uint32_t mz_size;
813         const struct rte_memzone *mz;
814
815         snprintf(s, sizeof(s), "%s_%u", name, socket_id);
816
817         /* Create SA context */
818         printf("Creating SA context with %u maximum entries on socket %d\n",
819                         nb_sa, socket_id);
820
821         mz_size = sizeof(struct ipsec_xf) * nb_sa;
822         mz = rte_memzone_reserve(s, mz_size, socket_id,
823                         RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
824         if (mz == NULL) {
825                 printf("Failed to allocate SA XFORM memory\n");
826                 rte_errno = ENOMEM;
827                 return NULL;
828         }
829
830         sa_ctx = rte_malloc(NULL, sizeof(struct sa_ctx) +
831                 sizeof(struct ipsec_sa) * nb_sa, RTE_CACHE_LINE_SIZE);
832
833         if (sa_ctx == NULL) {
834                 printf("Failed to allocate SA CTX memory\n");
835                 rte_errno = ENOMEM;
836                 rte_memzone_free(mz);
837                 return NULL;
838         }
839
840         sa_ctx->xf = (struct ipsec_xf *)mz->addr;
841         sa_ctx->nb_sa = nb_sa;
842
843         return sa_ctx;
844 }
845
846 static int
847 check_eth_dev_caps(uint16_t portid, uint32_t inbound)
848 {
849         struct rte_eth_dev_info dev_info;
850         int retval;
851
852         retval = rte_eth_dev_info_get(portid, &dev_info);
853         if (retval != 0) {
854                 RTE_LOG(ERR, IPSEC,
855                         "Error during getting device (port %u) info: %s\n",
856                         portid, strerror(-retval));
857
858                 return retval;
859         }
860
861         if (inbound) {
862                 if ((dev_info.rx_offload_capa &
863                                 DEV_RX_OFFLOAD_SECURITY) == 0) {
864                         RTE_LOG(WARNING, PORT,
865                                 "hardware RX IPSec offload is not supported\n");
866                         return -EINVAL;
867                 }
868
869         } else { /* outbound */
870                 if ((dev_info.tx_offload_capa &
871                                 DEV_TX_OFFLOAD_SECURITY) == 0) {
872                         RTE_LOG(WARNING, PORT,
873                                 "hardware TX IPSec offload is not supported\n");
874                         return -EINVAL;
875                 }
876         }
877         return 0;
878 }
879
880 /*
881  * Helper function, tries to determine next_proto for SPI
882  * by searching though SP rules.
883  */
884 static int
885 get_spi_proto(uint32_t spi, enum rte_security_ipsec_sa_direction dir,
886                 struct ip_addr ip_addr[2], uint32_t mask[2])
887 {
888         int32_t rc4, rc6;
889
890         rc4 = sp4_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
891                                 ip_addr, mask);
892         rc6 = sp6_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
893                                 ip_addr, mask);
894
895         if (rc4 >= 0) {
896                 if (rc6 >= 0) {
897                         RTE_LOG(ERR, IPSEC,
898                                 "%s: SPI %u used simultaeously by "
899                                 "IPv4(%d) and IPv6 (%d) SP rules\n",
900                                 __func__, spi, rc4, rc6);
901                         return -EINVAL;
902                 } else
903                         return IPPROTO_IPIP;
904         } else if (rc6 < 0) {
905                 RTE_LOG(ERR, IPSEC,
906                         "%s: SPI %u is not used by any SP rule\n",
907                         __func__, spi);
908                 return -EINVAL;
909         } else
910                 return IPPROTO_IPV6;
911 }
912
913 /*
914  * Helper function for getting source and destination IP addresses
915  * from SP. Needed for inline crypto transport mode, as addresses are not
916  * provided in config file for that mode. It checks if SP for current SA exists,
917  * and based on what type of protocol is returned, it stores appropriate
918  * addresses got from SP into SA.
919  */
920 static int
921 sa_add_address_inline_crypto(struct ipsec_sa *sa)
922 {
923         int protocol;
924         struct ip_addr ip_addr[2];
925         uint32_t mask[2];
926
927         protocol = get_spi_proto(sa->spi, sa->direction, ip_addr, mask);
928         if (protocol < 0)
929                 return protocol;
930         else if (protocol == IPPROTO_IPIP) {
931                 sa->flags |= IP4_TRANSPORT;
932                 if (mask[0] == IP4_FULL_MASK &&
933                                 mask[1] == IP4_FULL_MASK &&
934                                 ip_addr[0].ip.ip4 != 0 &&
935                                 ip_addr[1].ip.ip4 != 0) {
936
937                         sa->src.ip.ip4 = ip_addr[0].ip.ip4;
938                         sa->dst.ip.ip4 = ip_addr[1].ip.ip4;
939                 } else {
940                         RTE_LOG(ERR, IPSEC,
941                         "%s: No valid address or mask entry in"
942                         " IPv4 SP rule for SPI %u\n",
943                         __func__, sa->spi);
944                         return -EINVAL;
945                 }
946         } else if (protocol == IPPROTO_IPV6) {
947                 sa->flags |= IP6_TRANSPORT;
948                 if (mask[0] == IP6_FULL_MASK &&
949                                 mask[1] == IP6_FULL_MASK &&
950                                 (ip_addr[0].ip.ip6.ip6[0] != 0 ||
951                                 ip_addr[0].ip.ip6.ip6[1] != 0) &&
952                                 (ip_addr[1].ip.ip6.ip6[0] != 0 ||
953                                 ip_addr[1].ip.ip6.ip6[1] != 0)) {
954
955                         sa->src.ip.ip6 = ip_addr[0].ip.ip6;
956                         sa->dst.ip.ip6 = ip_addr[1].ip.ip6;
957                 } else {
958                         RTE_LOG(ERR, IPSEC,
959                         "%s: No valid address or mask entry in"
960                         " IPv6 SP rule for SPI %u\n",
961                         __func__, sa->spi);
962                         return -EINVAL;
963                 }
964         }
965         return 0;
966 }
967
968 static int
969 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
970                 uint32_t nb_entries, uint32_t inbound,
971                 struct socket_ctx *skt_ctx)
972 {
973         struct ipsec_sa *sa;
974         uint32_t i, idx;
975         uint16_t iv_length, aad_length;
976         int inline_status;
977         int32_t rc;
978         struct rte_ipsec_session *ips;
979
980         /* for ESN upper 32 bits of SQN also need to be part of AAD */
981         aad_length = (app_sa_prm.enable_esn != 0) ? sizeof(uint32_t) : 0;
982
983         for (i = 0; i < nb_entries; i++) {
984                 idx = i;
985                 sa = &sa_ctx->sa[idx];
986                 if (sa->spi != 0) {
987                         printf("Index %u already in use by SPI %u\n",
988                                         idx, sa->spi);
989                         return -EINVAL;
990                 }
991                 *sa = entries[i];
992
993                 if (inbound) {
994                         rc = ipsec_sad_add(&sa_ctx->sad, sa);
995                         if (rc != 0)
996                                 return rc;
997                 }
998
999                 sa->seq = 0;
1000                 ips = ipsec_get_primary_session(sa);
1001
1002                 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1003                         ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1004                         if (check_eth_dev_caps(sa->portid, inbound))
1005                                 return -EINVAL;
1006                 }
1007
1008
1009                 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1010                 case IP4_TUNNEL:
1011                         sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
1012                         sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
1013                         break;
1014                 case TRANSPORT:
1015                         if (ips->type ==
1016                                 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1017                                 inline_status =
1018                                         sa_add_address_inline_crypto(sa);
1019                                 if (inline_status < 0)
1020                                         return inline_status;
1021                         }
1022                         break;
1023                 }
1024
1025                 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
1026                         struct rte_ipsec_session *ips;
1027                         iv_length = 12;
1028
1029                         sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1030                         sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
1031                         sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
1032                         sa_ctx->xf[idx].a.aead.key.length =
1033                                 sa->cipher_key_len;
1034                         sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
1035                                 RTE_CRYPTO_AEAD_OP_DECRYPT :
1036                                 RTE_CRYPTO_AEAD_OP_ENCRYPT;
1037                         sa_ctx->xf[idx].a.next = NULL;
1038                         sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
1039                         sa_ctx->xf[idx].a.aead.iv.length = iv_length;
1040                         sa_ctx->xf[idx].a.aead.aad_length =
1041                                 sa->aad_len + aad_length;
1042                         sa_ctx->xf[idx].a.aead.digest_length =
1043                                 sa->digest_len;
1044
1045                         sa->xforms = &sa_ctx->xf[idx].a;
1046
1047                         ips = ipsec_get_primary_session(sa);
1048                         if (ips->type ==
1049                                 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1050                                 ips->type ==
1051                                 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1052                                 rc = create_inline_session(skt_ctx, sa, ips);
1053                                 if (rc != 0) {
1054                                         RTE_LOG(ERR, IPSEC_ESP,
1055                                                 "create_inline_session() failed\n");
1056                                         return -EINVAL;
1057                                 }
1058                         }
1059                         print_one_sa_rule(sa, inbound);
1060                 } else {
1061                         switch (sa->cipher_algo) {
1062                         case RTE_CRYPTO_CIPHER_NULL:
1063                         case RTE_CRYPTO_CIPHER_3DES_CBC:
1064                         case RTE_CRYPTO_CIPHER_AES_CBC:
1065                                 iv_length = sa->iv_len;
1066                                 break;
1067                         case RTE_CRYPTO_CIPHER_AES_CTR:
1068                                 iv_length = 16;
1069                                 break;
1070                         default:
1071                                 RTE_LOG(ERR, IPSEC_ESP,
1072                                                 "unsupported cipher algorithm %u\n",
1073                                                 sa->cipher_algo);
1074                                 return -EINVAL;
1075                         }
1076
1077                         if (inbound) {
1078                                 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1079                                 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
1080                                 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
1081                                 sa_ctx->xf[idx].b.cipher.key.length =
1082                                         sa->cipher_key_len;
1083                                 sa_ctx->xf[idx].b.cipher.op =
1084                                         RTE_CRYPTO_CIPHER_OP_DECRYPT;
1085                                 sa_ctx->xf[idx].b.next = NULL;
1086                                 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
1087                                 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
1088
1089                                 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1090                                 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
1091                                 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
1092                                 sa_ctx->xf[idx].a.auth.key.length =
1093                                         sa->auth_key_len;
1094                                 sa_ctx->xf[idx].a.auth.digest_length =
1095                                         sa->digest_len;
1096                                 sa_ctx->xf[idx].a.auth.op =
1097                                         RTE_CRYPTO_AUTH_OP_VERIFY;
1098                         } else { /* outbound */
1099                                 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1100                                 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
1101                                 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
1102                                 sa_ctx->xf[idx].a.cipher.key.length =
1103                                         sa->cipher_key_len;
1104                                 sa_ctx->xf[idx].a.cipher.op =
1105                                         RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1106                                 sa_ctx->xf[idx].a.next = NULL;
1107                                 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
1108                                 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
1109
1110                                 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1111                                 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
1112                                 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
1113                                 sa_ctx->xf[idx].b.auth.key.length =
1114                                         sa->auth_key_len;
1115                                 sa_ctx->xf[idx].b.auth.digest_length =
1116                                         sa->digest_len;
1117                                 sa_ctx->xf[idx].b.auth.op =
1118                                         RTE_CRYPTO_AUTH_OP_GENERATE;
1119                         }
1120
1121                         sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
1122                         sa_ctx->xf[idx].b.next = NULL;
1123                         sa->xforms = &sa_ctx->xf[idx].a;
1124
1125                         print_one_sa_rule(sa, inbound);
1126                 }
1127         }
1128
1129         return 0;
1130 }
1131
1132 static inline int
1133 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1134                 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1135 {
1136         return sa_add_rules(sa_ctx, entries, nb_entries, 0, skt_ctx);
1137 }
1138
1139 static inline int
1140 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1141                 uint32_t nb_entries, struct socket_ctx *skt_ctx)
1142 {
1143         return sa_add_rules(sa_ctx, entries, nb_entries, 1, skt_ctx);
1144 }
1145
1146 /*
1147  * helper function, fills parameters that are identical for all SAs
1148  */
1149 static void
1150 fill_ipsec_app_sa_prm(struct rte_ipsec_sa_prm *prm,
1151         const struct app_sa_prm *app_prm)
1152 {
1153         memset(prm, 0, sizeof(*prm));
1154
1155         prm->flags = app_prm->flags;
1156         prm->ipsec_xform.options.esn = app_prm->enable_esn;
1157         prm->ipsec_xform.replay_win_sz = app_prm->window_size;
1158 }
1159
1160 static int
1161 fill_ipsec_sa_prm(struct rte_ipsec_sa_prm *prm, const struct ipsec_sa *ss,
1162         const struct rte_ipv4_hdr *v4, struct rte_ipv6_hdr *v6)
1163 {
1164         int32_t rc;
1165
1166         /*
1167          * Try to get SPI next proto by searching that SPI in SPD.
1168          * probably not the optimal way, but there seems nothing
1169          * better right now.
1170          */
1171         rc = get_spi_proto(ss->spi, ss->direction, NULL, NULL);
1172         if (rc < 0)
1173                 return rc;
1174
1175         fill_ipsec_app_sa_prm(prm, &app_sa_prm);
1176         prm->userdata = (uintptr_t)ss;
1177
1178         /* setup ipsec xform */
1179         prm->ipsec_xform.spi = ss->spi;
1180         prm->ipsec_xform.salt = ss->salt;
1181         prm->ipsec_xform.direction = ss->direction;
1182         prm->ipsec_xform.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP;
1183         prm->ipsec_xform.mode = (IS_TRANSPORT(ss->flags)) ?
1184                 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT :
1185                 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
1186         prm->ipsec_xform.options.ecn = 1;
1187         prm->ipsec_xform.options.copy_dscp = 1;
1188
1189         if (IS_IP4_TUNNEL(ss->flags)) {
1190                 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV4;
1191                 prm->tun.hdr_len = sizeof(*v4);
1192                 prm->tun.next_proto = rc;
1193                 prm->tun.hdr = v4;
1194         } else if (IS_IP6_TUNNEL(ss->flags)) {
1195                 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV6;
1196                 prm->tun.hdr_len = sizeof(*v6);
1197                 prm->tun.next_proto = rc;
1198                 prm->tun.hdr = v6;
1199         } else {
1200                 /* transport mode */
1201                 prm->trs.proto = rc;
1202         }
1203
1204         /* setup crypto section */
1205         prm->crypto_xform = ss->xforms;
1206         return 0;
1207 }
1208
1209 static int
1210 fill_ipsec_session(struct rte_ipsec_session *ss, struct rte_ipsec_sa *sa)
1211 {
1212         int32_t rc = 0;
1213
1214         ss->sa = sa;
1215
1216         if (ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1217                 ss->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
1218                 if (ss->security.ses != NULL) {
1219                         rc = rte_ipsec_session_prepare(ss);
1220                         if (rc != 0)
1221                                 memset(ss, 0, sizeof(*ss));
1222                 }
1223         }
1224
1225         return rc;
1226 }
1227
1228 /*
1229  * Initialise related rte_ipsec_sa object.
1230  */
1231 static int
1232 ipsec_sa_init(struct ipsec_sa *lsa, struct rte_ipsec_sa *sa, uint32_t sa_size)
1233 {
1234         int rc;
1235         struct rte_ipsec_sa_prm prm;
1236         struct rte_ipsec_session *ips;
1237         struct rte_ipv4_hdr v4  = {
1238                 .version_ihl = IPVERSION << 4 |
1239                         sizeof(v4) / RTE_IPV4_IHL_MULTIPLIER,
1240                 .time_to_live = IPDEFTTL,
1241                 .next_proto_id = IPPROTO_ESP,
1242                 .src_addr = lsa->src.ip.ip4,
1243                 .dst_addr = lsa->dst.ip.ip4,
1244         };
1245         struct rte_ipv6_hdr v6 = {
1246                 .vtc_flow = htonl(IP6_VERSION << 28),
1247                 .proto = IPPROTO_ESP,
1248         };
1249
1250         if (IS_IP6_TUNNEL(lsa->flags)) {
1251                 memcpy(v6.src_addr, lsa->src.ip.ip6.ip6_b, sizeof(v6.src_addr));
1252                 memcpy(v6.dst_addr, lsa->dst.ip.ip6.ip6_b, sizeof(v6.dst_addr));
1253         }
1254
1255         rc = fill_ipsec_sa_prm(&prm, lsa, &v4, &v6);
1256         if (rc == 0)
1257                 rc = rte_ipsec_sa_init(sa, &prm, sa_size);
1258         if (rc < 0)
1259                 return rc;
1260
1261         /* init primary processing session */
1262         ips = ipsec_get_primary_session(lsa);
1263         rc = fill_ipsec_session(ips, sa);
1264         if (rc != 0)
1265                 return rc;
1266
1267         /* init inline fallback processing session */
1268         if (lsa->fallback_sessions == 1)
1269                 rc = fill_ipsec_session(ipsec_get_fallback_session(lsa), sa);
1270
1271         return rc;
1272 }
1273
1274 /*
1275  * Allocate space and init rte_ipsec_sa strcutures,
1276  * one per session.
1277  */
1278 static int
1279 ipsec_satbl_init(struct sa_ctx *ctx, uint32_t nb_ent, int32_t socket)
1280 {
1281         int32_t rc, sz;
1282         uint32_t i, idx;
1283         size_t tsz;
1284         struct rte_ipsec_sa *sa;
1285         struct ipsec_sa *lsa;
1286         struct rte_ipsec_sa_prm prm;
1287
1288         /* determine SA size */
1289         idx = 0;
1290         fill_ipsec_sa_prm(&prm, ctx->sa + idx, NULL, NULL);
1291         sz = rte_ipsec_sa_size(&prm);
1292         if (sz < 0) {
1293                 RTE_LOG(ERR, IPSEC, "%s(%p, %u, %d): "
1294                         "failed to determine SA size, error code: %d\n",
1295                         __func__, ctx, nb_ent, socket, sz);
1296                 return sz;
1297         }
1298
1299         tsz = sz * nb_ent;
1300
1301         ctx->satbl = rte_zmalloc_socket(NULL, tsz, RTE_CACHE_LINE_SIZE, socket);
1302         if (ctx->satbl == NULL) {
1303                 RTE_LOG(ERR, IPSEC,
1304                         "%s(%p, %u, %d): failed to allocate %zu bytes\n",
1305                         __func__,  ctx, nb_ent, socket, tsz);
1306                 return -ENOMEM;
1307         }
1308
1309         rc = 0;
1310         for (i = 0; i != nb_ent && rc == 0; i++) {
1311
1312                 idx = i;
1313
1314                 sa = (struct rte_ipsec_sa *)((uintptr_t)ctx->satbl + sz * i);
1315                 lsa = ctx->sa + idx;
1316
1317                 rc = ipsec_sa_init(lsa, sa, sz);
1318         }
1319
1320         return rc;
1321 }
1322
1323 /*
1324  * Walk through all SA rules to find an SA with given SPI
1325  */
1326 int
1327 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound)
1328 {
1329         uint32_t i, num;
1330         const struct ipsec_sa *sar;
1331
1332         sar = sa_ctx->sa;
1333         if (inbound != 0)
1334                 num = nb_sa_in;
1335         else
1336                 num = nb_sa_out;
1337
1338         for (i = 0; i != num; i++) {
1339                 if (sar[i].spi == spi)
1340                         return i;
1341         }
1342
1343         return -ENOENT;
1344 }
1345
1346 void
1347 sa_init(struct socket_ctx *ctx, int32_t socket_id)
1348 {
1349         int32_t rc;
1350         const char *name;
1351
1352         if (ctx == NULL)
1353                 rte_exit(EXIT_FAILURE, "NULL context.\n");
1354
1355         if (ctx->sa_in != NULL)
1356                 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
1357                                 "initialized\n", socket_id);
1358
1359         if (ctx->sa_out != NULL)
1360                 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
1361                                 "initialized\n", socket_id);
1362
1363         if (nb_sa_in > 0) {
1364                 name = "sa_in";
1365                 ctx->sa_in = sa_create(name, socket_id, nb_sa_in);
1366                 if (ctx->sa_in == NULL)
1367                         rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1368                                 "context %s in socket %d\n", rte_errno,
1369                                 name, socket_id);
1370
1371                 rc = ipsec_sad_create(name, &ctx->sa_in->sad, socket_id,
1372                                 &sa_in_cnt);
1373                 if (rc != 0)
1374                         rte_exit(EXIT_FAILURE, "failed to init SAD\n");
1375
1376                 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in, ctx);
1377
1378                 if (app_sa_prm.enable != 0) {
1379                         rc = ipsec_satbl_init(ctx->sa_in, nb_sa_in,
1380                                 socket_id);
1381                         if (rc != 0)
1382                                 rte_exit(EXIT_FAILURE,
1383                                         "failed to init inbound SAs\n");
1384                 }
1385         } else
1386                 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
1387
1388         if (nb_sa_out > 0) {
1389                 name = "sa_out";
1390                 ctx->sa_out = sa_create(name, socket_id, nb_sa_out);
1391                 if (ctx->sa_out == NULL)
1392                         rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1393                                 "context %s in socket %d\n", rte_errno,
1394                                 name, socket_id);
1395
1396                 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out, ctx);
1397
1398                 if (app_sa_prm.enable != 0) {
1399                         rc = ipsec_satbl_init(ctx->sa_out, nb_sa_out,
1400                                 socket_id);
1401                         if (rc != 0)
1402                                 rte_exit(EXIT_FAILURE,
1403                                         "failed to init outbound SAs\n");
1404                 }
1405         } else
1406                 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
1407                         "specified\n");
1408 }
1409
1410 int
1411 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
1412 {
1413         struct ipsec_mbuf_metadata *priv;
1414         struct ipsec_sa *sa;
1415
1416         priv = get_priv(m);
1417         sa = priv->sa;
1418         if (sa != NULL)
1419                 return (sa_ctx->sa[sa_idx].spi == sa->spi);
1420
1421         RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
1422         return 0;
1423 }
1424
1425 void
1426 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
1427                 void *sa_arr[], uint16_t nb_pkts)
1428 {
1429         uint32_t i;
1430         struct ip *ip;
1431         uint32_t *src4_addr;
1432         uint8_t *src6_addr;
1433         void *result_sa;
1434         struct ipsec_sa *sa;
1435
1436         sad_lookup(&sa_ctx->sad, pkts, sa_arr, nb_pkts);
1437
1438         /*
1439          * Mark need for inline offload fallback on the LSB of SA pointer.
1440          * Thanks to packet grouping mechanism which ipsec_process is using
1441          * packets marked for fallback processing will form separate group.
1442          *
1443          * Because it is not safe to use SA pointer it is casted to generic
1444          * pointer to prevent from unintentional use. Use ipsec_mask_saptr
1445          * to get valid struct pointer.
1446          */
1447         for (i = 0; i < nb_pkts; i++) {
1448                 if (sa_arr[i] == NULL)
1449                         continue;
1450
1451                 result_sa = sa = sa_arr[i];
1452                 if (MBUF_NO_SEC_OFFLOAD(pkts[i]) &&
1453                         sa->fallback_sessions > 0) {
1454                         uintptr_t intsa = (uintptr_t)sa;
1455                         intsa |= IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
1456                         result_sa = (void *)intsa;
1457                 }
1458
1459                 ip = rte_pktmbuf_mtod(pkts[i], struct ip *);
1460                 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1461                 case IP4_TUNNEL:
1462                         src4_addr = RTE_PTR_ADD(ip,
1463                                 offsetof(struct ip, ip_src));
1464                         if ((ip->ip_v == IPVERSION) &&
1465                                         (sa->src.ip.ip4 == *src4_addr) &&
1466                                         (sa->dst.ip.ip4 == *(src4_addr + 1)))
1467                                 sa_arr[i] = result_sa;
1468                         else
1469                                 sa_arr[i] = NULL;
1470                         break;
1471                 case IP6_TUNNEL:
1472                         src6_addr = RTE_PTR_ADD(ip,
1473                                 offsetof(struct ip6_hdr, ip6_src));
1474                         if ((ip->ip_v == IP6_VERSION) &&
1475                                 !memcmp(&sa->src.ip.ip6.ip6, src6_addr, 16) &&
1476                                 !memcmp(&sa->dst.ip.ip6.ip6, src6_addr + 16, 16))
1477                                 sa_arr[i] = result_sa;
1478                         else
1479                                 sa_arr[i] = NULL;
1480                         break;
1481                 case TRANSPORT:
1482                         sa_arr[i] = result_sa;
1483                 }
1484         }
1485 }
1486
1487 void
1488 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
1489                 void *sa[], uint16_t nb_pkts)
1490 {
1491         uint32_t i;
1492
1493         for (i = 0; i < nb_pkts; i++)
1494                 sa[i] = &sa_ctx->sa[sa_idx[i]];
1495 }
1496
1497 /*
1498  * Select HW offloads to be used.
1499  */
1500 int
1501 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
1502                 uint64_t *tx_offloads)
1503 {
1504         struct ipsec_sa *rule;
1505         uint32_t idx_sa;
1506         enum rte_security_session_action_type rule_type;
1507
1508         *rx_offloads = 0;
1509         *tx_offloads = 0;
1510
1511         /* Check for inbound rules that use offloads and use this port */
1512         for (idx_sa = 0; idx_sa < nb_sa_in; idx_sa++) {
1513                 rule = &sa_in[idx_sa];
1514                 rule_type = ipsec_get_action_type(rule);
1515                 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1516                                 rule_type ==
1517                                 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1518                                 && rule->portid == port_id)
1519                         *rx_offloads |= DEV_RX_OFFLOAD_SECURITY;
1520         }
1521
1522         /* Check for outbound rules that use offloads and use this port */
1523         for (idx_sa = 0; idx_sa < nb_sa_out; idx_sa++) {
1524                 rule = &sa_out[idx_sa];
1525                 rule_type = ipsec_get_action_type(rule);
1526                 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1527                                 rule_type ==
1528                                 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1529                                 && rule->portid == port_id)
1530                         *tx_offloads |= DEV_TX_OFFLOAD_SECURITY;
1531         }
1532         return 0;
1533 }