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>
21 #include <vnet/udp/udp.h>
23 #include <vnet/crypto/crypto.h>
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/esp.h>
28 #define foreach_esp_encrypt_next \
29 _(DROP, "error-drop") \
30 _(HANDOFF, "handoff") \
31 _(INTERFACE_OUTPUT, "interface-output")
33 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
36 foreach_esp_encrypt_next
41 #define foreach_esp_encrypt_error \
42 _(RX_PKTS, "ESP pkts received") \
43 _(SEQ_CYCLED, "sequence number cycled (packet dropped)") \
44 _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
45 _(NO_BUFFERS, "no buffers (packet dropped)") \
46 _(NO_TRAILER_SPACE, "no trailer space (packet dropped)")
50 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
51 foreach_esp_encrypt_error
54 } esp_encrypt_error_t;
56 static char *esp_encrypt_error_strings[] = {
57 #define _(sym,string) string,
58 foreach_esp_encrypt_error
69 ipsec_crypto_alg_t crypto_alg;
70 ipsec_integ_alg_t integ_alg;
71 } esp_encrypt_trace_t;
73 /* packet trace format function */
75 format_esp_encrypt_trace (u8 * s, va_list * args)
77 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
78 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
79 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
83 "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
84 t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
85 format_ipsec_crypto_alg,
86 t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
87 t->udp_encap ? " udp-encap-enabled" : "");
91 /* pad packet in input buffer */
92 static_always_inline u8 *
93 esp_add_footer_and_icv (vlib_buffer_t * b, u8 block_size, u8 icv_sz,
94 u16 * next, vlib_node_runtime_t * node,
95 u16 buffer_data_size, uword total_len)
97 static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
98 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
99 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00,
102 u16 min_length = total_len + sizeof (esp_footer_t);
103 u16 new_length = round_pow2 (min_length, block_size);
104 u8 pad_bytes = new_length - min_length;
105 esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (b) +
106 b->current_length + pad_bytes);
107 u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz;
109 if (b->current_data + tail_sz > buffer_data_size)
111 // TODO alloc new buffer
112 b->error = node->errors[ESP_ENCRYPT_ERROR_NO_TRAILER_SPACE];
113 next[0] = ESP_ENCRYPT_NEXT_DROP;
119 ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
120 pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
121 clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
124 f->pad_length = pad_bytes;
125 b->current_length += tail_sz;
126 return &f->next_header;
129 static_always_inline void
130 esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
135 len = clib_net_to_host_u16 (len);
136 old_len = ip4->length;
140 u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
142 sum = ip_csum_update (ip4->checksum, ip4->protocol,
143 prot, ip4_header_t, protocol);
144 ip4->protocol = prot;
146 sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
149 sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
152 ip4->checksum = ip_csum_fold (sum);
155 static_always_inline void
156 esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
158 clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
159 udp->length = clib_net_to_host_u16 (len);
162 static_always_inline u8
163 ext_hdr_is_pre_esp (u8 nexthdr)
165 #ifdef CLIB_HAVE_VEC128
166 static const u8x16 ext_hdr_types = {
167 IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
168 IP_PROTOCOL_IPV6_ROUTE,
169 IP_PROTOCOL_IPV6_FRAGMENTATION,
172 return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
174 return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
175 (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
176 (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0);
180 static_always_inline u8
181 esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
183 /* this code assumes that HbH, route and frag headers will be before
184 others, if that is not the case, they will end up encrypted */
185 u8 len = sizeof (ip6_header_t);
188 /* if next packet doesn't have ext header */
189 if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
195 p = (void *) (ip6 + 1);
196 len += ip6_ext_header_len (p);
198 while (ext_hdr_is_pre_esp (p->next_hdr))
200 len += ip6_ext_header_len (p);
201 p = ip6_ext_next_header (p);
208 static_always_inline void
209 esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
210 vnet_crypto_op_t * ops, vlib_buffer_t * b[],
211 u16 * nexts, vnet_crypto_op_chunk_t * chunks)
213 u32 n_fail, n_ops = vec_len (ops);
214 vnet_crypto_op_t *op = ops;
219 n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
223 ASSERT (op - ops < n_ops);
225 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
227 u32 bi = op->user_data;
228 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
229 nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
236 static_always_inline void
237 esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
238 vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
240 u32 n_fail, n_ops = vec_len (ops);
241 vnet_crypto_op_t *op = ops;
246 n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
250 ASSERT (op - ops < n_ops);
252 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
254 u32 bi = op->user_data;
255 b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
256 nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
267 } __clib_packed esp_gcm_nonce_t;
269 STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12);
272 esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
273 vlib_frame_t * frame, int is_ip6, int is_tun)
275 ipsec_main_t *im = &ipsec_main;
276 ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
277 u32 *from = vlib_frame_vector_args (frame);
278 u32 n_left = frame->n_vectors;
279 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
280 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
281 esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces;
282 u32 thread_index = vm->thread_index;
283 u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
284 u32 current_sa_index = ~0, current_sa_packets = 0;
285 u32 current_sa_bytes = 0, spi = 0;
286 u8 block_sz = 0, iv_sz = 0, icv_sz = 0;
288 vnet_crypto_op_chunk_t *ch;
290 vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
291 vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
293 vlib_get_buffers (vm, from, b, n_left);
294 vec_reset_length (ptd->crypto_ops);
295 vec_reset_length (ptd->integ_ops);
296 vec_reset_length (ptd->chained_crypto_ops);
297 vec_reset_length (ptd->chained_integ_ops);
298 vec_reset_length (ptd->chunks);
305 u8 *payload, *next_hdr_ptr;
306 u16 payload_len, payload_len_total, n_bufs;
307 u32 hdr_len, config_index;
312 vlib_prefetch_buffer_header (b[2], LOAD);
313 p = vlib_buffer_get_current (b[1]);
314 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
315 p -= CLIB_CACHE_LINE_BYTES;
316 CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
321 /* we are on a ipsec tunnel's feature arc */
323 config_index = b[0]->current_config_index;
324 sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
327 vnet_buffer (b[0])->ipsec.sad_index = sa_index0;
331 sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
333 if (sa_index0 != current_sa_index)
335 if (current_sa_packets)
336 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
340 current_sa_packets = current_sa_bytes = 0;
342 sa0 = pool_elt_at_index (im->sad, sa_index0);
343 current_sa_index = sa_index0;
344 spi = clib_net_to_host_u32 (sa0->spi);
345 block_sz = sa0->crypto_block_size;
346 icv_sz = sa0->integ_icv_size;
347 iv_sz = sa0->crypto_iv_size;
350 if (PREDICT_FALSE (~0 == sa0->encrypt_thread_index))
352 /* this is the first packet to use this SA, claim the SA
353 * for this thread. this could happen simultaneously on
355 clib_atomic_cmp_and_swap (&sa0->encrypt_thread_index, ~0,
356 ipsec_sa_assign_thread (thread_index));
359 if (PREDICT_TRUE (thread_index != sa0->encrypt_thread_index))
361 next[0] = ESP_ENCRYPT_NEXT_HANDOFF;
364 b[0]->current_config_index = config_index;
370 n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
373 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS];
374 next[0] = ESP_ENCRYPT_NEXT_DROP;
380 crypto_ops = &ptd->chained_crypto_ops;
381 integ_ops = &ptd->chained_integ_ops;
383 /* find last buffer in the chain */
384 while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
385 lb = vlib_get_buffer (vm, lb->next_buffer);
389 crypto_ops = &ptd->crypto_ops;
390 integ_ops = &ptd->integ_ops;
393 if (PREDICT_FALSE (esp_seq_advance (sa0)))
395 b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
396 next[0] = ESP_ENCRYPT_NEXT_DROP;
403 if (ipsec_sa_is_set_IS_TUNNEL (sa0))
405 payload = vlib_buffer_get_current (b[0]);
406 next_hdr_ptr = esp_add_footer_and_icv (lb, block_sz, icv_sz,
409 vlib_buffer_length_in_chain
413 b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
414 payload_len = b[0]->current_length;
415 payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
418 hdr_len += sizeof (*esp);
419 esp = (esp_header_t *) (payload - hdr_len);
421 /* optional UDP header */
422 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
424 hdr_len += sizeof (udp_header_t);
425 esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
426 payload_len_total + hdr_len);
430 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
433 u16 len = sizeof (ip6_header_t);
435 ip6 = (ip6_header_t *) (payload - hdr_len);
436 clib_memcpy_fast (ip6, &sa0->ip6_hdr, len);
437 *next_hdr_ptr = (is_ip6 ?
438 IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
439 len = payload_len_total + hdr_len - len;
440 ip6->payload_length = clib_net_to_host_u16 (len);
445 u16 len = sizeof (ip4_header_t);
447 ip4 = (ip4_header_t *) (payload - hdr_len);
448 clib_memcpy_fast (ip4, &sa0->ip4_hdr, len);
449 *next_hdr_ptr = (is_ip6 ?
450 IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
451 len = payload_len_total + hdr_len;
452 esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
458 next[0] = dpo->dpoi_next_node;
459 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
462 else /* transport mode */
464 u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
465 ip6_ext_header_t *ext_hdr;
466 udp_header_t *udp = 0;
467 u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
470 esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
471 ip4_header_bytes ((ip4_header_t *) old_ip_hdr);
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,
478 vlib_buffer_length_in_chain
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]);
488 hdr_len += sizeof (*esp);
489 esp = (esp_header_t *) (payload - hdr_len);
491 /* optional UDP header */
492 if (ipsec_sa_is_set_UDP_ENCAP (sa0))
494 hdr_len += sizeof (udp_header_t);
495 udp = (udp_header_t *) (payload - hdr_len);
500 ip_hdr = payload - hdr_len;
505 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
507 l2_hdr = payload - hdr_len;
509 /* copy l2 and ip header */
510 clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
517 ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
518 if (PREDICT_TRUE (NULL == ext_hdr))
520 *next_hdr_ptr = ip6->protocol;
521 ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
525 *next_hdr_ptr = ext_hdr->next_hdr;
526 ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
528 ip6->payload_length =
529 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
530 sizeof (ip6_header_t));
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;
540 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
541 esp_fill_udp_hdr (sa0, udp, len - ip_len);
544 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
547 clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
550 next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
554 esp->seq = clib_net_to_host_u32 (sa0->seq);
556 if (sa0->crypto_enc_op_id)
558 vnet_crypto_op_t *op;
559 vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
560 vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
562 op->src = op->dst = payload;
563 op->key_index = sa0->crypto_key_index;
564 op->len = payload_len - icv_sz;
565 op->user_data = b - bufs;
567 if (ipsec_sa_is_set_IS_AEAD (sa0))
570 * construct the AAD in a scratch space in front
573 op->aad = payload - hdr_len - sizeof (esp_aead_t);
575 esp_aad_fill (op, esp, sa0);
577 op->tag = payload + op->len;
580 u64 *iv = (u64 *) (payload - iv_sz);
581 nonce->salt = sa0->salt;
582 nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++);
583 op->iv = (u8 *) nonce;
588 op->iv = payload - iv_sz;
589 op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
595 vlib_buffer_t *cb = b[0];
596 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
597 op->chunk_index = vec_len (ptd->chunks);
598 op->tag = vlib_buffer_get_tail (lb) - icv_sz;
599 vec_add2 (ptd->chunks, ch, 1);
600 ch->len = payload_len;
601 ch->src = ch->dst = payload;
602 cb = vlib_get_buffer (vm, cb->next_buffer);
607 vec_add2 (ptd->chunks, ch, 1);
610 ch->len = cb->current_length - icv_sz;
612 ch->len = cb->current_length;
613 ch->src = ch->dst = vlib_buffer_get_current (cb);
615 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
618 cb = vlib_get_buffer (vm, cb->next_buffer);
623 if (sa0->integ_op_id)
625 vnet_crypto_op_t *op;
626 vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
627 vnet_crypto_op_init (op, sa0->integ_op_id);
628 op->src = payload - iv_sz - sizeof (esp_header_t);
629 op->digest = payload + payload_len - icv_sz;
630 op->key_index = sa0->integ_key_index;
631 op->digest_len = icv_sz;
632 op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
633 op->user_data = b - bufs;
638 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
639 vlib_buffer_t *cb = b[0];
640 op->chunk_index = vec_len (ptd->chunks);
641 op->digest = vlib_buffer_get_tail (lb) - icv_sz;
642 vec_add2 (ptd->chunks, ch, 1);
643 ch->len = payload_len + iv_sz + sizeof (esp_header_t);
644 ch->src = payload - iv_sz - sizeof (esp_header_t);
645 cb = vlib_get_buffer (vm, cb->next_buffer);
650 vec_add2 (ptd->chunks, ch, 1);
654 ch->len = cb->current_length - icv_sz;
655 if (ipsec_sa_is_set_USE_ESN (sa0))
657 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
658 clib_memcpy_fast (op->digest, &seq_hi,
660 ch->len += sizeof (seq_hi);
664 ch->len = cb->current_length;
665 ch->src = vlib_buffer_get_current (cb);
667 if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
670 cb = vlib_get_buffer (vm, cb->next_buffer);
673 else if (ipsec_sa_is_set_USE_ESN (sa0))
675 u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
676 clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi));
677 op->len += sizeof (seq_hi);
681 vlib_buffer_advance (b[0], 0LL - hdr_len);
683 current_sa_packets += 1;
684 current_sa_bytes += payload_len_total;
687 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
689 esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
691 tr->sa_index = sa_index0;
694 tr->sa_seq_hi = sa0->seq_hi;
695 tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
696 tr->crypto_alg = sa0->crypto_alg;
697 tr->integ_alg = sa0->integ_alg;
705 vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
706 current_sa_index, current_sa_packets,
709 esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts);
710 esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, bufs, nexts,
713 esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
714 esp_process_chained_ops (vm, node, ptd->chained_integ_ops, bufs, nexts,
717 vlib_node_increment_counter (vm, node->node_index,
718 ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors);
720 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
721 return frame->n_vectors;
724 VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
725 vlib_node_runtime_t * node,
726 vlib_frame_t * from_frame)
728 return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0);
732 VLIB_REGISTER_NODE (esp4_encrypt_node) = {
733 .name = "esp4-encrypt",
734 .vector_size = sizeof (u32),
735 .format_trace = format_esp_encrypt_trace,
736 .type = VLIB_NODE_TYPE_INTERNAL,
738 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
739 .error_strings = esp_encrypt_error_strings,
741 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
743 [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
744 [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-handoff",
745 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
750 VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
751 vlib_node_runtime_t * node,
752 vlib_frame_t * from_frame)
754 return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0);
758 VLIB_REGISTER_NODE (esp6_encrypt_node) = {
759 .name = "esp6-encrypt",
760 .vector_size = sizeof (u32),
761 .format_trace = format_esp_encrypt_trace,
762 .type = VLIB_NODE_TYPE_INTERNAL,
764 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
765 .error_strings = esp_encrypt_error_strings,
767 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
769 [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
770 [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-handoff",
771 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
776 VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
777 vlib_node_runtime_t * node,
778 vlib_frame_t * from_frame)
780 return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1);
784 VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
785 .name = "esp4-encrypt-tun",
786 .vector_size = sizeof (u32),
787 .format_trace = format_esp_encrypt_trace,
788 .type = VLIB_NODE_TYPE_INTERNAL,
790 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
791 .error_strings = esp_encrypt_error_strings,
793 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
795 [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
796 [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-tun-handoff",
797 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop",
801 VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) =
803 .arc_name = "ip4-output",
804 .node_name = "esp4-encrypt-tun",
805 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
808 VNET_FEATURE_INIT (esp6o4_encrypt_tun_feat_node, static) =
810 .arc_name = "ip6-output",
811 .node_name = "esp4-encrypt-tun",
812 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
815 VNET_FEATURE_INIT (esp4_ethernet_encrypt_tun_feat_node, static) =
817 .arc_name = "ethernet-output",
818 .node_name = "esp4-encrypt-tun",
819 .runs_before = VNET_FEATURES ("adj-midchain-tx", "adj-midchain-tx-no-count"),
823 VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
824 vlib_node_runtime_t * node,
825 vlib_frame_t * from_frame)
827 return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1);
831 VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
832 .name = "esp6-encrypt-tun",
833 .vector_size = sizeof (u32),
834 .format_trace = format_esp_encrypt_trace,
835 .type = VLIB_NODE_TYPE_INTERNAL,
837 .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
838 .error_strings = esp_encrypt_error_strings,
840 .n_next_nodes = ESP_ENCRYPT_N_NEXT,
842 [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
843 [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-tun-handoff",
844 [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop",
848 VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) =
850 .arc_name = "ip6-output",
851 .node_name = "esp6-encrypt-tun",
852 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
855 VNET_FEATURE_INIT (esp4o6_encrypt_tun_feat_node, static) =
857 .arc_name = "ip4-output",
858 .node_name = "esp6-encrypt-tun",
859 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
867 } esp_no_crypto_trace_t;
870 format_esp_no_crypto_trace (u8 * s, va_list * args)
872 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
873 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
874 esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
876 s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
883 ESP_NO_CRYPTO_NEXT_DROP,
884 ESP_NO_CRYPTO_N_NEXT,
889 ESP_NO_CRYPTO_ERROR_RX_PKTS,
892 static char *esp_no_crypto_error_strings[] = {
893 "Outbound ESP packets received",
897 esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
898 vlib_frame_t * frame)
900 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
901 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
902 u32 *from = vlib_frame_vector_args (frame);
903 u32 n_left = frame->n_vectors;
905 vlib_get_buffers (vm, from, b, n_left);
912 /* packets are always going to be dropped, but get the sa_index */
913 sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
916 next[0] = ESP_NO_CRYPTO_NEXT_DROP;
918 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
920 esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
922 tr->sa_index = sa_index0;
930 vlib_node_increment_counter (vm, node->node_index,
931 ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
933 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
935 return frame->n_vectors;
938 VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
939 vlib_node_runtime_t * node,
940 vlib_frame_t * from_frame)
942 return esp_no_crypto_inline (vm, node, from_frame);
946 VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
948 .name = "esp4-no-crypto",
949 .vector_size = sizeof (u32),
950 .format_trace = format_esp_no_crypto_trace,
951 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
952 .error_strings = esp_no_crypto_error_strings,
953 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
955 [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
959 VNET_FEATURE_INIT (esp4_no_crypto_tun_feat_node, static) =
961 .arc_name = "ip4-output",
962 .node_name = "esp4-no-crypto",
963 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
966 VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
967 vlib_node_runtime_t * node,
968 vlib_frame_t * from_frame)
970 return esp_no_crypto_inline (vm, node, from_frame);
974 VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
976 .name = "esp6-no-crypto",
977 .vector_size = sizeof (u32),
978 .format_trace = format_esp_no_crypto_trace,
979 .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
980 .error_strings = esp_no_crypto_error_strings,
981 .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
983 [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
987 VNET_FEATURE_INIT (esp6_no_crypto_tun_feat_node, static) =
989 .arc_name = "ip6-output",
990 .node_name = "esp6-no-crypto",
991 .runs_before = VNET_FEATURES ("adj-midchain-tx"),
996 * fd.io coding-style-patch-verification: ON
999 * eval: (c-set-style "gnu")