112b96a12bd48a1429fbd53557ebc435812b274a
[vpp.git] / src / plugins / dpdk / ipsec / esp_decrypt.c
1 /*
2  * esp_decrypt.c : IPSec ESP Decrypt node using DPDK Cryptodev
3  *
4  * Copyright (c) 2017 Intel 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 opy 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
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <dpdk/buffer.h>
25 #include <dpdk/ipsec/ipsec.h>
26 #include <dpdk/device/dpdk.h>
27 #include <dpdk/device/dpdk_priv.h>
28
29 #define foreach_esp_decrypt_next               \
30 _(DROP, "error-drop")                          \
31 _(IP4_INPUT, "ip4-input-no-checksum")          \
32 _(IP6_INPUT, "ip6-input")
33
34 #define _(v, s) ESP_DECRYPT_NEXT_##v,
35 typedef enum
36 {
37   foreach_esp_decrypt_next
38 #undef _
39     ESP_DECRYPT_N_NEXT,
40 } esp_decrypt_next_t;
41
42 #define foreach_esp_decrypt_error                \
43  _(RX_PKTS, "ESP pkts received")                 \
44  _(DECRYPTION_FAILED, "ESP decryption failed")   \
45  _(REPLAY, "SA replayed packet")                 \
46  _(NOT_IP, "Not IP packet (dropped)")            \
47  _(ENQ_FAIL, "Enqueue failed (buffer full)")     \
48  _(DISCARD, "Not enough crypto operations, discarding frame")  \
49  _(BAD_LEN, "Invalid ciphertext length")         \
50  _(SESSION, "Failed to get crypto session")      \
51  _(NOSUP, "Cipher/Auth not supported")
52
53
54 typedef enum
55 {
56 #define _(sym,str) ESP_DECRYPT_ERROR_##sym,
57   foreach_esp_decrypt_error
58 #undef _
59     ESP_DECRYPT_N_ERROR,
60 } esp_decrypt_error_t;
61
62 static char *esp_decrypt_error_strings[] = {
63 #define _(sym,string) string,
64   foreach_esp_decrypt_error
65 #undef _
66 };
67
68 extern vlib_node_registration_t dpdk_esp4_decrypt_node;
69 extern vlib_node_registration_t dpdk_esp6_decrypt_node;
70
71 typedef struct
72 {
73   ipsec_crypto_alg_t crypto_alg;
74   ipsec_integ_alg_t integ_alg;
75   u8 packet_data[64];
76 } esp_decrypt_trace_t;
77
78 /* packet trace format function */
79 static u8 *
80 format_esp_decrypt_trace (u8 * s, va_list * args)
81 {
82   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
83   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
84   esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
85   u32 indent = format_get_indent (s);
86
87   s = format (s, "cipher %U auth %U\n",
88               format_ipsec_crypto_alg, t->crypto_alg,
89               format_ipsec_integ_alg, t->integ_alg);
90   s = format (s, "%U%U",
91               format_white_space, indent, format_esp_header, t->packet_data);
92   return s;
93 }
94
95 always_inline uword
96 dpdk_esp_decrypt_inline (vlib_main_t * vm,
97                          vlib_node_runtime_t * node,
98                          vlib_frame_t * from_frame, int is_ip6)
99 {
100   u32 n_left_from, *from, *to_next, next_index, thread_index;
101   ipsec_main_t *im = &ipsec_main;
102   u32 thread_idx = vlib_get_thread_index ();
103   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
104   crypto_resource_t *res = 0;
105   ipsec_sa_t *sa0 = 0;
106   crypto_alg_t *cipher_alg = 0, *auth_alg = 0;
107   struct rte_cryptodev_sym_session *session = 0;
108   u32 ret, last_sa_index = ~0;
109   u8 numa = rte_socket_id ();
110   u8 is_aead = 0;
111   crypto_worker_main_t *cwm =
112     vec_elt_at_index (dcm->workers_main, thread_idx);
113   struct rte_crypto_op **ops = cwm->ops;
114
115   from = vlib_frame_vector_args (from_frame);
116   n_left_from = from_frame->n_vectors;
117   thread_index = vm->thread_index;
118
119   ret = crypto_alloc_ops (numa, ops, n_left_from);
120   if (ret)
121     {
122       if (is_ip6)
123         vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index,
124                                      ESP_DECRYPT_ERROR_DISCARD, 1);
125       else
126         vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index,
127                                      ESP_DECRYPT_ERROR_DISCARD, 1);
128       /* Discard whole frame */
129       return n_left_from;
130     }
131
132   next_index = ESP_DECRYPT_NEXT_DROP;
133
134   while (n_left_from > 0)
135     {
136       u32 n_left_to_next;
137
138       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
139
140       while (n_left_from > 0 && n_left_to_next > 0)
141         {
142           clib_error_t *error;
143           u32 bi0, sa_index0, iv_size;
144           u8 trunc_size;
145           vlib_buffer_t *b0;
146           esp_header_t *esp0;
147           struct rte_mbuf *mb0;
148           struct rte_crypto_op *op;
149           u16 res_idx;
150
151           bi0 = from[0];
152           from += 1;
153           n_left_from -= 1;
154
155           b0 = vlib_get_buffer (vm, bi0);
156           mb0 = rte_mbuf_from_vlib_buffer (b0);
157           esp0 = vlib_buffer_get_current (b0);
158
159           /* ih0/ih6_0 */
160           CLIB_PREFETCH (esp0, sizeof (esp0[0]) + 16, LOAD);
161           /* mb0 */
162           CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE);
163
164           op = ops[0];
165           ops += 1;
166           ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED);
167
168           dpdk_op_priv_t *priv = crypto_op_get_priv (op);
169           /* store bi in op private */
170           priv->bi = bi0;
171           priv->encrypt = 0;
172
173           u16 op_len =
174             sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]);
175           CLIB_PREFETCH (op, op_len, STORE);
176
177           sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
178           vlib_prefetch_combined_counter (&ipsec_sa_counters,
179                                           thread_index, sa_index0);
180
181           if (sa_index0 != last_sa_index)
182             {
183               sa0 = pool_elt_at_index (im->sad, sa_index0);
184
185               cipher_alg =
186                 vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
187               auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
188
189               is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD);
190               if (is_aead)
191                 auth_alg = cipher_alg;
192
193               res_idx = get_resource (cwm, sa0);
194
195               if (PREDICT_FALSE (res_idx == (u16) ~ 0))
196                 {
197                   clib_warning ("unsupported SA by thread index %u",
198                                 thread_idx);
199                   if (is_ip6)
200                     vlib_node_increment_counter (vm,
201                                                  dpdk_esp6_decrypt_node.index,
202                                                  ESP_DECRYPT_ERROR_NOSUP, 1);
203                   else
204                     vlib_node_increment_counter (vm,
205                                                  dpdk_esp4_decrypt_node.index,
206                                                  ESP_DECRYPT_ERROR_NOSUP, 1);
207                   to_next[0] = bi0;
208                   to_next += 1;
209                   n_left_to_next -= 1;
210                   goto trace;
211                 }
212               res = vec_elt_at_index (dcm->resource, res_idx);
213
214               error = crypto_get_session (&session, sa_index0, res, cwm, 0);
215               if (PREDICT_FALSE (error || !session))
216                 {
217                   clib_warning ("failed to get crypto session");
218                   if (is_ip6)
219                     vlib_node_increment_counter (vm,
220                                                  dpdk_esp6_decrypt_node.index,
221                                                  ESP_DECRYPT_ERROR_SESSION,
222                                                  1);
223                   else
224                     vlib_node_increment_counter (vm,
225                                                  dpdk_esp4_decrypt_node.index,
226                                                  ESP_DECRYPT_ERROR_SESSION,
227                                                  1);
228                   to_next[0] = bi0;
229                   to_next += 1;
230                   n_left_to_next -= 1;
231                   goto trace;
232                 }
233
234               last_sa_index = sa_index0;
235             }
236
237           /* anti-replay check */
238           if (ipsec_sa_anti_replay_check
239               (sa0, clib_host_to_net_u32 (esp0->seq)))
240             {
241               clib_warning ("failed anti-replay check");
242               if (is_ip6)
243                 vlib_node_increment_counter (vm,
244                                              dpdk_esp6_decrypt_node.index,
245                                              ESP_DECRYPT_ERROR_REPLAY, 1);
246               else
247                 vlib_node_increment_counter (vm,
248                                              dpdk_esp4_decrypt_node.index,
249                                              ESP_DECRYPT_ERROR_REPLAY, 1);
250               to_next[0] = bi0;
251               to_next += 1;
252               n_left_to_next -= 1;
253               goto trace;
254             }
255
256           if (is_ip6)
257             priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT6_POST;
258           else
259             priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT4_POST;
260
261           /* FIXME multi-seg */
262           vlib_increment_combined_counter
263             (&ipsec_sa_counters, thread_index, sa_index0,
264              1, b0->current_length);
265
266           res->ops[res->n_ops] = op;
267           res->bi[res->n_ops] = bi0;
268           res->n_ops += 1;
269
270           /* Convert vlib buffer to mbuf */
271           mb0->data_len = b0->current_length;
272           mb0->pkt_len = b0->current_length;
273           mb0->data_off = RTE_PKTMBUF_HEADROOM + b0->current_data;
274
275           trunc_size = auth_alg->trunc_size;
276           iv_size = cipher_alg->iv_len;
277
278           /* Outer IP header has already been stripped */
279           u16 payload_len =
280             b0->current_length - sizeof (esp_header_t) - iv_size - trunc_size;
281
282           ASSERT (payload_len >= 4);
283
284           if (payload_len & (cipher_alg->boundary - 1))
285             {
286               clib_warning ("payload %u not multiple of %d\n",
287                             payload_len, cipher_alg->boundary);
288               if (is_ip6)
289                 vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index,
290                                              ESP_DECRYPT_ERROR_BAD_LEN, 1);
291               else
292                 vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index,
293                                              ESP_DECRYPT_ERROR_BAD_LEN, 1);
294               res->n_ops -= 1;
295               to_next[0] = bi0;
296               to_next += 1;
297               n_left_to_next -= 1;
298               goto trace;
299             }
300
301           u32 cipher_off, cipher_len;
302           u32 auth_len = 0;
303           u8 *aad = NULL;
304
305           u8 *iv = (u8 *) (esp0 + 1);
306
307           dpdk_gcm_cnt_blk *icb = &priv->cb;
308
309           cipher_off = sizeof (esp_header_t) + iv_size;
310           cipher_len = payload_len;
311
312           u8 *digest = vlib_buffer_get_tail (b0) - trunc_size;
313           u64 digest_paddr =
314             mb0->buf_physaddr + digest - ((u8 *) mb0->buf_addr);
315
316           if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC)
317             clib_memcpy_fast (icb, iv, 16);
318           else                  /* CTR/GCM */
319             {
320               u32 *_iv = (u32 *) iv;
321
322               crypto_set_icb (icb, sa0->salt, _iv[0], _iv[1]);
323             }
324
325           if (is_aead)
326             {
327               aad = priv->aad;
328               u32 *_aad = (u32 *) aad;
329               clib_memcpy_fast (aad, esp0, 8);
330
331               /* _aad[3] should always be 0 */
332               if (PREDICT_FALSE (ipsec_sa_is_set_USE_ESN (sa0)))
333                 {
334                   _aad[2] = _aad[1];
335                   _aad[1] = clib_host_to_net_u32 (sa0->seq_hi);
336                 }
337               else
338                 _aad[2] = 0;
339             }
340           else
341             {
342               auth_len = sizeof (esp_header_t) + iv_size + payload_len;
343
344               if (ipsec_sa_is_set_USE_ESN (sa0))
345                 {
346                   clib_memcpy_fast (priv->icv, digest, trunc_size);
347                   u32 *_digest = (u32 *) digest;
348                   _digest[0] = clib_host_to_net_u32 (sa0->seq_hi);
349                   auth_len += sizeof (sa0->seq_hi);
350
351                   digest = priv->icv;
352                   digest_paddr =
353                     op->phys_addr + (uintptr_t) priv->icv - (uintptr_t) op;
354                 }
355             }
356
357           crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len,
358                            0, auth_len, aad, digest, digest_paddr);
359         trace:
360           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
361             {
362               esp_decrypt_trace_t *tr =
363                 vlib_add_trace (vm, node, b0, sizeof (*tr));
364               tr->crypto_alg = sa0->crypto_alg;
365               tr->integ_alg = sa0->integ_alg;
366               clib_memcpy_fast (tr->packet_data, vlib_buffer_get_current (b0),
367                                 sizeof (esp_header_t));
368             }
369         }
370       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
371     }
372
373   if (is_ip6)
374     {
375       vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index,
376                                    ESP_DECRYPT_ERROR_RX_PKTS,
377                                    from_frame->n_vectors);
378
379       crypto_enqueue_ops (vm, cwm, dpdk_esp6_decrypt_node.index,
380                           ESP_DECRYPT_ERROR_ENQ_FAIL, numa, 0 /* encrypt */ );
381     }
382   else
383     {
384       vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index,
385                                    ESP_DECRYPT_ERROR_RX_PKTS,
386                                    from_frame->n_vectors);
387
388       crypto_enqueue_ops (vm, cwm, dpdk_esp4_decrypt_node.index,
389                           ESP_DECRYPT_ERROR_ENQ_FAIL, numa, 0 /* encrypt */ );
390     }
391
392   crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops);
393
394   return from_frame->n_vectors;
395 }
396
397 VLIB_NODE_FN (dpdk_esp4_decrypt_node) (vlib_main_t * vm,
398                                        vlib_node_runtime_t * node,
399                                        vlib_frame_t * from_frame)
400 {
401   return dpdk_esp_decrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ );
402 }
403
404 /* *INDENT-OFF* */
405 VLIB_REGISTER_NODE (dpdk_esp4_decrypt_node) = {
406   .name = "dpdk-esp4-decrypt",
407   .vector_size = sizeof (u32),
408   .format_trace = format_esp_decrypt_trace,
409   .type = VLIB_NODE_TYPE_INTERNAL,
410
411   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
412   .error_strings = esp_decrypt_error_strings,
413
414   .n_next_nodes = ESP_DECRYPT_N_NEXT,
415   .next_nodes = {
416 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
417     foreach_esp_decrypt_next
418 #undef _
419   },
420 };
421 /* *INDENT-ON* */
422
423 VLIB_NODE_FN (dpdk_esp6_decrypt_node) (vlib_main_t * vm,
424                                        vlib_node_runtime_t * node,
425                                        vlib_frame_t * from_frame)
426 {
427   return dpdk_esp_decrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ );
428 }
429
430 /* *INDENT-OFF* */
431 VLIB_REGISTER_NODE (dpdk_esp6_decrypt_node) = {
432   .name = "dpdk-esp6-decrypt",
433   .vector_size = sizeof (u32),
434   .format_trace = format_esp_decrypt_trace,
435   .type = VLIB_NODE_TYPE_INTERNAL,
436
437   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
438   .error_strings = esp_decrypt_error_strings,
439
440   .n_next_nodes = ESP_DECRYPT_N_NEXT,
441   .next_nodes = {
442 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
443     foreach_esp_decrypt_next
444 #undef _
445   },
446 };
447 /* *INDENT-ON* */
448
449 /*
450  * Decrypt Post Node
451  */
452
453 #define foreach_esp_decrypt_post_error        \
454  _(PKTS, "ESP post pkts")
455
456 typedef enum
457 {
458 #define _(sym,str) ESP_DECRYPT_POST_ERROR_##sym,
459   foreach_esp_decrypt_post_error
460 #undef _
461     ESP_DECRYPT_POST_N_ERROR,
462 } esp_decrypt_post_error_t;
463
464 static char *esp_decrypt_post_error_strings[] = {
465 #define _(sym,string) string,
466   foreach_esp_decrypt_post_error
467 #undef _
468 };
469
470 extern vlib_node_registration_t dpdk_esp4_decrypt_post_node;
471 extern vlib_node_registration_t dpdk_esp6_decrypt_post_node;
472
473 static u8 *
474 format_esp_decrypt_post_trace (u8 * s, va_list * args)
475 {
476   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
477   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
478   esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
479   u32 indent = format_get_indent (s);
480
481   s = format (s, "cipher %U auth %U\n",
482               format_ipsec_crypto_alg, t->crypto_alg,
483               format_ipsec_integ_alg, t->integ_alg);
484
485   ip4_header_t *ih4 = (ip4_header_t *) t->packet_data;
486   if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
487     s =
488       format (s, "%U%U", format_white_space, indent, format_ip6_header, ih4);
489   else
490     s =
491       format (s, "%U%U", format_white_space, indent, format_ip4_header, ih4);
492
493   return s;
494 }
495
496 always_inline uword
497 dpdk_esp_decrypt_post_inline (vlib_main_t * vm,
498                               vlib_node_runtime_t * node,
499                               vlib_frame_t * from_frame, int is_ip6)
500 {
501   u32 n_left_from, *from, *to_next = 0, next_index;
502   ipsec_sa_t *sa0;
503   u32 sa_index0 = ~0;
504   ipsec_main_t *im = &ipsec_main;
505   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
506
507   from = vlib_frame_vector_args (from_frame);
508   n_left_from = from_frame->n_vectors;
509
510   next_index = node->cached_next_index;
511
512   while (n_left_from > 0)
513     {
514       u32 n_left_to_next;
515
516       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
517
518       while (n_left_from > 0 && n_left_to_next > 0)
519         {
520           esp_footer_t *f0;
521           u32 bi0, iv_size, next0;
522           vlib_buffer_t *b0 = 0;
523           ip4_header_t *ih4 = 0, *oh4 = 0;
524           ip6_header_t *ih6 = 0, *oh6 = 0;
525           crypto_alg_t *cipher_alg, *auth_alg;
526           esp_header_t *esp0;
527           u8 trunc_size, is_aead;
528           u16 udp_encap_adv = 0;
529
530           next0 = ESP_DECRYPT_NEXT_DROP;
531
532           bi0 = from[0];
533           from += 1;
534           n_left_from -= 1;
535           n_left_to_next -= 1;
536
537           b0 = vlib_get_buffer (vm, bi0);
538           esp0 = vlib_buffer_get_current (b0);
539
540           sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
541           sa0 = pool_elt_at_index (im->sad, sa_index0);
542
543           to_next[0] = bi0;
544           to_next += 1;
545
546           cipher_alg = vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
547           auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
548           is_aead = cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD;
549           if (is_aead)
550             auth_alg = cipher_alg;
551
552           trunc_size = auth_alg->trunc_size;
553
554           iv_size = cipher_alg->iv_len;
555
556           ipsec_sa_anti_replay_advance (sa0,
557                                         clib_host_to_net_u32 (esp0->seq));
558
559           /* if UDP encapsulation is used adjust the address of the IP header */
560           if (ipsec_sa_is_set_UDP_ENCAP (sa0)
561               && (b0->flags & VNET_BUFFER_F_IS_IP4))
562             {
563               udp_encap_adv = sizeof (udp_header_t);
564             }
565
566           if (b0->flags & VNET_BUFFER_F_IS_IP4)
567             ih4 = (ip4_header_t *)
568               ((u8 *) esp0 - udp_encap_adv - sizeof (ip4_header_t));
569           else
570             ih4 = (ip4_header_t *) ((u8 *) esp0 - sizeof (ip6_header_t));
571
572           vlib_buffer_advance (b0, sizeof (esp_header_t) + iv_size);
573
574           b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
575           f0 = (esp_footer_t *) (vlib_buffer_get_tail (b0) - trunc_size - 2);
576           b0->current_length -= (f0->pad_length + trunc_size + 2);
577 #if 0
578           /* check padding */
579           const u8 *padding = vlib_buffer_get_tail (b0);
580           if (PREDICT_FALSE (memcmp (padding, pad_data, f0->pad_length)))
581             {
582               clib_warning ("bad padding");
583               vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
584                                            ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
585                                            1);
586               goto trace;
587             }
588 #endif
589           if (ipsec_sa_is_set_IS_TUNNEL (sa0))
590             {
591               if (f0->next_header == IP_PROTOCOL_IP_IN_IP)
592                 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
593               else if (f0->next_header == IP_PROTOCOL_IPV6)
594                 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
595               else
596                 {
597                   clib_warning ("next header: 0x%x", f0->next_header);
598                   if (is_ip6)
599                     vlib_node_increment_counter (vm,
600                                                  dpdk_esp6_decrypt_node.index,
601                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
602                                                  1);
603                   else
604                     vlib_node_increment_counter (vm,
605                                                  dpdk_esp4_decrypt_node.index,
606                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
607                                                  1);
608                   goto trace;
609                 }
610             }
611           else                  /* transport mode */
612             {
613               if ((ih4->ip_version_and_header_length & 0xF0) == 0x40)
614                 {
615                   u16 ih4_len = ip4_header_bytes (ih4);
616                   vlib_buffer_advance (b0, -ih4_len - udp_encap_adv);
617                   next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
618                   if (!ipsec_sa_is_set_UDP_ENCAP (sa0))
619                     {
620                       oh4 = vlib_buffer_get_current (b0);
621                       memmove (oh4, ih4, ih4_len);
622                       oh4->protocol = f0->next_header;
623                       oh4->length = clib_host_to_net_u16 (b0->current_length);
624                       oh4->checksum = ip4_header_checksum (oh4);
625                     }
626                 }
627               else if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
628                 {
629                   ih6 = (ip6_header_t *) ih4;
630                   vlib_buffer_advance (b0, -sizeof (ip6_header_t));
631                   oh6 = vlib_buffer_get_current (b0);
632                   memmove (oh6, ih6, sizeof (ip6_header_t));
633
634                   next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
635                   oh6->protocol = f0->next_header;
636                   u16 len = b0->current_length - sizeof (ip6_header_t);
637                   oh6->payload_length = clib_host_to_net_u16 (len);
638                 }
639               else
640                 {
641                   clib_warning ("next header: 0x%x", f0->next_header);
642                   if (is_ip6)
643                     vlib_node_increment_counter (vm,
644                                                  dpdk_esp6_decrypt_node.index,
645                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
646                                                  1);
647                   else
648                     vlib_node_increment_counter (vm,
649                                                  dpdk_esp4_decrypt_node.index,
650                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
651                                                  1);
652                   goto trace;
653                 }
654             }
655
656           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
657
658         trace:
659           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
660             {
661               esp_decrypt_trace_t *tr =
662                 vlib_add_trace (vm, node, b0, sizeof (*tr));
663               tr->crypto_alg = sa0->crypto_alg;
664               tr->integ_alg = sa0->integ_alg;
665               ih4 = vlib_buffer_get_current (b0);
666               clib_memcpy_fast (tr->packet_data, ih4, sizeof (ip6_header_t));
667             }
668
669           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
670                                            to_next, n_left_to_next, bi0,
671                                            next0);
672         }
673       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
674     }
675
676   if (is_ip6)
677     vlib_node_increment_counter (vm, dpdk_esp6_decrypt_post_node.index,
678                                  ESP_DECRYPT_POST_ERROR_PKTS,
679                                  from_frame->n_vectors);
680   else
681     vlib_node_increment_counter (vm, dpdk_esp4_decrypt_post_node.index,
682                                  ESP_DECRYPT_POST_ERROR_PKTS,
683                                  from_frame->n_vectors);
684
685   return from_frame->n_vectors;
686 }
687
688 VLIB_NODE_FN (dpdk_esp4_decrypt_post_node) (vlib_main_t * vm,
689                                             vlib_node_runtime_t * node,
690                                             vlib_frame_t * from_frame)
691 {
692   return dpdk_esp_decrypt_post_inline (vm, node, from_frame, 0 /*is_ip6 */ );
693 }
694
695 /* *INDENT-OFF* */
696 VLIB_REGISTER_NODE (dpdk_esp4_decrypt_post_node) = {
697   .name = "dpdk-esp4-decrypt-post",
698   .vector_size = sizeof (u32),
699   .format_trace = format_esp_decrypt_post_trace,
700   .type = VLIB_NODE_TYPE_INTERNAL,
701
702   .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings),
703   .error_strings = esp_decrypt_post_error_strings,
704
705   .n_next_nodes = ESP_DECRYPT_N_NEXT,
706   .next_nodes = {
707 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
708     foreach_esp_decrypt_next
709 #undef _
710   },
711 };
712 /* *INDENT-ON* */
713
714 VLIB_NODE_FN (dpdk_esp6_decrypt_post_node) (vlib_main_t * vm,
715                                             vlib_node_runtime_t * node,
716                                             vlib_frame_t * from_frame)
717 {
718   return dpdk_esp_decrypt_post_inline (vm, node, from_frame, 0 /*is_ip6 */ );
719 }
720
721 /* *INDENT-OFF* */
722 VLIB_REGISTER_NODE (dpdk_esp6_decrypt_post_node) = {
723   .name = "dpdk-esp6-decrypt-post",
724   .vector_size = sizeof (u32),
725   .format_trace = format_esp_decrypt_post_trace,
726   .type = VLIB_NODE_TYPE_INTERNAL,
727
728   .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings),
729   .error_strings = esp_decrypt_post_error_strings,
730
731   .n_next_nodes = ESP_DECRYPT_N_NEXT,
732   .next_nodes = {
733 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
734     foreach_esp_decrypt_next
735 #undef _
736   },
737 };
738 /* *INDENT-ON* */
739
740 /*
741  * fd.io coding-style-patch-verification: ON
742  *
743  * Local Variables:
744  * eval: (c-set-style "gnu")
745  * End:
746  */