2 * esp_decrypt.c : IPSec ESP decrypt node
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vnet/vnet.h>
18 #include <vnet/api_errno.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/l2/l2_input.h>
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <vnet/ipsec/ipsec_io.h>
25 #include <vnet/ipsec/ipsec_tun.h>
27 #include <vnet/gre/packet.h>
29 #define foreach_esp_decrypt_next \
30 _ (DROP, "error-drop") \
31 _ (IP4_INPUT, "ip4-input-no-checksum") \
32 _ (IP6_INPUT, "ip6-input") \
33 _ (L2_INPUT, "l2-input") \
34 _ (MPLS_INPUT, "mpls-input") \
35 _ (HANDOFF, "handoff")
37 #define _(v, s) ESP_DECRYPT_NEXT_##v,
40 foreach_esp_decrypt_next
45 #define foreach_esp_decrypt_post_next \
46 _ (DROP, "error-drop") \
47 _ (IP4_INPUT, "ip4-input-no-checksum") \
48 _ (IP6_INPUT, "ip6-input") \
49 _ (MPLS_INPUT, "mpls-input") \
50 _ (L2_INPUT, "l2-input")
52 #define _(v, s) ESP_DECRYPT_POST_NEXT_##v,
55 foreach_esp_decrypt_post_next
57 ESP_DECRYPT_POST_N_NEXT,
58 } esp_decrypt_post_next_t;
66 ipsec_crypto_alg_t crypto_alg;
67 ipsec_integ_alg_t integ_alg;
68 } esp_decrypt_trace_t;
70 typedef vl_counter_esp_decrypt_enum_t esp_decrypt_error_t;
72 /* The number of byres in the hisequence number */
73 #define N_HI_ESN_BYTES 4
75 /* packet trace format function */
77 format_esp_decrypt_trace (u8 * s, va_list * args)
79 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
80 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
81 esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
84 "esp: crypto %U integrity %U pkt-seq %d sa-seq %u sa-seq-hi %u "
86 format_ipsec_crypto_alg, t->crypto_alg, format_ipsec_integ_alg,
87 t->integ_alg, t->seq, t->sa_seq, t->sa_seq_hi, t->pkt_seq_hi);
91 #define ESP_ENCRYPT_PD_F_FD_TRANSPORT (1 << 2)
93 static_always_inline void
94 esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
95 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
98 vnet_crypto_op_t *op = ops;
99 u32 n_fail, n_ops = vec_len (ops);
104 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
108 ASSERT (op - ops < n_ops);
109 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
111 u32 err, bi = op->user_data;
112 if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC)
115 err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
116 esp_decrypt_set_next_index (b[bi], node, vm->thread_index, err, bi,
117 nexts, ESP_DECRYPT_NEXT_DROP,
118 vnet_buffer (b[bi])->ipsec.sad_index);
125 static_always_inline void
126 esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
127 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
128 u16 * nexts, vnet_crypto_op_chunk_t * chunks, int e)
131 vnet_crypto_op_t *op = ops;
132 u32 n_fail, n_ops = vec_len (ops);
134 if (PREDICT_TRUE (n_ops == 0))
137 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
141 ASSERT (op - ops < n_ops);
142 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
144 u32 err, bi = op->user_data;
145 if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC)
148 err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR;
149 esp_decrypt_set_next_index (b[bi], node, vm->thread_index, err, bi,
150 nexts, ESP_DECRYPT_NEXT_DROP,
151 vnet_buffer (b[bi])->ipsec.sad_index);
159 esp_remove_tail (vlib_main_t * vm, vlib_buffer_t * b, vlib_buffer_t * last,
162 vlib_buffer_t *before_last = b;
165 b->total_length_not_including_first_buffer -= tail;
167 if (last->current_length > tail)
169 last->current_length -= tail;
172 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
174 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
177 b = vlib_get_buffer (vm, b->next_buffer);
179 before_last->current_length -= tail - last->current_length;
180 vlib_buffer_free_one (vm, before_last->next_buffer);
181 before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
185 esp_remove_tail_and_tfc_padding (vlib_main_t *vm, vlib_node_runtime_t *node,
186 const esp_decrypt_packet_data_t *pd,
187 vlib_buffer_t *b, vlib_buffer_t *last,
188 u16 *next, u16 tail, int is_ip6)
190 const u16 total_buffer_length = vlib_buffer_length_in_chain (vm, b);
191 u16 ip_packet_length;
194 const ip6_header_t *ip6 = vlib_buffer_get_current (b);
196 clib_net_to_host_u16 (ip6->payload_length) + sizeof (ip6_header_t);
200 const ip4_header_t *ip4 = vlib_buffer_get_current (b);
201 ip_packet_length = clib_net_to_host_u16 (ip4->length);
203 /* In case of TFC padding, the size of the buffer data needs to be adjusted
204 * to the ip packet length */
205 if (PREDICT_FALSE (total_buffer_length < ip_packet_length + tail))
207 esp_decrypt_set_next_index (b, node, vm->thread_index,
208 ESP_DECRYPT_ERROR_NO_TAIL_SPACE, 0, next,
209 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
212 esp_remove_tail (vm, b, last, total_buffer_length - ip_packet_length);
215 /* ICV is splitted in last two buffers so move it to the last buffer and
216 return pointer to it */
217 static_always_inline u8 *
218 esp_move_icv (vlib_main_t * vm, vlib_buffer_t * first,
219 esp_decrypt_packet_data_t * pd,
220 esp_decrypt_packet_data2_t * pd2, u16 icv_sz, u16 * dif)
222 vlib_buffer_t *before_last, *bp;
223 u16 last_sz = pd2->lb->current_length;
224 u16 first_sz = icv_sz - last_sz;
226 bp = before_last = first;
227 while (bp->flags & VLIB_BUFFER_NEXT_PRESENT)
230 bp = vlib_get_buffer (vm, bp->next_buffer);
233 u8 *lb_curr = vlib_buffer_get_current (pd2->lb);
234 memmove (lb_curr + first_sz, lb_curr, last_sz);
235 clib_memcpy_fast (lb_curr, vlib_buffer_get_tail (before_last) - first_sz,
237 before_last->current_length -= first_sz;
238 if (before_last == first)
239 pd->current_length -= first_sz;
241 first->total_length_not_including_first_buffer -= first_sz;
242 clib_memset (vlib_buffer_get_tail (before_last), 0, first_sz);
245 first->total_length_not_including_first_buffer -= last_sz;
246 pd2->lb = before_last;
247 pd2->icv_removed = 1;
248 pd2->free_buffer_index = before_last->next_buffer;
249 before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
253 static_always_inline u16
254 esp_insert_esn (vlib_main_t *vm, ipsec_sa_t *sa, esp_decrypt_packet_data_t *pd,
255 esp_decrypt_packet_data2_t *pd2, u32 *data_len, u8 **digest,
256 u16 *len, vlib_buffer_t *b, u8 *payload)
258 if (!ipsec_sa_is_set_USE_ESN (sa))
260 /* shift ICV by 4 bytes to insert ESN */
261 u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi);
262 u8 tmp[ESP_MAX_ICV_SIZE];
264 if (pd2->icv_removed)
266 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
267 if (space_left >= N_HI_ESN_BYTES)
269 clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi,
271 *data_len += N_HI_ESN_BYTES;
274 return N_HI_ESN_BYTES;
276 len[0] = b->current_length;
280 clib_memcpy_fast (tmp, payload + len[0], ESP_MAX_ICV_SIZE);
281 clib_memcpy_fast (payload + len[0], &seq_hi, N_HI_ESN_BYTES);
282 clib_memcpy_fast (payload + len[0] + N_HI_ESN_BYTES, tmp,
284 *data_len += N_HI_ESN_BYTES;
285 *digest += N_HI_ESN_BYTES;
287 return N_HI_ESN_BYTES;
290 static_always_inline u8 *
291 esp_move_icv_esn (vlib_main_t * vm, vlib_buffer_t * first,
292 esp_decrypt_packet_data_t * pd,
293 esp_decrypt_packet_data2_t * pd2, u16 icv_sz,
294 ipsec_sa_t * sa, u8 * extra_esn, u32 * len)
297 u8 *digest = esp_move_icv (vm, first, pd, pd2, icv_sz, &dif);
301 if (ipsec_sa_is_set_USE_ESN (sa))
303 u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi);
304 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
306 if (space_left >= N_HI_ESN_BYTES)
308 clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi,
310 *len += N_HI_ESN_BYTES;
314 /* no space for ESN at the tail, use the next buffer
316 ASSERT (pd2->icv_removed);
317 vlib_buffer_t *tmp = vlib_get_buffer (vm, pd2->free_buffer_index);
318 clib_memcpy_fast (vlib_buffer_get_current (tmp) - N_HI_ESN_BYTES,
319 &seq_hi, N_HI_ESN_BYTES);
326 static_always_inline int
327 esp_decrypt_chain_integ (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
328 const esp_decrypt_packet_data_t *pd,
329 esp_decrypt_packet_data2_t *pd2, ipsec_sa_t *sa0,
330 vlib_buffer_t *b, u8 icv_sz, u8 *start_src,
331 u32 start_len, u8 **digest, u16 *n_ch,
332 u32 *integ_total_len)
334 vnet_crypto_op_chunk_t *ch;
335 vlib_buffer_t *cb = vlib_get_buffer (vm, b->next_buffer);
338 vec_add2 (ptd->chunks, ch, 1);
339 total_len = ch->len = start_len;
344 vec_add2 (ptd->chunks, ch, 1);
346 ch->src = vlib_buffer_get_current (cb);
349 if (pd2->icv_removed)
350 ch->len = cb->current_length;
352 ch->len = cb->current_length - icv_sz;
353 if (ipsec_sa_is_set_USE_ESN (sa0))
355 u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi);
356 u8 tmp[ESP_MAX_ICV_SIZE];
358 vlib_buffer_t *tmp_b;
359 u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb);
360 if (space_left < N_HI_ESN_BYTES)
362 if (pd2->icv_removed)
364 /* use pre-data area from the last bufer
365 that was removed from the chain */
366 tmp_b = vlib_get_buffer (vm, pd2->free_buffer_index);
367 esn = tmp_b->data - N_HI_ESN_BYTES;
371 /* no space, need to allocate new buffer */
373 if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
375 tmp_b = vlib_get_buffer (vm, tmp_bi);
377 pd2->free_buffer_index = tmp_bi;
379 clib_memcpy_fast (esn, &seq_hi, N_HI_ESN_BYTES);
381 vec_add2 (ptd->chunks, ch, 1);
384 ch->len = N_HI_ESN_BYTES;
388 if (pd2->icv_removed)
390 clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb),
391 &seq_hi, N_HI_ESN_BYTES);
395 clib_memcpy_fast (tmp, *digest, ESP_MAX_ICV_SIZE);
396 clib_memcpy_fast (*digest, &seq_hi, N_HI_ESN_BYTES);
397 clib_memcpy_fast (*digest + N_HI_ESN_BYTES, tmp,
399 *digest += N_HI_ESN_BYTES;
401 ch->len += N_HI_ESN_BYTES;
404 total_len += ch->len;
408 total_len += ch->len = cb->current_length;
410 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
413 cb = vlib_get_buffer (vm, cb->next_buffer);
419 *integ_total_len = total_len;
424 static_always_inline u32
425 esp_decrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
426 esp_decrypt_packet_data_t * pd,
427 esp_decrypt_packet_data2_t * pd2,
428 ipsec_sa_t * sa0, vlib_buffer_t * b, u8 icv_sz,
429 u8 * start, u32 start_len, u8 ** tag, u16 * n_ch)
431 vnet_crypto_op_chunk_t *ch;
432 vlib_buffer_t *cb = b;
435 vec_add2 (ptd->chunks, ch, 1);
436 total_len = ch->len = start_len;
437 ch->src = ch->dst = start;
438 cb = vlib_get_buffer (vm, cb->next_buffer);
443 vec_add2 (ptd->chunks, ch, 1);
445 ch->src = ch->dst = vlib_buffer_get_current (cb);
448 if (ipsec_sa_is_set_IS_AEAD (sa0))
450 if (pd2->lb->current_length < icv_sz)
453 *tag = esp_move_icv (vm, b, pd, pd2, icv_sz, &dif);
455 /* this chunk does not contain crypto data */
457 /* and fix previous chunk's length as it might have
459 ASSERT (n_chunks > 0);
467 total_len = total_len + pd2->lb->current_length -
469 ch[-1].len = pd2->lb->current_length;
474 *tag = vlib_buffer_get_tail (pd2->lb) - icv_sz;
477 if (pd2->icv_removed)
478 total_len += ch->len = cb->current_length;
480 total_len += ch->len = cb->current_length - icv_sz;
483 total_len += ch->len = cb->current_length;
485 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
488 cb = vlib_get_buffer (vm, cb->next_buffer);
497 static_always_inline esp_decrypt_error_t
498 esp_decrypt_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
499 ipsec_sa_t *sa0, u8 *payload, u16 len, u8 icv_sz,
500 u8 iv_sz, esp_decrypt_packet_data_t *pd,
501 esp_decrypt_packet_data2_t *pd2, vlib_buffer_t *b,
504 vnet_crypto_op_t **crypto_ops;
505 vnet_crypto_op_t **integ_ops;
506 vnet_crypto_op_t _op, *op = &_op;
507 const u8 esp_sz = sizeof (esp_header_t);
509 if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE))
511 vnet_crypto_op_init (op, sa0->integ_op_id);
512 op->key_index = sa0->integ_key_index;
514 op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
515 op->user_data = index;
516 op->digest = payload + len;
517 op->digest_len = icv_sz;
522 /* buffer is chained */
523 integ_ops = &ptd->chained_integ_ops;
525 op->len = pd->current_length;
527 /* special case when ICV is splitted and needs to be reassembled
528 * first -> move it to the last buffer. Also take into account
529 * that ESN needs to be added after encrypted data and may or
530 * may not fit in the tail.*/
531 if (pd2->lb->current_length < icv_sz)
535 esp_move_icv_esn (vm, b, pd, pd2, icv_sz, sa0,
536 &extra_esn, &op->len);
540 /* esn is in the last buffer, that was unlinked from
542 op->len = b->current_length;
548 /* we now have a single buffer of crypto data, adjust
549 * the length (second buffer contains only ICV) */
550 integ_ops = &ptd->integ_ops;
551 len = b->current_length;
557 op->digest = vlib_buffer_get_tail (pd2->lb) - icv_sz;
559 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
560 op->chunk_index = vec_len (ptd->chunks);
561 if (esp_decrypt_chain_integ (vm, ptd, pd, pd2, sa0, b, icv_sz,
562 payload, pd->current_length,
563 &op->digest, &op->n_chunks, 0) < 0)
564 return ESP_DECRYPT_ERROR_NO_BUFFERS;
568 integ_ops = &ptd->integ_ops;
569 esp_insert_esn (vm, sa0, pd, pd2, &op->len, &op->digest, &len, b,
573 vec_add_aligned (*integ_ops, op, 1, CLIB_CACHE_LINE_BYTES);
579 if (sa0->crypto_dec_op_id != VNET_CRYPTO_OP_NONE)
581 vnet_crypto_op_init (op, sa0->crypto_dec_op_id);
582 op->key_index = sa0->crypto_key_index;
585 if (ipsec_sa_is_set_IS_CTR (sa0))
587 /* construct nonce in a scratch space in front of the IP header */
588 esp_ctr_nonce_t *nonce =
589 (esp_ctr_nonce_t *) (payload - esp_sz - pd->hdr_sz -
591 if (ipsec_sa_is_set_IS_AEAD (sa0))
593 /* constuct aad in a scratch space in front of the nonce */
594 esp_header_t *esp0 = (esp_header_t *) (payload - esp_sz);
595 op->aad = (u8 *) nonce - sizeof (esp_aead_t);
596 op->aad_len = esp_aad_fill (op->aad, esp0, sa0, pd->seq_hi);
597 op->tag = payload + len;
599 if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa0)))
601 /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */
608 nonce->ctr = clib_host_to_net_u32 (1);
610 nonce->salt = sa0->salt;
611 ASSERT (sizeof (u64) == iv_sz);
612 nonce->iv = *(u64 *) op->iv;
613 op->iv = (u8 *) nonce;
615 op->src = op->dst = payload += iv_sz;
616 op->len = len - iv_sz;
617 op->user_data = index;
619 if (pd->is_chain && (pd2->lb != b))
621 /* buffer is chained */
622 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
623 op->chunk_index = vec_len (ptd->chunks);
624 esp_decrypt_chain_crypto (vm, ptd, pd, pd2, sa0, b, icv_sz,
625 payload, len - pd->iv_sz + pd->icv_sz,
626 &op->tag, &op->n_chunks);
627 crypto_ops = &ptd->chained_crypto_ops;
631 crypto_ops = &ptd->crypto_ops;
634 vec_add_aligned (*crypto_ops, op, 1, CLIB_CACHE_LINE_BYTES);
637 return ESP_DECRYPT_ERROR_RX_PKTS;
640 static_always_inline esp_decrypt_error_t
641 esp_decrypt_prepare_async_frame (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
642 vnet_crypto_async_frame_t *f, ipsec_sa_t *sa0,
643 u8 *payload, u16 len, u8 icv_sz, u8 iv_sz,
644 esp_decrypt_packet_data_t *pd,
645 esp_decrypt_packet_data2_t *pd2, u32 bi,
646 vlib_buffer_t *b, u16 async_next)
648 const u8 esp_sz = sizeof (esp_header_t);
649 esp_decrypt_packet_data_t *async_pd = &(esp_post_data (b))->decrypt_data;
650 esp_decrypt_packet_data2_t *async_pd2 = esp_post_data2 (b);
651 u8 *tag = payload + len, *iv = payload + esp_sz, *aad = 0;
652 const u32 key_index = sa0->crypto_key_index;
653 u32 crypto_len, integ_len = 0;
654 i16 crypto_start_offset, integ_start_offset = 0;
657 if (!ipsec_sa_is_set_IS_AEAD (sa0))
660 integ_start_offset = payload - b->data;
662 if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE))
663 flags |= VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
667 /* buffer is chained */
668 integ_len = pd->current_length;
670 /* special case when ICV is splitted and needs to be reassembled
671 * first -> move it to the last buffer. Also take into account
672 * that ESN needs to be added after encrypted data and may or
673 * may not fit in the tail.*/
674 if (pd2->lb->current_length < icv_sz)
677 tag = esp_move_icv_esn (vm, b, pd, pd2, icv_sz, sa0,
678 &extra_esn, &integ_len);
682 /* esn is in the last buffer, that was unlinked from
684 integ_len = b->current_length;
690 /* we now have a single buffer of crypto data, adjust
691 * the length (second buffer contains only ICV) */
692 len = b->current_length;
698 tag = vlib_buffer_get_tail (pd2->lb) - icv_sz;
700 flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
701 if (esp_decrypt_chain_integ (vm, ptd, pd, pd2, sa0, b, icv_sz,
702 payload, pd->current_length, &tag, 0,
705 /* allocate buffer failed, will not add to frame and drop */
706 return (ESP_DECRYPT_ERROR_NO_BUFFERS);
710 esp_insert_esn (vm, sa0, pd, pd2, &integ_len, &tag, &len, b, payload);
719 if (ipsec_sa_is_set_IS_CTR (sa0))
721 /* construct nonce in a scratch space in front of the IP header */
722 esp_ctr_nonce_t *nonce =
723 (esp_ctr_nonce_t *) (payload - esp_sz - pd->hdr_sz - sizeof (*nonce));
724 if (ipsec_sa_is_set_IS_AEAD (sa0))
726 /* constuct aad in a scratch space in front of the nonce */
727 esp_header_t *esp0 = (esp_header_t *) (payload - esp_sz);
728 aad = (u8 *) nonce - sizeof (esp_aead_t);
729 esp_aad_fill (aad, esp0, sa0, pd->seq_hi);
731 if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa0)))
733 /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */
740 nonce->ctr = clib_host_to_net_u32 (1);
742 nonce->salt = sa0->salt;
743 ASSERT (sizeof (u64) == iv_sz);
744 nonce->iv = *(u64 *) iv;
748 crypto_start_offset = (payload += iv_sz) - b->data;
749 crypto_len = len - iv_sz;
751 if (pd->is_chain && (pd2->lb != b))
753 /* buffer is chained */
754 flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
756 crypto_len = esp_decrypt_chain_crypto (vm, ptd, pd, pd2, sa0, b, icv_sz,
758 len - pd->iv_sz + pd->icv_sz,
765 /* for AEAD integ_len - crypto_len will be negative, it is ok since it
766 * is ignored by the engine. */
767 vnet_crypto_async_add_to_frame (
768 vm, f, key_index, crypto_len, integ_len - crypto_len, crypto_start_offset,
769 integ_start_offset, bi, async_next, iv, tag, aad, flags);
771 return (ESP_DECRYPT_ERROR_RX_PKTS);
774 static_always_inline void
775 esp_decrypt_post_crypto (vlib_main_t *vm, vlib_node_runtime_t *node,
776 const u16 *next_by_next_header,
777 const esp_decrypt_packet_data_t *pd,
778 const esp_decrypt_packet_data2_t *pd2,
779 vlib_buffer_t *b, u16 *next, int is_ip6, int is_tun,
782 ipsec_sa_t *sa0 = ipsec_sa_get (pd->sa_index);
783 vlib_buffer_t *lb = b;
784 const u8 esp_sz = sizeof (esp_header_t);
785 const u8 tun_flags = IPSEC_SA_FLAG_IS_TUNNEL | IPSEC_SA_FLAG_IS_TUNNEL_V6;
786 u8 pad_length = 0, next_header = 0;
791 * redo the anti-reply check
792 * in this frame say we have sequence numbers, s, s+1, s+1, s+1
793 * and s and s+1 are in the window. When we did the anti-replay
794 * check above we did so against the state of the window (W),
795 * after packet s-1. So each of the packets in the sequence will be
797 * This time s will be cheked against Ws-1, s+1 checked against Ws
798 * (i.e. the window state is updated/advanced)
799 * so this time the successive s+1 packet will be dropped.
800 * This is a consequence of batching the decrypts. If the
801 * check-decrypt-advance process was done for each packet it would
802 * be fine. But we batch the decrypts because it's much more efficient
803 * to do so in SW and if we offload to HW and the process is async.
805 * You're probably thinking, but this means an attacker can send the
806 * above sequence and cause VPP to perform decrypts that will fail,
807 * and that's true. But if the attacker can determine s (a valid
808 * sequence number in the window) which is non-trivial, it can generate
809 * a sequence s, s+1, s+2, s+3, ... s+n and nothing will prevent any
810 * implementation, sequential or batching, from decrypting these.
812 if (PREDICT_FALSE (ipsec_sa_is_set_ANTI_REPLAY_HUGE (sa0)))
814 if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, pd->seq_hi, true,
817 esp_decrypt_set_next_index (b, node, vm->thread_index,
818 ESP_DECRYPT_ERROR_REPLAY, 0, next,
819 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
822 n_lost = ipsec_sa_anti_replay_advance (sa0, vm->thread_index, pd->seq,
827 if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, pd->seq_hi, true,
830 esp_decrypt_set_next_index (b, node, vm->thread_index,
831 ESP_DECRYPT_ERROR_REPLAY, 0, next,
832 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
835 n_lost = ipsec_sa_anti_replay_advance (sa0, vm->thread_index, pd->seq,
839 vlib_prefetch_simple_counter (&ipsec_sa_err_counters[IPSEC_SA_ERROR_LOST],
840 vm->thread_index, pd->sa_index);
845 icv_sz = pd2->icv_removed ? 0 : pd->icv_sz;
846 if (pd2->free_buffer_index)
848 vlib_buffer_free_one (vm, pd2->free_buffer_index);
851 if (lb->current_length < sizeof (esp_footer_t) + icv_sz)
853 /* esp footer is either splitted in two buffers or in the before
856 vlib_buffer_t *before_last = b, *bp = b;
857 while (bp->flags & VLIB_BUFFER_NEXT_PRESENT)
860 bp = vlib_get_buffer (vm, bp->next_buffer);
862 u8 *bt = vlib_buffer_get_tail (before_last);
864 if (lb->current_length == icv_sz)
866 esp_footer_t *f = (esp_footer_t *) (bt - sizeof (*f));
867 pad_length = f->pad_length;
868 next_header = f->next_header;
872 pad_length = (bt - 1)[0];
873 next_header = ((u8 *) vlib_buffer_get_current (lb))[0];
879 (esp_footer_t *) (lb->data + lb->current_data +
880 lb->current_length - sizeof (esp_footer_t) -
882 pad_length = f->pad_length;
883 next_header = f->next_header;
890 (esp_footer_t *) (lb->data + lb->current_data + lb->current_length -
891 sizeof (esp_footer_t) - icv_sz);
892 pad_length = f->pad_length;
893 next_header = f->next_header;
896 u16 adv = pd->iv_sz + esp_sz;
897 u16 tail = sizeof (esp_footer_t) + pad_length + icv_sz;
898 u16 tail_orig = sizeof (esp_footer_t) + pad_length + pd->icv_sz;
900 ~(VNET_BUFFER_F_L4_CHECKSUM_COMPUTED | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
902 if ((pd->flags & tun_flags) == 0 && !is_tun) /* transport mode */
904 u8 udp_sz = (is_ip6 == 0 && pd->flags & IPSEC_SA_FLAG_UDP_ENCAP) ?
905 sizeof (udp_header_t) : 0;
906 u16 ip_hdr_sz = pd->hdr_sz - udp_sz;
907 u8 *old_ip = b->data + pd->current_data - ip_hdr_sz - udp_sz;
908 u8 *ip = old_ip + adv + udp_sz;
910 if (is_ip6 && ip_hdr_sz > 64)
911 memmove (ip, old_ip, ip_hdr_sz);
913 clib_memcpy_le64 (ip, old_ip, ip_hdr_sz);
915 b->current_data = pd->current_data + adv - ip_hdr_sz;
916 b->current_length += ip_hdr_sz - adv;
917 esp_remove_tail (vm, b, lb, tail);
921 ip6_header_t *ip6 = (ip6_header_t *) ip;
922 u16 len = clib_net_to_host_u16 (ip6->payload_length);
923 len -= adv + tail_orig;
924 ip6->payload_length = clib_host_to_net_u16 (len);
925 ip6->protocol = next_header;
926 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
930 ip4_header_t *ip4 = (ip4_header_t *) ip;
931 ip_csum_t sum = ip4->checksum;
932 u16 len = clib_net_to_host_u16 (ip4->length);
933 len = clib_host_to_net_u16 (len - adv - tail_orig - udp_sz);
934 sum = ip_csum_update (sum, ip4->protocol, next_header,
935 ip4_header_t, protocol);
936 sum = ip_csum_update (sum, ip4->length, len, ip4_header_t, length);
937 ip4->checksum = ip_csum_fold (sum);
938 ip4->protocol = next_header;
940 next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
945 if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP))
947 next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
948 b->current_data = pd->current_data + adv;
949 b->current_length = pd->current_length - adv;
950 esp_remove_tail_and_tfc_padding (vm, node, pd, b, lb, next, tail,
953 else if (next_header == IP_PROTOCOL_IPV6)
955 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
956 b->current_data = pd->current_data + adv;
957 b->current_length = pd->current_length - adv;
958 esp_remove_tail_and_tfc_padding (vm, node, pd, b, lb, next, tail,
961 else if (next_header == IP_PROTOCOL_MPLS_IN_IP)
963 next[0] = ESP_DECRYPT_NEXT_MPLS_INPUT;
964 b->current_data = pd->current_data + adv;
965 b->current_length = pd->current_length - adv;
966 esp_remove_tail (vm, b, lb, tail);
968 else if (is_tun && next_header == IP_PROTOCOL_GRE)
972 b->current_data = pd->current_data + adv;
973 b->current_length = pd->current_length - adv - tail;
975 gre = vlib_buffer_get_current (b);
977 vlib_buffer_advance (b, sizeof (*gre));
979 switch (clib_net_to_host_u16 (gre->protocol))
981 case GRE_PROTOCOL_teb:
982 vnet_update_l2_len (b);
983 next[0] = ESP_DECRYPT_NEXT_L2_INPUT;
985 case GRE_PROTOCOL_ip4:
986 next[0] = ESP_DECRYPT_NEXT_IP4_INPUT;
988 case GRE_PROTOCOL_ip6:
989 next[0] = ESP_DECRYPT_NEXT_IP6_INPUT;
992 esp_decrypt_set_next_index (
993 b, node, vm->thread_index, ESP_DECRYPT_ERROR_UNSUP_PAYLOAD, 0,
994 next, ESP_DECRYPT_NEXT_DROP, pd->sa_index);
998 else if ((next[0] = vec_elt (next_by_next_header, next_header)) !=
1001 b->current_data = pd->current_data + adv;
1002 b->current_length = pd->current_length - adv;
1003 esp_remove_tail (vm, b, lb, tail);
1007 esp_decrypt_set_next_index (b, node, vm->thread_index,
1008 ESP_DECRYPT_ERROR_UNSUP_PAYLOAD, 0, next,
1009 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
1015 if (ipsec_sa_is_set_IS_PROTECT (sa0))
1018 * There are two encap possibilities
1019 * 1) the tunnel and ths SA are prodiving encap, i.e. it's
1020 * MAC | SA-IP | TUN-IP | ESP | PAYLOAD
1021 * implying the SA is in tunnel mode (on a tunnel interface)
1022 * 2) only the tunnel provides encap
1023 * MAC | TUN-IP | ESP | PAYLOAD
1024 * implying the SA is in transport mode.
1026 * For 2) we need only strip the tunnel encap and we're good.
1027 * since the tunnel and crypto ecnap (int the tun=protect
1028 * object) are the same and we verified above that these match
1029 * for 1) we need to strip the SA-IP outer headers, to
1030 * reveal the tunnel IP and then check that this matches
1031 * the configured tunnel.
1033 const ipsec_tun_protect_t *itp;
1036 ipsec_tun_protect_get (vnet_buffer (b)->ipsec.protect_index);
1038 if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP))
1040 const ip4_header_t *ip4;
1042 ip4 = vlib_buffer_get_current (b);
1044 if (!ip46_address_is_equal_v4 (&itp->itp_tun.src,
1045 &ip4->dst_address) ||
1046 !ip46_address_is_equal_v4 (&itp->itp_tun.dst,
1049 esp_decrypt_set_next_index (
1050 b, node, vm->thread_index,
1051 ESP_DECRYPT_ERROR_TUN_NO_PROTO, 0, next,
1052 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
1055 else if (next_header == IP_PROTOCOL_IPV6)
1057 const ip6_header_t *ip6;
1059 ip6 = vlib_buffer_get_current (b);
1061 if (!ip46_address_is_equal_v6 (&itp->itp_tun.src,
1062 &ip6->dst_address) ||
1063 !ip46_address_is_equal_v6 (&itp->itp_tun.dst,
1066 esp_decrypt_set_next_index (
1067 b, node, vm->thread_index,
1068 ESP_DECRYPT_ERROR_TUN_NO_PROTO, 0, next,
1069 ESP_DECRYPT_NEXT_DROP, pd->sa_index);
1076 if (PREDICT_FALSE (n_lost))
1077 vlib_increment_simple_counter (&ipsec_sa_err_counters[IPSEC_SA_ERROR_LOST],
1078 vm->thread_index, pd->sa_index, n_lost);
1082 esp_decrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1083 vlib_frame_t *from_frame, int is_ip6, int is_tun,
1084 u16 async_next_node)
1086 ipsec_main_t *im = &ipsec_main;
1087 const u16 *next_by_next_header = im->next_header_registrations;
1088 u32 thread_index = vm->thread_index;
1090 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index);
1091 u32 *from = vlib_frame_vector_args (from_frame);
1092 u32 n_left = from_frame->n_vectors;
1093 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1094 vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
1095 u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0;
1096 u16 noop_nexts[VLIB_FRAME_SIZE], n_noop = 0;
1097 u32 sync_bi[VLIB_FRAME_SIZE];
1098 u32 noop_bi[VLIB_FRAME_SIZE];
1099 esp_decrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data;
1100 esp_decrypt_packet_data2_t pkt_data2[VLIB_FRAME_SIZE], *pd2 = pkt_data2;
1101 esp_decrypt_packet_data_t cpd = { };
1102 u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0;
1103 const u8 esp_sz = sizeof (esp_header_t);
1104 ipsec_sa_t *sa0 = 0;
1105 bool anti_replay_result;
1106 int is_async = im->async_mode;
1107 vnet_crypto_async_op_id_t async_op = ~0;
1108 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
1109 esp_decrypt_error_t err;
1111 vlib_get_buffers (vm, from, b, n_left);
1114 vec_reset_length (ptd->crypto_ops);
1115 vec_reset_length (ptd->integ_ops);
1116 vec_reset_length (ptd->chained_crypto_ops);
1117 vec_reset_length (ptd->chained_integ_ops);
1119 vec_reset_length (ptd->async_frames);
1120 vec_reset_length (ptd->chunks);
1121 clib_memset (sync_nexts, -1, sizeof (sync_nexts));
1122 clib_memset (async_frames, 0, sizeof (async_frames));
1128 err = ESP_DECRYPT_ERROR_RX_PKTS;
1132 vlib_prefetch_buffer_header (b[2], LOAD);
1133 p = vlib_buffer_get_current (b[1]);
1134 clib_prefetch_load (p);
1135 p -= CLIB_CACHE_LINE_BYTES;
1136 clib_prefetch_load (p);
1139 u32 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
1142 err = ESP_DECRYPT_ERROR_NO_BUFFERS;
1143 esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop,
1144 noop_nexts, ESP_DECRYPT_NEXT_DROP,
1145 vnet_buffer (b[0])->ipsec.sad_index);
1149 if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
1151 if (current_sa_pkts)
1152 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1153 current_sa_index, current_sa_pkts,
1155 current_sa_bytes = current_sa_pkts = 0;
1157 current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
1158 vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index,
1160 sa0 = ipsec_sa_get (current_sa_index);
1162 /* fetch the second cacheline ASAP */
1163 clib_prefetch_load (sa0->cacheline1);
1164 cpd.icv_sz = sa0->integ_icv_size;
1165 cpd.iv_sz = sa0->crypto_iv_size;
1166 cpd.flags = sa0->flags;
1167 cpd.sa_index = current_sa_index;
1168 is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0);
1171 if (PREDICT_FALSE ((u16) ~0 == sa0->thread_index))
1173 /* this is the first packet to use this SA, claim the SA
1174 * for this thread. this could happen simultaneously on
1176 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
1177 ipsec_sa_assign_thread (thread_index));
1180 if (PREDICT_FALSE (thread_index != sa0->thread_index))
1182 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
1183 err = ESP_DECRYPT_ERROR_HANDOFF;
1184 esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop,
1185 noop_nexts, ESP_DECRYPT_NEXT_HANDOFF,
1190 /* store packet data for next round for easier prefetch */
1191 pd->sa_data = cpd.sa_data;
1192 pd->current_data = b[0]->current_data;
1193 pd->hdr_sz = pd->current_data - vnet_buffer (b[0])->l3_hdr_offset;
1194 payload = b[0]->data + pd->current_data;
1195 pd->seq = clib_host_to_net_u32 (((esp_header_t *) payload)->seq);
1198 pd2->free_buffer_index = 0;
1199 pd2->icv_removed = 0;
1204 /* find last buffer in the chain */
1205 while (pd2->lb->flags & VLIB_BUFFER_NEXT_PRESENT)
1206 pd2->lb = vlib_get_buffer (vm, pd2->lb->next_buffer);
1209 pd->current_length = b[0]->current_length;
1211 /* anti-reply check */
1212 if (PREDICT_FALSE (ipsec_sa_is_set_ANTI_REPLAY_HUGE (sa0)))
1214 anti_replay_result = ipsec_sa_anti_replay_and_sn_advance (
1215 sa0, pd->seq, ~0, false, &pd->seq_hi, true);
1219 anti_replay_result = ipsec_sa_anti_replay_and_sn_advance (
1220 sa0, pd->seq, ~0, false, &pd->seq_hi, false);
1223 if (anti_replay_result)
1225 err = ESP_DECRYPT_ERROR_REPLAY;
1226 esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop,
1227 noop_nexts, ESP_DECRYPT_NEXT_DROP,
1232 if (pd->current_length < cpd.icv_sz + esp_sz + cpd.iv_sz)
1234 err = ESP_DECRYPT_ERROR_RUNT;
1235 esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop,
1236 noop_nexts, ESP_DECRYPT_NEXT_DROP,
1241 len = pd->current_length - cpd.icv_sz;
1242 current_sa_pkts += 1;
1243 current_sa_bytes += vlib_buffer_length_in_chain (vm, b[0]);
1247 async_op = sa0->crypto_async_dec_op_id;
1249 /* get a frame for this op if we don't yet have one or it's full
1251 if (NULL == async_frames[async_op] ||
1252 vnet_crypto_async_frame_is_full (async_frames[async_op]))
1254 async_frames[async_op] =
1255 vnet_crypto_async_get_frame (vm, async_op);
1256 if (PREDICT_FALSE (!async_frames[async_op]))
1258 err = ESP_DECRYPT_ERROR_NO_AVAIL_FRAME;
1259 esp_decrypt_set_next_index (
1260 b[0], node, thread_index, err, n_noop, noop_nexts,
1261 ESP_DECRYPT_NEXT_DROP, current_sa_index);
1265 /* Save the frame to the list we'll submit at the end */
1266 vec_add1 (ptd->async_frames, async_frames[async_op]);
1269 err = esp_decrypt_prepare_async_frame (
1270 vm, ptd, async_frames[async_op], sa0, payload, len, cpd.icv_sz,
1271 cpd.iv_sz, pd, pd2, from[b - bufs], b[0], async_next_node);
1272 if (ESP_DECRYPT_ERROR_RX_PKTS != err)
1274 esp_decrypt_set_next_index (
1275 b[0], node, thread_index, err, n_noop, noop_nexts,
1276 ESP_DECRYPT_NEXT_DROP, current_sa_index);
1281 err = esp_decrypt_prepare_sync_op (vm, ptd, sa0, payload, len,
1282 cpd.icv_sz, cpd.iv_sz, pd, pd2,
1284 if (err != ESP_DECRYPT_ERROR_RX_PKTS)
1286 esp_decrypt_set_next_index (b[0], node, thread_index, err, 0,
1287 sync_next, ESP_DECRYPT_NEXT_DROP,
1293 if (ESP_DECRYPT_ERROR_RX_PKTS != err)
1295 noop_bi[n_noop] = from[b - bufs];
1300 sync_bi[n_sync] = from[b - bufs];
1301 sync_bufs[n_sync] = b[0];
1312 if (PREDICT_TRUE (~0 != current_sa_index))
1313 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
1314 current_sa_index, current_sa_pkts,
1317 /* submit or free all of the open frames */
1318 vnet_crypto_async_frame_t **async_frame;
1320 vec_foreach (async_frame, ptd->async_frames)
1322 /* free frame and move on if no ops were successfully added */
1323 if (PREDICT_FALSE (!(*async_frame)->n_elts))
1325 vnet_crypto_async_free_frame (vm, *async_frame);
1328 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1330 n_noop += esp_async_recycle_failed_submit (
1331 vm, *async_frame, node, ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR,
1332 IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR, n_noop, noop_bi, noop_nexts,
1333 ESP_DECRYPT_NEXT_DROP, false);
1334 vnet_crypto_async_reset_frame (*async_frame);
1335 vnet_crypto_async_free_frame (vm, *async_frame);
1341 esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts,
1342 ESP_DECRYPT_ERROR_INTEG_ERROR);
1343 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs,
1344 sync_nexts, ptd->chunks,
1345 ESP_DECRYPT_ERROR_INTEG_ERROR);
1347 esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts,
1348 ESP_DECRYPT_ERROR_DECRYPTION_FAILED);
1349 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs,
1350 sync_nexts, ptd->chunks,
1351 ESP_DECRYPT_ERROR_DECRYPTION_FAILED);
1354 /* Post decryption ronud - adjust packet data start and length and next
1358 sync_next = sync_nexts;
1367 void *data = b[1]->data + pd[1].current_data;
1369 /* buffer metadata */
1370 vlib_prefetch_buffer_header (b[1], LOAD);
1373 CLIB_PREFETCH (data + pd[1].current_length - pd[1].icv_sz - 2,
1374 CLIB_CACHE_LINE_BYTES, LOAD);
1376 /* packet headers */
1377 CLIB_PREFETCH (data - CLIB_CACHE_LINE_BYTES,
1378 CLIB_CACHE_LINE_BYTES * 2, LOAD);
1381 /* save the sa_index as GRE_teb post_crypto changes L2 opaque */
1382 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1383 current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
1385 if (sync_next[0] >= ESP_DECRYPT_N_NEXT)
1386 esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, pd2, b[0],
1387 sync_next, is_ip6, is_tun, 0);
1390 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1392 esp_decrypt_trace_t *tr;
1393 tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
1394 sa0 = ipsec_sa_get (current_sa_index);
1395 tr->crypto_alg = sa0->crypto_alg;
1396 tr->integ_alg = sa0->integ_alg;
1398 tr->sa_seq = sa0->seq;
1399 tr->sa_seq_hi = sa0->seq_hi;
1400 tr->pkt_seq_hi = pd->seq_hi;
1411 vlib_node_increment_counter (vm, node->node_index, ESP_DECRYPT_ERROR_RX_PKTS,
1412 from_frame->n_vectors);
1415 vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync);
1418 vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
1420 return (from_frame->n_vectors);
1424 esp_decrypt_post_inline (vlib_main_t * vm,
1425 vlib_node_runtime_t * node,
1426 vlib_frame_t * from_frame, int is_ip6, int is_tun)
1428 const ipsec_main_t *im = &ipsec_main;
1429 const u16 *next_by_next_header = im->next_header_registrations;
1430 u32 *from = vlib_frame_vector_args (from_frame);
1431 u32 n_left = from_frame->n_vectors;
1432 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1433 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1434 vlib_get_buffers (vm, from, b, n_left);
1438 esp_decrypt_packet_data_t *pd = &(esp_post_data (b[0]))->decrypt_data;
1442 vlib_prefetch_buffer_header (b[2], LOAD);
1443 vlib_prefetch_buffer_header (b[1], LOAD);
1447 esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, 0, b[0],
1448 next, is_ip6, is_tun, 1);
1451 esp_decrypt_packet_data2_t *pd2 = esp_post_data2 (b[0]);
1452 esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, pd2,
1453 b[0], next, is_ip6, is_tun, 1);
1457 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1459 ipsec_sa_t *sa0 = ipsec_sa_get (pd->sa_index);
1460 esp_decrypt_trace_t *tr;
1461 esp_decrypt_packet_data_t *async_pd =
1462 &(esp_post_data (b[0]))->decrypt_data;
1463 tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
1464 sa0 = ipsec_sa_get (async_pd->sa_index);
1466 tr->crypto_alg = sa0->crypto_alg;
1467 tr->integ_alg = sa0->integ_alg;
1469 tr->sa_seq = sa0->seq;
1470 tr->sa_seq_hi = sa0->seq_hi;
1478 n_left = from_frame->n_vectors;
1479 vlib_node_increment_counter (vm, node->node_index,
1480 ESP_DECRYPT_ERROR_RX_POST_PKTS, n_left);
1482 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left);
1487 VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
1488 vlib_node_runtime_t * node,
1489 vlib_frame_t * from_frame)
1491 return esp_decrypt_inline (vm, node, from_frame, 0, 0,
1492 esp_decrypt_async_next.esp4_post_next);
1495 VLIB_NODE_FN (esp4_decrypt_post_node) (vlib_main_t * vm,
1496 vlib_node_runtime_t * node,
1497 vlib_frame_t * from_frame)
1499 return esp_decrypt_post_inline (vm, node, from_frame, 0, 0);
1502 VLIB_NODE_FN (esp4_decrypt_tun_node) (vlib_main_t * vm,
1503 vlib_node_runtime_t * node,
1504 vlib_frame_t * from_frame)
1506 return esp_decrypt_inline (vm, node, from_frame, 0, 1,
1507 esp_decrypt_async_next.esp4_tun_post_next);
1510 VLIB_NODE_FN (esp4_decrypt_tun_post_node) (vlib_main_t * vm,
1511 vlib_node_runtime_t * node,
1512 vlib_frame_t * from_frame)
1514 return esp_decrypt_post_inline (vm, node, from_frame, 0, 1);
1517 VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
1518 vlib_node_runtime_t * node,
1519 vlib_frame_t * from_frame)
1521 return esp_decrypt_inline (vm, node, from_frame, 1, 0,
1522 esp_decrypt_async_next.esp6_post_next);
1525 VLIB_NODE_FN (esp6_decrypt_post_node) (vlib_main_t * vm,
1526 vlib_node_runtime_t * node,
1527 vlib_frame_t * from_frame)
1529 return esp_decrypt_post_inline (vm, node, from_frame, 1, 0);
1532 VLIB_NODE_FN (esp6_decrypt_tun_node) (vlib_main_t * vm,
1533 vlib_node_runtime_t * node,
1534 vlib_frame_t * from_frame)
1536 return esp_decrypt_inline (vm, node, from_frame, 1, 1,
1537 esp_decrypt_async_next.esp6_tun_post_next);
1540 VLIB_NODE_FN (esp6_decrypt_tun_post_node) (vlib_main_t * vm,
1541 vlib_node_runtime_t * node,
1542 vlib_frame_t * from_frame)
1544 return esp_decrypt_post_inline (vm, node, from_frame, 1, 1);
1547 VLIB_REGISTER_NODE (esp4_decrypt_node) = {
1548 .name = "esp4-decrypt",
1549 .vector_size = sizeof (u32),
1550 .format_trace = format_esp_decrypt_trace,
1551 .type = VLIB_NODE_TYPE_INTERNAL,
1553 .n_errors = ESP_DECRYPT_N_ERROR,
1554 .error_counters = esp_decrypt_error_counters,
1556 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1558 [ESP_DECRYPT_NEXT_DROP] = "ip4-drop",
1559 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1560 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
1561 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-drop",
1562 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
1563 [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-handoff",
1567 VLIB_REGISTER_NODE (esp4_decrypt_post_node) = {
1568 .name = "esp4-decrypt-post",
1569 .vector_size = sizeof (u32),
1570 .format_trace = format_esp_decrypt_trace,
1571 .type = VLIB_NODE_TYPE_INTERNAL,
1573 .n_errors = ESP_DECRYPT_N_ERROR,
1574 .error_counters = esp_decrypt_error_counters,
1576 .sibling_of = "esp4-decrypt",
1579 VLIB_REGISTER_NODE (esp6_decrypt_node) = {
1580 .name = "esp6-decrypt",
1581 .vector_size = sizeof (u32),
1582 .format_trace = format_esp_decrypt_trace,
1583 .type = VLIB_NODE_TYPE_INTERNAL,
1585 .n_errors = ESP_DECRYPT_N_ERROR,
1586 .error_counters = esp_decrypt_error_counters,
1588 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1590 [ESP_DECRYPT_NEXT_DROP] = "ip6-drop",
1591 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1592 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
1593 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-drop",
1594 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
1595 [ESP_DECRYPT_NEXT_HANDOFF]= "esp6-decrypt-handoff",
1599 VLIB_REGISTER_NODE (esp6_decrypt_post_node) = {
1600 .name = "esp6-decrypt-post",
1601 .vector_size = sizeof (u32),
1602 .format_trace = format_esp_decrypt_trace,
1603 .type = VLIB_NODE_TYPE_INTERNAL,
1605 .n_errors = ESP_DECRYPT_N_ERROR,
1606 .error_counters = esp_decrypt_error_counters,
1608 .sibling_of = "esp6-decrypt",
1611 VLIB_REGISTER_NODE (esp4_decrypt_tun_node) = {
1612 .name = "esp4-decrypt-tun",
1613 .vector_size = sizeof (u32),
1614 .format_trace = format_esp_decrypt_trace,
1615 .type = VLIB_NODE_TYPE_INTERNAL,
1616 .n_errors = ESP_DECRYPT_N_ERROR,
1617 .error_counters = esp_decrypt_error_counters,
1618 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1620 [ESP_DECRYPT_NEXT_DROP] = "ip4-drop",
1621 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1622 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
1623 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-input",
1624 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
1625 [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-tun-handoff",
1629 VLIB_REGISTER_NODE (esp4_decrypt_tun_post_node) = {
1630 .name = "esp4-decrypt-tun-post",
1631 .vector_size = sizeof (u32),
1632 .format_trace = format_esp_decrypt_trace,
1633 .type = VLIB_NODE_TYPE_INTERNAL,
1635 .n_errors = ESP_DECRYPT_N_ERROR,
1636 .error_counters = esp_decrypt_error_counters,
1638 .sibling_of = "esp4-decrypt-tun",
1641 VLIB_REGISTER_NODE (esp6_decrypt_tun_node) = {
1642 .name = "esp6-decrypt-tun",
1643 .vector_size = sizeof (u32),
1644 .format_trace = format_esp_decrypt_trace,
1645 .type = VLIB_NODE_TYPE_INTERNAL,
1646 .n_errors = ESP_DECRYPT_N_ERROR,
1647 .error_counters = esp_decrypt_error_counters,
1648 .n_next_nodes = ESP_DECRYPT_N_NEXT,
1650 [ESP_DECRYPT_NEXT_DROP] = "ip6-drop",
1651 [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
1652 [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input",
1653 [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-input",
1654 [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input",
1655 [ESP_DECRYPT_NEXT_HANDOFF]= "esp6-decrypt-tun-handoff",
1659 VLIB_REGISTER_NODE (esp6_decrypt_tun_post_node) = {
1660 .name = "esp6-decrypt-tun-post",
1661 .vector_size = sizeof (u32),
1662 .format_trace = format_esp_decrypt_trace,
1663 .type = VLIB_NODE_TYPE_INTERNAL,
1665 .n_errors = ESP_DECRYPT_N_ERROR,
1666 .error_counters = esp_decrypt_error_counters,
1668 .sibling_of = "esp6-decrypt-tun",
1671 #ifndef CLIB_MARCH_VARIANT
1673 static clib_error_t *
1674 esp_decrypt_init (vlib_main_t *vm)
1676 ipsec_main_t *im = &ipsec_main;
1678 im->esp4_dec_fq_index =
1679 vlib_frame_queue_main_init (esp4_decrypt_node.index, 0);
1680 im->esp6_dec_fq_index =
1681 vlib_frame_queue_main_init (esp6_decrypt_node.index, 0);
1682 im->esp4_dec_tun_fq_index =
1683 vlib_frame_queue_main_init (esp4_decrypt_tun_node.index, 0);
1684 im->esp6_dec_tun_fq_index =
1685 vlib_frame_queue_main_init (esp6_decrypt_tun_node.index, 0);
1690 VLIB_INIT_FUNCTION (esp_decrypt_init);
1695 * fd.io coding-style-patch-verification: ON
1698 * eval: (c-set-style "gnu")