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