crypto: introduce async crypto infra
[vpp.git] / src / vnet / ipsec / esp_decrypt.c
1 /*
2  * esp_decrypt.c : IPSec ESP decrypt node
3  *
4  * Copyright (c) 2015 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/l2/l2_input.h>
22
23 #include <vnet/ipsec/ipsec.h>
24 #include <vnet/ipsec/esp.h>
25 #include <vnet/ipsec/ipsec_io.h>
26 #include <vnet/ipsec/ipsec_tun.h>
27
28 #include <vnet/gre/gre.h>
29
30 #define foreach_esp_decrypt_next                \
31 _(DROP, "error-drop")                           \
32 _(IP4_INPUT, "ip4-input-no-checksum")           \
33 _(IP6_INPUT, "ip6-input")                       \
34 _(L2_INPUT, "l2-input")                         \
35 _(HANDOFF, "handoff")                           \
36 _(PENDING, "pending")
37
38 #define _(v, s) ESP_DECRYPT_NEXT_##v,
39 typedef enum
40 {
41   foreach_esp_decrypt_next
42 #undef _
43     ESP_DECRYPT_N_NEXT,
44 } esp_decrypt_next_t;
45
46 #define foreach_esp_decrypt_post_next                  \
47 _(DROP, "error-drop")                                  \
48 _(IP4_INPUT, "ip4-input-no-checksum")                  \
49 _(IP6_INPUT, "ip6-input")                              \
50 _(L2_INPUT, "l2-input")
51
52 #define _(v, s) ESP_DECRYPT_POST_NEXT_##v,
53 typedef enum
54 {
55   foreach_esp_decrypt_post_next
56 #undef _
57     ESP_DECRYPT_POST_N_NEXT,
58 } esp_decrypt_post_next_t;
59
60 #define foreach_esp_decrypt_error                               \
61  _(RX_PKTS, "ESP pkts received")                                \
62  _(RX_POST_PKTS, "ESP-POST pkts received")                      \
63  _(DECRYPTION_FAILED, "ESP decryption failed")                  \
64  _(INTEG_ERROR, "Integrity check failed")                       \
65  _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
66  _(REPLAY, "SA replayed packet")                                \
67  _(RUNT, "undersized packet")                                   \
68  _(NO_BUFFERS, "no buffers (packet dropped)")                   \
69  _(OVERSIZED_HEADER, "buffer with oversized header (dropped)")  \
70  _(NO_TAIL_SPACE, "no enough buffer tail space (dropped)")      \
71  _(TUN_NO_PROTO, "no tunnel protocol")                          \
72  _(UNSUP_PAYLOAD, "unsupported payload")                        \
73
74
75 typedef enum
76 {
77 #define _(sym,str) ESP_DECRYPT_ERROR_##sym,
78   foreach_esp_decrypt_error
79 #undef _
80     ESP_DECRYPT_N_ERROR,
81 } esp_decrypt_error_t;
82
83 static char *esp_decrypt_error_strings[] = {
84 #define _(sym,string) string,
85   foreach_esp_decrypt_error
86 #undef _
87 };
88
89 typedef struct
90 {
91   u32 seq;
92   u32 sa_seq;
93   u32 sa_seq_hi;
94   ipsec_crypto_alg_t crypto_alg;
95   ipsec_integ_alg_t integ_alg;
96 } esp_decrypt_trace_t;
97
98 /* packet trace format function */
99 static u8 *
100 format_esp_decrypt_trace (u8 * s, va_list * args)
101 {
102   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
103   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
104   esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
105
106   s =
107     format (s,
108             "esp: crypto %U integrity %U pkt-seq %d sa-seq %u sa-seq-hi %u",
109             format_ipsec_crypto_alg, t->crypto_alg, format_ipsec_integ_alg,
110             t->integ_alg, t->seq, t->sa_seq, t->sa_seq_hi);
111   return s;
112 }
113
114 #define ESP_ENCRYPT_PD_F_FD_TRANSPORT (1 << 2)
115
116 static_always_inline void
117 esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
118                  vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
119                  int e)
120 {
121   vnet_crypto_op_t *op = ops;
122   u32 n_fail, n_ops = vec_len (ops);
123
124   if (n_ops == 0)
125     return;
126
127   n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
128
129   while (n_fail)
130     {
131       ASSERT (op - ops < n_ops);
132       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
133         {
134           u32 err, bi = op->user_data;
135           if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC)
136             err = e;
137           else
138             err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
139           b[bi]->error = node->errors[err];
140           nexts[bi] = ESP_DECRYPT_NEXT_DROP;
141           n_fail--;
142         }
143       op++;
144     }
145 }
146
147 static_always_inline void
148 esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
149                          vnet_crypto_op_t * ops, vlib_buffer_t * b[],
150                          u16 * nexts, vnet_crypto_op_chunk_t * chunks, int e)
151 {
152
153   vnet_crypto_op_t *op = ops;
154   u32 n_fail, n_ops = vec_len (ops);
155
156   if (n_ops == 0)
157     return;
158
159   n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
160
161   while (n_fail)
162     {
163       ASSERT (op - ops < n_ops);
164       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
165         {
166           u32 err, bi = op->user_data;
167           if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC)
168             err = e;
169           else
170             err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
171           b[bi]->error = node->errors[err];
172           nexts[bi] = ESP_DECRYPT_NEXT_DROP;
173           n_fail--;
174         }
175       op++;
176     }
177 }
178
179 always_inline void
180 esp_remove_tail (vlib_main_t * vm, vlib_buffer_t * b, vlib_buffer_t * last,
181                  u16 tail)
182 {
183   vlib_buffer_t *before_last = b;
184
185   if (last->current_length > tail)
186     {
187       last->current_length -= tail;
188       return;
189     }
190   ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
191
192   while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
193     {
194       before_last = b;
195       b = vlib_get_buffer (vm, b->next_buffer);
196     }
197   before_last->current_length -= tail - last->current_length;
198   vlib_buffer_free_one (vm, before_last->next_buffer);
199   before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
200 }
201
202 /* ICV is splitted in last two buffers so move it to the last buffer and
203    return pointer to it */
204 static_always_inline u8 *
205 esp_move_icv (vlib_main_t * vm, vlib_buffer_t * first,
206               esp_decrypt_packet_data2_t * pd2, u16 icv_sz, u16 * dif)
207 {
208   vlib_buffer_t *before_last, *bp;
209   u16 last_sz = pd2->lb->current_length;
210   u16 first_sz = icv_sz - last_sz;
211
212   bp = before_last = first;
213   while (bp->flags & VLIB_BUFFER_NEXT_PRESENT)
214     {
215       before_last = bp;
216       bp = vlib_get_buffer (vm, bp->next_buffer);
217     }
218
219   u8 *lb_curr = vlib_buffer_get_current (pd2->lb);
220   memmove (lb_curr + first_sz, lb_curr, last_sz);
221   clib_memcpy_fast (lb_curr, vlib_buffer_get_tail (before_last) - first_sz,
222                     first_sz);
223   before_last->current_length -= first_sz;
224   clib_memset (vlib_buffer_get_tail (before_last), 0, first_sz);
225   if (dif)
226     dif[0] = first_sz;
227   pd2->lb = before_last;
228   pd2->icv_removed = 1;
229   pd2->free_buffer_index = before_last->next_buffer;
230   before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
231   return lb_curr;
232 }
233
234 static_always_inline i16
235 esp_insert_esn (vlib_main_t * vm, ipsec_sa_t * sa,
236                 esp_decrypt_packet_data2_t * pd2, u32 * data_len,
237                 u8 ** digest, u16 * len, vlib_buffer_t * b, u8 * payload)
238 {
239   if (!ipsec_sa_is_set_USE_ESN (sa))
240     return 0;
241
242   /* shift ICV by 4 bytes to insert ESN */
243   u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
244   u8 tmp[ESP_MAX_ICV_SIZE], sz = sizeof (sa->seq_hi);
245
246   if (pd2->icv_removed)
247     {
248       u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
249       if (space_left >= sz)
250         {
251           clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi, sz);
252           *data_len += sz;
253         }
254       else
255         return sz;
256
257       len[0] = b->current_length;
258     }
259   else
260     {
261       clib_memcpy_fast (tmp, payload + len[0], ESP_MAX_ICV_SIZE);
262       clib_memcpy_fast (payload + len[0], &seq_hi, sz);
263       clib_memcpy_fast (payload + len[0] + sz, tmp, ESP_MAX_ICV_SIZE);
264       *data_len += sz;
265       *digest += sz;
266     }
267   return sz;
268 }
269
270 static_always_inline u8 *
271 esp_move_icv_esn (vlib_main_t * vm, vlib_buffer_t * first,
272                   esp_decrypt_packet_data2_t * pd2, u16 icv_sz,
273                   ipsec_sa_t * sa, u8 * extra_esn, u32 * len)
274 {
275   u16 dif = 0;
276   u8 *digest = esp_move_icv (vm, first, pd2, icv_sz, &dif);
277   if (dif)
278     *len -= dif;
279
280   if (ipsec_sa_is_set_USE_ESN (sa))
281     {
282       u8 sz = sizeof (sa->seq_hi);
283       u32 seq_hi = clib_host_to_net_u32 (sa->seq_hi);
284       u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
285
286       if (space_left >= sz)
287         {
288           clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi, sz);
289           *len += sz;
290         }
291       else
292         {
293           /* no space for ESN at the tail, use the next buffer
294            * (with ICV data) */
295           ASSERT (pd2->icv_removed);
296           vlib_buffer_t *tmp = vlib_get_buffer (vm, pd2->free_buffer_index);
297           clib_memcpy_fast (vlib_buffer_get_current (tmp) - sz, &seq_hi, sz);
298           extra_esn[0] = 1;
299         }
300     }
301   return digest;
302 }
303
304 static_always_inline int
305 esp_decrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
306                          esp_decrypt_packet_data2_t * pd2,
307                          ipsec_sa_t * sa0, vlib_buffer_t * b, u8 icv_sz,
308                          u8 * start_src, u32 start_len,
309                          u8 ** digest, u16 * n_ch, u32 * integ_total_len)
310 {
311   vnet_crypto_op_chunk_t *ch;
312   vlib_buffer_t *cb = vlib_get_buffer (vm, b->next_buffer);
313   u16 n_chunks = 1;
314   u32 total_len;
315   vec_add2 (ptd->chunks, ch, 1);
316   total_len = ch->len = start_len;
317   ch->src = start_src;
318
319   while (1)
320     {
321       vec_add2 (ptd->chunks, ch, 1);
322       n_chunks += 1;
323       ch->src = vlib_buffer_get_current (cb);
324       if (pd2->lb == cb)
325         {
326           if (pd2->icv_removed)
327             ch->len = cb->current_length;
328           else
329             ch->len = cb->current_length - icv_sz;
330           if (ipsec_sa_is_set_USE_ESN (sa0))
331             {
332               u32 seq_hi = clib_host_to_net_u32 (sa0->seq_hi);
333               u8 tmp[ESP_MAX_ICV_SIZE], sz = sizeof (sa0->seq_hi);
334               u8 *esn;
335               vlib_buffer_t *tmp_b;
336               u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
337               if (space_left < sz)
338                 {
339                   if (pd2->icv_removed)
340                     {
341                       /* use pre-data area from the last bufer
342                          that was removed from the chain */
343                       tmp_b = vlib_get_buffer (vm, pd2->free_buffer_index);
344                       esn = tmp_b->data - sz;
345                     }
346                   else
347                     {
348                       /* no space, need to allocate new buffer */
349                       u32 tmp_bi = 0;
350                       if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
351                         return -1;
352                       tmp_b = vlib_get_buffer (vm, tmp_bi);
353                       esn = tmp_b->data;
354                       pd2->free_buffer_index = tmp_bi;
355                     }
356                   clib_memcpy_fast (esn, &seq_hi, sz);
357
358                   vec_add2 (ptd->chunks, ch, 1);
359                   n_chunks += 1;
360                   ch->src = esn;
361                   ch->len = sz;
362                 }
363               else
364                 {
365                   if (pd2->icv_removed)
366                     {
367                       clib_memcpy_fast (vlib_buffer_get_tail
368                                         (pd2->lb), &seq_hi, sz);
369                     }
370                   else
371                     {
372                       clib_memcpy_fast (tmp, *digest, ESP_MAX_ICV_SIZE);
373                       clib_memcpy_fast (*digest, &seq_hi, sz);
374                       clib_memcpy_fast (*digest + sz, tmp, ESP_MAX_ICV_SIZE);
375                       *digest += sz;
376                     }
377                   ch->len += sz;
378                 }
379             }
380           total_len += ch->len;
381           break;
382         }
383       else
384         total_len += ch->len = cb->current_length;
385
386       if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
387         break;
388
389       cb = vlib_get_buffer (vm, cb->next_buffer);
390     }
391
392   if (n_ch)
393     *n_ch = n_chunks;
394   if (integ_total_len)
395     *integ_total_len = total_len;
396
397   return 0;
398 }
399
400 static_always_inline u32
401 esp_decrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
402                           esp_decrypt_packet_data2_t * pd2,
403                           ipsec_sa_t * sa0, vlib_buffer_t * b, u8 icv_sz,
404                           u8 * start, u32 start_len, u8 ** tag, u16 * n_ch)
405 {
406   vnet_crypto_op_chunk_t *ch;
407   vlib_buffer_t *cb = b;
408   u16 n_chunks = 1;
409   u32 total_len;
410   vec_add2 (ptd->chunks, ch, 1);
411   total_len = ch->len = start_len;
412   ch->src = ch->dst = start;
413   cb = vlib_get_buffer (vm, cb->next_buffer);
414   n_chunks = 1;
415
416   while (1)
417     {
418       vec_add2 (ptd->chunks, ch, 1);
419       n_chunks += 1;
420       ch->src = ch->dst = vlib_buffer_get_current (cb);
421       if (pd2->lb == cb)
422         {
423           if (ipsec_sa_is_set_IS_AEAD (sa0))
424             {
425               if (pd2->lb->current_length < icv_sz)
426                 {
427                   u16 dif = 0;
428                   *tag = esp_move_icv (vm, b, pd2, icv_sz, &dif);
429
430                   /* this chunk does not contain crypto data */
431                   n_chunks -= 1;
432                   /* and fix previous chunk's length as it might have
433                      been changed */
434                   ASSERT (n_chunks > 0);
435                   if (pd2->lb == b)
436                     {
437                       total_len -= dif;
438                       ch[-1].len -= dif;
439                     }
440                   else
441                     {
442                       total_len = total_len + pd2->lb->current_length -
443                         ch[-1].len;
444                       ch[-1].len = pd2->lb->current_length;
445                     }
446                   break;
447                 }
448               else
449                 *tag = vlib_buffer_get_tail (pd2->lb) - icv_sz;
450             }
451
452           if (pd2->icv_removed)
453             total_len += ch->len = cb->current_length;
454           else
455             total_len += ch->len = cb->current_length - icv_sz;
456         }
457       else
458         total_len += ch->len = cb->current_length;
459
460       if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
461         break;
462
463       cb = vlib_get_buffer (vm, cb->next_buffer);
464     }
465
466   if (n_ch)
467     *n_ch = n_chunks;
468
469   return total_len;
470 }
471
472 static_always_inline void
473 esp_decrypt_prepare_sync_op (vlib_main_t * vm, vlib_node_runtime_t * node,
474                              ipsec_per_thread_data_t * ptd,
475                              vnet_crypto_op_t *** crypto_ops,
476                              vnet_crypto_op_t *** integ_ops,
477                              vnet_crypto_op_t * op,
478                              ipsec_sa_t * sa0, u8 * payload,
479                              u16 len, u8 icv_sz, u8 iv_sz,
480                              esp_decrypt_packet_data_t * pd,
481                              esp_decrypt_packet_data2_t * pd2,
482                              vlib_buffer_t * b, u16 * next, u32 index)
483 {
484   const u8 esp_sz = sizeof (esp_header_t);
485
486   if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE))
487     {
488       vnet_crypto_op_init (op, sa0->integ_op_id);
489       op->key_index = sa0->integ_key_index;
490       op->src = payload;
491       op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
492       op->user_data = index;
493       op->digest = payload + len;
494       op->digest_len = icv_sz;
495       op->len = len;
496
497       if (pd->is_chain)
498         {
499           /* buffer is chained */
500           op->len = pd->current_length;
501
502           /* special case when ICV is splitted and needs to be reassembled
503            * first -> move it to the last buffer. Also take into account
504            * that ESN needs to be added after encrypted data and may or
505            * may not fit in the tail.*/
506           if (pd2->lb->current_length < icv_sz)
507             {
508               u8 extra_esn = 0;
509               op->digest =
510                 esp_move_icv_esn (vm, b, pd2, icv_sz, sa0,
511                                   &extra_esn, &op->len);
512
513               if (extra_esn)
514                 {
515                   /* esn is in the last buffer, that was unlinked from
516                    * the chain */
517                   op->len = b->current_length;
518                 }
519               else
520                 {
521                   if (pd2->lb == b)
522                     {
523                       /* we now have a single buffer of crypto data, adjust
524                        * the length (second buffer contains only ICV) */
525                       *integ_ops = &ptd->integ_ops;
526                       *crypto_ops = &ptd->crypto_ops;
527                       len = b->current_length;
528                       goto out;
529                     }
530                 }
531             }
532           else
533             op->digest = vlib_buffer_get_tail (pd2->lb) - icv_sz;
534
535           op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
536           op->chunk_index = vec_len (ptd->chunks);
537           if (esp_decrypt_chain_integ (vm, ptd, pd2, sa0, b, icv_sz,
538                                        payload, pd->current_length,
539                                        &op->digest, &op->n_chunks, 0) < 0)
540             {
541               b->error = node->errors[ESP_DECRYPT_ERROR_NO_BUFFERS];
542               next[0] = ESP_DECRYPT_NEXT_DROP;
543               return;
544             }
545         }
546       else
547         esp_insert_esn (vm, sa0, pd2, &op->len, &op->digest, &len, b,
548                         payload);
549     out:
550       vec_add_aligned (*(integ_ops[0]), op, 1, CLIB_CACHE_LINE_BYTES);
551     }
552
553   payload += esp_sz;
554   len -= esp_sz;
555
556   if (sa0->crypto_dec_op_id != VNET_CRYPTO_OP_NONE)
557     {
558       vnet_crypto_op_init (op, sa0->crypto_dec_op_id);
559       op->key_index = sa0->crypto_key_index;
560       op->iv = payload;
561
562       if (ipsec_sa_is_set_IS_AEAD (sa0))
563         {
564           esp_header_t *esp0;
565           esp_aead_t *aad;
566           u8 *scratch;
567
568           /*
569            * construct the AAD and the nonce (Salt || IV) in a scratch
570            * space in front of the IP header.
571            */
572           scratch = payload - esp_sz;
573           esp0 = (esp_header_t *) (scratch);
574
575           scratch -= (sizeof (*aad) + pd->hdr_sz);
576           op->aad = scratch;
577
578           op->aad_len = esp_aad_fill (op->aad, esp0, sa0);
579
580           /*
581            * we don't need to refer to the ESP header anymore so we
582            * can overwrite it with the salt and use the IV where it is
583            * to form the nonce = (Salt + IV)
584            */
585           op->iv -= sizeof (sa0->salt);
586           clib_memcpy_fast (op->iv, &sa0->salt, sizeof (sa0->salt));
587
588           op->tag = payload + len;
589           op->tag_len = 16;
590         }
591       op->src = op->dst = payload += iv_sz;
592       op->len = len - iv_sz;
593       op->user_data = index;
594
595       if (pd->is_chain && (pd2->lb != b))
596         {
597           /* buffer is chained */
598           op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
599           op->chunk_index = vec_len (ptd->chunks);
600           esp_decrypt_chain_crypto (vm, ptd, pd2, sa0, b, icv_sz,
601                                     payload, len - pd->iv_sz + pd->icv_sz,
602                                     &op->tag, &op->n_chunks);
603         }
604
605       vec_add_aligned (*(crypto_ops[0]), op, 1, CLIB_CACHE_LINE_BYTES);
606     }
607 }
608
609 static_always_inline int
610 esp_decrypt_prepare_async_frame (vlib_main_t * vm,
611                                  vlib_node_runtime_t * node,
612                                  ipsec_per_thread_data_t * ptd,
613                                  vnet_crypto_async_frame_t ** f,
614                                  ipsec_sa_t * sa0, u8 * payload, u16 len,
615                                  u8 icv_sz, u8 iv_sz,
616                                  esp_decrypt_packet_data_t * pd,
617                                  esp_decrypt_packet_data2_t * pd2, u32 bi,
618                                  vlib_buffer_t * b, u16 * next,
619                                  u16 async_next)
620 {
621   const u8 esp_sz = sizeof (esp_header_t);
622   u32 current_protect_index = vnet_buffer (b)->ipsec.protect_index;
623   esp_decrypt_packet_data_t *async_pd = &(esp_post_data (b))->decrypt_data;
624   esp_decrypt_packet_data2_t *async_pd2 = esp_post_data2 (b);
625   u8 *tag = payload + len, *iv = payload + esp_sz, *aad = 0;
626   u32 key_index;
627   u32 crypto_len, integ_len = 0;
628   i16 crypto_start_offset, integ_start_offset = 0;
629   u8 flags = 0;
630
631   if (!ipsec_sa_is_set_IS_AEAD (sa0))
632     {
633       /* linked algs */
634       key_index = sa0->linked_key_index;
635       integ_start_offset = payload - b->data;
636       integ_len = len;
637
638       if (pd->is_chain)
639         {
640           /* buffer is chained */
641           integ_len = pd->current_length;
642
643           /* special case when ICV is splitted and needs to be reassembled
644            * first -> move it to the last buffer. Also take into account
645            * that ESN needs to be added after encrypted data and may or
646            * may not fit in the tail.*/
647           if (pd2->lb->current_length < icv_sz)
648             {
649               u8 extra_esn = 0;
650               tag = esp_move_icv_esn (vm, b, pd2, icv_sz, sa0,
651                                       &extra_esn, &integ_len);
652
653               if (extra_esn)
654                 {
655                   /* esn is in the last buffer, that was unlinked from
656                    * the chain */
657                   integ_len = b->current_length;
658                 }
659               else
660                 {
661                   if (pd2->lb == b)
662                     {
663                       /* we now have a single buffer of crypto data, adjust
664                        * the length (second buffer contains only ICV) */
665                       len = b->current_length;
666                       goto out;
667                     }
668                 }
669             }
670           else
671             tag = vlib_buffer_get_tail (pd2->lb) - icv_sz;
672
673           flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
674           if (esp_decrypt_chain_integ (vm, ptd, pd2, sa0, b, icv_sz, payload,
675                                        pd->current_length, &tag,
676                                        0, &integ_len) < 0)
677             {
678               /* allocate buffer failed, will not add to frame and drop */
679               b->error = node->errors[ESP_DECRYPT_ERROR_NO_BUFFERS];
680               next[0] = ESP_DECRYPT_NEXT_DROP;
681               return 0;
682             }
683         }
684       else
685         esp_insert_esn (vm, sa0, pd2, &integ_len, &tag, &len, b, payload);
686     }
687   else
688     key_index = sa0->crypto_key_index;
689
690 out:
691   /* crypto */
692   payload += esp_sz;
693   len -= esp_sz;
694   iv = payload;
695
696   if (ipsec_sa_is_set_IS_AEAD (sa0))
697     {
698       esp_header_t *esp0;
699       u8 *scratch;
700
701       /*
702        * construct the AAD and the nonce (Salt || IV) in a scratch
703        * space in front of the IP header.
704        */
705       scratch = payload - esp_sz;
706       esp0 = (esp_header_t *) (scratch);
707
708       scratch -= (sizeof (esp_aead_t) + pd->hdr_sz);
709       aad = scratch;
710
711       esp_aad_fill (aad, esp0, sa0);
712
713       /*
714        * we don't need to refer to the ESP header anymore so we
715        * can overwrite it with the salt and use the IV where it is
716        * to form the nonce = (Salt + IV)
717        */
718       iv -= sizeof (sa0->salt);
719       clib_memcpy_fast (iv, &sa0->salt, sizeof (sa0->salt));
720
721       tag = payload + len;
722     }
723
724   crypto_start_offset = (payload += iv_sz) - b->data;
725   crypto_len = len - iv_sz;
726
727   if (pd->is_chain && (pd2->lb != b))
728     {
729       /* buffer is chained */
730       flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
731
732       crypto_len = esp_decrypt_chain_crypto (vm, ptd, pd2, sa0, b, icv_sz,
733                                              payload,
734                                              len - pd->iv_sz + pd->icv_sz,
735                                              &tag, 0);
736     }
737
738   *async_pd = *pd;
739   *async_pd2 = *pd2;
740   pd->protect_index = current_protect_index;
741   next[0] = ESP_DECRYPT_NEXT_PENDING;
742
743   /* for AEAD integ_len - crypto_len will be negative, it is ok since it
744    * is ignored by the engine. */
745   return vnet_crypto_async_add_to_frame (vm, f, key_index, crypto_len,
746                                          integ_len - crypto_len,
747                                          crypto_start_offset,
748                                          integ_start_offset,
749                                          bi, async_next, iv, tag, aad, flags);
750 }
751
752 static_always_inline void
753 esp_decrypt_post_crypto (vlib_main_t * vm, vlib_node_runtime_t * node,
754                          esp_decrypt_packet_data_t * pd,
755                          esp_decrypt_packet_data2_t * pd2, vlib_buffer_t * b,
756                          u16 * next, int is_ip6, int is_tun, int is_async)
757 {
758   ipsec_main_t *im = &ipsec_main;
759   ipsec_sa_t *sa0 = vec_elt_at_index (im->sad, pd->sa_index);
760   vlib_buffer_t *lb = b;
761   const u8 esp_sz = sizeof (esp_header_t);
762   const u8 tun_flags = IPSEC_SA_FLAG_IS_TUNNEL | IPSEC_SA_FLAG_IS_TUNNEL_V6;
763   u8 pad_length = 0, next_header = 0;
764   u16 icv_sz;
765
766   /*
767    * redo the anti-reply check
768    * in this frame say we have sequence numbers, s, s+1, s+1, s+1
769    * and s and s+1 are in the window. When we did the anti-replay
770    * check above we did so against the state of the window (W),
771    * after packet s-1. So each of the packets in the sequence will be
772    * accepted.
773    * This time s will be cheked against Ws-1, s+1 chceked against Ws
774    * (i.e. the window state is updated/advnaced)
775    * so this time the successive s+! packet will be dropped.
776    * This is a consequence of batching the decrypts. If the
777    * check-dcrypt-advance process was done for each packet it would
778    * be fine. But we batch the decrypts because it's much more efficient
779    * to do so in SW and if we offload to HW and the process is async.
780    *
781    * You're probably thinking, but this means an attacker can send the
782    * above sequence and cause VPP to perform decrpyts that will fail,
783    * and that's true. But if the attacker can determine s (a valid
784    * sequence number in the window) which is non-trivial, it can generate
785    * a sequence s, s+1, s+2, s+3, ... s+n and nothing will prevent any
786    * implementation, sequential or batching, from decrypting these.
787    */
788   if (ipsec_sa_anti_replay_check (sa0, pd->seq))
789     {
790       b->error = node->errors[ESP_DECRYPT_ERROR_REPLAY];
791       next[0] = ESP_DECRYPT_NEXT_DROP;
792       return;
793     }
794
795   ipsec_sa_anti_replay_advance (sa0, pd->seq);
796
797   if (pd->is_chain)
798     {
799       lb = pd2->lb;
800       icv_sz = pd2->icv_removed ? 0 : pd->icv_sz;
801       if (pd2->free_buffer_index)
802         {
803           vlib_buffer_free_one (vm, pd2->free_buffer_index);
804           lb->next_buffer = 0;
805         }
806       if (lb->current_length < sizeof (esp_footer_t) + icv_sz)
807         {
808           /* esp footer is either splitted in two buffers or in the before
809            * last buffer */
810
811           vlib_buffer_t *before_last = b, *bp = b;
812           while (bp->flags & VLIB_BUFFER_NEXT_PRESENT)
813             {
814               before_last = bp;
815               bp = vlib_get_buffer (vm, bp->next_buffer);
816             }
817           u8 *bt = vlib_buffer_get_tail (before_last);
818
819           if (lb->current_length == icv_sz)
820             {
821               esp_footer_t *f = (esp_footer_t *) (bt - sizeof (*f));
822               pad_length = f->pad_length;
823               next_header = f->next_header;
824             }
825           else
826             {
827               pad_length = (bt - 1)[0];
828               next_header = ((u8 *) vlib_buffer_get_current (lb))[0];
829             }
830         }
831       else
832         {
833           esp_footer_t *f =
834             (esp_footer_t *) (lb->data + lb->current_data +
835                               lb->current_length - sizeof (esp_footer_t) -
836                               icv_sz);
837           pad_length = f->pad_length;
838           next_header = f->next_header;
839         }
840     }
841   else
842     {
843       icv_sz = pd->icv_sz;
844       esp_footer_t *f =
845         (esp_footer_t *) (lb->data + lb->current_data + lb->current_length -
846                           sizeof (esp_footer_t) - icv_sz);
847       pad_length = f->pad_length;
848       next_header = f->next_header;
849     }
850
851   u16 adv = pd->iv_sz + esp_sz;
852   u16 tail = sizeof (esp_footer_t) + pad_length + icv_sz;
853   u16 tail_orig = sizeof (esp_footer_t) + pad_length + pd->icv_sz;
854   b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
855
856   if ((pd->flags & tun_flags) == 0 && !is_tun)  /* transport mode */
857     {
858       u8 udp_sz = (is_ip6 == 0 && pd->flags & IPSEC_SA_FLAG_UDP_ENCAP) ?
859         sizeof (udp_header_t) : 0;
860       u16 ip_hdr_sz = pd->hdr_sz - udp_sz;
861       u8 *old_ip = b->data + pd->current_data - ip_hdr_sz - udp_sz;
862       u8 *ip = old_ip + adv + udp_sz;
863
864       if (is_ip6 && ip_hdr_sz > 64)
865         memmove (ip, old_ip, ip_hdr_sz);
866       else
867         clib_memcpy_le64 (ip, old_ip, ip_hdr_sz);
868
869       b->current_data = pd->current_data + adv - ip_hdr_sz;
870       b->current_length += ip_hdr_sz - adv;
871       esp_remove_tail (vm, b, lb, tail);
872
873       if (is_ip6)
874         {
875           ip6_header_t *ip6 = (ip6_header_t *) ip;
876           u16 len = clib_net_to_host_u16 (ip6->payload_length);
877           len -= adv + tail_orig;
878           ip6->payload_length = clib_host_to_net_u16 (len);
879           ip6->protocol = next_header;
880           next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
881         }
882       else
883         {
884           ip4_header_t *ip4 = (ip4_header_t *) ip;
885           ip_csum_t sum = ip4->checksum;
886           u16 len = clib_net_to_host_u16 (ip4->length);
887           len = clib_host_to_net_u16 (len - adv - tail_orig - udp_sz);
888           sum = ip_csum_update (sum, ip4->protocol, next_header,
889                                 ip4_header_t, protocol);
890           sum = ip_csum_update (sum, ip4->length, len, ip4_header_t, length);
891           ip4->checksum = ip_csum_fold (sum);
892           ip4->protocol = next_header;
893           ip4->length = len;
894           next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
895         }
896     }
897   else
898     {
899       if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP))
900         {
901           next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
902           b->current_data = pd->current_data + adv;
903           b->current_length = pd->current_length - adv;
904           esp_remove_tail (vm, b, lb, tail);
905         }
906       else if (next_header == IP_PROTOCOL_IPV6)
907         {
908           next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
909           b->current_data = pd->current_data + adv;
910           b->current_length = pd->current_length - adv;
911           esp_remove_tail (vm, b, lb, tail);
912         }
913       else
914         {
915           if (is_tun && next_header == IP_PROTOCOL_GRE)
916             {
917               gre_header_t *gre;
918
919               b->current_data = pd->current_data + adv;
920               b->current_length = pd->current_length - adv - tail;
921
922               gre = vlib_buffer_get_current (b);
923
924               vlib_buffer_advance (b, sizeof (*gre));
925
926               switch (clib_net_to_host_u16 (gre->protocol))
927                 {
928                 case GRE_PROTOCOL_teb:
929                   vnet_update_l2_len (b);
930                   next[0] = ESP_DECRYPT_NEXT_L2_INPUT;
931                   break;
932                 case GRE_PROTOCOL_ip4:
933                   next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
934                   break;
935                 case GRE_PROTOCOL_ip6:
936                   next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
937                   break;
938                 default:
939                   b->error = node->errors[ESP_DECRYPT_ERROR_UNSUP_PAYLOAD];
940                   next[0] = ESP_DECRYPT_NEXT_DROP;
941                   break;
942                 }
943             }
944           else
945             {
946               next[0] = ESP_DECRYPT_NEXT_DROP;
947               b->error = node->errors[ESP_DECRYPT_ERROR_UNSUP_PAYLOAD];
948               return;
949             }
950         }
951       if (is_tun)
952         {
953           if (ipsec_sa_is_set_IS_PROTECT (sa0))
954             {
955               /*
956                * There are two encap possibilities
957                * 1) the tunnel and ths SA are prodiving encap, i.e. it's
958                *   MAC | SA-IP | TUN-IP | ESP | PAYLOAD
959                * implying the SA is in tunnel mode (on a tunnel interface)
960                * 2) only the tunnel provides encap
961                *   MAC | TUN-IP | ESP | PAYLOAD
962                * implying the SA is in transport mode.
963                *
964                * For 2) we need only strip the tunnel encap and we're good.
965                *  since the tunnel and crypto ecnap (int the tun=protect
966                * object) are the same and we verified above that these match
967                * for 1) we need to strip the SA-IP outer headers, to
968                * reveal the tunnel IP and then check that this matches
969                * the configured tunnel.
970                */
971               const ipsec_tun_protect_t *itp;
972
973               if (is_async)
974                 itp = ipsec_tun_protect_get (pd->protect_index);
975               else
976                 itp =
977                   ipsec_tun_protect_get (vnet_buffer (b)->
978                                          ipsec.protect_index);
979
980               if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP))
981                 {
982                   const ip4_header_t *ip4;
983
984                   ip4 = vlib_buffer_get_current (b);
985
986                   if (!ip46_address_is_equal_v4 (&itp->itp_tun.src,
987                                                  &ip4->dst_address) ||
988                       !ip46_address_is_equal_v4 (&itp->itp_tun.dst,
989                                                  &ip4->src_address))
990                     {
991                       next[0] = ESP_DECRYPT_NEXT_DROP;
992                       b->error = node->errors[ESP_DECRYPT_ERROR_TUN_NO_PROTO];
993                     }
994                 }
995               else if (next_header == IP_PROTOCOL_IPV6)
996                 {
997                   const ip6_header_t *ip6;
998
999                   ip6 = vlib_buffer_get_current (b);
1000
1001                   if (!ip46_address_is_equal_v6 (&itp->itp_tun.src,
1002                                                  &ip6->dst_address) ||
1003                       !ip46_address_is_equal_v6 (&itp->itp_tun.dst,
1004                                                  &ip6->src_address))
1005                     {
1006                       next[0] = ESP_DECRYPT_NEXT_DROP;
1007                       b->error = node->errors[ESP_DECRYPT_ERROR_TUN_NO_PROTO];
1008                     }
1009                 }
1010             }
1011         }
1012     }
1013 }
1014
1015 /* when submitting a frame is failed, drop all buffers in the frame */
1016 static_always_inline void
1017 esp_async_recycle_failed_submit (vnet_crypto_async_frame_t * f,
1018                                  vlib_buffer_t ** b, u16 * next)
1019 {
1020   u32 n_drop = f->n_elts;
1021   while (--n_drop)
1022     {
1023       (b - n_drop)[0]->error = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
1024       (next - n_drop)[0] = ESP_DECRYPT_NEXT_DROP;
1025     }
1026   vnet_crypto_async_reset_frame (f);
1027 }
1028
1029 always_inline uword
1030 esp_decrypt_inline (vlib_main_t * vm,
1031                     vlib_node_runtime_t * node, vlib_frame_t * from_frame,
1032                     int is_ip6, int is_tun, u16 async_next)
1033 {
1034   ipsec_main_t *im = &ipsec_main;
1035   u32 thread_index = vm->thread_index;
1036   u16 len;
1037   ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
1038   u32 *from = vlib_frame_vector_args (from_frame);
1039   u32 n_left = from_frame->n_vectors;
1040   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1041   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1042   esp_decrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
1043   esp_decrypt_packet_data2_t pkt_data2[VLIB_FRAME_SIZE], *pd2 = pkt_data2;
1044   esp_decrypt_packet_data_t cpd = { };
1045   u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
1046   const u8 esp_sz = sizeof (esp_header_t);
1047   ipsec_sa_t *sa0 = 0;
1048   vnet_crypto_op_t _op, *op = &_op;
1049   vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
1050   vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
1051   vnet_crypto_async_frame_t *async_frame = 0;
1052   int is_async = im->async_mode;
1053   vnet_crypto_async_op_id_t last_async_op = ~0;
1054
1055   vlib_get_buffers (vm, from, b, n_left);
1056   if (!is_async)
1057     {
1058       vec_reset_length (ptd->crypto_ops);
1059       vec_reset_length (ptd->integ_ops);
1060       vec_reset_length (ptd->chained_crypto_ops);
1061       vec_reset_length (ptd->chained_integ_ops);
1062     }
1063   vec_reset_length (ptd->chunks);
1064   clib_memset_u16 (nexts, -1, n_left);
1065
1066   while (n_left > 0)
1067     {
1068       u8 *payload;
1069
1070       if (n_left > 2)
1071         {
1072           u8 *p;
1073           vlib_prefetch_buffer_header (b[2], LOAD);
1074           p = vlib_buffer_get_current (b[1]);
1075           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
1076           p -= CLIB_CACHE_LINE_BYTES;
1077           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
1078         }
1079
1080       u32 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
1081       if (n_bufs == 0)
1082         {
1083           b[0]->error = node->errors[ESP_DECRYPT_ERROR_NO_BUFFERS];
1084           next[0] = ESP_DECRYPT_NEXT_DROP;
1085           goto next;
1086         }
1087
1088       if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
1089         {
1090           if (current_sa_pkts)
1091             vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1092                                              current_sa_index,
1093                                              current_sa_pkts,
1094                                              current_sa_bytes);
1095           current_sa_bytes = current_sa_pkts = 0;
1096
1097           current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
1098           sa0 = pool_elt_at_index (im->sad, current_sa_index);
1099           cpd.icv_sz = sa0->integ_icv_size;
1100           cpd.iv_sz = sa0->crypto_iv_size;
1101           cpd.flags = sa0->flags;
1102           cpd.sa_index = current_sa_index;
1103
1104           /* submit frame when op_id is different then the old one */
1105           if (is_async && last_async_op != sa0->crypto_async_dec_op_id)
1106             {
1107               if (async_frame && async_frame->n_elts)
1108                 {
1109                   if (vnet_crypto_async_submit_open_frame (vm, async_frame))
1110                     esp_async_recycle_failed_submit (async_frame, b, next);
1111                 }
1112               async_frame =
1113                 vnet_crypto_async_get_frame (vm, sa0->crypto_async_dec_op_id);
1114               last_async_op = sa0->crypto_async_dec_op_id;
1115             }
1116         }
1117
1118       if (PREDICT_FALSE (~0 == sa0->decrypt_thread_index))
1119         {
1120           /* this is the first packet to use this SA, claim the SA
1121            * for this thread. this could happen simultaneously on
1122            * another thread */
1123           clib_atomic_cmp_and_swap (&sa0->decrypt_thread_index, ~0,
1124                                     ipsec_sa_assign_thread (thread_index));
1125         }
1126
1127       if (PREDICT_TRUE (thread_index != sa0->decrypt_thread_index))
1128         {
1129           next[0] = ESP_DECRYPT_NEXT_HANDOFF;
1130           goto next;
1131         }
1132
1133       /* store packet data for next round for easier prefetch */
1134       pd->sa_data = cpd.sa_data;
1135       pd->current_data = b[0]->current_data;
1136       pd->hdr_sz = pd->current_data - vnet_buffer (b[0])->l3_hdr_offset;
1137       payload = b[0]->data + pd->current_data;
1138       pd->seq = clib_host_to_net_u32 (((esp_header_t *) payload)->seq);
1139       pd->is_chain = 0;
1140       pd2->lb = b[0];
1141       pd2->free_buffer_index = 0;
1142       pd2->icv_removed = 0;
1143
1144       if (n_bufs > 1)
1145         {
1146           pd->is_chain = 1;
1147           /* find last buffer in the chain */
1148           while (pd2->lb->flags & VLIB_BUFFER_NEXT_PRESENT)
1149             pd2->lb = vlib_get_buffer (vm, pd2->lb->next_buffer);
1150
1151           crypto_ops = &ptd->chained_crypto_ops;
1152           integ_ops = &ptd->chained_integ_ops;
1153         }
1154
1155       pd->current_length = b[0]->current_length;
1156
1157       /* anti-reply check */
1158       if (ipsec_sa_anti_replay_check (sa0, pd->seq))
1159         {
1160           b[0]->error = node->errors[ESP_DECRYPT_ERROR_REPLAY];
1161           next[0] = ESP_DECRYPT_NEXT_DROP;
1162           goto next;
1163         }
1164
1165       if (pd->current_length < cpd.icv_sz + esp_sz + cpd.iv_sz)
1166         {
1167           b[0]->error = node->errors[ESP_DECRYPT_ERROR_RUNT];
1168           next[0] = ESP_DECRYPT_NEXT_DROP;
1169           goto next;
1170         }
1171
1172       len = pd->current_length - cpd.icv_sz;
1173       current_sa_pkts += 1;
1174       current_sa_bytes += vlib_buffer_length_in_chain (vm, b[0]);
1175
1176       if (is_async)
1177         {
1178           int ret = esp_decrypt_prepare_async_frame (vm, node, ptd,
1179                                                      &async_frame,
1180                                                      sa0, payload, len,
1181                                                      cpd.icv_sz,
1182                                                      cpd.iv_sz,
1183                                                      pd, pd2,
1184                                                      from[b - bufs],
1185                                                      b[0], next, async_next);
1186           if (PREDICT_FALSE (ret < 0))
1187             {
1188               esp_async_recycle_failed_submit (async_frame, b, next);
1189               goto next;
1190             }
1191         }
1192       else
1193         esp_decrypt_prepare_sync_op (vm, node, ptd, &crypto_ops, &integ_ops,
1194                                      op, sa0, payload, len, cpd.icv_sz,
1195                                      cpd.iv_sz, pd, pd2, b[0], next,
1196                                      b - bufs);
1197       /* next */
1198     next:
1199       n_left -= 1;
1200       next += 1;
1201       pd += 1;
1202       pd2 += 1;
1203       b += 1;
1204     }
1205
1206   if (PREDICT_TRUE (~0 != current_sa_index))
1207     vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1208                                      current_sa_index, current_sa_pkts,
1209                                      current_sa_bytes);
1210
1211   if (is_async)
1212     {
1213       if (async_frame && async_frame->n_elts)
1214         {
1215           if (vnet_crypto_async_submit_open_frame (vm, async_frame) < 0)
1216             esp_async_recycle_failed_submit (async_frame, b, next);
1217         }
1218
1219       /* no post process in async */
1220       n_left = from_frame->n_vectors;
1221       vlib_node_increment_counter (vm, node->node_index,
1222                                    ESP_DECRYPT_ERROR_RX_PKTS, n_left);
1223       vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
1224
1225       return n_left;
1226     }
1227   else
1228     {
1229       esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts,
1230                        ESP_DECRYPT_ERROR_INTEG_ERROR);
1231       esp_process_chained_ops (vm, node, ptd->chained_integ_ops, bufs, nexts,
1232                                ptd->chunks, ESP_DECRYPT_ERROR_INTEG_ERROR);
1233
1234       esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts,
1235                        ESP_DECRYPT_ERROR_DECRYPTION_FAILED);
1236       esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, bufs, nexts,
1237                                ptd->chunks,
1238                                ESP_DECRYPT_ERROR_DECRYPTION_FAILED);
1239     }
1240
1241   /* Post decryption ronud - adjust packet data start and length and next
1242      node */
1243
1244   n_left = from_frame->n_vectors;
1245   next = nexts;
1246   pd = pkt_data;
1247   pd2 = pkt_data2;
1248   b = bufs;
1249
1250   while (n_left)
1251     {
1252       if (n_left >= 2)
1253         {
1254           void *data = b[1]->data + pd[1].current_data;
1255
1256           /* buffer metadata */
1257           vlib_prefetch_buffer_header (b[1], LOAD);
1258
1259           /* esp_footer_t */
1260           CLIB_PREFETCH (data + pd[1].current_length - pd[1].icv_sz - 2,
1261                          CLIB_CACHE_LINE_BYTES, LOAD);
1262
1263           /* packet headers */
1264           CLIB_PREFETCH (data - CLIB_CACHE_LINE_BYTES,
1265                          CLIB_CACHE_LINE_BYTES * 2, LOAD);
1266         }
1267
1268       if (next[0] >= ESP_DECRYPT_N_NEXT)
1269         esp_decrypt_post_crypto (vm, node, pd, pd2, b[0], next, is_ip6,
1270                                  is_tun, 0);
1271
1272       /* trace: */
1273       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1274         {
1275           esp_decrypt_trace_t *tr;
1276           tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
1277           sa0 = pool_elt_at_index (im->sad,
1278                                    vnet_buffer (b[0])->ipsec.sad_index);
1279           tr->crypto_alg = sa0->crypto_alg;
1280           tr->integ_alg = sa0->integ_alg;
1281           tr->seq = pd->seq;
1282           tr->sa_seq = sa0->last_seq;
1283           tr->sa_seq_hi = sa0->seq_hi;
1284         }
1285
1286       /* next */
1287       n_left -= 1;
1288       next += 1;
1289       pd += 1;
1290       pd2 += 1;
1291       b += 1;
1292     }
1293
1294   n_left = from_frame->n_vectors;
1295   vlib_node_increment_counter (vm, node->node_index,
1296                                ESP_DECRYPT_ERROR_RX_PKTS, n_left);
1297
1298   vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
1299
1300   return n_left;
1301 }
1302
1303 always_inline uword
1304 esp_decrypt_post_inline (vlib_main_t * vm,
1305                          vlib_node_runtime_t * node,
1306                          vlib_frame_t * from_frame, int is_ip6, int is_tun)
1307 {
1308   ipsec_main_t *im = &ipsec_main;
1309   u32 *from = vlib_frame_vector_args (from_frame);
1310   u32 n_left = from_frame->n_vectors;
1311   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1312   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1313   vlib_get_buffers (vm, from, b, n_left);
1314
1315   while (n_left > 0)
1316     {
1317       esp_decrypt_packet_data_t *pd = &(esp_post_data (b[0]))->decrypt_data;
1318
1319       if (n_left > 2)
1320         {
1321           vlib_prefetch_buffer_header (b[2], LOAD);
1322           vlib_prefetch_buffer_header (b[1], LOAD);
1323         }
1324
1325       if (!pd->is_chain)
1326         esp_decrypt_post_crypto (vm, node, pd, 0, b[0], next, is_ip6, is_tun,
1327                                  1);
1328       else
1329         {
1330           esp_decrypt_packet_data2_t *pd2 = esp_post_data2 (b[0]);
1331           esp_decrypt_post_crypto (vm, node, pd, pd2, b[0], next, is_ip6,
1332                                    is_tun, 1);
1333         }
1334
1335       /*trace: */
1336       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1337         {
1338           ipsec_sa_t *sa0 = pool_elt_at_index (im->sad, pd->sa_index);
1339           esp_decrypt_trace_t *tr;
1340           esp_decrypt_packet_data_t *async_pd =
1341             &(esp_post_data (b[0]))->decrypt_data;
1342           tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
1343           sa0 = pool_elt_at_index (im->sad, async_pd->sa_index);
1344
1345           tr->crypto_alg = sa0->crypto_alg;
1346           tr->integ_alg = sa0->integ_alg;
1347           tr->seq = pd->seq;
1348           tr->sa_seq = sa0->last_seq;
1349           tr->sa_seq_hi = sa0->seq_hi;
1350         }
1351
1352       n_left--;
1353       next++;
1354       b++;
1355     }
1356
1357   n_left = from_frame->n_vectors;
1358   vlib_node_increment_counter (vm, node->node_index,
1359                                ESP_DECRYPT_ERROR_RX_POST_PKTS, n_left);
1360
1361   vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
1362
1363   return n_left;
1364 }
1365
1366 VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
1367                                   vlib_node_runtime_t * node,
1368                                   vlib_frame_t * from_frame)
1369 {
1370   return esp_decrypt_inline (vm, node, from_frame, 0, 0,
1371                              esp_decrypt_async_next.esp4_post_next);
1372 }
1373
1374 VLIB_NODE_FN (esp4_decrypt_post_node) (vlib_main_t * vm,
1375                                        vlib_node_runtime_t * node,
1376                                        vlib_frame_t * from_frame)
1377 {
1378   return esp_decrypt_post_inline (vm, node, from_frame, 0, 0);
1379 }
1380
1381 VLIB_NODE_FN (esp4_decrypt_tun_node) (vlib_main_t * vm,
1382                                       vlib_node_runtime_t * node,
1383                                       vlib_frame_t * from_frame)
1384 {
1385   return esp_decrypt_inline (vm, node, from_frame, 0, 1,
1386                              esp_decrypt_async_next.esp4_tun_post_next);
1387 }
1388
1389 VLIB_NODE_FN (esp4_decrypt_tun_post_node) (vlib_main_t * vm,
1390                                            vlib_node_runtime_t * node,
1391                                            vlib_frame_t * from_frame)
1392 {
1393   return esp_decrypt_post_inline (vm, node, from_frame, 0, 1);
1394 }
1395
1396 VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
1397                                   vlib_node_runtime_t * node,
1398                                   vlib_frame_t * from_frame)
1399 {
1400   return esp_decrypt_inline (vm, node, from_frame, 1, 0,
1401                              esp_decrypt_async_next.esp6_post_next);
1402 }
1403
1404 VLIB_NODE_FN (esp6_decrypt_post_node) (vlib_main_t * vm,
1405                                        vlib_node_runtime_t * node,
1406                                        vlib_frame_t * from_frame)
1407 {
1408   return esp_decrypt_post_inline (vm, node, from_frame, 1, 0);
1409 }
1410
1411 VLIB_NODE_FN (esp6_decrypt_tun_node) (vlib_main_t * vm,
1412                                       vlib_node_runtime_t * node,
1413                                       vlib_frame_t * from_frame)
1414 {
1415   return esp_decrypt_inline (vm, node, from_frame, 1, 1,
1416                              esp_decrypt_async_next.esp6_tun_post_next);
1417 }
1418
1419 VLIB_NODE_FN (esp6_decrypt_tun_post_node) (vlib_main_t * vm,
1420                                            vlib_node_runtime_t * node,
1421                                            vlib_frame_t * from_frame)
1422 {
1423   return esp_decrypt_post_inline (vm, node, from_frame, 1, 1);
1424 }
1425
1426 VLIB_NODE_FN (esp_decrypt_pending_node) (vlib_main_t * vm,
1427                                          vlib_node_runtime_t * node,
1428                                          vlib_frame_t * from_frame)
1429 {
1430   return from_frame->n_vectors;
1431 }
1432
1433 /* *INDENT-OFF* */
1434 VLIB_REGISTER_NODE (esp_decrypt_pending_node) = {
1435   .name = "esp-decrypt-pending",
1436   .vector_size = sizeof (u32),
1437   .type = VLIB_NODE_TYPE_INTERNAL,
1438
1439   .n_next_nodes = 0
1440 };
1441 /* *INDENT-ON* */
1442
1443 /* *INDENT-OFF* */
1444 VLIB_REGISTER_NODE (esp4_decrypt_node) = {
1445   .name = "esp4-decrypt",
1446   .vector_size = sizeof (u32),
1447   .format_trace = format_esp_decrypt_trace,
1448   .type = VLIB_NODE_TYPE_INTERNAL,
1449
1450   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1451   .error_strings = esp_decrypt_error_strings,
1452
1453   .n_next_nodes = ESP_DECRYPT_N_NEXT,
1454   .next_nodes = {
1455     [ESP_DECRYPT_NEXT_DROP] = "ip4-drop",
1456     [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1457     [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
1458     [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
1459     [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-handoff",
1460     [ESP_DECRYPT_NEXT_PENDING] = "esp-decrypt-pending"
1461   },
1462 };
1463
1464 VLIB_REGISTER_NODE (esp4_decrypt_post_node) = {
1465   .name = "esp4-decrypt-post",
1466   .vector_size = sizeof (u32),
1467   .format_trace = format_esp_decrypt_trace,
1468   .type = VLIB_NODE_TYPE_INTERNAL,
1469
1470   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1471   .error_strings = esp_decrypt_error_strings,
1472
1473   .sibling_of = "esp4-decrypt",
1474 };
1475
1476 VLIB_REGISTER_NODE (esp6_decrypt_node) = {
1477   .name = "esp6-decrypt",
1478   .vector_size = sizeof (u32),
1479   .format_trace = format_esp_decrypt_trace,
1480   .type = VLIB_NODE_TYPE_INTERNAL,
1481
1482   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1483   .error_strings = esp_decrypt_error_strings,
1484
1485   .n_next_nodes = ESP_DECRYPT_N_NEXT,
1486   .next_nodes = {
1487     [ESP_DECRYPT_NEXT_DROP] = "ip6-drop",
1488     [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1489     [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
1490     [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
1491     [ESP_DECRYPT_NEXT_HANDOFF]=  "esp6-decrypt-handoff",
1492     [ESP_DECRYPT_NEXT_PENDING] = "esp-decrypt-pending"
1493   },
1494 };
1495
1496 VLIB_REGISTER_NODE (esp6_decrypt_post_node) = {
1497   .name = "esp6-decrypt-post",
1498   .vector_size = sizeof (u32),
1499   .format_trace = format_esp_decrypt_trace,
1500   .type = VLIB_NODE_TYPE_INTERNAL,
1501
1502   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1503   .error_strings = esp_decrypt_error_strings,
1504
1505   .sibling_of = "esp6-decrypt",
1506 };
1507
1508 VLIB_REGISTER_NODE (esp4_decrypt_tun_node) = {
1509   .name = "esp4-decrypt-tun",
1510   .vector_size = sizeof (u32),
1511   .format_trace = format_esp_decrypt_trace,
1512   .type = VLIB_NODE_TYPE_INTERNAL,
1513   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1514   .error_strings = esp_decrypt_error_strings,
1515   .n_next_nodes = ESP_DECRYPT_N_NEXT,
1516   .next_nodes = {
1517     [ESP_DECRYPT_NEXT_DROP] = "ip4-drop",
1518     [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1519     [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
1520     [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
1521     [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-tun-handoff",
1522     [ESP_DECRYPT_NEXT_PENDING] = "esp-decrypt-pending"
1523   },
1524 };
1525
1526 VLIB_REGISTER_NODE (esp4_decrypt_tun_post_node) = {
1527   .name = "esp4-decrypt-tun-post",
1528   .vector_size = sizeof (u32),
1529   .format_trace = format_esp_decrypt_trace,
1530   .type = VLIB_NODE_TYPE_INTERNAL,
1531
1532   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1533   .error_strings = esp_decrypt_error_strings,
1534
1535   .sibling_of = "esp4-decrypt-tun",
1536 };
1537
1538 VLIB_REGISTER_NODE (esp6_decrypt_tun_node) = {
1539   .name = "esp6-decrypt-tun",
1540   .vector_size = sizeof (u32),
1541   .format_trace = format_esp_decrypt_trace,
1542   .type = VLIB_NODE_TYPE_INTERNAL,
1543   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1544   .error_strings = esp_decrypt_error_strings,
1545   .n_next_nodes = ESP_DECRYPT_N_NEXT,
1546   .next_nodes = {
1547     [ESP_DECRYPT_NEXT_DROP] = "ip6-drop",
1548     [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1549     [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
1550     [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
1551     [ESP_DECRYPT_NEXT_HANDOFF]=  "esp6-decrypt-tun-handoff",
1552     [ESP_DECRYPT_NEXT_PENDING] = "esp-decrypt-pending"
1553   },
1554 };
1555
1556 VLIB_REGISTER_NODE (esp6_decrypt_tun_post_node) = {
1557   .name = "esp6-decrypt-tun-post",
1558   .vector_size = sizeof (u32),
1559   .format_trace = format_esp_decrypt_trace,
1560   .type = VLIB_NODE_TYPE_INTERNAL,
1561
1562   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
1563   .error_strings = esp_decrypt_error_strings,
1564
1565   .sibling_of = "esp6-decrypt-tun",
1566 };
1567 /* *INDENT-ON* */
1568
1569 /*
1570  * fd.io coding-style-patch-verification: ON
1571  *
1572  * Local Variables:
1573  * eval: (c-set-style "gnu")
1574  * End:
1575  */