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