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