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