ipsec: compress ipsec_sa_t so data used by dataplane code fits in cacheline
[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, seq, 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
172           u16 op_len =
173             sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]);
174           CLIB_PREFETCH (op, op_len, STORE);
175
176           sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
177           vlib_prefetch_combined_counter (&ipsec_sa_counters,
178                                           thread_index, sa_index0);
179
180           if (sa_index0 != last_sa_index)
181             {
182               sa0 = pool_elt_at_index (im->sad, sa_index0);
183
184               cipher_alg =
185                 vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
186               auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
187
188               is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD);
189               if (is_aead)
190                 auth_alg = cipher_alg;
191
192               res_idx = get_resource (cwm, sa0);
193
194               if (PREDICT_FALSE (res_idx == (u16) ~ 0))
195                 {
196                   clib_warning ("unsupported SA by thread index %u",
197                                 thread_idx);
198                   if (is_ip6)
199                     vlib_node_increment_counter (vm,
200                                                  dpdk_esp6_decrypt_node.index,
201                                                  ESP_DECRYPT_ERROR_NOSUP, 1);
202                   else
203                     vlib_node_increment_counter (vm,
204                                                  dpdk_esp4_decrypt_node.index,
205                                                  ESP_DECRYPT_ERROR_NOSUP, 1);
206                   to_next[0] = bi0;
207                   to_next += 1;
208                   n_left_to_next -= 1;
209                   goto trace;
210                 }
211               res = vec_elt_at_index (dcm->resource, res_idx);
212
213               error = crypto_get_session (&session, sa_index0, res, cwm, 0);
214               if (PREDICT_FALSE (error || !session))
215                 {
216                   clib_warning ("failed to get crypto session");
217                   if (is_ip6)
218                     vlib_node_increment_counter (vm,
219                                                  dpdk_esp6_decrypt_node.index,
220                                                  ESP_DECRYPT_ERROR_SESSION,
221                                                  1);
222                   else
223                     vlib_node_increment_counter (vm,
224                                                  dpdk_esp4_decrypt_node.index,
225                                                  ESP_DECRYPT_ERROR_SESSION,
226                                                  1);
227                   to_next[0] = bi0;
228                   to_next += 1;
229                   n_left_to_next -= 1;
230                   goto trace;
231                 }
232
233               last_sa_index = sa_index0;
234             }
235
236           /* anti-replay check */
237           if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa0))
238             {
239               int rv = 0;
240
241               seq = clib_net_to_host_u32 (esp0->seq);
242
243               if (PREDICT_TRUE (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0)))
244                 rv = esp_replay_check_esn (sa0, seq);
245               else
246                 rv = esp_replay_check (sa0, seq);
247
248               if (PREDICT_FALSE (rv))
249                 {
250                   clib_warning ("failed anti-replay check");
251                   if (is_ip6)
252                     vlib_node_increment_counter (vm,
253                                                  dpdk_esp6_decrypt_node.index,
254                                                  ESP_DECRYPT_ERROR_REPLAY, 1);
255                   else
256                     vlib_node_increment_counter (vm,
257                                                  dpdk_esp4_decrypt_node.index,
258                                                  ESP_DECRYPT_ERROR_REPLAY, 1);
259                   to_next[0] = bi0;
260                   to_next += 1;
261                   n_left_to_next -= 1;
262                   goto trace;
263                 }
264             }
265
266           if (is_ip6)
267             priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT6_POST;
268           else
269             priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT4_POST;
270
271           /* FIXME multi-seg */
272           vlib_increment_combined_counter
273             (&ipsec_sa_counters, thread_index, sa_index0,
274              1, b0->current_length);
275
276           res->ops[res->n_ops] = op;
277           res->bi[res->n_ops] = bi0;
278           res->n_ops += 1;
279
280           /* Convert vlib buffer to mbuf */
281           mb0->data_len = b0->current_length;
282           mb0->pkt_len = b0->current_length;
283           mb0->data_off = RTE_PKTMBUF_HEADROOM + b0->current_data;
284
285           trunc_size = auth_alg->trunc_size;
286           iv_size = cipher_alg->iv_len;
287
288           /* Outer IP header has already been stripped */
289           u16 payload_len =
290             b0->current_length - sizeof (esp_header_t) - iv_size - trunc_size;
291
292           ASSERT (payload_len >= 4);
293
294           if (payload_len & (cipher_alg->boundary - 1))
295             {
296               clib_warning ("payload %u not multiple of %d\n",
297                             payload_len, cipher_alg->boundary);
298               if (is_ip6)
299                 vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index,
300                                              ESP_DECRYPT_ERROR_BAD_LEN, 1);
301               else
302                 vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index,
303                                              ESP_DECRYPT_ERROR_BAD_LEN, 1);
304               res->n_ops -= 1;
305               to_next[0] = bi0;
306               to_next += 1;
307               n_left_to_next -= 1;
308               goto trace;
309             }
310
311           u32 cipher_off, cipher_len;
312           u32 auth_len = 0;
313           u8 *aad = NULL;
314
315           u8 *iv = (u8 *) (esp0 + 1);
316
317           dpdk_gcm_cnt_blk *icb = &priv->cb;
318
319           cipher_off = sizeof (esp_header_t) + iv_size;
320           cipher_len = payload_len;
321
322           u8 *digest = vlib_buffer_get_tail (b0) - trunc_size;
323           u64 digest_paddr =
324             mb0->buf_physaddr + digest - ((u8 *) mb0->buf_addr);
325
326           if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC)
327             clib_memcpy_fast (icb, iv, 16);
328           else                  /* CTR/GCM */
329             {
330               u32 *_iv = (u32 *) iv;
331
332               crypto_set_icb (icb, sa0->salt, _iv[0], _iv[1]);
333             }
334
335           if (is_aead)
336             {
337               aad = priv->aad;
338               u32 *_aad = (u32 *) aad;
339               clib_memcpy_fast (aad, esp0, 8);
340
341               /* _aad[3] should always be 0 */
342               if (PREDICT_FALSE (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0)))
343                 _aad[2] = clib_host_to_net_u32 (sa0->seq_hi);
344               else
345                 _aad[2] = 0;
346             }
347           else
348             {
349               auth_len = sizeof (esp_header_t) + iv_size + payload_len;
350
351               if (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0))
352                 {
353                   clib_memcpy_fast (priv->icv, digest, trunc_size);
354                   u32 *_digest = (u32 *) digest;
355                   _digest[0] = clib_host_to_net_u32 (sa0->seq_hi);
356                   auth_len += sizeof (sa0->seq_hi);
357
358                   digest = priv->icv;
359                   digest_paddr =
360                     op->phys_addr + (uintptr_t) priv->icv - (uintptr_t) op;
361                 }
362             }
363
364           crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len,
365                            0, auth_len, aad, digest, digest_paddr);
366         trace:
367           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
368             {
369               esp_decrypt_trace_t *tr =
370                 vlib_add_trace (vm, node, b0, sizeof (*tr));
371               tr->crypto_alg = sa0->crypto_alg;
372               tr->integ_alg = sa0->integ_alg;
373               clib_memcpy_fast (tr->packet_data, vlib_buffer_get_current (b0),
374                                 sizeof (esp_header_t));
375             }
376         }
377       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
378     }
379
380   if (is_ip6)
381     {
382       vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index,
383                                    ESP_DECRYPT_ERROR_RX_PKTS,
384                                    from_frame->n_vectors);
385
386       crypto_enqueue_ops (vm, cwm, dpdk_esp6_decrypt_node.index,
387                           ESP_DECRYPT_ERROR_ENQ_FAIL, numa);
388     }
389   else
390     {
391       vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index,
392                                    ESP_DECRYPT_ERROR_RX_PKTS,
393                                    from_frame->n_vectors);
394
395       crypto_enqueue_ops (vm, cwm, dpdk_esp4_decrypt_node.index,
396                           ESP_DECRYPT_ERROR_ENQ_FAIL, numa);
397     }
398
399   crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops);
400
401   return from_frame->n_vectors;
402 }
403
404 VLIB_NODE_FN (dpdk_esp4_decrypt_node) (vlib_main_t * vm,
405                                        vlib_node_runtime_t * node,
406                                        vlib_frame_t * from_frame)
407 {
408   return dpdk_esp_decrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ );
409 }
410
411 /* *INDENT-OFF* */
412 VLIB_REGISTER_NODE (dpdk_esp4_decrypt_node) = {
413   .name = "dpdk-esp4-decrypt",
414   .vector_size = sizeof (u32),
415   .format_trace = format_esp_decrypt_trace,
416   .type = VLIB_NODE_TYPE_INTERNAL,
417
418   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
419   .error_strings = esp_decrypt_error_strings,
420
421   .n_next_nodes = ESP_DECRYPT_N_NEXT,
422   .next_nodes = {
423 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
424     foreach_esp_decrypt_next
425 #undef _
426   },
427 };
428 /* *INDENT-ON* */
429
430 VLIB_NODE_FN (dpdk_esp6_decrypt_node) (vlib_main_t * vm,
431                                        vlib_node_runtime_t * node,
432                                        vlib_frame_t * from_frame)
433 {
434   return dpdk_esp_decrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ );
435 }
436
437 /* *INDENT-OFF* */
438 VLIB_REGISTER_NODE (dpdk_esp6_decrypt_node) = {
439   .name = "dpdk-esp6-decrypt",
440   .vector_size = sizeof (u32),
441   .format_trace = format_esp_decrypt_trace,
442   .type = VLIB_NODE_TYPE_INTERNAL,
443
444   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
445   .error_strings = esp_decrypt_error_strings,
446
447   .n_next_nodes = ESP_DECRYPT_N_NEXT,
448   .next_nodes = {
449 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
450     foreach_esp_decrypt_next
451 #undef _
452   },
453 };
454 /* *INDENT-ON* */
455
456 /*
457  * Decrypt Post Node
458  */
459
460 #define foreach_esp_decrypt_post_error        \
461  _(PKTS, "ESP post pkts")
462
463 typedef enum
464 {
465 #define _(sym,str) ESP_DECRYPT_POST_ERROR_##sym,
466   foreach_esp_decrypt_post_error
467 #undef _
468     ESP_DECRYPT_POST_N_ERROR,
469 } esp_decrypt_post_error_t;
470
471 static char *esp_decrypt_post_error_strings[] = {
472 #define _(sym,string) string,
473   foreach_esp_decrypt_post_error
474 #undef _
475 };
476
477 extern vlib_node_registration_t dpdk_esp4_decrypt_post_node;
478 extern vlib_node_registration_t dpdk_esp6_decrypt_post_node;
479
480 static u8 *
481 format_esp_decrypt_post_trace (u8 * s, va_list * args)
482 {
483   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
484   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
485   esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
486   u32 indent = format_get_indent (s);
487
488   s = format (s, "cipher %U auth %U\n",
489               format_ipsec_crypto_alg, t->crypto_alg,
490               format_ipsec_integ_alg, t->integ_alg);
491
492   ip4_header_t *ih4 = (ip4_header_t *) t->packet_data;
493   if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
494     s =
495       format (s, "%U%U", format_white_space, indent, format_ip6_header, ih4);
496   else
497     s =
498       format (s, "%U%U", format_white_space, indent, format_ip4_header, ih4);
499
500   return s;
501 }
502
503 always_inline uword
504 dpdk_esp_decrypt_post_inline (vlib_main_t * vm,
505                               vlib_node_runtime_t * node,
506                               vlib_frame_t * from_frame, int is_ip6)
507 {
508   u32 n_left_from, *from, *to_next = 0, next_index;
509   ipsec_sa_t *sa0;
510   u32 sa_index0 = ~0;
511   ipsec_main_t *im = &ipsec_main;
512   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
513
514   from = vlib_frame_vector_args (from_frame);
515   n_left_from = from_frame->n_vectors;
516
517   next_index = node->cached_next_index;
518
519   while (n_left_from > 0)
520     {
521       u32 n_left_to_next;
522
523       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
524
525       while (n_left_from > 0 && n_left_to_next > 0)
526         {
527           esp_footer_t *f0;
528           u32 bi0, iv_size, next0;
529           vlib_buffer_t *b0 = 0;
530           ip4_header_t *ih4 = 0, *oh4 = 0;
531           ip6_header_t *ih6 = 0, *oh6 = 0;
532           crypto_alg_t *cipher_alg, *auth_alg;
533           esp_header_t *esp0;
534           u8 trunc_size, is_aead;
535           u16 udp_encap_adv = 0;
536
537           next0 = ESP_DECRYPT_NEXT_DROP;
538
539           bi0 = from[0];
540           from += 1;
541           n_left_from -= 1;
542           n_left_to_next -= 1;
543
544           b0 = vlib_get_buffer (vm, bi0);
545           esp0 = vlib_buffer_get_current (b0);
546
547           sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
548           sa0 = pool_elt_at_index (im->sad, sa_index0);
549
550           to_next[0] = bi0;
551           to_next += 1;
552
553           cipher_alg = vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
554           auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
555           is_aead = cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD;
556           if (is_aead)
557             auth_alg = cipher_alg;
558
559           trunc_size = auth_alg->trunc_size;
560
561           iv_size = cipher_alg->iv_len;
562
563           if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa0))
564             {
565               u32 seq;
566               seq = clib_host_to_net_u32 (esp0->seq);
567               if (PREDICT_TRUE (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0)))
568                 esp_replay_advance_esn (sa0, seq);
569               else
570                 esp_replay_advance (sa0, seq);
571             }
572
573           /* if UDP encapsulation is used adjust the address of the IP header */
574           if (ipsec_sa_is_set_UDP_ENCAP (sa0)
575               && (b0->flags & VNET_BUFFER_F_IS_IP4))
576             {
577               udp_encap_adv = sizeof (udp_header_t);
578             }
579
580           if (b0->flags & VNET_BUFFER_F_IS_IP4)
581             ih4 = (ip4_header_t *)
582               ((u8 *) esp0 - udp_encap_adv - sizeof (ip4_header_t));
583           else
584             ih4 = (ip4_header_t *) ((u8 *) esp0 - sizeof (ip6_header_t));
585
586           vlib_buffer_advance (b0, sizeof (esp_header_t) + iv_size);
587
588           b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
589           f0 = (esp_footer_t *) (vlib_buffer_get_tail (b0) - trunc_size - 2);
590           b0->current_length -= (f0->pad_length + trunc_size + 2);
591 #if 0
592           /* check padding */
593           const u8 *padding = vlib_buffer_get_tail (b0);
594           if (PREDICT_FALSE (memcmp (padding, pad_data, f0->pad_length)))
595             {
596               clib_warning ("bad padding");
597               vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
598                                            ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
599                                            1);
600               goto trace;
601             }
602 #endif
603           if (ipsec_sa_is_set_IS_TUNNEL (sa0))
604             {
605               if (f0->next_header == IP_PROTOCOL_IP_IN_IP)
606                 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
607               else if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)
608                        && f0->next_header == IP_PROTOCOL_IPV6)
609                 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
610               else
611                 {
612                   clib_warning ("next header: 0x%x", f0->next_header);
613                   if (is_ip6)
614                     vlib_node_increment_counter (vm,
615                                                  dpdk_esp6_decrypt_node.index,
616                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
617                                                  1);
618                   else
619                     vlib_node_increment_counter (vm,
620                                                  dpdk_esp4_decrypt_node.index,
621                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
622                                                  1);
623                   goto trace;
624                 }
625             }
626           else                  /* transport mode */
627             {
628               if ((ih4->ip_version_and_header_length & 0xF0) == 0x40)
629                 {
630                   u16 ih4_len = ip4_header_bytes (ih4);
631                   vlib_buffer_advance (b0, -ih4_len - udp_encap_adv);
632                   next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
633                   if (!ipsec_sa_is_set_UDP_ENCAP (sa0))
634                     {
635                       oh4 = vlib_buffer_get_current (b0);
636                       memmove (oh4, ih4, ih4_len);
637                       oh4->protocol = f0->next_header;
638                       oh4->length = clib_host_to_net_u16 (b0->current_length);
639                       oh4->checksum = ip4_header_checksum (oh4);
640                     }
641                 }
642               else if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
643                 {
644                   ih6 = (ip6_header_t *) ih4;
645                   vlib_buffer_advance (b0, -sizeof (ip6_header_t));
646                   oh6 = vlib_buffer_get_current (b0);
647                   memmove (oh6, ih6, sizeof (ip6_header_t));
648
649                   next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
650                   oh6->protocol = f0->next_header;
651                   u16 len = b0->current_length - sizeof (ip6_header_t);
652                   oh6->payload_length = clib_host_to_net_u16 (len);
653                 }
654               else
655                 {
656                   clib_warning ("next header: 0x%x", f0->next_header);
657                   if (is_ip6)
658                     vlib_node_increment_counter (vm,
659                                                  dpdk_esp6_decrypt_node.index,
660                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
661                                                  1);
662                   else
663                     vlib_node_increment_counter (vm,
664                                                  dpdk_esp4_decrypt_node.index,
665                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
666                                                  1);
667                   goto trace;
668                 }
669             }
670
671           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
672
673         trace:
674           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
675             {
676               esp_decrypt_trace_t *tr =
677                 vlib_add_trace (vm, node, b0, sizeof (*tr));
678               tr->crypto_alg = sa0->crypto_alg;
679               tr->integ_alg = sa0->integ_alg;
680               ih4 = vlib_buffer_get_current (b0);
681               clib_memcpy_fast (tr->packet_data, ih4, sizeof (ip6_header_t));
682             }
683
684           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
685                                            to_next, n_left_to_next, bi0,
686                                            next0);
687         }
688       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
689     }
690
691   if (is_ip6)
692     vlib_node_increment_counter (vm, dpdk_esp6_decrypt_post_node.index,
693                                  ESP_DECRYPT_POST_ERROR_PKTS,
694                                  from_frame->n_vectors);
695   else
696     vlib_node_increment_counter (vm, dpdk_esp4_decrypt_post_node.index,
697                                  ESP_DECRYPT_POST_ERROR_PKTS,
698                                  from_frame->n_vectors);
699
700   return from_frame->n_vectors;
701 }
702
703 VLIB_NODE_FN (dpdk_esp4_decrypt_post_node) (vlib_main_t * vm,
704                                             vlib_node_runtime_t * node,
705                                             vlib_frame_t * from_frame)
706 {
707   return dpdk_esp_decrypt_post_inline (vm, node, from_frame, 0 /*is_ip6 */ );
708 }
709
710 /* *INDENT-OFF* */
711 VLIB_REGISTER_NODE (dpdk_esp4_decrypt_post_node) = {
712   .name = "dpdk-esp4-decrypt-post",
713   .vector_size = sizeof (u32),
714   .format_trace = format_esp_decrypt_post_trace,
715   .type = VLIB_NODE_TYPE_INTERNAL,
716
717   .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings),
718   .error_strings = esp_decrypt_post_error_strings,
719
720   .n_next_nodes = ESP_DECRYPT_N_NEXT,
721   .next_nodes = {
722 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
723     foreach_esp_decrypt_next
724 #undef _
725   },
726 };
727 /* *INDENT-ON* */
728
729 VLIB_NODE_FN (dpdk_esp6_decrypt_post_node) (vlib_main_t * vm,
730                                             vlib_node_runtime_t * node,
731                                             vlib_frame_t * from_frame)
732 {
733   return dpdk_esp_decrypt_post_inline (vm, node, from_frame, 0 /*is_ip6 */ );
734 }
735
736 /* *INDENT-OFF* */
737 VLIB_REGISTER_NODE (dpdk_esp6_decrypt_post_node) = {
738   .name = "dpdk-esp6-decrypt-post",
739   .vector_size = sizeof (u32),
740   .format_trace = format_esp_decrypt_post_trace,
741   .type = VLIB_NODE_TYPE_INTERNAL,
742
743   .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings),
744   .error_strings = esp_decrypt_post_error_strings,
745
746   .n_next_nodes = ESP_DECRYPT_N_NEXT,
747   .next_nodes = {
748 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
749     foreach_esp_decrypt_next
750 #undef _
751   },
752 };
753 /* *INDENT-ON* */
754
755 /*
756  * fd.io coding-style-patch-verification: ON
757  *
758  * Local Variables:
759  * eval: (c-set-style "gnu")
760  * End:
761  */