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