ipsec: Targeted unit testing
[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 #include <vnet/udp/udp.h>
22
23 #include <vnet/crypto/crypto.h>
24
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/esp.h>
27
28 #define foreach_esp_encrypt_next                   \
29 _(DROP, "error-drop")                              \
30 _(HANDOFF, "handoff")                              \
31 _(INTERFACE_OUTPUT, "interface-output")
32
33 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
34 typedef enum
35 {
36   foreach_esp_encrypt_next
37 #undef _
38     ESP_ENCRYPT_N_NEXT,
39 } esp_encrypt_next_t;
40
41 #define foreach_esp_encrypt_error                               \
42  _(RX_PKTS, "ESP pkts received")                                \
43  _(SEQ_CYCLED, "sequence number cycled (packet dropped)")       \
44  _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
45  _(CHAINED_BUFFER, "chained buffers (packet dropped)")          \
46  _(NO_TRAILER_SPACE, "no trailer space (packet dropped)")
47
48 typedef enum
49 {
50 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
51   foreach_esp_encrypt_error
52 #undef _
53     ESP_ENCRYPT_N_ERROR,
54 } esp_encrypt_error_t;
55
56 static char *esp_encrypt_error_strings[] = {
57 #define _(sym,string) string,
58   foreach_esp_encrypt_error
59 #undef _
60 };
61
62 typedef struct
63 {
64   u32 sa_index;
65   u32 spi;
66   u32 seq;
67   u32 sa_seq_hi;
68   u8 udp_encap;
69   ipsec_crypto_alg_t crypto_alg;
70   ipsec_integ_alg_t integ_alg;
71 } esp_encrypt_trace_t;
72
73 /* packet trace format function */
74 static u8 *
75 format_esp_encrypt_trace (u8 * s, va_list * args)
76 {
77   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
78   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
79   esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
80
81   s =
82     format (s,
83             "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
84             t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
85             format_ipsec_crypto_alg,
86             t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
87             t->udp_encap ? " udp-encap-enabled" : "");
88   return s;
89 }
90
91 /* pad packet in input buffer */
92 static_always_inline u8 *
93 esp_add_footer_and_icv (vlib_buffer_t * b, u8 block_size, u8 icv_sz,
94                         u16 * next, vlib_node_runtime_t * node,
95                         u16 buffer_data_size)
96 {
97   static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
98     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
99     0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00,
100   };
101
102   u16 min_length = b->current_length + sizeof (esp_footer_t);
103   u16 new_length = round_pow2 (min_length, block_size);
104   u8 pad_bytes = new_length - min_length;
105   esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (b) +
106                                       new_length - sizeof (esp_footer_t));
107
108   if (b->current_data + new_length + icv_sz > buffer_data_size)
109     {
110       b->error = node->errors[ESP_ENCRYPT_ERROR_NO_TRAILER_SPACE];
111       next[0] = ESP_ENCRYPT_NEXT_DROP;
112       return 0;
113     }
114
115   if (pad_bytes)
116     {
117       ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
118       pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
119       clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
120     }
121
122   f->pad_length = pad_bytes;
123   b->current_length = new_length + icv_sz;
124   return &f->next_header;
125 }
126
127 static_always_inline void
128 esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
129 {
130   ip_csum_t sum;
131   u16 old_len;
132
133   len = clib_net_to_host_u16 (len);
134   old_len = ip4->length;
135
136   if (is_transport)
137     {
138       u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
139
140       sum = ip_csum_update (ip4->checksum, ip4->protocol,
141                             prot, ip4_header_t, protocol);
142       ip4->protocol = prot;
143
144       sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
145     }
146   else
147     sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
148
149   ip4->length = len;
150   ip4->checksum = ip_csum_fold (sum);
151 }
152
153 static_always_inline void
154 esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
155 {
156   clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
157   udp->length = clib_net_to_host_u16 (len);
158 }
159
160 static_always_inline u8
161 ext_hdr_is_pre_esp (u8 nexthdr)
162 {
163 #ifdef CLIB_HAVE_VEC128
164   static const u8x16 ext_hdr_types = {
165     IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
166     IP_PROTOCOL_IPV6_ROUTE,
167     IP_PROTOCOL_IPV6_FRAGMENTATION,
168   };
169
170   return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
171 #else
172   return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
173           (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
174           (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0);
175 #endif
176 }
177
178 static_always_inline u8
179 esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
180 {
181   /* this code assumes that HbH, route and frag headers will be before
182      others, if that is not the case, they will end up encrypted */
183   u8 len = sizeof (ip6_header_t);
184   ip6_ext_header_t *p;
185
186   /* if next packet doesn't have ext header */
187   if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
188     {
189       *ext_hdr = NULL;
190       return len;
191     }
192
193   p = (void *) (ip6 + 1);
194   len += ip6_ext_header_len (p);
195
196   while (ext_hdr_is_pre_esp (p->next_hdr))
197     {
198       len += ip6_ext_header_len (p);
199       p = ip6_ext_next_header (p);
200     }
201
202   *ext_hdr = p;
203   return len;
204 }
205
206 static_always_inline void
207 esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
208                  vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
209 {
210   u32 n_fail, n_ops = vec_len (ops);
211   vnet_crypto_op_t *op = ops;
212
213   if (n_ops == 0)
214     return;
215
216   n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
217
218   while (n_fail)
219     {
220       ASSERT (op - ops < n_ops);
221
222       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
223         {
224           u32 bi = op->user_data;
225           b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
226           nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
227           n_fail--;
228         }
229       op++;
230     }
231 }
232
233 typedef struct
234 {
235   u32 salt;
236   u64 iv;
237 } __clib_packed esp_gcm_nonce_t;
238
239 STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12);
240
241 always_inline uword
242 esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
243                     vlib_frame_t * frame, int is_ip6, int is_tun)
244 {
245   ipsec_main_t *im = &ipsec_main;
246   ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
247   u32 *from = vlib_frame_vector_args (frame);
248   u32 n_left = frame->n_vectors;
249   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
250   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
251   esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces;
252   u32 thread_index = vm->thread_index;
253   u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
254   u32 current_sa_index = ~0, current_sa_packets = 0;
255   u32 current_sa_bytes = 0, spi = 0;
256   u8 block_sz = 0, iv_sz = 0, icv_sz = 0;
257   ipsec_sa_t *sa0 = 0;
258
259   vlib_get_buffers (vm, from, b, n_left);
260   vec_reset_length (ptd->crypto_ops);
261   vec_reset_length (ptd->integ_ops);
262
263   while (n_left > 0)
264     {
265       u32 sa_index0;
266       dpo_id_t *dpo;
267       esp_header_t *esp;
268       u8 *payload, *next_hdr_ptr;
269       u16 payload_len;
270       u32 hdr_len, config_index;
271
272       if (n_left > 2)
273         {
274           u8 *p;
275           vlib_prefetch_buffer_header (b[2], LOAD);
276           p = vlib_buffer_get_current (b[1]);
277           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
278           p -= CLIB_CACHE_LINE_BYTES;
279           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
280         }
281
282       if (is_tun)
283         {
284           /* we are on a ipsec tunnel's feature arc */
285           u32 next0;
286           config_index = b[0]->current_config_index;
287           sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
288                                                             sizeof
289                                                             (sa_index0));
290           vnet_buffer (b[0])->ipsec.sad_index = sa_index0;
291           next[0] = next0;
292         }
293       else
294         sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
295
296       if (sa_index0 != current_sa_index)
297         {
298           if (current_sa_packets)
299             vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
300                                              current_sa_index,
301                                              current_sa_packets,
302                                              current_sa_bytes);
303           current_sa_packets = current_sa_bytes = 0;
304
305           sa0 = pool_elt_at_index (im->sad, sa_index0);
306           current_sa_index = sa_index0;
307           spi = clib_net_to_host_u32 (sa0->spi);
308           block_sz = sa0->crypto_block_size;
309           icv_sz = sa0->integ_icv_size;
310           iv_sz = sa0->crypto_iv_size;
311         }
312
313       if (PREDICT_FALSE (~0 == sa0->encrypt_thread_index))
314         {
315           /* this is the first packet to use this SA, claim the SA
316            * for this thread. this could happen simultaneously on
317            * another thread */
318           clib_atomic_cmp_and_swap (&sa0->encrypt_thread_index, ~0,
319                                     ipsec_sa_assign_thread (thread_index));
320         }
321
322       if (PREDICT_TRUE (thread_index != sa0->encrypt_thread_index))
323         {
324           next[0] = ESP_ENCRYPT_NEXT_HANDOFF;
325           if (is_tun)
326             {
327               b[0]->current_config_index = config_index;
328             }
329           goto trace;
330         }
331
332       if (vlib_buffer_chain_linearize (vm, b[0]) != 1)
333         {
334           b[0]->error = node->errors[ESP_ENCRYPT_ERROR_CHAINED_BUFFER];
335           next[0] = ESP_ENCRYPT_NEXT_DROP;
336           goto trace;
337         }
338
339       if (PREDICT_FALSE (esp_seq_advance (sa0)))
340         {
341           b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
342           next[0] = ESP_ENCRYPT_NEXT_DROP;
343           goto trace;
344         }
345
346       /* space for IV */
347       hdr_len = iv_sz;
348
349       if (ipsec_sa_is_set_IS_TUNNEL (sa0))
350         {
351           payload = vlib_buffer_get_current (b[0]);
352           next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz,
353                                                  next, node,
354                                                  buffer_data_size);
355           if (!next_hdr_ptr)
356             goto trace;
357           payload_len = b[0]->current_length;
358
359           /* ESP header */
360           hdr_len += sizeof (*esp);
361           esp = (esp_header_t *) (payload - hdr_len);
362
363           /* optional UDP header */
364           if (ipsec_sa_is_set_UDP_ENCAP (sa0))
365             {
366               hdr_len += sizeof (udp_header_t);
367               esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
368                                 payload_len + hdr_len);
369             }
370
371           /* IP header */
372           if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
373             {
374               ip6_header_t *ip6;
375               u16 len = sizeof (ip6_header_t);
376               hdr_len += len;
377               ip6 = (ip6_header_t *) (payload - hdr_len);
378               clib_memcpy_fast (ip6, &sa0->ip6_hdr, len);
379               *next_hdr_ptr = (is_ip6 ?
380                                IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
381               len = payload_len + hdr_len - len;
382               ip6->payload_length = clib_net_to_host_u16 (len);
383             }
384           else
385             {
386               ip4_header_t *ip4;
387               u16 len = sizeof (ip4_header_t);
388               hdr_len += len;
389               ip4 = (ip4_header_t *) (payload - hdr_len);
390               clib_memcpy_fast (ip4, &sa0->ip4_hdr, len);
391               *next_hdr_ptr = (is_ip6 ?
392                                IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
393               len = payload_len + hdr_len;
394               esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
395             }
396
397           dpo = &sa0->dpo;
398           if (!is_tun)
399             {
400               next[0] = dpo->dpoi_next_node;
401               vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
402             }
403         }
404       else                      /* transport mode */
405         {
406           u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
407           ip6_ext_header_t *ext_hdr;
408           udp_header_t *udp = 0;
409           u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
410
411           ip_len = is_ip6 ?
412             esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
413             ip4_header_bytes ((ip4_header_t *) old_ip_hdr);
414
415           vlib_buffer_advance (b[0], ip_len);
416           payload = vlib_buffer_get_current (b[0]);
417           next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz,
418                                                  next, node,
419                                                  buffer_data_size);
420           if (!next_hdr_ptr)
421             goto trace;
422           payload_len = b[0]->current_length;
423
424           /* ESP header */
425           hdr_len += sizeof (*esp);
426           esp = (esp_header_t *) (payload - hdr_len);
427
428           /* optional UDP header */
429           if (ipsec_sa_is_set_UDP_ENCAP (sa0))
430             {
431               hdr_len += sizeof (udp_header_t);
432               udp = (udp_header_t *) (payload - hdr_len);
433             }
434
435           /* IP header */
436           hdr_len += ip_len;
437           ip_hdr = payload - hdr_len;
438
439           /* L2 header */
440           if (!is_tun)
441             {
442               l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
443               hdr_len += l2_len;
444               l2_hdr = payload - hdr_len;
445
446               /* copy l2 and ip header */
447               clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
448             }
449           else
450             l2_len = 0;
451
452           if (is_ip6)
453             {
454               ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
455               if (PREDICT_TRUE (NULL == ext_hdr))
456                 {
457                   *next_hdr_ptr = ip6->protocol;
458                   ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
459                 }
460               else
461                 {
462                   *next_hdr_ptr = ext_hdr->next_hdr;
463                   ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
464                 }
465               ip6->payload_length =
466                 clib_host_to_net_u16 (payload_len + hdr_len - l2_len -
467                                       sizeof (ip6_header_t));
468             }
469           else
470             {
471               u16 len;
472               ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
473               *next_hdr_ptr = ip4->protocol;
474               len = payload_len + hdr_len - l2_len;
475               if (udp)
476                 {
477                   esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
478                   esp_fill_udp_hdr (sa0, udp, len - ip_len);
479                 }
480               else
481                 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
482             }
483
484           clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
485
486           if (!is_tun)
487             next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
488         }
489
490       esp->spi = spi;
491       esp->seq = clib_net_to_host_u32 (sa0->seq);
492
493       if (sa0->crypto_enc_op_id)
494         {
495           vnet_crypto_op_t *op;
496           vec_add2_aligned (ptd->crypto_ops, op, 1, CLIB_CACHE_LINE_BYTES);
497           vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
498           op->src = op->dst = payload;
499           op->key_index = sa0->crypto_key_index;
500           op->len = payload_len - icv_sz;
501           op->user_data = b - bufs;
502
503           if (ipsec_sa_is_set_IS_AEAD (sa0))
504             {
505               /*
506                * construct the AAD in a scratch space in front
507                * of the IP header.
508                */
509               op->aad = payload - hdr_len - sizeof (esp_aead_t);
510
511               esp_aad_fill (op, esp, sa0);
512
513               op->tag = payload + op->len;
514               op->tag_len = 16;
515
516               u64 *iv = (u64 *) (payload - iv_sz);
517               nonce->salt = sa0->salt;
518               nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++);
519               op->iv = (u8 *) nonce;
520               nonce++;
521             }
522           else
523             {
524               op->iv = payload - iv_sz;
525               op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
526             }
527         }
528
529       if (sa0->integ_op_id)
530         {
531           vnet_crypto_op_t *op;
532           vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
533           vnet_crypto_op_init (op, sa0->integ_op_id);
534           op->src = payload - iv_sz - sizeof (esp_header_t);
535           op->digest = payload + payload_len - icv_sz;
536           op->key_index = sa0->integ_key_index;
537           op->digest_len = icv_sz;
538           op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
539           op->user_data = b - bufs;
540           if (ipsec_sa_is_set_USE_ESN (sa0))
541             {
542               u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
543               clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi));
544               op->len += sizeof (seq_hi);
545             }
546         }
547
548       vlib_buffer_advance (b[0], 0LL - hdr_len);
549
550       current_sa_packets += 1;
551       current_sa_bytes += payload_len;
552
553     trace:
554       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
555         {
556           esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
557                                                     sizeof (*tr));
558           tr->sa_index = sa_index0;
559           tr->spi = sa0->spi;
560           tr->seq = sa0->seq;
561           tr->sa_seq_hi = sa0->seq_hi;
562           tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
563           tr->crypto_alg = sa0->crypto_alg;
564           tr->integ_alg = sa0->integ_alg;
565         }
566       /* next */
567       n_left -= 1;
568       next += 1;
569       b += 1;
570     }
571
572   vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
573                                    current_sa_index, current_sa_packets,
574                                    current_sa_bytes);
575   esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts);
576   esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
577
578   vlib_node_increment_counter (vm, node->node_index,
579                                ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors);
580
581   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
582   return frame->n_vectors;
583 }
584
585 VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
586                                   vlib_node_runtime_t * node,
587                                   vlib_frame_t * from_frame)
588 {
589   return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0);
590 }
591
592 /* *INDENT-OFF* */
593 VLIB_REGISTER_NODE (esp4_encrypt_node) = {
594   .name = "esp4-encrypt",
595   .vector_size = sizeof (u32),
596   .format_trace = format_esp_encrypt_trace,
597   .type = VLIB_NODE_TYPE_INTERNAL,
598
599   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
600   .error_strings = esp_encrypt_error_strings,
601
602   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
603   .next_nodes = {
604     [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
605     [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-handoff",
606     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
607   },
608 };
609 /* *INDENT-ON* */
610
611 VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
612                                   vlib_node_runtime_t * node,
613                                   vlib_frame_t * from_frame)
614 {
615   return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0);
616 }
617
618 /* *INDENT-OFF* */
619 VLIB_REGISTER_NODE (esp6_encrypt_node) = {
620   .name = "esp6-encrypt",
621   .vector_size = sizeof (u32),
622   .format_trace = format_esp_encrypt_trace,
623   .type = VLIB_NODE_TYPE_INTERNAL,
624
625   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
626   .error_strings = esp_encrypt_error_strings,
627
628   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
629   .next_nodes = {
630     [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
631     [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-handoff",
632     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
633   },
634 };
635 /* *INDENT-ON* */
636
637 VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
638                                       vlib_node_runtime_t * node,
639                                       vlib_frame_t * from_frame)
640 {
641   return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1);
642 }
643
644 /* *INDENT-OFF* */
645 VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
646   .name = "esp4-encrypt-tun",
647   .vector_size = sizeof (u32),
648   .format_trace = format_esp_encrypt_trace,
649   .type = VLIB_NODE_TYPE_INTERNAL,
650
651   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
652   .error_strings = esp_encrypt_error_strings,
653
654   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
655   .next_nodes = {
656     [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
657     [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-tun-handoff",
658     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop",
659   },
660 };
661
662 VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) =
663 {
664   .arc_name = "ip4-output",
665   .node_name = "esp4-encrypt-tun",
666   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
667 };
668
669 VNET_FEATURE_INIT (esp6o4_encrypt_tun_feat_node, static) =
670 {
671   .arc_name = "ip6-output",
672   .node_name = "esp4-encrypt-tun",
673   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
674 };
675
676 VNET_FEATURE_INIT (esp4_ethernet_encrypt_tun_feat_node, static) =
677 {
678   .arc_name = "ethernet-output",
679   .node_name = "esp4-encrypt-tun",
680   .runs_before = VNET_FEATURES ("adj-midchain-tx", "adj-midchain-tx-no-count"),
681 };
682 /* *INDENT-ON* */
683
684 VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
685                                       vlib_node_runtime_t * node,
686                                       vlib_frame_t * from_frame)
687 {
688   return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1);
689 }
690
691 /* *INDENT-OFF* */
692 VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
693   .name = "esp6-encrypt-tun",
694   .vector_size = sizeof (u32),
695   .format_trace = format_esp_encrypt_trace,
696   .type = VLIB_NODE_TYPE_INTERNAL,
697
698   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
699   .error_strings = esp_encrypt_error_strings,
700
701   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
702   .next_nodes = {
703     [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
704     [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-tun-handoff",
705     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop",
706   },
707 };
708
709 VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) =
710 {
711   .arc_name = "ip6-output",
712   .node_name = "esp6-encrypt-tun",
713   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
714 };
715
716 VNET_FEATURE_INIT (esp4o6_encrypt_tun_feat_node, static) =
717 {
718   .arc_name = "ip4-output",
719   .node_name = "esp6-encrypt-tun",
720   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
721 };
722
723 /* *INDENT-ON* */
724
725 typedef struct
726 {
727   u32 sa_index;
728 } esp_no_crypto_trace_t;
729
730 static u8 *
731 format_esp_no_crypto_trace (u8 * s, va_list * args)
732 {
733   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
734   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
735   esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
736
737   s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
738
739   return s;
740 }
741
742 enum
743 {
744   ESP_NO_CRYPTO_NEXT_DROP,
745   ESP_NO_CRYPTO_N_NEXT,
746 };
747
748 enum
749 {
750   ESP_NO_CRYPTO_ERROR_RX_PKTS,
751 };
752
753 static char *esp_no_crypto_error_strings[] = {
754   "Outbound ESP packets received",
755 };
756
757 always_inline uword
758 esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
759                       vlib_frame_t * frame)
760 {
761   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
762   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
763   u32 *from = vlib_frame_vector_args (frame);
764   u32 n_left = frame->n_vectors;
765
766   vlib_get_buffers (vm, from, b, n_left);
767
768   while (n_left > 0)
769     {
770       u32 next0;
771       u32 sa_index0;
772
773       /* packets are always going to be dropped, but get the sa_index */
774       sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
775                                                         sizeof (sa_index0));
776
777       next[0] = ESP_NO_CRYPTO_NEXT_DROP;
778
779       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
780         {
781           esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
782                                                       sizeof (*tr));
783           tr->sa_index = sa_index0;
784         }
785
786       n_left -= 1;
787       next += 1;
788       b += 1;
789     }
790
791   vlib_node_increment_counter (vm, node->node_index,
792                                ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
793
794   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
795
796   return frame->n_vectors;
797 }
798
799 VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
800                                         vlib_node_runtime_t * node,
801                                         vlib_frame_t * from_frame)
802 {
803   return esp_no_crypto_inline (vm, node, from_frame);
804 }
805
806 /* *INDENT-OFF* */
807 VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
808 {
809   .name = "esp4-no-crypto",
810   .vector_size = sizeof (u32),
811   .format_trace = format_esp_no_crypto_trace,
812   .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
813   .error_strings = esp_no_crypto_error_strings,
814   .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
815   .next_nodes = {
816     [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
817   },
818 };
819
820 VNET_FEATURE_INIT (esp4_no_crypto_tun_feat_node, static) =
821 {
822   .arc_name = "ip4-output",
823   .node_name = "esp4-no-crypto",
824   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
825 };
826
827 VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
828                                         vlib_node_runtime_t * node,
829                                         vlib_frame_t * from_frame)
830 {
831   return esp_no_crypto_inline (vm, node, from_frame);
832 }
833
834 /* *INDENT-OFF* */
835 VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
836 {
837   .name = "esp6-no-crypto",
838   .vector_size = sizeof (u32),
839   .format_trace = format_esp_no_crypto_trace,
840   .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
841   .error_strings = esp_no_crypto_error_strings,
842   .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
843   .next_nodes = {
844     [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
845   },
846 };
847
848 VNET_FEATURE_INIT (esp6_no_crypto_tun_feat_node, static) =
849 {
850   .arc_name = "ip6-output",
851   .node_name = "esp6-no-crypto",
852   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
853 };
854 /* *INDENT-ON* */
855
856 /*
857  * fd.io coding-style-patch-verification: ON
858  *
859  * Local Variables:
860  * eval: (c-set-style "gnu")
861  * End:
862  */