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