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