fib: Don't use [midchain] adjacencies to change an interface's feature arc
[vpp.git] / src / vnet / ipsec / esp_encrypt.c
1 /*
2  * esp_encrypt.c : IPSec ESP encrypt 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
22 #include <vnet/crypto/crypto.h>
23
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/ipsec_tun.h>
26 #include <vnet/ipsec/esp.h>
27 #include <vnet/tunnel/tunnel_dp.h>
28
29 #define foreach_esp_encrypt_next                                              \
30   _ (DROP4, "ip4-drop")                                                       \
31   _ (DROP6, "ip6-drop")                                                       \
32   _ (DROP_MPLS, "mpls-drop")                                                  \
33   _ (HANDOFF4, "handoff4")                                                    \
34   _ (HANDOFF6, "handoff6")                                                    \
35   _ (HANDOFF_MPLS, "handoff-mpls")                                            \
36   _ (INTERFACE_OUTPUT, "interface-output")
37
38 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
39 typedef enum
40 {
41   foreach_esp_encrypt_next
42 #undef _
43     ESP_ENCRYPT_N_NEXT,
44 } esp_encrypt_next_t;
45
46 #define foreach_esp_encrypt_error                                             \
47   _ (RX_PKTS, "ESP pkts received")                                            \
48   _ (POST_RX_PKTS, "ESP-post pkts received")                                  \
49   _ (HANDOFF, "Hand-off")                                                     \
50   _ (SEQ_CYCLED, "sequence number cycled (packet dropped)")                   \
51   _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)")             \
52   _ (CRYPTO_QUEUE_FULL, "crypto queue full (packet dropped)")                 \
53   _ (NO_BUFFERS, "no buffers (packet dropped)")                               \
54   _ (NO_PROTECTION, "no protecting SA (packet dropped)")                      \
55   _ (NO_ENCRYPTION, "no Encrypting SA (packet dropped)")
56
57 typedef enum
58 {
59 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
60   foreach_esp_encrypt_error
61 #undef _
62     ESP_ENCRYPT_N_ERROR,
63 } esp_encrypt_error_t;
64
65 static char *esp_encrypt_error_strings[] = {
66 #define _(sym,string) string,
67   foreach_esp_encrypt_error
68 #undef _
69 };
70
71 typedef struct
72 {
73   u32 sa_index;
74   u32 spi;
75   u32 seq;
76   u32 sa_seq_hi;
77   u8 udp_encap;
78   ipsec_crypto_alg_t crypto_alg;
79   ipsec_integ_alg_t integ_alg;
80 } esp_encrypt_trace_t;
81
82 typedef struct
83 {
84   u32 next_index;
85 } esp_encrypt_post_trace_t;
86
87 /* packet trace format function */
88 static u8 *
89 format_esp_encrypt_trace (u8 * s, va_list * args)
90 {
91   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
92   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
93   esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
94
95   s =
96     format (s,
97             "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
98             t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
99             format_ipsec_crypto_alg,
100             t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
101             t->udp_encap ? " udp-encap-enabled" : "");
102   return s;
103 }
104
105 static u8 *
106 format_esp_post_encrypt_trace (u8 * s, va_list * args)
107 {
108   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
109   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
110   esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *);
111
112   s = format (s, "esp-post: next node index %u", t->next_index);
113   return s;
114 }
115
116 /* pad packet in input buffer */
117 static_always_inline u8 *
118 esp_add_footer_and_icv (vlib_main_t *vm, vlib_buffer_t **last, u8 esp_align,
119                         u8 icv_sz, vlib_node_runtime_t *node,
120                         u16 buffer_data_size, uword total_len)
121 {
122   static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
123     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
124     0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
125   };
126
127   u16 min_length = total_len + sizeof (esp_footer_t);
128   u16 new_length = round_pow2 (min_length, esp_align);
129   u8 pad_bytes = new_length - min_length;
130   esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (last[0]) +
131                                       last[0]->current_length + pad_bytes);
132   u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz;
133
134   if (last[0]->current_data + last[0]->current_length + tail_sz >
135       buffer_data_size)
136     {
137       u32 tmp_bi = 0;
138       if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
139         return 0;
140
141       vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
142       last[0]->next_buffer = tmp_bi;
143       last[0]->flags |= VLIB_BUFFER_NEXT_PRESENT;
144       f = (esp_footer_t *) (vlib_buffer_get_current (tmp) + pad_bytes);
145       tmp->current_length += tail_sz;
146       last[0] = tmp;
147     }
148   else
149     last[0]->current_length += tail_sz;
150
151   f->pad_length = pad_bytes;
152   if (pad_bytes)
153     {
154       ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
155       pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
156       clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
157     }
158
159   return &f->next_header;
160 }
161
162 static_always_inline void
163 esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
164 {
165   ip_csum_t sum;
166   u16 old_len;
167
168   len = clib_net_to_host_u16 (len);
169   old_len = ip4->length;
170
171   if (is_transport)
172     {
173       u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
174
175       sum = ip_csum_update (ip4->checksum, ip4->protocol,
176                             prot, ip4_header_t, protocol);
177       ip4->protocol = prot;
178
179       sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
180     }
181   else
182     sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
183
184   ip4->length = len;
185   ip4->checksum = ip_csum_fold (sum);
186 }
187
188 static_always_inline void
189 esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
190 {
191   clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
192   udp->length = clib_net_to_host_u16 (len);
193 }
194
195 static_always_inline u8
196 ext_hdr_is_pre_esp (u8 nexthdr)
197 {
198 #ifdef CLIB_HAVE_VEC128
199   static const u8x16 ext_hdr_types = {
200     IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
201     IP_PROTOCOL_IPV6_ROUTE,
202     IP_PROTOCOL_IPV6_FRAGMENTATION,
203   };
204
205   return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
206 #else
207   return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
208           (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
209           ((nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0));
210 #endif
211 }
212
213 static_always_inline u8
214 esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
215 {
216   /* this code assumes that HbH, route and frag headers will be before
217      others, if that is not the case, they will end up encrypted */
218   u8 len = sizeof (ip6_header_t);
219   ip6_ext_header_t *p;
220
221   /* if next packet doesn't have ext header */
222   if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
223     {
224       *ext_hdr = NULL;
225       return len;
226     }
227
228   p = (void *) (ip6 + 1);
229   len += ip6_ext_header_len (p);
230
231   while (ext_hdr_is_pre_esp (p->next_hdr))
232     {
233       len += ip6_ext_header_len (p);
234       p = ip6_ext_next_header (p);
235     }
236
237   *ext_hdr = p;
238   return len;
239 }
240
241 static_always_inline void
242 esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
243                          vnet_crypto_op_t * ops, vlib_buffer_t * b[],
244                          u16 * nexts, vnet_crypto_op_chunk_t * chunks,
245                          u16 drop_next)
246 {
247   u32 n_fail, n_ops = vec_len (ops);
248   vnet_crypto_op_t *op = ops;
249
250   if (n_ops == 0)
251     return;
252
253   n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
254
255   while (n_fail)
256     {
257       ASSERT (op - ops < n_ops);
258
259       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
260         {
261           u32 bi = op->user_data;
262           b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
263           nexts[bi] = drop_next;
264           n_fail--;
265         }
266       op++;
267     }
268 }
269
270 static_always_inline void
271 esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
272                  vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
273                  u16 drop_next)
274 {
275   u32 n_fail, n_ops = vec_len (ops);
276   vnet_crypto_op_t *op = ops;
277
278   if (n_ops == 0)
279     return;
280
281   n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
282
283   while (n_fail)
284     {
285       ASSERT (op - ops < n_ops);
286
287       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
288         {
289           u32 bi = op->user_data;
290           b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
291           nexts[bi] = drop_next;
292           n_fail--;
293         }
294       op++;
295     }
296 }
297
298 static_always_inline u32
299 esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
300                           ipsec_sa_t * sa0, vlib_buffer_t * b,
301                           vlib_buffer_t * lb, u8 icv_sz, u8 * start,
302                           u32 start_len, u16 * n_ch)
303 {
304   vnet_crypto_op_chunk_t *ch;
305   vlib_buffer_t *cb = b;
306   u32 n_chunks = 1;
307   u32 total_len;
308   vec_add2 (ptd->chunks, ch, 1);
309   total_len = ch->len = start_len;
310   ch->src = ch->dst = start;
311   cb = vlib_get_buffer (vm, cb->next_buffer);
312
313   while (1)
314     {
315       vec_add2 (ptd->chunks, ch, 1);
316       n_chunks += 1;
317       if (lb == cb)
318         total_len += ch->len = cb->current_length - icv_sz;
319       else
320         total_len += ch->len = cb->current_length;
321       ch->src = ch->dst = vlib_buffer_get_current (cb);
322
323       if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
324         break;
325
326       cb = vlib_get_buffer (vm, cb->next_buffer);
327     }
328
329   if (n_ch)
330     *n_ch = n_chunks;
331
332   return total_len;
333 }
334
335 static_always_inline u32
336 esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
337                          ipsec_sa_t * sa0, vlib_buffer_t * b,
338                          vlib_buffer_t * lb, u8 icv_sz, u8 * start,
339                          u32 start_len, u8 * digest, u16 * n_ch)
340 {
341   vnet_crypto_op_chunk_t *ch;
342   vlib_buffer_t *cb = b;
343   u32 n_chunks = 1;
344   u32 total_len;
345   vec_add2 (ptd->chunks, ch, 1);
346   total_len = ch->len = start_len;
347   ch->src = start;
348   cb = vlib_get_buffer (vm, cb->next_buffer);
349
350   while (1)
351     {
352       vec_add2 (ptd->chunks, ch, 1);
353       n_chunks += 1;
354       if (lb == cb)
355         {
356           total_len += ch->len = cb->current_length - icv_sz;
357           if (ipsec_sa_is_set_USE_ESN (sa0))
358             {
359               u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
360               clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
361               ch->len += sizeof (seq_hi);
362               total_len += sizeof (seq_hi);
363             }
364         }
365       else
366         total_len += ch->len = cb->current_length;
367       ch->src = vlib_buffer_get_current (cb);
368
369       if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
370         break;
371
372       cb = vlib_get_buffer (vm, cb->next_buffer);
373     }
374
375   if (n_ch)
376     *n_ch = n_chunks;
377
378   return total_len;
379 }
380
381 always_inline void
382 esp_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
383                      vnet_crypto_op_t **crypto_ops,
384                      vnet_crypto_op_t **integ_ops, ipsec_sa_t *sa0, u32 seq_hi,
385                      u8 *payload, u16 payload_len, u8 iv_sz, u8 icv_sz, u32 bi,
386                      vlib_buffer_t **b, vlib_buffer_t *lb, u32 hdr_len,
387                      esp_header_t *esp)
388 {
389   if (sa0->crypto_enc_op_id)
390     {
391       vnet_crypto_op_t *op;
392       vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
393       vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
394
395       op->src = op->dst = payload;
396       op->key_index = sa0->crypto_key_index;
397       op->len = payload_len - icv_sz;
398       op->user_data = bi;
399
400       if (ipsec_sa_is_set_IS_CTR (sa0))
401         {
402           ASSERT (sizeof (u64) == iv_sz);
403           /* construct nonce in a scratch space in front of the IP header */
404           esp_ctr_nonce_t *nonce =
405             (esp_ctr_nonce_t *) (payload - sizeof (u64) - hdr_len -
406                                  sizeof (*nonce));
407           u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
408
409           if (ipsec_sa_is_set_IS_AEAD (sa0))
410             {
411               /* constuct aad in a scratch space in front of the nonce */
412               op->aad = (u8 *) nonce - sizeof (esp_aead_t);
413               op->aad_len = esp_aad_fill (op->aad, esp, sa0, seq_hi);
414               op->tag = payload + op->len;
415               op->tag_len = 16;
416             }
417           else
418             {
419               nonce->ctr = clib_host_to_net_u32 (1);
420             }
421
422           nonce->salt = sa0->salt;
423           nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa0->ctr_iv_counter++);
424           op->iv = (u8 *) nonce;
425         }
426       else
427         {
428           op->iv = payload - iv_sz;
429           op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
430         }
431
432       if (lb != b[0])
433         {
434           /* is chained */
435           op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
436           op->chunk_index = vec_len (ptd->chunks);
437           op->tag = vlib_buffer_get_tail (lb) - icv_sz;
438           esp_encrypt_chain_crypto (vm, ptd, sa0, b[0], lb, icv_sz, payload,
439                                     payload_len, &op->n_chunks);
440         }
441     }
442
443   if (sa0->integ_op_id)
444     {
445       vnet_crypto_op_t *op;
446       vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
447       vnet_crypto_op_init (op, sa0->integ_op_id);
448       op->src = payload - iv_sz - sizeof (esp_header_t);
449       op->digest = payload + payload_len - icv_sz;
450       op->key_index = sa0->integ_key_index;
451       op->digest_len = icv_sz;
452       op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
453       op->user_data = bi;
454
455       if (lb != b[0])
456         {
457           /* is chained */
458           op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
459           op->chunk_index = vec_len (ptd->chunks);
460           op->digest = vlib_buffer_get_tail (lb) - icv_sz;
461
462           esp_encrypt_chain_integ (vm, ptd, sa0, b[0], lb, icv_sz,
463                                    payload - iv_sz - sizeof (esp_header_t),
464                                    payload_len + iv_sz +
465                                    sizeof (esp_header_t), op->digest,
466                                    &op->n_chunks);
467         }
468       else if (ipsec_sa_is_set_USE_ESN (sa0))
469         {
470           u32 tmp = clib_net_to_host_u32 (seq_hi);
471           clib_memcpy_fast (op->digest, &tmp, sizeof (seq_hi));
472           op->len += sizeof (seq_hi);
473         }
474     }
475 }
476
477 static_always_inline void
478 esp_prepare_async_frame (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
479                          vnet_crypto_async_frame_t *async_frame,
480                          ipsec_sa_t *sa, vlib_buffer_t *b, esp_header_t *esp,
481                          u8 *payload, u32 payload_len, u8 iv_sz, u8 icv_sz,
482                          u32 bi, u16 next, u32 hdr_len, u16 async_next,
483                          vlib_buffer_t *lb)
484 {
485   esp_post_data_t *post = esp_post_data (b);
486   u8 *tag, *iv, *aad = 0;
487   u8 flag = 0;
488   u32 key_index;
489   i16 crypto_start_offset, integ_start_offset = 0;
490   u16 crypto_total_len, integ_total_len;
491
492   post->next_index = next;
493
494   /* crypto */
495   crypto_start_offset = payload - b->data;
496   crypto_total_len = integ_total_len = payload_len - icv_sz;
497   tag = payload + crypto_total_len;
498
499   key_index = sa->linked_key_index;
500
501   if (ipsec_sa_is_set_IS_CTR (sa))
502     {
503       ASSERT (sizeof (u64) == iv_sz);
504       /* construct nonce in a scratch space in front of the IP header */
505       esp_ctr_nonce_t *nonce = (esp_ctr_nonce_t *) (payload - sizeof (u64) -
506                                                     hdr_len - sizeof (*nonce));
507       u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
508
509       if (ipsec_sa_is_set_IS_AEAD (sa))
510         {
511           /* constuct aad in a scratch space in front of the nonce */
512           aad = (u8 *) nonce - sizeof (esp_aead_t);
513           esp_aad_fill (aad, esp, sa, sa->seq_hi);
514           key_index = sa->crypto_key_index;
515         }
516       else
517         {
518           nonce->ctr = clib_host_to_net_u32 (1);
519         }
520
521       nonce->salt = sa->salt;
522       nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa->ctr_iv_counter++);
523       iv = (u8 *) nonce;
524     }
525   else
526     {
527       iv = payload - iv_sz;
528       flag |= VNET_CRYPTO_OP_FLAG_INIT_IV;
529     }
530
531   if (lb != b)
532     {
533       /* chain */
534       flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
535       tag = vlib_buffer_get_tail (lb) - icv_sz;
536       crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb, icv_sz,
537                                                    payload, payload_len, 0);
538     }
539
540   if (sa->integ_op_id)
541     {
542       integ_start_offset = crypto_start_offset - iv_sz - sizeof (esp_header_t);
543       integ_total_len += iv_sz + sizeof (esp_header_t);
544
545       if (b != lb)
546         {
547           integ_total_len = esp_encrypt_chain_integ (
548             vm, ptd, sa, b, lb, icv_sz,
549             payload - iv_sz - sizeof (esp_header_t),
550             payload_len + iv_sz + sizeof (esp_header_t), tag, 0);
551         }
552       else if (ipsec_sa_is_set_USE_ESN (sa))
553         {
554           u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
555           clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
556           integ_total_len += sizeof (seq_hi);
557         }
558     }
559
560   /* this always succeeds because we know the frame is not full */
561   vnet_crypto_async_add_to_frame (vm, async_frame, key_index, crypto_total_len,
562                                   integ_total_len - crypto_total_len,
563                                   crypto_start_offset, integ_start_offset, bi,
564                                   async_next, iv, tag, aad, flag);
565 }
566
567 always_inline uword
568 esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
569                     vlib_frame_t *frame, vnet_link_t lt, int is_tun,
570                     u16 async_next_node)
571 {
572   ipsec_main_t *im = &ipsec_main;
573   ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
574   u32 *from = vlib_frame_vector_args (frame);
575   u32 n_left = frame->n_vectors;
576   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
577   u32 thread_index = vm->thread_index;
578   u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
579   u32 current_sa_index = ~0, current_sa_packets = 0;
580   u32 current_sa_bytes = 0, spi = 0;
581   u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
582   ipsec_sa_t *sa0 = 0;
583   vlib_buffer_t *lb;
584   vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
585   vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
586   vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
587   int is_async = im->async_mode;
588   vnet_crypto_async_op_id_t async_op = ~0;
589   u16 drop_next =
590     (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
591                            (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
592                                                   ESP_ENCRYPT_NEXT_DROP_MPLS));
593   u16 handoff_next = (lt == VNET_LINK_IP6 ?
594                         ESP_ENCRYPT_NEXT_HANDOFF6 :
595                         (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
596                                                ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
597   vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
598   u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
599   u16 async_nexts[VLIB_FRAME_SIZE], *async_next = async_nexts, n_async = 0;
600   u16 noop_nexts[VLIB_FRAME_SIZE], *noop_next = noop_nexts, n_noop = 0;
601   u32 sync_bi[VLIB_FRAME_SIZE];
602   u32 noop_bi[VLIB_FRAME_SIZE];
603   esp_encrypt_error_t err;
604
605   vlib_get_buffers (vm, from, b, n_left);
606
607   vec_reset_length (ptd->crypto_ops);
608   vec_reset_length (ptd->integ_ops);
609   vec_reset_length (ptd->chained_crypto_ops);
610   vec_reset_length (ptd->chained_integ_ops);
611   vec_reset_length (ptd->async_frames);
612   vec_reset_length (ptd->chunks);
613   clib_memset (async_frames, 0, sizeof (async_frames));
614
615   while (n_left > 0)
616     {
617       u32 sa_index0;
618       dpo_id_t *dpo;
619       esp_header_t *esp;
620       u8 *payload, *next_hdr_ptr;
621       u16 payload_len, payload_len_total, n_bufs;
622       u32 hdr_len;
623
624       err = ESP_ENCRYPT_ERROR_RX_PKTS;
625
626       if (n_left > 2)
627         {
628           u8 *p;
629           vlib_prefetch_buffer_header (b[2], LOAD);
630           p = vlib_buffer_get_current (b[1]);
631           clib_prefetch_load (p);
632           p -= CLIB_CACHE_LINE_BYTES;
633           clib_prefetch_load (p);
634           /* speculate that the trailer goes in the first buffer */
635           CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
636                          CLIB_CACHE_LINE_BYTES, LOAD);
637         }
638
639       if (is_tun)
640         {
641           /* we are on a ipsec tunnel's feature arc */
642           vnet_buffer (b[0])->ipsec.sad_index =
643             sa_index0 = ipsec_tun_protect_get_sa_out
644             (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
645
646           if (PREDICT_FALSE (INDEX_INVALID == sa_index0))
647             {
648               err = ESP_ENCRYPT_ERROR_NO_PROTECTION;
649               esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
650                                   drop_next);
651               goto trace;
652             }
653         }
654       else
655         sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
656
657       if (sa_index0 != current_sa_index)
658         {
659           if (current_sa_packets)
660             vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
661                                              current_sa_index,
662                                              current_sa_packets,
663                                              current_sa_bytes);
664           current_sa_packets = current_sa_bytes = 0;
665
666           sa0 = ipsec_sa_get (sa_index0);
667
668           if (PREDICT_FALSE ((sa0->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
669                               sa0->integ_alg == IPSEC_INTEG_ALG_NONE) &&
670                              !ipsec_sa_is_set_NO_ALGO_NO_DROP (sa0)))
671             {
672               err = ESP_ENCRYPT_ERROR_NO_ENCRYPTION;
673               esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
674                                   drop_next);
675               goto trace;
676             }
677           /* fetch the second cacheline ASAP */
678           clib_prefetch_load (sa0->cacheline1);
679
680           current_sa_index = sa_index0;
681           spi = clib_net_to_host_u32 (sa0->spi);
682           esp_align = sa0->esp_block_align;
683           icv_sz = sa0->integ_icv_size;
684           iv_sz = sa0->crypto_iv_size;
685           is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
686         }
687
688       if (PREDICT_FALSE (~0 == sa0->thread_index))
689         {
690           /* this is the first packet to use this SA, claim the SA
691            * for this thread. this could happen simultaneously on
692            * another thread */
693           clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
694                                     ipsec_sa_assign_thread (thread_index));
695         }
696
697       if (PREDICT_FALSE (thread_index != sa0->thread_index))
698         {
699           vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
700           err = ESP_ENCRYPT_ERROR_HANDOFF;
701           esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
702                               handoff_next);
703           goto trace;
704         }
705
706       lb = b[0];
707       n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
708       if (n_bufs == 0)
709         {
710           err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
711           esp_set_next_index (b[0], node, err, n_noop, noop_nexts, drop_next);
712           goto trace;
713         }
714
715       if (n_bufs > 1)
716         {
717           /* find last buffer in the chain */
718           while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
719             lb = vlib_get_buffer (vm, lb->next_buffer);
720         }
721
722       if (PREDICT_FALSE (esp_seq_advance (sa0)))
723         {
724           err = ESP_ENCRYPT_ERROR_SEQ_CYCLED;
725           esp_set_next_index (b[0], node, err, n_noop, noop_nexts, drop_next);
726           goto trace;
727         }
728
729       /* space for IV */
730       hdr_len = iv_sz;
731
732       if (ipsec_sa_is_set_IS_TUNNEL (sa0))
733         {
734           payload = vlib_buffer_get_current (b[0]);
735           next_hdr_ptr = esp_add_footer_and_icv (
736             vm, &lb, esp_align, icv_sz, node, buffer_data_size,
737             vlib_buffer_length_in_chain (vm, b[0]));
738           if (!next_hdr_ptr)
739             {
740               err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
741               esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
742                                   drop_next);
743               goto trace;
744             }
745           b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
746           payload_len = b[0]->current_length;
747           payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
748
749           /* ESP header */
750           hdr_len += sizeof (*esp);
751           esp = (esp_header_t *) (payload - hdr_len);
752
753           /* optional UDP header */
754           if (ipsec_sa_is_set_UDP_ENCAP (sa0))
755             {
756               hdr_len += sizeof (udp_header_t);
757               esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
758                                 payload_len_total + hdr_len);
759             }
760
761           /* IP header */
762           if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
763             {
764               ip6_header_t *ip6;
765               u16 len = sizeof (ip6_header_t);
766               hdr_len += len;
767               ip6 = (ip6_header_t *) (payload - hdr_len);
768               clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
769
770               if (VNET_LINK_IP6 == lt)
771                 {
772                   *next_hdr_ptr = IP_PROTOCOL_IPV6;
773                   tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
774                                           (const ip6_header_t *) payload,
775                                           ip6);
776                 }
777               else if (VNET_LINK_IP4 == lt)
778                 {
779                   *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
780                   tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0],
781                                           (const ip4_header_t *) payload, ip6);
782                 }
783               else if (VNET_LINK_MPLS == lt)
784                 {
785                   *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
786                   tunnel_encap_fixup_mplso6 (
787                     sa0->tunnel_flags, b[0],
788                     (const mpls_unicast_header_t *) payload, ip6);
789                 }
790               else
791                 ASSERT (0);
792
793               len = payload_len_total + hdr_len - len;
794               ip6->payload_length = clib_net_to_host_u16 (len);
795               b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
796             }
797           else
798             {
799               ip4_header_t *ip4;
800               u16 len = sizeof (ip4_header_t);
801               hdr_len += len;
802               ip4 = (ip4_header_t *) (payload - hdr_len);
803               clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
804
805               if (VNET_LINK_IP6 == lt)
806                 {
807                   *next_hdr_ptr = IP_PROTOCOL_IPV6;
808                   tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
809                                                    (const ip6_header_t *)
810                                                    payload, ip4);
811                 }
812               else if (VNET_LINK_IP4 == lt)
813                 {
814                   *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
815                   tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
816                                                    (const ip4_header_t *)
817                                                    payload, ip4);
818                 }
819               else if (VNET_LINK_MPLS == lt)
820                 {
821                   *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
822                   tunnel_encap_fixup_mplso4_w_chksum (
823                     sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
824                     ip4);
825                 }
826               else
827                 ASSERT (0);
828
829               len = payload_len_total + hdr_len;
830               esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
831             }
832
833           dpo = &sa0->dpo;
834           if (!is_tun)
835             {
836               sync_next[0] = dpo->dpoi_next_node;
837               vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
838             }
839           else
840             sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
841           b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
842         }
843       else                      /* transport mode */
844         {
845           u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
846           ip6_ext_header_t *ext_hdr;
847           udp_header_t *udp = 0;
848           u16 udp_len = 0;
849           u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
850
851           ip_len =
852             (VNET_LINK_IP6 == lt ?
853                esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
854                ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
855
856           vlib_buffer_advance (b[0], ip_len);
857           payload = vlib_buffer_get_current (b[0]);
858           next_hdr_ptr = esp_add_footer_and_icv (
859             vm, &lb, esp_align, icv_sz, node, buffer_data_size,
860             vlib_buffer_length_in_chain (vm, b[0]));
861           if (!next_hdr_ptr)
862             {
863               err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
864               esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
865                                   drop_next);
866               goto trace;
867             }
868
869           b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
870           payload_len = b[0]->current_length;
871           payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
872
873           /* ESP header */
874           hdr_len += sizeof (*esp);
875           esp = (esp_header_t *) (payload - hdr_len);
876
877           /* optional UDP header */
878           if (ipsec_sa_is_set_UDP_ENCAP (sa0))
879             {
880               hdr_len += sizeof (udp_header_t);
881               udp = (udp_header_t *) (payload - hdr_len);
882             }
883
884           /* IP header */
885           hdr_len += ip_len;
886           ip_hdr = payload - hdr_len;
887
888           /* L2 header */
889           if (!is_tun)
890             {
891               l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
892               hdr_len += l2_len;
893               l2_hdr = payload - hdr_len;
894
895               /* copy l2 and ip header */
896               clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
897             }
898           else
899             l2_len = 0;
900
901           if (VNET_LINK_IP6 == lt)
902             {
903               ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
904               if (PREDICT_TRUE (NULL == ext_hdr))
905                 {
906                   *next_hdr_ptr = ip6->protocol;
907                   ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
908                 }
909               else
910                 {
911                   *next_hdr_ptr = ext_hdr->next_hdr;
912                   ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
913                 }
914               ip6->payload_length =
915                 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
916                                       sizeof (ip6_header_t));
917             }
918           else if (VNET_LINK_IP4 == lt)
919             {
920               u16 len;
921               ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
922               *next_hdr_ptr = ip4->protocol;
923               len = payload_len_total + hdr_len - l2_len;
924               if (udp)
925                 {
926                   esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
927                   udp_len = len - ip_len;
928                 }
929               else
930                 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
931             }
932
933           clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
934
935           if (udp)
936             {
937               esp_fill_udp_hdr (sa0, udp, udp_len);
938             }
939
940           sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
941         }
942
943       if (lb != b[0])
944         {
945           crypto_ops = &ptd->chained_crypto_ops;
946           integ_ops = &ptd->chained_integ_ops;
947         }
948       else
949         {
950           crypto_ops = &ptd->crypto_ops;
951           integ_ops = &ptd->integ_ops;
952         }
953
954       esp->spi = spi;
955       esp->seq = clib_net_to_host_u32 (sa0->seq);
956
957       if (is_async)
958         {
959           async_op = sa0->crypto_async_enc_op_id;
960
961           /* get a frame for this op if we don't yet have one or it's full
962            */
963           if (NULL == async_frames[async_op] ||
964               vnet_crypto_async_frame_is_full (async_frames[async_op]))
965             {
966               async_frames[async_op] =
967                 vnet_crypto_async_get_frame (vm, async_op);
968               /* Save the frame to the list we'll submit at the end */
969               vec_add1 (ptd->async_frames, async_frames[async_op]);
970             }
971
972           esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0],
973                                    esp, payload, payload_len, iv_sz, icv_sz,
974                                    from[b - bufs], sync_next[0], hdr_len,
975                                    async_next_node, lb);
976         }
977       else
978         esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, sa0->seq_hi,
979                              payload, payload_len, iv_sz, icv_sz, n_sync, b,
980                              lb, hdr_len, esp);
981
982       vlib_buffer_advance (b[0], 0LL - hdr_len);
983
984       current_sa_packets += 1;
985       current_sa_bytes += payload_len_total;
986
987     trace:
988       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
989         {
990           esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
991                                                     sizeof (*tr));
992           if (INDEX_INVALID == sa_index0)
993             clib_memset_u8 (tr, 0xff, sizeof (*tr));
994           else
995             {
996               tr->sa_index = sa_index0;
997               tr->spi = sa0->spi;
998               tr->spi = sa0->spi;
999               tr->seq = sa0->seq;
1000               tr->sa_seq_hi = sa0->seq_hi;
1001               tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
1002               tr->crypto_alg = sa0->crypto_alg;
1003               tr->integ_alg = sa0->integ_alg;
1004             }
1005         }
1006
1007       /* next */
1008       if (ESP_ENCRYPT_ERROR_RX_PKTS != err)
1009         {
1010           noop_bi[n_noop] = from[b - bufs];
1011           n_noop++;
1012           noop_next++;
1013         }
1014       else if (!is_async)
1015         {
1016           sync_bi[n_sync] = from[b - bufs];
1017           sync_bufs[n_sync] = b[0];
1018           n_sync++;
1019           sync_next++;
1020         }
1021       else
1022         {
1023           n_async++;
1024           async_next++;
1025         }
1026       n_left -= 1;
1027       b += 1;
1028     }
1029
1030   if (INDEX_INVALID != current_sa_index)
1031     vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1032                                      current_sa_index, current_sa_packets,
1033                                      current_sa_bytes);
1034   if (n_sync)
1035     {
1036       esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
1037                        drop_next);
1038       esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1039                                sync_nexts, ptd->chunks, drop_next);
1040
1041       esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1042                        drop_next);
1043       esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1044                                sync_nexts, ptd->chunks, drop_next);
1045
1046       vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
1047     }
1048   if (n_async)
1049     {
1050       /* submit all of the open frames */
1051       vnet_crypto_async_frame_t **async_frame;
1052
1053       vec_foreach (async_frame, ptd->async_frames)
1054         {
1055           if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1056             {
1057               n_noop += esp_async_recycle_failed_submit (
1058                 vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
1059                 n_sync, noop_bi, noop_nexts, drop_next);
1060               vnet_crypto_async_reset_frame (*async_frame);
1061               vnet_crypto_async_free_frame (vm, *async_frame);
1062             }
1063         }
1064     }
1065   if (n_noop)
1066     vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1067
1068   vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS,
1069                                frame->n_vectors);
1070
1071   return frame->n_vectors;
1072 }
1073
1074 always_inline uword
1075 esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1076                          vlib_frame_t * frame)
1077 {
1078   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1079   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1080   u32 *from = vlib_frame_vector_args (frame);
1081   u32 n_left = frame->n_vectors;
1082
1083   vlib_get_buffers (vm, from, b, n_left);
1084
1085   if (n_left >= 4)
1086     {
1087       vlib_prefetch_buffer_header (b[0], LOAD);
1088       vlib_prefetch_buffer_header (b[1], LOAD);
1089       vlib_prefetch_buffer_header (b[2], LOAD);
1090       vlib_prefetch_buffer_header (b[3], LOAD);
1091     }
1092
1093   while (n_left > 8)
1094     {
1095       vlib_prefetch_buffer_header (b[4], LOAD);
1096       vlib_prefetch_buffer_header (b[5], LOAD);
1097       vlib_prefetch_buffer_header (b[6], LOAD);
1098       vlib_prefetch_buffer_header (b[7], LOAD);
1099
1100       next[0] = (esp_post_data (b[0]))->next_index;
1101       next[1] = (esp_post_data (b[1]))->next_index;
1102       next[2] = (esp_post_data (b[2]))->next_index;
1103       next[3] = (esp_post_data (b[3]))->next_index;
1104
1105       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1106         {
1107           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1108             {
1109               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1110                                                              sizeof (*tr));
1111               tr->next_index = next[0];
1112             }
1113           if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1114             {
1115               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1116                                                              sizeof (*tr));
1117               tr->next_index = next[1];
1118             }
1119           if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1120             {
1121               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1122                                                              sizeof (*tr));
1123               tr->next_index = next[2];
1124             }
1125           if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1126             {
1127               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1128                                                              sizeof (*tr));
1129               tr->next_index = next[3];
1130             }
1131         }
1132
1133       b += 4;
1134       next += 4;
1135       n_left -= 4;
1136     }
1137
1138   while (n_left > 0)
1139     {
1140       next[0] = (esp_post_data (b[0]))->next_index;
1141       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1142         {
1143           esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1144                                                          sizeof (*tr));
1145           tr->next_index = next[0];
1146         }
1147
1148       b += 1;
1149       next += 1;
1150       n_left -= 1;
1151     }
1152
1153   vlib_node_increment_counter (vm, node->node_index,
1154                                ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1155                                frame->n_vectors);
1156   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1157   return frame->n_vectors;
1158 }
1159
1160 VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1161                                   vlib_node_runtime_t * node,
1162                                   vlib_frame_t * from_frame)
1163 {
1164   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
1165                              esp_encrypt_async_next.esp4_post_next);
1166 }
1167
1168 /* *INDENT-OFF* */
1169 VLIB_REGISTER_NODE (esp4_encrypt_node) = {
1170   .name = "esp4-encrypt",
1171   .vector_size = sizeof (u32),
1172   .format_trace = format_esp_encrypt_trace,
1173   .type = VLIB_NODE_TYPE_INTERNAL,
1174
1175   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1176   .error_strings = esp_encrypt_error_strings,
1177
1178   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1179   .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1180                   [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1181                   [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1182                   [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1183                   [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1184                   [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1185                   [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
1186 };
1187 /* *INDENT-ON* */
1188
1189 VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1190                                        vlib_node_runtime_t * node,
1191                                        vlib_frame_t * from_frame)
1192 {
1193   return esp_encrypt_post_inline (vm, node, from_frame);
1194 }
1195
1196 /* *INDENT-OFF* */
1197 VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1198   .name = "esp4-encrypt-post",
1199   .vector_size = sizeof (u32),
1200   .format_trace = format_esp_post_encrypt_trace,
1201   .type = VLIB_NODE_TYPE_INTERNAL,
1202   .sibling_of = "esp4-encrypt",
1203
1204   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1205   .error_strings = esp_encrypt_error_strings,
1206 };
1207 /* *INDENT-ON* */
1208
1209 VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1210                                   vlib_node_runtime_t * node,
1211                                   vlib_frame_t * from_frame)
1212 {
1213   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
1214                              esp_encrypt_async_next.esp6_post_next);
1215 }
1216
1217 /* *INDENT-OFF* */
1218 VLIB_REGISTER_NODE (esp6_encrypt_node) = {
1219   .name = "esp6-encrypt",
1220   .vector_size = sizeof (u32),
1221   .format_trace = format_esp_encrypt_trace,
1222   .type = VLIB_NODE_TYPE_INTERNAL,
1223   .sibling_of = "esp4-encrypt",
1224
1225   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1226   .error_strings = esp_encrypt_error_strings,
1227 };
1228 /* *INDENT-ON* */
1229
1230 VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1231                                        vlib_node_runtime_t * node,
1232                                        vlib_frame_t * from_frame)
1233 {
1234   return esp_encrypt_post_inline (vm, node, from_frame);
1235 }
1236
1237 /* *INDENT-OFF* */
1238 VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1239   .name = "esp6-encrypt-post",
1240   .vector_size = sizeof (u32),
1241   .format_trace = format_esp_post_encrypt_trace,
1242   .type = VLIB_NODE_TYPE_INTERNAL,
1243   .sibling_of = "esp4-encrypt",
1244
1245   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1246   .error_strings = esp_encrypt_error_strings,
1247 };
1248 /* *INDENT-ON* */
1249
1250 VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1251                                       vlib_node_runtime_t * node,
1252                                       vlib_frame_t * from_frame)
1253 {
1254   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
1255                              esp_encrypt_async_next.esp4_tun_post_next);
1256 }
1257
1258 /* *INDENT-OFF* */
1259 VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1260   .name = "esp4-encrypt-tun",
1261   .vector_size = sizeof (u32),
1262   .format_trace = format_esp_encrypt_trace,
1263   .type = VLIB_NODE_TYPE_INTERNAL,
1264
1265   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1266   .error_strings = esp_encrypt_error_strings,
1267
1268   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1269   .next_nodes = {
1270     [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1271     [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1272     [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1273     [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1274     [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1275     [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1276     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1277   },
1278 };
1279
1280 VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1281                                   vlib_node_runtime_t * node,
1282                                   vlib_frame_t * from_frame)
1283 {
1284   return esp_encrypt_post_inline (vm, node, from_frame);
1285 }
1286
1287 /* *INDENT-OFF* */
1288 VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1289   .name = "esp4-encrypt-tun-post",
1290   .vector_size = sizeof (u32),
1291   .format_trace = format_esp_post_encrypt_trace,
1292   .type = VLIB_NODE_TYPE_INTERNAL,
1293   .sibling_of = "esp4-encrypt-tun",
1294
1295   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1296   .error_strings = esp_encrypt_error_strings,
1297 };
1298 /* *INDENT-ON* */
1299
1300 VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1301                                       vlib_node_runtime_t * node,
1302                                       vlib_frame_t * from_frame)
1303 {
1304   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
1305                              esp_encrypt_async_next.esp6_tun_post_next);
1306 }
1307
1308 /* *INDENT-OFF* */
1309 VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1310   .name = "esp6-encrypt-tun",
1311   .vector_size = sizeof (u32),
1312   .format_trace = format_esp_encrypt_trace,
1313   .type = VLIB_NODE_TYPE_INTERNAL,
1314
1315   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1316   .error_strings = esp_encrypt_error_strings,
1317
1318   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1319   .next_nodes = {
1320     [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1321     [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1322     [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1323     [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1324     [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1325     [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1326     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1327   },
1328 };
1329
1330 /* *INDENT-ON* */
1331
1332 VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1333                                            vlib_node_runtime_t * node,
1334                                            vlib_frame_t * from_frame)
1335 {
1336   return esp_encrypt_post_inline (vm, node, from_frame);
1337 }
1338
1339 /* *INDENT-OFF* */
1340 VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1341   .name = "esp6-encrypt-tun-post",
1342   .vector_size = sizeof (u32),
1343   .format_trace = format_esp_post_encrypt_trace,
1344   .type = VLIB_NODE_TYPE_INTERNAL,
1345   .sibling_of = "esp-mpls-encrypt-tun",
1346
1347   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1348   .error_strings = esp_encrypt_error_strings,
1349 };
1350 /* *INDENT-ON* */
1351
1352 VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1353 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1354 {
1355   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1356                              esp_encrypt_async_next.esp_mpls_tun_post_next);
1357 }
1358
1359 VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1360   .name = "esp-mpls-encrypt-tun",
1361   .vector_size = sizeof (u32),
1362   .format_trace = format_esp_encrypt_trace,
1363   .type = VLIB_NODE_TYPE_INTERNAL,
1364
1365   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1366   .error_strings = esp_encrypt_error_strings,
1367
1368   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1369   .next_nodes = {
1370     [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1371     [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1372     [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1373     [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1374     [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1375     [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1376     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1377   },
1378 };
1379
1380 VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1381 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1382 {
1383   return esp_encrypt_post_inline (vm, node, from_frame);
1384 }
1385
1386 VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1387   .name = "esp-mpls-encrypt-tun-post",
1388   .vector_size = sizeof (u32),
1389   .format_trace = format_esp_post_encrypt_trace,
1390   .type = VLIB_NODE_TYPE_INTERNAL,
1391   .sibling_of = "esp-mpls-encrypt-tun",
1392
1393   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1394   .error_strings = esp_encrypt_error_strings,
1395 };
1396
1397 #ifndef CLIB_MARCH_VARIANT
1398
1399 static clib_error_t *
1400 esp_encrypt_init (vlib_main_t *vm)
1401 {
1402   ipsec_main_t *im = &ipsec_main;
1403
1404   im->esp4_enc_fq_index =
1405     vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
1406   im->esp6_enc_fq_index =
1407     vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
1408   im->esp4_enc_tun_fq_index =
1409     vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
1410   im->esp6_enc_tun_fq_index =
1411     vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
1412   im->esp_mpls_enc_tun_fq_index =
1413     vlib_frame_queue_main_init (esp_mpls_encrypt_tun_node.index, 0);
1414
1415   return 0;
1416 }
1417
1418 VLIB_INIT_FUNCTION (esp_encrypt_init);
1419
1420 #endif
1421
1422 /*
1423  * fd.io coding-style-patch-verification: ON
1424  *
1425  * Local Variables:
1426  * eval: (c-set-style "gnu")
1427  * End:
1428  */