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