ipsec: fix async crypto frame leak
[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 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 static_always_inline u32
296 esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
297                           ipsec_sa_t * sa0, vlib_buffer_t * b,
298                           vlib_buffer_t * lb, u8 icv_sz, u8 * start,
299                           u32 start_len, u16 * n_ch)
300 {
301   vnet_crypto_op_chunk_t *ch;
302   vlib_buffer_t *cb = b;
303   u32 n_chunks = 1;
304   u32 total_len;
305   vec_add2 (ptd->chunks, ch, 1);
306   total_len = ch->len = start_len;
307   ch->src = ch->dst = start;
308   cb = vlib_get_buffer (vm, cb->next_buffer);
309
310   while (1)
311     {
312       vec_add2 (ptd->chunks, ch, 1);
313       n_chunks += 1;
314       if (lb == cb)
315         total_len += ch->len = cb->current_length - icv_sz;
316       else
317         total_len += ch->len = cb->current_length;
318       ch->src = ch->dst = vlib_buffer_get_current (cb);
319
320       if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
321         break;
322
323       cb = vlib_get_buffer (vm, cb->next_buffer);
324     }
325
326   if (n_ch)
327     *n_ch = n_chunks;
328
329   return total_len;
330 }
331
332 static_always_inline u32
333 esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
334                          ipsec_sa_t * sa0, vlib_buffer_t * b,
335                          vlib_buffer_t * lb, u8 icv_sz, u8 * start,
336                          u32 start_len, u8 * digest, u16 * n_ch)
337 {
338   vnet_crypto_op_chunk_t *ch;
339   vlib_buffer_t *cb = b;
340   u32 n_chunks = 1;
341   u32 total_len;
342   vec_add2 (ptd->chunks, ch, 1);
343   total_len = ch->len = start_len;
344   ch->src = start;
345   cb = vlib_get_buffer (vm, cb->next_buffer);
346
347   while (1)
348     {
349       vec_add2 (ptd->chunks, ch, 1);
350       n_chunks += 1;
351       if (lb == cb)
352         {
353           total_len += ch->len = cb->current_length - icv_sz;
354           if (ipsec_sa_is_set_USE_ESN (sa0))
355             {
356               u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
357               clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
358               ch->len += sizeof (seq_hi);
359               total_len += sizeof (seq_hi);
360             }
361         }
362       else
363         total_len += ch->len = cb->current_length;
364       ch->src = vlib_buffer_get_current (cb);
365
366       if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
367         break;
368
369       cb = vlib_get_buffer (vm, cb->next_buffer);
370     }
371
372   if (n_ch)
373     *n_ch = n_chunks;
374
375   return total_len;
376 }
377
378 always_inline void
379 esp_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
380                      vnet_crypto_op_t **crypto_ops,
381                      vnet_crypto_op_t **integ_ops, ipsec_sa_t *sa0,
382                      u8 *payload, u16 payload_len, u8 iv_sz, u8 icv_sz, u32 bi,
383                      vlib_buffer_t **b, vlib_buffer_t *lb, u32 hdr_len,
384                      esp_header_t *esp)
385 {
386   if (sa0->crypto_enc_op_id)
387     {
388       vnet_crypto_op_t *op;
389       vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
390       vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
391
392       op->src = op->dst = payload;
393       op->key_index = sa0->crypto_key_index;
394       op->len = payload_len - icv_sz;
395       op->user_data = bi;
396
397       if (ipsec_sa_is_set_IS_CTR (sa0))
398         {
399           ASSERT (sizeof (u64) == iv_sz);
400           /* construct nonce in a scratch space in front of the IP header */
401           esp_ctr_nonce_t *nonce =
402             (esp_ctr_nonce_t *) (payload - sizeof (u64) - hdr_len -
403                                  sizeof (*nonce));
404           u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
405
406           if (ipsec_sa_is_set_IS_AEAD (sa0))
407             {
408               /* constuct aad in a scratch space in front of the nonce */
409               op->aad = (u8 *) nonce - sizeof (esp_aead_t);
410               op->aad_len = esp_aad_fill (op->aad, esp, sa0);
411               op->tag = payload + op->len;
412               op->tag_len = 16;
413             }
414           else
415             {
416               nonce->ctr = clib_host_to_net_u32 (1);
417             }
418
419           nonce->salt = sa0->salt;
420           nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa0->ctr_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 = bi;
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 void
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, esp_header_t *esp,
478                          u8 *payload, u32 payload_len, u8 iv_sz, u8 icv_sz,
479                          u32 bi, u16 next, u32 hdr_len, u16 async_next,
480                          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   key_index = sa->linked_key_index;
497
498   if (ipsec_sa_is_set_IS_CTR (sa))
499     {
500       ASSERT (sizeof (u64) == iv_sz);
501       /* construct nonce in a scratch space in front of the IP header */
502       esp_ctr_nonce_t *nonce = (esp_ctr_nonce_t *) (payload - sizeof (u64) -
503                                                     hdr_len - sizeof (*nonce));
504       u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
505
506       if (ipsec_sa_is_set_IS_AEAD (sa))
507         {
508           /* constuct aad in a scratch space in front of the nonce */
509           aad = (u8 *) nonce - sizeof (esp_aead_t);
510           esp_aad_fill (aad, esp, sa);
511           key_index = sa->crypto_key_index;
512         }
513       else
514         {
515           nonce->ctr = clib_host_to_net_u32 (1);
516         }
517
518       nonce->salt = sa->salt;
519       nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa->ctr_iv_counter++);
520       iv = (u8 *) nonce;
521     }
522   else
523     {
524       iv = payload - iv_sz;
525       flag |= VNET_CRYPTO_OP_FLAG_INIT_IV;
526     }
527
528   if (lb != b)
529     {
530       /* chain */
531       flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
532       tag = vlib_buffer_get_tail (lb) - icv_sz;
533       crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb, icv_sz,
534                                                    payload, payload_len, 0);
535     }
536
537   if (sa->integ_op_id)
538     {
539       integ_start_offset = crypto_start_offset - iv_sz - sizeof (esp_header_t);
540       integ_total_len += iv_sz + sizeof (esp_header_t);
541
542       if (b != lb)
543         {
544           integ_total_len = esp_encrypt_chain_integ (
545             vm, ptd, sa, b, lb, icv_sz,
546             payload - iv_sz - sizeof (esp_header_t),
547             payload_len + iv_sz + sizeof (esp_header_t), tag, 0);
548         }
549       else if (ipsec_sa_is_set_USE_ESN (sa))
550         {
551           u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
552           clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
553           integ_total_len += sizeof (seq_hi);
554         }
555     }
556
557   /* this always succeeds because we know the frame is not full */
558   vnet_crypto_async_add_to_frame (vm, async_frame, key_index, crypto_total_len,
559                                   integ_total_len - crypto_total_len,
560                                   crypto_start_offset, integ_start_offset, bi,
561                                   async_next, iv, tag, aad, flag);
562 }
563
564 always_inline uword
565 esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
566                     vlib_frame_t *frame, vnet_link_t lt, int is_tun,
567                     u16 async_next_node)
568 {
569   ipsec_main_t *im = &ipsec_main;
570   ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
571   u32 *from = vlib_frame_vector_args (frame);
572   u32 n_left = frame->n_vectors;
573   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
574   u32 thread_index = vm->thread_index;
575   u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
576   u32 current_sa_index = ~0, current_sa_packets = 0;
577   u32 current_sa_bytes = 0, spi = 0;
578   u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
579   ipsec_sa_t *sa0 = 0;
580   vlib_buffer_t *lb;
581   vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
582   vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
583   vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
584   int is_async = im->async_mode;
585   vnet_crypto_async_op_id_t async_op = ~0;
586   u16 drop_next =
587     (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
588                            (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
589                                                   ESP_ENCRYPT_NEXT_DROP_MPLS));
590   u16 handoff_next = (lt == VNET_LINK_IP6 ?
591                         ESP_ENCRYPT_NEXT_HANDOFF6 :
592                         (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
593                                                ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
594   vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
595   u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
596   u16 async_nexts[VLIB_FRAME_SIZE], *async_next = async_nexts, n_async = 0;
597   u16 noop_nexts[VLIB_FRAME_SIZE], *noop_next = noop_nexts, n_noop = 0;
598   u32 sync_bi[VLIB_FRAME_SIZE];
599   u32 noop_bi[VLIB_FRAME_SIZE];
600   esp_encrypt_error_t err;
601
602   vlib_get_buffers (vm, from, b, n_left);
603
604   vec_reset_length (ptd->crypto_ops);
605   vec_reset_length (ptd->integ_ops);
606   vec_reset_length (ptd->chained_crypto_ops);
607   vec_reset_length (ptd->chained_integ_ops);
608   vec_reset_length (ptd->async_frames);
609   vec_reset_length (ptd->chunks);
610   clib_memset (async_frames, 0, sizeof (async_frames));
611
612   while (n_left > 0)
613     {
614       u32 sa_index0;
615       dpo_id_t *dpo;
616       esp_header_t *esp;
617       u8 *payload, *next_hdr_ptr;
618       u16 payload_len, payload_len_total, n_bufs;
619       u32 hdr_len;
620
621       err = ESP_ENCRYPT_ERROR_RX_PKTS;
622
623       if (n_left > 2)
624         {
625           u8 *p;
626           vlib_prefetch_buffer_header (b[2], LOAD);
627           p = vlib_buffer_get_current (b[1]);
628           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
629           p -= CLIB_CACHE_LINE_BYTES;
630           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
631           /* speculate that the trailer goes in the first buffer */
632           CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
633                          CLIB_CACHE_LINE_BYTES, LOAD);
634         }
635
636       if (is_tun)
637         {
638           /* we are on a ipsec tunnel's feature arc */
639           vnet_buffer (b[0])->ipsec.sad_index =
640             sa_index0 = ipsec_tun_protect_get_sa_out
641             (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
642         }
643       else
644         sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
645
646       if (sa_index0 != current_sa_index)
647         {
648           if (current_sa_packets)
649             vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
650                                              current_sa_index,
651                                              current_sa_packets,
652                                              current_sa_bytes);
653           current_sa_packets = current_sa_bytes = 0;
654
655           sa0 = ipsec_sa_get (sa_index0);
656
657           /* fetch the second cacheline ASAP */
658           CLIB_PREFETCH (sa0->cacheline1, CLIB_CACHE_LINE_BYTES, LOAD);
659
660           current_sa_index = sa_index0;
661           spi = clib_net_to_host_u32 (sa0->spi);
662           esp_align = sa0->esp_block_align;
663           icv_sz = sa0->integ_icv_size;
664           iv_sz = sa0->crypto_iv_size;
665           is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
666         }
667
668       if (PREDICT_FALSE (~0 == sa0->thread_index))
669         {
670           /* this is the first packet to use this SA, claim the SA
671            * for this thread. this could happen simultaneously on
672            * another thread */
673           clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
674                                     ipsec_sa_assign_thread (thread_index));
675         }
676
677       if (PREDICT_FALSE (thread_index != sa0->thread_index))
678         {
679           vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
680           err = ESP_ENCRYPT_ERROR_HANDOFF;
681           esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
682                               handoff_next);
683           goto trace;
684         }
685
686       lb = b[0];
687       n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
688       if (n_bufs == 0)
689         {
690           err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
691           esp_set_next_index (b[0], node, err, n_noop, noop_nexts, drop_next);
692           goto trace;
693         }
694
695       if (n_bufs > 1)
696         {
697           /* find last buffer in the chain */
698           while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
699             lb = vlib_get_buffer (vm, lb->next_buffer);
700         }
701
702       if (PREDICT_FALSE (esp_seq_advance (sa0)))
703         {
704           err = ESP_ENCRYPT_ERROR_SEQ_CYCLED;
705           esp_set_next_index (b[0], node, err, n_noop, noop_nexts, drop_next);
706           goto trace;
707         }
708
709       /* space for IV */
710       hdr_len = iv_sz;
711
712       if (ipsec_sa_is_set_IS_TUNNEL (sa0))
713         {
714           payload = vlib_buffer_get_current (b[0]);
715           next_hdr_ptr = esp_add_footer_and_icv (
716             vm, &lb, esp_align, icv_sz, node, buffer_data_size,
717             vlib_buffer_length_in_chain (vm, b[0]));
718           if (!next_hdr_ptr)
719             {
720               err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
721               esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
722                                   drop_next);
723               goto trace;
724             }
725           b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
726           payload_len = b[0]->current_length;
727           payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
728
729           /* ESP header */
730           hdr_len += sizeof (*esp);
731           esp = (esp_header_t *) (payload - hdr_len);
732
733           /* optional UDP header */
734           if (ipsec_sa_is_set_UDP_ENCAP (sa0))
735             {
736               hdr_len += sizeof (udp_header_t);
737               esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
738                                 payload_len_total + hdr_len);
739             }
740
741           /* IP header */
742           if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
743             {
744               ip6_header_t *ip6;
745               u16 len = sizeof (ip6_header_t);
746               hdr_len += len;
747               ip6 = (ip6_header_t *) (payload - hdr_len);
748               clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
749
750               if (VNET_LINK_IP6 == lt)
751                 {
752                   *next_hdr_ptr = IP_PROTOCOL_IPV6;
753                   tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
754                                           (const ip6_header_t *) payload,
755                                           ip6);
756                 }
757               else if (VNET_LINK_IP4 == lt)
758                 {
759                   *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
760                   tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0],
761                                           (const ip4_header_t *) payload, ip6);
762                 }
763               else if (VNET_LINK_MPLS == lt)
764                 {
765                   *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
766                   tunnel_encap_fixup_mplso6 (
767                     sa0->tunnel_flags, b[0],
768                     (const mpls_unicast_header_t *) payload, ip6);
769                 }
770               else
771                 ASSERT (0);
772
773               len = payload_len_total + hdr_len - len;
774               ip6->payload_length = clib_net_to_host_u16 (len);
775               b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
776             }
777           else
778             {
779               ip4_header_t *ip4;
780               u16 len = sizeof (ip4_header_t);
781               hdr_len += len;
782               ip4 = (ip4_header_t *) (payload - hdr_len);
783               clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
784
785               if (VNET_LINK_IP6 == lt)
786                 {
787                   *next_hdr_ptr = IP_PROTOCOL_IPV6;
788                   tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
789                                                    (const ip6_header_t *)
790                                                    payload, ip4);
791                 }
792               else if (VNET_LINK_IP4 == lt)
793                 {
794                   *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
795                   tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
796                                                    (const ip4_header_t *)
797                                                    payload, ip4);
798                 }
799               else if (VNET_LINK_MPLS == lt)
800                 {
801                   *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
802                   tunnel_encap_fixup_mplso4_w_chksum (
803                     sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
804                     ip4);
805                 }
806               else
807                 ASSERT (0);
808
809               len = payload_len_total + hdr_len;
810               esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
811             }
812
813           dpo = &sa0->dpo;
814           if (!is_tun)
815             {
816               sync_next[0] = dpo->dpoi_next_node;
817               vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
818             }
819           else
820             sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
821           b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
822         }
823       else                      /* transport mode */
824         {
825           u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
826           ip6_ext_header_t *ext_hdr;
827           udp_header_t *udp = 0;
828           u16 udp_len = 0;
829           u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
830
831           ip_len =
832             (VNET_LINK_IP6 == lt ?
833                esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
834                ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
835
836           vlib_buffer_advance (b[0], ip_len);
837           payload = vlib_buffer_get_current (b[0]);
838           next_hdr_ptr = esp_add_footer_and_icv (
839             vm, &lb, esp_align, icv_sz, node, buffer_data_size,
840             vlib_buffer_length_in_chain (vm, b[0]));
841           if (!next_hdr_ptr)
842             {
843               err = ESP_ENCRYPT_ERROR_NO_BUFFERS;
844               esp_set_next_index (b[0], node, err, n_noop, noop_nexts,
845                                   drop_next);
846               goto trace;
847             }
848
849           b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
850           payload_len = b[0]->current_length;
851           payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
852
853           /* ESP header */
854           hdr_len += sizeof (*esp);
855           esp = (esp_header_t *) (payload - hdr_len);
856
857           /* optional UDP header */
858           if (ipsec_sa_is_set_UDP_ENCAP (sa0))
859             {
860               hdr_len += sizeof (udp_header_t);
861               udp = (udp_header_t *) (payload - hdr_len);
862             }
863
864           /* IP header */
865           hdr_len += ip_len;
866           ip_hdr = payload - hdr_len;
867
868           /* L2 header */
869           if (!is_tun)
870             {
871               l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
872               hdr_len += l2_len;
873               l2_hdr = payload - hdr_len;
874
875               /* copy l2 and ip header */
876               clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
877             }
878           else
879             l2_len = 0;
880
881           if (VNET_LINK_IP6 == lt)
882             {
883               ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
884               if (PREDICT_TRUE (NULL == ext_hdr))
885                 {
886                   *next_hdr_ptr = ip6->protocol;
887                   ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
888                 }
889               else
890                 {
891                   *next_hdr_ptr = ext_hdr->next_hdr;
892                   ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
893                 }
894               ip6->payload_length =
895                 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
896                                       sizeof (ip6_header_t));
897             }
898           else if (VNET_LINK_IP4 == lt)
899             {
900               u16 len;
901               ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
902               *next_hdr_ptr = ip4->protocol;
903               len = payload_len_total + hdr_len - l2_len;
904               if (udp)
905                 {
906                   esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
907                   udp_len = len - ip_len;
908                 }
909               else
910                 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
911             }
912
913           clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
914
915           if (udp)
916             {
917               esp_fill_udp_hdr (sa0, udp, udp_len);
918             }
919
920           sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
921         }
922
923       if (lb != b[0])
924         {
925           crypto_ops = &ptd->chained_crypto_ops;
926           integ_ops = &ptd->chained_integ_ops;
927         }
928       else
929         {
930           crypto_ops = &ptd->crypto_ops;
931           integ_ops = &ptd->integ_ops;
932         }
933
934       esp->spi = spi;
935       esp->seq = clib_net_to_host_u32 (sa0->seq);
936
937       if (is_async)
938         {
939           async_op = sa0->crypto_async_enc_op_id;
940
941           /* get a frame for this op if we don't yet have one or it's full
942            */
943           if (NULL == async_frames[async_op] ||
944               vnet_crypto_async_frame_is_full (async_frames[async_op]))
945             {
946               async_frames[async_op] =
947                 vnet_crypto_async_get_frame (vm, async_op);
948               /* Save the frame to the list we'll submit at the end */
949               vec_add1 (ptd->async_frames, async_frames[async_op]);
950             }
951
952           esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0],
953                                    esp, payload, payload_len, iv_sz, icv_sz,
954                                    from[b - bufs], sync_next[0], hdr_len,
955                                    async_next_node, lb);
956         }
957       else
958         esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, payload,
959                              payload_len, iv_sz, icv_sz, n_sync, b, lb,
960                              hdr_len, esp);
961
962       vlib_buffer_advance (b[0], 0LL - hdr_len);
963
964       current_sa_packets += 1;
965       current_sa_bytes += payload_len_total;
966
967     trace:
968       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
969         {
970           esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
971                                                     sizeof (*tr));
972           tr->sa_index = sa_index0;
973           tr->spi = sa0->spi;
974           tr->seq = sa0->seq;
975           tr->sa_seq_hi = sa0->seq_hi;
976           tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
977           tr->crypto_alg = sa0->crypto_alg;
978           tr->integ_alg = sa0->integ_alg;
979         }
980
981       /* next */
982       if (ESP_ENCRYPT_ERROR_RX_PKTS != err)
983         {
984           noop_bi[n_noop] = from[b - bufs];
985           n_noop++;
986           noop_next++;
987         }
988       else if (!is_async)
989         {
990           sync_bi[n_sync] = from[b - bufs];
991           sync_bufs[n_sync] = b[0];
992           n_sync++;
993           sync_next++;
994         }
995       else
996         {
997           n_async++;
998           async_next++;
999         }
1000       n_left -= 1;
1001       b += 1;
1002     }
1003
1004   vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1005                                    current_sa_index, current_sa_packets,
1006                                    current_sa_bytes);
1007   if (n_sync)
1008     {
1009       esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
1010                        drop_next);
1011       esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1012                                sync_nexts, ptd->chunks, drop_next);
1013
1014       esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1015                        drop_next);
1016       esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1017                                sync_nexts, ptd->chunks, drop_next);
1018
1019       vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
1020     }
1021   if (n_async)
1022     {
1023       /* submit all of the open frames */
1024       vnet_crypto_async_frame_t **async_frame;
1025
1026       vec_foreach (async_frame, ptd->async_frames)
1027         {
1028           if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1029             {
1030               n_noop += esp_async_recycle_failed_submit (
1031                 vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR,
1032                 n_sync, noop_bi, noop_nexts, drop_next);
1033               vnet_crypto_async_reset_frame (*async_frame);
1034               vnet_crypto_async_free_frame (vm, *async_frame);
1035             }
1036         }
1037     }
1038   if (n_noop)
1039     vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1040
1041   vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS,
1042                                frame->n_vectors);
1043
1044   return frame->n_vectors;
1045 }
1046
1047 always_inline uword
1048 esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1049                          vlib_frame_t * frame)
1050 {
1051   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1052   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1053   u32 *from = vlib_frame_vector_args (frame);
1054   u32 n_left = frame->n_vectors;
1055
1056   vlib_get_buffers (vm, from, b, n_left);
1057
1058   if (n_left >= 4)
1059     {
1060       vlib_prefetch_buffer_header (b[0], LOAD);
1061       vlib_prefetch_buffer_header (b[1], LOAD);
1062       vlib_prefetch_buffer_header (b[2], LOAD);
1063       vlib_prefetch_buffer_header (b[3], LOAD);
1064     }
1065
1066   while (n_left > 8)
1067     {
1068       vlib_prefetch_buffer_header (b[4], LOAD);
1069       vlib_prefetch_buffer_header (b[5], LOAD);
1070       vlib_prefetch_buffer_header (b[6], LOAD);
1071       vlib_prefetch_buffer_header (b[7], LOAD);
1072
1073       next[0] = (esp_post_data (b[0]))->next_index;
1074       next[1] = (esp_post_data (b[1]))->next_index;
1075       next[2] = (esp_post_data (b[2]))->next_index;
1076       next[3] = (esp_post_data (b[3]))->next_index;
1077
1078       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1079         {
1080           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1081             {
1082               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1083                                                              sizeof (*tr));
1084               tr->next_index = next[0];
1085             }
1086           if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1087             {
1088               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1089                                                              sizeof (*tr));
1090               tr->next_index = next[1];
1091             }
1092           if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1093             {
1094               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1095                                                              sizeof (*tr));
1096               tr->next_index = next[2];
1097             }
1098           if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1099             {
1100               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1101                                                              sizeof (*tr));
1102               tr->next_index = next[3];
1103             }
1104         }
1105
1106       b += 4;
1107       next += 4;
1108       n_left -= 4;
1109     }
1110
1111   while (n_left > 0)
1112     {
1113       next[0] = (esp_post_data (b[0]))->next_index;
1114       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1115         {
1116           esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1117                                                          sizeof (*tr));
1118           tr->next_index = next[0];
1119         }
1120
1121       b += 1;
1122       next += 1;
1123       n_left -= 1;
1124     }
1125
1126   vlib_node_increment_counter (vm, node->node_index,
1127                                ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1128                                frame->n_vectors);
1129   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1130   return frame->n_vectors;
1131 }
1132
1133 VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1134                                   vlib_node_runtime_t * node,
1135                                   vlib_frame_t * from_frame)
1136 {
1137   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
1138                              esp_encrypt_async_next.esp4_post_next);
1139 }
1140
1141 /* *INDENT-OFF* */
1142 VLIB_REGISTER_NODE (esp4_encrypt_node) = {
1143   .name = "esp4-encrypt",
1144   .vector_size = sizeof (u32),
1145   .format_trace = format_esp_encrypt_trace,
1146   .type = VLIB_NODE_TYPE_INTERNAL,
1147
1148   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1149   .error_strings = esp_encrypt_error_strings,
1150
1151   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1152   .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1153                   [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1154                   [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1155                   [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1156                   [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1157                   [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1158                   [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
1159 };
1160 /* *INDENT-ON* */
1161
1162 VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1163                                        vlib_node_runtime_t * node,
1164                                        vlib_frame_t * from_frame)
1165 {
1166   return esp_encrypt_post_inline (vm, node, from_frame);
1167 }
1168
1169 /* *INDENT-OFF* */
1170 VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1171   .name = "esp4-encrypt-post",
1172   .vector_size = sizeof (u32),
1173   .format_trace = format_esp_post_encrypt_trace,
1174   .type = VLIB_NODE_TYPE_INTERNAL,
1175   .sibling_of = "esp4-encrypt",
1176
1177   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1178   .error_strings = esp_encrypt_error_strings,
1179 };
1180 /* *INDENT-ON* */
1181
1182 VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1183                                   vlib_node_runtime_t * node,
1184                                   vlib_frame_t * from_frame)
1185 {
1186   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
1187                              esp_encrypt_async_next.esp6_post_next);
1188 }
1189
1190 /* *INDENT-OFF* */
1191 VLIB_REGISTER_NODE (esp6_encrypt_node) = {
1192   .name = "esp6-encrypt",
1193   .vector_size = sizeof (u32),
1194   .format_trace = format_esp_encrypt_trace,
1195   .type = VLIB_NODE_TYPE_INTERNAL,
1196   .sibling_of = "esp4-encrypt",
1197
1198   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1199   .error_strings = esp_encrypt_error_strings,
1200 };
1201 /* *INDENT-ON* */
1202
1203 VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1204                                        vlib_node_runtime_t * node,
1205                                        vlib_frame_t * from_frame)
1206 {
1207   return esp_encrypt_post_inline (vm, node, from_frame);
1208 }
1209
1210 /* *INDENT-OFF* */
1211 VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1212   .name = "esp6-encrypt-post",
1213   .vector_size = sizeof (u32),
1214   .format_trace = format_esp_post_encrypt_trace,
1215   .type = VLIB_NODE_TYPE_INTERNAL,
1216   .sibling_of = "esp4-encrypt",
1217
1218   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1219   .error_strings = esp_encrypt_error_strings,
1220 };
1221 /* *INDENT-ON* */
1222
1223 VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1224                                       vlib_node_runtime_t * node,
1225                                       vlib_frame_t * from_frame)
1226 {
1227   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
1228                              esp_encrypt_async_next.esp4_tun_post_next);
1229 }
1230
1231 /* *INDENT-OFF* */
1232 VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1233   .name = "esp4-encrypt-tun",
1234   .vector_size = sizeof (u32),
1235   .format_trace = format_esp_encrypt_trace,
1236   .type = VLIB_NODE_TYPE_INTERNAL,
1237
1238   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1239   .error_strings = esp_encrypt_error_strings,
1240
1241   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1242   .next_nodes = {
1243     [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1244     [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1245     [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1246     [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1247     [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1248     [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1249     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1250   },
1251 };
1252
1253 VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1254                                   vlib_node_runtime_t * node,
1255                                   vlib_frame_t * from_frame)
1256 {
1257   return esp_encrypt_post_inline (vm, node, from_frame);
1258 }
1259
1260 /* *INDENT-OFF* */
1261 VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1262   .name = "esp4-encrypt-tun-post",
1263   .vector_size = sizeof (u32),
1264   .format_trace = format_esp_post_encrypt_trace,
1265   .type = VLIB_NODE_TYPE_INTERNAL,
1266   .sibling_of = "esp4-encrypt-tun",
1267
1268   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1269   .error_strings = esp_encrypt_error_strings,
1270 };
1271 /* *INDENT-ON* */
1272
1273 VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1274                                       vlib_node_runtime_t * node,
1275                                       vlib_frame_t * from_frame)
1276 {
1277   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
1278                              esp_encrypt_async_next.esp6_tun_post_next);
1279 }
1280
1281 /* *INDENT-OFF* */
1282 VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1283   .name = "esp6-encrypt-tun",
1284   .vector_size = sizeof (u32),
1285   .format_trace = format_esp_encrypt_trace,
1286   .type = VLIB_NODE_TYPE_INTERNAL,
1287
1288   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1289   .error_strings = esp_encrypt_error_strings,
1290
1291   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1292   .next_nodes = {
1293     [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1294     [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1295     [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1296     [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1297     [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1298     [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1299     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1300   },
1301 };
1302
1303 /* *INDENT-ON* */
1304
1305 VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1306                                            vlib_node_runtime_t * node,
1307                                            vlib_frame_t * from_frame)
1308 {
1309   return esp_encrypt_post_inline (vm, node, from_frame);
1310 }
1311
1312 /* *INDENT-OFF* */
1313 VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1314   .name = "esp6-encrypt-tun-post",
1315   .vector_size = sizeof (u32),
1316   .format_trace = format_esp_post_encrypt_trace,
1317   .type = VLIB_NODE_TYPE_INTERNAL,
1318   .sibling_of = "esp-mpls-encrypt-tun",
1319
1320   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1321   .error_strings = esp_encrypt_error_strings,
1322 };
1323 /* *INDENT-ON* */
1324
1325 VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1326 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1327 {
1328   return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1329                              esp_encrypt_async_next.esp_mpls_tun_post_next);
1330 }
1331
1332 VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1333   .name = "esp-mpls-encrypt-tun",
1334   .vector_size = sizeof (u32),
1335   .format_trace = format_esp_encrypt_trace,
1336   .type = VLIB_NODE_TYPE_INTERNAL,
1337
1338   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1339   .error_strings = esp_encrypt_error_strings,
1340
1341   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1342   .next_nodes = {
1343     [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1344     [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1345     [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1346     [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1347     [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1348     [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1349     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1350   },
1351 };
1352
1353 VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1354 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1355 {
1356   return esp_encrypt_post_inline (vm, node, from_frame);
1357 }
1358
1359 VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1360   .name = "esp-mpls-encrypt-tun-post",
1361   .vector_size = sizeof (u32),
1362   .format_trace = format_esp_post_encrypt_trace,
1363   .type = VLIB_NODE_TYPE_INTERNAL,
1364   .sibling_of = "esp-mpls-encrypt-tun",
1365
1366   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1367   .error_strings = esp_encrypt_error_strings,
1368 };
1369
1370 typedef struct
1371 {
1372   u32 sa_index;
1373 } esp_no_crypto_trace_t;
1374
1375 static u8 *
1376 format_esp_no_crypto_trace (u8 * s, va_list * args)
1377 {
1378   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1379   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1380   esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
1381
1382   s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
1383
1384   return s;
1385 }
1386
1387 enum
1388 {
1389   ESP_NO_CRYPTO_NEXT_DROP,
1390   ESP_NO_CRYPTO_N_NEXT,
1391 };
1392
1393 enum
1394 {
1395   ESP_NO_CRYPTO_ERROR_RX_PKTS,
1396 };
1397
1398 static char *esp_no_crypto_error_strings[] = {
1399   "Outbound ESP packets received",
1400 };
1401
1402 always_inline uword
1403 esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1404                       vlib_frame_t * frame)
1405 {
1406   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1407   u32 *from = vlib_frame_vector_args (frame);
1408   u32 n_left = frame->n_vectors;
1409
1410   vlib_get_buffers (vm, from, b, n_left);
1411
1412   while (n_left > 0)
1413     {
1414       u32 sa_index0;
1415
1416       /* packets are always going to be dropped, but get the sa_index */
1417       sa_index0 = ipsec_tun_protect_get_sa_out
1418         (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
1419
1420       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1421         {
1422           esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
1423                                                       sizeof (*tr));
1424           tr->sa_index = sa_index0;
1425         }
1426
1427       n_left -= 1;
1428       b += 1;
1429     }
1430
1431   vlib_node_increment_counter (vm, node->node_index,
1432                                ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
1433
1434   vlib_buffer_enqueue_to_single_next (vm, node, from,
1435                                       ESP_NO_CRYPTO_NEXT_DROP,
1436                                       frame->n_vectors);
1437
1438   return frame->n_vectors;
1439 }
1440
1441 VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
1442                                         vlib_node_runtime_t * node,
1443                                         vlib_frame_t * from_frame)
1444 {
1445   return esp_no_crypto_inline (vm, node, from_frame);
1446 }
1447
1448 /* *INDENT-OFF* */
1449 VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
1450 {
1451   .name = "esp4-no-crypto",
1452   .vector_size = sizeof (u32),
1453   .format_trace = format_esp_no_crypto_trace,
1454   .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1455   .error_strings = esp_no_crypto_error_strings,
1456   .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1457   .next_nodes = {
1458     [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
1459   },
1460 };
1461
1462 VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
1463                                         vlib_node_runtime_t * node,
1464                                         vlib_frame_t * from_frame)
1465 {
1466   return esp_no_crypto_inline (vm, node, from_frame);
1467 }
1468
1469 /* *INDENT-OFF* */
1470 VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
1471 {
1472   .name = "esp6-no-crypto",
1473   .vector_size = sizeof (u32),
1474   .format_trace = format_esp_no_crypto_trace,
1475   .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1476   .error_strings = esp_no_crypto_error_strings,
1477   .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1478   .next_nodes = {
1479     [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
1480   },
1481 };
1482 /* *INDENT-ON* */
1483
1484 #ifndef CLIB_MARCH_VARIANT
1485
1486 static clib_error_t *
1487 esp_encrypt_init (vlib_main_t *vm)
1488 {
1489   ipsec_main_t *im = &ipsec_main;
1490
1491   im->esp4_enc_fq_index =
1492     vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
1493   im->esp6_enc_fq_index =
1494     vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
1495   im->esp4_enc_tun_fq_index =
1496     vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
1497   im->esp6_enc_tun_fq_index =
1498     vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
1499   im->esp_mpls_enc_tun_fq_index =
1500     vlib_frame_queue_main_init (esp_mpls_encrypt_tun_node.index, 0);
1501
1502   return 0;
1503 }
1504
1505 VLIB_INIT_FUNCTION (esp_encrypt_init);
1506
1507 #endif
1508
1509 /*
1510  * fd.io coding-style-patch-verification: ON
1511  *
1512  * Local Variables:
1513  * eval: (c-set-style "gnu")
1514  * End:
1515  */