2 * esp_encrypt.c : IPSec ESP encrypt 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.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
22 #include <vnet/crypto/crypto.h>
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/ipsec_tun.h>
26 #include <vnet/ipsec/esp.h>
27 #include <vnet/tunnel/tunnel_dp.h>
29 #define foreach_esp_encrypt_next \
30 _ (DROP4, "ip4-drop") \
31 _ (DROP6, "ip6-drop") \
32 _ (DROP_MPLS, "mpls-drop") \
33 _ (HANDOFF4, "handoff4") \
34 _ (HANDOFF6, "handoff6") \
35 _ (HANDOFF_MPLS, "handoff-mpls") \
36 _ (INTERFACE_OUTPUT, "interface-output")
38 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
41 foreach_esp_encrypt_next
46 #define foreach_esp_encrypt_error \
47 _(RX_PKTS, "ESP pkts received") \
48 _(POST_RX_PKTS, "ESP-post pkts received") \
49 _(SEQ_CYCLED, "sequence number cycled (packet dropped)") \
50 _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
51 _(CRYPTO_QUEUE_FULL, "crypto queue full (packet dropped)") \
52 _(NO_BUFFERS, "no buffers (packet dropped)") \
56 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
57 foreach_esp_encrypt_error
60 } esp_encrypt_error_t;
62 static char *esp_encrypt_error_strings[] = {
63 #define _(sym,string) string,
64 foreach_esp_encrypt_error
75 ipsec_crypto_alg_t crypto_alg;
76 ipsec_integ_alg_t integ_alg;
77 } esp_encrypt_trace_t;
82 } esp_encrypt_post_trace_t;
84 /* packet trace format function */
86 format_esp_encrypt_trace (u8 * s, va_list * args)
88 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
89 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
90 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
94 "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
95 t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
96 format_ipsec_crypto_alg,
97 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
98 t->udp_encap ? " udp-encap-enabled" : "");
103 format_esp_post_encrypt_trace (u8 * s, va_list * args)
105 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
106 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
107 esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *);
109 s = format (s, "esp-post: next node index %u", t->next_index);
113 /* pad packet in input buffer */
114 static_always_inline u8 *
115 esp_add_footer_and_icv (vlib_main_t * vm, vlib_buffer_t ** last,
116 u8 esp_align, u8 icv_sz,
117 u16 * next, vlib_node_runtime_t * node,
118 u16 buffer_data_size, uword total_len)
120 static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
121 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
122 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
125 u16 min_length = total_len + sizeof (esp_footer_t);
126 u16 new_length = round_pow2 (min_length, esp_align);
127 u8 pad_bytes = new_length - min_length;
128 esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (last[0]) +
129 last[0]->current_length + pad_bytes);
130 u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz;
132 if (last[0]->current_length + tail_sz > buffer_data_size)
135 if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
138 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
139 last[0]->next_buffer = tmp_bi;
140 last[0]->flags |= VLIB_BUFFER_NEXT_PRESENT;
141 f = (esp_footer_t *) (vlib_buffer_get_current (tmp) + pad_bytes);
142 tmp->current_length += tail_sz;
146 last[0]->current_length += tail_sz;
148 f->pad_length = pad_bytes;
151 ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
152 pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
153 clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
156 return &f->next_header;
159 static_always_inline void
160 esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
165 len = clib_net_to_host_u16 (len);
166 old_len = ip4->length;
170 u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
172 sum = ip_csum_update (ip4->checksum, ip4->protocol,
173 prot, ip4_header_t, protocol);
174 ip4->protocol = prot;
176 sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
179 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
182 ip4->checksum = ip_csum_fold (sum);
185 static_always_inline void
186 esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
188 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
189 udp->length = clib_net_to_host_u16 (len);
192 static_always_inline u8
193 ext_hdr_is_pre_esp (u8 nexthdr)
195 #ifdef CLIB_HAVE_VEC128
196 static const u8x16 ext_hdr_types = {
197 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
198 IP_PROTOCOL_IPV6_ROUTE,
199 IP_PROTOCOL_IPV6_FRAGMENTATION,
202 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
204 return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
205 (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
206 (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0);
210 static_always_inline u8
211 esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
213 /* this code assumes that HbH, route and frag headers will be before
214 others, if that is not the case, they will end up encrypted */
215 u8 len = sizeof (ip6_header_t);
218 /* if next packet doesn't have ext header */
219 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
225 p = (void *) (ip6 + 1);
226 len += ip6_ext_header_len (p);
228 while (ext_hdr_is_pre_esp (p->next_hdr))
230 len += ip6_ext_header_len (p);
231 p = ip6_ext_next_header (p);
238 static_always_inline void
239 esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
240 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
241 u16 * nexts, vnet_crypto_op_chunk_t * chunks,
244 u32 n_fail, n_ops = vec_len (ops);
245 vnet_crypto_op_t *op = ops;
250 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
254 ASSERT (op - ops < n_ops);
256 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
258 u32 bi = op->user_data;
259 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
260 nexts[bi] = drop_next;
267 static_always_inline void
268 esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
269 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts,
272 u32 n_fail, n_ops = vec_len (ops);
273 vnet_crypto_op_t *op = ops;
278 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
282 ASSERT (op - ops < n_ops);
284 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
286 u32 bi = op->user_data;
287 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
288 nexts[bi] = drop_next;
295 static_always_inline u32
296 esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
297 ipsec_sa_t * sa0, vlib_buffer_t * b,
298 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
299 u32 start_len, u16 * n_ch)
301 vnet_crypto_op_chunk_t *ch;
302 vlib_buffer_t *cb = b;
305 vec_add2 (ptd->chunks, ch, 1);
306 total_len = ch->len = start_len;
307 ch->src = ch->dst = start;
308 cb = vlib_get_buffer (vm, cb->next_buffer);
312 vec_add2 (ptd->chunks, ch, 1);
315 total_len += ch->len = cb->current_length - icv_sz;
317 total_len += ch->len = cb->current_length;
318 ch->src = ch->dst = vlib_buffer_get_current (cb);
320 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
323 cb = vlib_get_buffer (vm, cb->next_buffer);
332 static_always_inline u32
333 esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
334 ipsec_sa_t * sa0, vlib_buffer_t * b,
335 vlib_buffer_t * lb, u8 icv_sz, u8 * start,
336 u32 start_len, u8 * digest, u16 * n_ch)
338 vnet_crypto_op_chunk_t *ch;
339 vlib_buffer_t *cb = b;
342 vec_add2 (ptd->chunks, ch, 1);
343 total_len = ch->len = start_len;
345 cb = vlib_get_buffer (vm, cb->next_buffer);
349 vec_add2 (ptd->chunks, ch, 1);
353 total_len += ch->len = cb->current_length - icv_sz;
354 if (ipsec_sa_is_set_USE_ESN (sa0))
356 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
357 clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
358 ch->len += sizeof (seq_hi);
359 total_len += sizeof (seq_hi);
363 total_len += ch->len = cb->current_length;
364 ch->src = vlib_buffer_get_current (cb);
366 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
369 cb = vlib_get_buffer (vm, cb->next_buffer);
379 esp_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
380 vnet_crypto_op_t **crypto_ops,
381 vnet_crypto_op_t **integ_ops, ipsec_sa_t *sa0,
382 u8 *payload, u16 payload_len, u8 iv_sz, u8 icv_sz,
383 vlib_buffer_t **bufs, vlib_buffer_t **b,
384 vlib_buffer_t *lb, u32 hdr_len, esp_header_t *esp)
386 if (sa0->crypto_enc_op_id)
388 vnet_crypto_op_t *op;
389 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
390 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
392 op->src = op->dst = payload;
393 op->key_index = sa0->crypto_key_index;
394 op->len = payload_len - icv_sz;
395 op->user_data = b - bufs;
397 if (ipsec_sa_is_set_IS_CTR (sa0))
399 ASSERT (sizeof (u64) == iv_sz);
400 /* construct nonce in a scratch space in front of the IP header */
401 esp_ctr_nonce_t *nonce =
402 (esp_ctr_nonce_t *) (payload - sizeof (u64) - hdr_len -
404 u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
406 if (ipsec_sa_is_set_IS_AEAD (sa0))
408 /* constuct aad in a scratch space in front of the nonce */
409 op->aad = (u8 *) nonce - sizeof (esp_aead_t);
410 op->aad_len = esp_aad_fill (op->aad, esp, sa0);
411 op->tag = payload + op->len;
416 nonce->ctr = clib_host_to_net_u32 (1);
419 nonce->salt = sa0->salt;
420 nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa0->ctr_iv_counter++);
421 op->iv = (u8 *) nonce;
425 op->iv = payload - iv_sz;
426 op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
432 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
433 op->chunk_index = vec_len (ptd->chunks);
434 op->tag = vlib_buffer_get_tail (lb) - icv_sz;
435 esp_encrypt_chain_crypto (vm, ptd, sa0, b[0], lb, icv_sz, payload,
436 payload_len, &op->n_chunks);
440 if (sa0->integ_op_id)
442 vnet_crypto_op_t *op;
443 vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
444 vnet_crypto_op_init (op, sa0->integ_op_id);
445 op->src = payload - iv_sz - sizeof (esp_header_t);
446 op->digest = payload + payload_len - icv_sz;
447 op->key_index = sa0->integ_key_index;
448 op->digest_len = icv_sz;
449 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
450 op->user_data = b - bufs;
455 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
456 op->chunk_index = vec_len (ptd->chunks);
457 op->digest = vlib_buffer_get_tail (lb) - icv_sz;
459 esp_encrypt_chain_integ (vm, ptd, sa0, b[0], lb, icv_sz,
460 payload - iv_sz - sizeof (esp_header_t),
461 payload_len + iv_sz +
462 sizeof (esp_header_t), op->digest,
465 else if (ipsec_sa_is_set_USE_ESN (sa0))
467 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
468 clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi));
469 op->len += sizeof (seq_hi);
474 static_always_inline void
475 esp_prepare_async_frame (vlib_main_t *vm, ipsec_per_thread_data_t *ptd,
476 vnet_crypto_async_frame_t *async_frame,
477 ipsec_sa_t *sa, vlib_buffer_t *b, esp_header_t *esp,
478 u8 *payload, u32 payload_len, u8 iv_sz, u8 icv_sz,
479 u32 bi, u16 next, u32 hdr_len, u16 async_next,
482 esp_post_data_t *post = esp_post_data (b);
483 u8 *tag, *iv, *aad = 0;
486 i16 crypto_start_offset, integ_start_offset = 0;
487 u16 crypto_total_len, integ_total_len;
489 post->next_index = next;
492 crypto_start_offset = payload - b->data;
493 crypto_total_len = integ_total_len = payload_len - icv_sz;
494 tag = payload + crypto_total_len;
496 key_index = sa->linked_key_index;
498 if (ipsec_sa_is_set_IS_CTR (sa))
500 ASSERT (sizeof (u64) == iv_sz);
501 /* construct nonce in a scratch space in front of the IP header */
502 esp_ctr_nonce_t *nonce = (esp_ctr_nonce_t *) (payload - sizeof (u64) -
503 hdr_len - sizeof (*nonce));
504 u64 *pkt_iv = (u64 *) (payload - sizeof (u64));
506 if (ipsec_sa_is_set_IS_AEAD (sa))
508 /* constuct aad in a scratch space in front of the nonce */
509 aad = (u8 *) nonce - sizeof (esp_aead_t);
510 esp_aad_fill (aad, esp, sa);
511 key_index = sa->crypto_key_index;
515 nonce->ctr = clib_host_to_net_u32 (1);
518 nonce->salt = sa->salt;
519 nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa->ctr_iv_counter++);
524 iv = payload - iv_sz;
525 flag |= VNET_CRYPTO_OP_FLAG_INIT_IV;
531 flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
532 tag = vlib_buffer_get_tail (lb) - icv_sz;
533 crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb, icv_sz,
534 payload, payload_len, 0);
539 integ_start_offset = crypto_start_offset - iv_sz - sizeof (esp_header_t);
540 integ_total_len += iv_sz + sizeof (esp_header_t);
544 integ_total_len = esp_encrypt_chain_integ (
545 vm, ptd, sa, b, lb, icv_sz,
546 payload - iv_sz - sizeof (esp_header_t),
547 payload_len + iv_sz + sizeof (esp_header_t), tag, 0);
549 else if (ipsec_sa_is_set_USE_ESN (sa))
551 u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
552 clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
553 integ_total_len += sizeof (seq_hi);
557 /* this always succeeds because we know the frame is not full */
558 vnet_crypto_async_add_to_frame (vm, async_frame, key_index, crypto_total_len,
559 integ_total_len - crypto_total_len,
560 crypto_start_offset, integ_start_offset, bi,
561 async_next, iv, tag, aad, flag);
565 esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
566 vlib_frame_t *frame, vnet_link_t lt, int is_tun,
569 ipsec_main_t *im = &ipsec_main;
570 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
571 u32 *from = vlib_frame_vector_args (frame);
572 u32 n_left = frame->n_vectors;
573 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
574 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
575 u32 thread_index = vm->thread_index;
576 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
577 u32 current_sa_index = ~0, current_sa_packets = 0;
578 u32 current_sa_bytes = 0, spi = 0;
579 u8 esp_align = 4, iv_sz = 0, icv_sz = 0;
582 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
583 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
584 vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
585 int is_async = im->async_mode;
586 vnet_crypto_async_op_id_t async_op = ~0;
588 (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 :
589 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 :
590 ESP_ENCRYPT_NEXT_DROP_MPLS));
591 u16 handoff_next = (lt == VNET_LINK_IP6 ?
592 ESP_ENCRYPT_NEXT_HANDOFF6 :
593 (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 :
594 ESP_ENCRYPT_NEXT_HANDOFF_MPLS));
595 u16 n_async_drop = 0;
597 vlib_get_buffers (vm, from, b, n_left);
600 vec_reset_length (ptd->crypto_ops);
601 vec_reset_length (ptd->integ_ops);
602 vec_reset_length (ptd->chained_crypto_ops);
603 vec_reset_length (ptd->chained_integ_ops);
605 vec_reset_length (ptd->async_frames);
606 vec_reset_length (ptd->chunks);
607 clib_memset (async_frames, 0, sizeof (async_frames));
614 u8 *payload, *next_hdr_ptr;
615 u16 payload_len, payload_len_total, n_bufs;
621 vlib_prefetch_buffer_header (b[2], LOAD);
622 p = vlib_buffer_get_current (b[1]);
623 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
624 p -= CLIB_CACHE_LINE_BYTES;
625 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
626 /* speculate that the trailer goes in the first buffer */
627 CLIB_PREFETCH (vlib_buffer_get_tail (b[1]),
628 CLIB_CACHE_LINE_BYTES, LOAD);
633 /* we are on a ipsec tunnel's feature arc */
634 vnet_buffer (b[0])->ipsec.sad_index =
635 sa_index0 = ipsec_tun_protect_get_sa_out
636 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
639 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
641 if (sa_index0 != current_sa_index)
643 if (current_sa_packets)
644 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
648 current_sa_packets = current_sa_bytes = 0;
650 sa0 = ipsec_sa_get (sa_index0);
652 /* fetch the second cacheline ASAP */
653 CLIB_PREFETCH (sa0->cacheline1, CLIB_CACHE_LINE_BYTES, LOAD);
655 current_sa_index = sa_index0;
656 spi = clib_net_to_host_u32 (sa0->spi);
657 esp_align = sa0->esp_block_align;
658 icv_sz = sa0->integ_icv_size;
659 iv_sz = sa0->crypto_iv_size;
664 async_op = sa0->crypto_async_enc_op_id;
666 if (PREDICT_FALSE (async_op == 0))
668 esp_set_next_index (is_async, from, nexts, from[b - bufs],
669 &n_async_drop, drop_next, next);
673 /* get a frame for this op if we don't yet have one or it's full
675 if (NULL == async_frames[async_op] ||
676 vnet_crypto_async_frame_is_full (async_frames[async_op]))
678 async_frames[async_op] =
679 vnet_crypto_async_get_frame (vm, async_op);
680 /* Save the frame to the list we'll submit at the end */
681 vec_add1 (ptd->async_frames, async_frames[async_op]);
685 if (PREDICT_FALSE (~0 == sa0->thread_index))
687 /* this is the first packet to use this SA, claim the SA
688 * for this thread. this could happen simultaneously on
690 clib_atomic_cmp_and_swap (&sa0->thread_index, ~0,
691 ipsec_sa_assign_thread (thread_index));
694 if (PREDICT_FALSE (thread_index != sa0->thread_index))
696 vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index;
697 esp_set_next_index (is_async, from, nexts, from[b - bufs],
698 &n_async_drop, handoff_next, next);
703 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
706 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS];
707 esp_set_next_index (is_async, from, nexts, from[b - bufs],
708 &n_async_drop, drop_next, next);
714 /* find last buffer in the chain */
715 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
716 lb = vlib_get_buffer (vm, lb->next_buffer);
719 if (PREDICT_FALSE (esp_seq_advance (sa0)))
721 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
722 esp_set_next_index (is_async, from, nexts, from[b - bufs],
723 &n_async_drop, drop_next, next);
730 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
732 payload = vlib_buffer_get_current (b[0]);
733 next_hdr_ptr = esp_add_footer_and_icv (vm, &lb, esp_align, icv_sz,
736 vlib_buffer_length_in_chain
740 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS];
741 esp_set_next_index (is_async, from, nexts, from[b - bufs],
742 &n_async_drop, drop_next, next);
745 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
746 payload_len = b[0]->current_length;
747 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
750 hdr_len += sizeof (*esp);
751 esp = (esp_header_t *) (payload - hdr_len);
753 /* optional UDP header */
754 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
756 hdr_len += sizeof (udp_header_t);
757 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
758 payload_len_total + hdr_len);
762 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
765 u16 len = sizeof (ip6_header_t);
767 ip6 = (ip6_header_t *) (payload - hdr_len);
768 clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t));
770 if (VNET_LINK_IP6 == lt)
772 *next_hdr_ptr = IP_PROTOCOL_IPV6;
773 tunnel_encap_fixup_6o6 (sa0->tunnel_flags,
774 (const ip6_header_t *) payload,
777 else if (VNET_LINK_IP4 == lt)
779 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
780 tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0],
781 (const ip4_header_t *) payload, ip6);
783 else if (VNET_LINK_MPLS == lt)
785 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
786 tunnel_encap_fixup_mplso6 (
787 sa0->tunnel_flags, b[0],
788 (const mpls_unicast_header_t *) payload, ip6);
793 len = payload_len_total + hdr_len - len;
794 ip6->payload_length = clib_net_to_host_u16 (len);
795 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
800 u16 len = sizeof (ip4_header_t);
802 ip4 = (ip4_header_t *) (payload - hdr_len);
803 clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t));
805 if (VNET_LINK_IP6 == lt)
807 *next_hdr_ptr = IP_PROTOCOL_IPV6;
808 tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags,
809 (const ip6_header_t *)
812 else if (VNET_LINK_IP4 == lt)
814 *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP;
815 tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags,
816 (const ip4_header_t *)
819 else if (VNET_LINK_MPLS == lt)
821 *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP;
822 tunnel_encap_fixup_mplso4_w_chksum (
823 sa0->tunnel_flags, (const mpls_unicast_header_t *) payload,
829 len = payload_len_total + hdr_len;
830 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
836 next[0] = dpo->dpoi_next_node;
837 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
840 next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
841 b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
843 else /* transport mode */
845 u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
846 ip6_ext_header_t *ext_hdr;
847 udp_header_t *udp = 0;
849 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
852 (VNET_LINK_IP6 == lt ?
853 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
854 ip4_header_bytes ((ip4_header_t *) old_ip_hdr));
856 vlib_buffer_advance (b[0], ip_len);
857 payload = vlib_buffer_get_current (b[0]);
858 next_hdr_ptr = esp_add_footer_and_icv (vm, &lb, esp_align, icv_sz,
861 vlib_buffer_length_in_chain
865 esp_set_next_index (is_async, from, nexts, from[b - bufs],
866 &n_async_drop, drop_next, next);
870 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
871 payload_len = b[0]->current_length;
872 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
875 hdr_len += sizeof (*esp);
876 esp = (esp_header_t *) (payload - hdr_len);
878 /* optional UDP header */
879 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
881 hdr_len += sizeof (udp_header_t);
882 udp = (udp_header_t *) (payload - hdr_len);
887 ip_hdr = payload - hdr_len;
892 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
894 l2_hdr = payload - hdr_len;
896 /* copy l2 and ip header */
897 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
902 if (VNET_LINK_IP6 == lt)
904 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
905 if (PREDICT_TRUE (NULL == ext_hdr))
907 *next_hdr_ptr = ip6->protocol;
908 ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
912 *next_hdr_ptr = ext_hdr->next_hdr;
913 ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
915 ip6->payload_length =
916 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
917 sizeof (ip6_header_t));
919 else if (VNET_LINK_IP4 == lt)
922 ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
923 *next_hdr_ptr = ip4->protocol;
924 len = payload_len_total + hdr_len - l2_len;
927 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
928 udp_len = len - ip_len;
931 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
934 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
938 esp_fill_udp_hdr (sa0, udp, udp_len);
941 next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
946 crypto_ops = &ptd->chained_crypto_ops;
947 integ_ops = &ptd->chained_integ_ops;
951 crypto_ops = &ptd->crypto_ops;
952 integ_ops = &ptd->integ_ops;
956 esp->seq = clib_net_to_host_u32 (sa0->seq);
959 esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0],
960 esp, payload, payload_len, iv_sz, icv_sz,
961 from[b - bufs], next[0], hdr_len, async_next,
964 esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, payload,
965 payload_len, iv_sz, icv_sz, bufs, b, lb,
968 vlib_buffer_advance (b[0], 0LL - hdr_len);
970 current_sa_packets += 1;
971 current_sa_bytes += payload_len_total;
974 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
976 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
978 tr->sa_index = sa_index0;
981 tr->sa_seq_hi = sa0->seq_hi;
982 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
983 tr->crypto_alg = sa0->crypto_alg;
984 tr->integ_alg = sa0->integ_alg;
992 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
993 current_sa_index, current_sa_packets,
997 esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts, drop_next);
998 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, bufs, nexts,
999 ptd->chunks, drop_next);
1001 esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts, drop_next);
1002 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, bufs, nexts,
1003 ptd->chunks, drop_next);
1005 vlib_node_increment_counter (
1006 vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors);
1008 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1012 /* submit all of the open frames */
1013 vnet_crypto_async_frame_t **async_frame;
1015 vec_foreach (async_frame, ptd->async_frames)
1017 if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0)
1019 esp_async_recycle_failed_submit (
1020 *async_frame, b, from, nexts, &n_async_drop, drop_next,
1021 ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR);
1022 vnet_crypto_async_reset_frame (*async_frame);
1023 vnet_crypto_async_free_frame (vm, *async_frame);
1027 vlib_node_increment_counter (vm, node->node_index,
1028 ESP_ENCRYPT_ERROR_RX_PKTS,
1031 vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_async_drop);
1034 return frame->n_vectors;
1038 esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1039 vlib_frame_t * frame)
1041 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1042 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1043 u32 *from = vlib_frame_vector_args (frame);
1044 u32 n_left = frame->n_vectors;
1046 vlib_get_buffers (vm, from, b, n_left);
1050 vlib_prefetch_buffer_header (b[0], LOAD);
1051 vlib_prefetch_buffer_header (b[1], LOAD);
1052 vlib_prefetch_buffer_header (b[2], LOAD);
1053 vlib_prefetch_buffer_header (b[3], LOAD);
1058 vlib_prefetch_buffer_header (b[4], LOAD);
1059 vlib_prefetch_buffer_header (b[5], LOAD);
1060 vlib_prefetch_buffer_header (b[6], LOAD);
1061 vlib_prefetch_buffer_header (b[7], LOAD);
1063 next[0] = (esp_post_data (b[0]))->next_index;
1064 next[1] = (esp_post_data (b[1]))->next_index;
1065 next[2] = (esp_post_data (b[2]))->next_index;
1066 next[3] = (esp_post_data (b[3]))->next_index;
1068 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1070 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
1072 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1074 tr->next_index = next[0];
1076 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
1078 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
1080 tr->next_index = next[1];
1082 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
1084 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1086 tr->next_index = next[2];
1088 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1090 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1092 tr->next_index = next[3];
1103 next[0] = (esp_post_data (b[0]))->next_index;
1104 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1106 esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1108 tr->next_index = next[0];
1116 vlib_node_increment_counter (vm, node->node_index,
1117 ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1119 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1120 return frame->n_vectors;
1123 VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1124 vlib_node_runtime_t * node,
1125 vlib_frame_t * from_frame)
1127 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0,
1128 esp_encrypt_async_next.esp4_post_next);
1132 VLIB_REGISTER_NODE (esp4_encrypt_node) = {
1133 .name = "esp4-encrypt",
1134 .vector_size = sizeof (u32),
1135 .format_trace = format_esp_encrypt_trace,
1136 .type = VLIB_NODE_TYPE_INTERNAL,
1138 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1139 .error_strings = esp_encrypt_error_strings,
1141 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1142 .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1143 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1144 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1145 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff",
1146 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff",
1147 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop",
1148 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" },
1152 VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1153 vlib_node_runtime_t * node,
1154 vlib_frame_t * from_frame)
1156 return esp_encrypt_post_inline (vm, node, from_frame);
1160 VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1161 .name = "esp4-encrypt-post",
1162 .vector_size = sizeof (u32),
1163 .format_trace = format_esp_post_encrypt_trace,
1164 .type = VLIB_NODE_TYPE_INTERNAL,
1165 .sibling_of = "esp4-encrypt",
1167 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1168 .error_strings = esp_encrypt_error_strings,
1172 VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1173 vlib_node_runtime_t * node,
1174 vlib_frame_t * from_frame)
1176 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0,
1177 esp_encrypt_async_next.esp6_post_next);
1181 VLIB_REGISTER_NODE (esp6_encrypt_node) = {
1182 .name = "esp6-encrypt",
1183 .vector_size = sizeof (u32),
1184 .format_trace = format_esp_encrypt_trace,
1185 .type = VLIB_NODE_TYPE_INTERNAL,
1186 .sibling_of = "esp4-encrypt",
1188 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1189 .error_strings = esp_encrypt_error_strings,
1193 VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1194 vlib_node_runtime_t * node,
1195 vlib_frame_t * from_frame)
1197 return esp_encrypt_post_inline (vm, node, from_frame);
1201 VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1202 .name = "esp6-encrypt-post",
1203 .vector_size = sizeof (u32),
1204 .format_trace = format_esp_post_encrypt_trace,
1205 .type = VLIB_NODE_TYPE_INTERNAL,
1206 .sibling_of = "esp4-encrypt",
1208 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1209 .error_strings = esp_encrypt_error_strings,
1213 VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1214 vlib_node_runtime_t * node,
1215 vlib_frame_t * from_frame)
1217 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1,
1218 esp_encrypt_async_next.esp4_tun_post_next);
1222 VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1223 .name = "esp4-encrypt-tun",
1224 .vector_size = sizeof (u32),
1225 .format_trace = format_esp_encrypt_trace,
1226 .type = VLIB_NODE_TYPE_INTERNAL,
1228 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1229 .error_strings = esp_encrypt_error_strings,
1231 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1233 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1234 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1235 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1236 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1237 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1238 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1239 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1243 VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1244 vlib_node_runtime_t * node,
1245 vlib_frame_t * from_frame)
1247 return esp_encrypt_post_inline (vm, node, from_frame);
1251 VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1252 .name = "esp4-encrypt-tun-post",
1253 .vector_size = sizeof (u32),
1254 .format_trace = format_esp_post_encrypt_trace,
1255 .type = VLIB_NODE_TYPE_INTERNAL,
1256 .sibling_of = "esp4-encrypt-tun",
1258 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1259 .error_strings = esp_encrypt_error_strings,
1263 VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1264 vlib_node_runtime_t * node,
1265 vlib_frame_t * from_frame)
1267 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1,
1268 esp_encrypt_async_next.esp6_tun_post_next);
1272 VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1273 .name = "esp6-encrypt-tun",
1274 .vector_size = sizeof (u32),
1275 .format_trace = format_esp_encrypt_trace,
1276 .type = VLIB_NODE_TYPE_INTERNAL,
1278 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1279 .error_strings = esp_encrypt_error_strings,
1281 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1283 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1284 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1285 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1286 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1287 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1288 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1289 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1295 VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1296 vlib_node_runtime_t * node,
1297 vlib_frame_t * from_frame)
1299 return esp_encrypt_post_inline (vm, node, from_frame);
1303 VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1304 .name = "esp6-encrypt-tun-post",
1305 .vector_size = sizeof (u32),
1306 .format_trace = format_esp_post_encrypt_trace,
1307 .type = VLIB_NODE_TYPE_INTERNAL,
1308 .sibling_of = "esp-mpls-encrypt-tun",
1310 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1311 .error_strings = esp_encrypt_error_strings,
1315 VLIB_NODE_FN (esp_mpls_encrypt_tun_node)
1316 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1318 return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1,
1319 esp_encrypt_async_next.esp_mpls_tun_post_next);
1322 VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = {
1323 .name = "esp-mpls-encrypt-tun",
1324 .vector_size = sizeof (u32),
1325 .format_trace = format_esp_encrypt_trace,
1326 .type = VLIB_NODE_TYPE_INTERNAL,
1328 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1329 .error_strings = esp_encrypt_error_strings,
1331 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1333 [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop",
1334 [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop",
1335 [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop",
1336 [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff",
1337 [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff",
1338 [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff",
1339 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
1343 VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node)
1344 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
1346 return esp_encrypt_post_inline (vm, node, from_frame);
1349 VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = {
1350 .name = "esp-mpls-encrypt-tun-post",
1351 .vector_size = sizeof (u32),
1352 .format_trace = format_esp_post_encrypt_trace,
1353 .type = VLIB_NODE_TYPE_INTERNAL,
1354 .sibling_of = "esp-mpls-encrypt-tun",
1356 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
1357 .error_strings = esp_encrypt_error_strings,
1363 } esp_no_crypto_trace_t;
1366 format_esp_no_crypto_trace (u8 * s, va_list * args)
1368 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1369 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1370 esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
1372 s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
1379 ESP_NO_CRYPTO_NEXT_DROP,
1380 ESP_NO_CRYPTO_N_NEXT,
1385 ESP_NO_CRYPTO_ERROR_RX_PKTS,
1388 static char *esp_no_crypto_error_strings[] = {
1389 "Outbound ESP packets received",
1393 esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1394 vlib_frame_t * frame)
1396 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1397 u32 *from = vlib_frame_vector_args (frame);
1398 u32 n_left = frame->n_vectors;
1400 vlib_get_buffers (vm, from, b, n_left);
1406 /* packets are always going to be dropped, but get the sa_index */
1407 sa_index0 = ipsec_tun_protect_get_sa_out
1408 (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
1410 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1412 esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
1414 tr->sa_index = sa_index0;
1421 vlib_node_increment_counter (vm, node->node_index,
1422 ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
1424 vlib_buffer_enqueue_to_single_next (vm, node, from,
1425 ESP_NO_CRYPTO_NEXT_DROP,
1428 return frame->n_vectors;
1431 VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
1432 vlib_node_runtime_t * node,
1433 vlib_frame_t * from_frame)
1435 return esp_no_crypto_inline (vm, node, from_frame);
1439 VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
1441 .name = "esp4-no-crypto",
1442 .vector_size = sizeof (u32),
1443 .format_trace = format_esp_no_crypto_trace,
1444 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1445 .error_strings = esp_no_crypto_error_strings,
1446 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1448 [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
1452 VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
1453 vlib_node_runtime_t * node,
1454 vlib_frame_t * from_frame)
1456 return esp_no_crypto_inline (vm, node, from_frame);
1460 VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
1462 .name = "esp6-no-crypto",
1463 .vector_size = sizeof (u32),
1464 .format_trace = format_esp_no_crypto_trace,
1465 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1466 .error_strings = esp_no_crypto_error_strings,
1467 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1469 [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
1474 #ifndef CLIB_MARCH_VARIANT
1476 static clib_error_t *
1477 esp_encrypt_init (vlib_main_t *vm)
1479 ipsec_main_t *im = &ipsec_main;
1481 im->esp4_enc_fq_index =
1482 vlib_frame_queue_main_init (esp4_encrypt_node.index, 0);
1483 im->esp6_enc_fq_index =
1484 vlib_frame_queue_main_init (esp6_encrypt_node.index, 0);
1485 im->esp4_enc_tun_fq_index =
1486 vlib_frame_queue_main_init (esp4_encrypt_tun_node.index, 0);
1487 im->esp6_enc_tun_fq_index =
1488 vlib_frame_queue_main_init (esp6_encrypt_tun_node.index, 0);
1489 im->esp_mpls_enc_tun_fq_index =
1490 vlib_frame_queue_main_init (esp_mpls_encrypt_tun_node.index, 0);
1495 VLIB_INIT_FUNCTION (esp_encrypt_init);
1500 * fd.io coding-style-patch-verification: ON
1503 * eval: (c-set-style "gnu")