2 * decap.c : IPSec tunnel decapsulation
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/feature/feature.h>
22 #include <vnet/ipsec/ipsec_spd_fp_lookup.h>
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/esp.h>
26 #include <vnet/ipsec/ah.h>
27 #include <vnet/ipsec/ipsec_io.h>
29 #define foreach_ipsec_input_error \
30 _(RX_PKTS, "IPSec pkts received") \
31 _(RX_POLICY_MATCH, "IPSec policy match") \
32 _(RX_POLICY_NO_MATCH, "IPSec policy not matched") \
33 _(RX_POLICY_BYPASS, "IPSec policy bypass") \
34 _(RX_POLICY_DISCARD, "IPSec policy discard")
38 #define _(sym,str) IPSEC_INPUT_ERROR_##sym,
39 foreach_ipsec_input_error
42 } ipsec_input_error_t;
44 static char *ipsec_input_error_strings[] = {
45 #define _(sym,string) string,
46 foreach_ipsec_input_error
59 } ipsec_input_trace_t;
61 /* packet trace format function */
63 format_ipsec_input_trace (u8 * s, va_list * args)
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67 ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
70 format (s, "%U: sa_id %u type: %u spd %u policy %d spi %u (0x%08x) seq %u",
71 format_ip_protocol, t->proto, t->sa_id, t->policy_type, t->spd,
72 t->policy_index, t->spi, t->spi, t->seq);
78 ipsec4_input_spd_add_flow_cache_entry (ipsec_main_t *im, u32 sa, u32 da,
79 ipsec_spd_policy_type_t policy_type,
83 u8 is_overwrite = 0, is_stale_overwrite = 0;
84 /* Store in network byte order to avoid conversion on lookup */
85 ipsec4_inbound_spd_tuple_t ip4_tuple = {
86 .ip4_src_addr = (ip4_address_t) clib_host_to_net_u32 (sa),
87 .ip4_dest_addr = (ip4_address_t) clib_host_to_net_u32 (da),
88 .policy_type = policy_type
91 ip4_tuple.kv_16_8.value =
92 (((u64) pol_id) << 32) | ((u64) im->input_epoch_count);
94 hash = ipsec4_hash_16_8 (&ip4_tuple.kv_16_8);
95 hash &= (im->ipsec4_in_spd_hash_num_buckets - 1);
97 ipsec_spinlock_lock (&im->ipsec4_in_spd_hash_tbl[hash].bucket_lock);
98 /* Check if we are overwriting an existing entry so we know
99 whether to increment the flow cache counter. Since flow
100 cache counter is reset on any policy add/remove, but
101 hash table values are not, we need to check if the entry
102 we are overwriting is stale or not. If it's a stale entry
103 overwrite, we still want to increment flow cache counter */
104 is_overwrite = (im->ipsec4_in_spd_hash_tbl[hash].value != 0);
105 /* Check if we are overwriting a stale entry by comparing
106 with current epoch count */
107 if (PREDICT_FALSE (is_overwrite))
109 (im->input_epoch_count !=
110 ((u32) (im->ipsec4_in_spd_hash_tbl[hash].value & 0xFFFFFFFF)));
111 clib_memcpy_fast (&im->ipsec4_in_spd_hash_tbl[hash], &ip4_tuple.kv_16_8,
112 sizeof (ip4_tuple.kv_16_8));
113 ipsec_spinlock_unlock (&im->ipsec4_in_spd_hash_tbl[hash].bucket_lock);
115 /* Increment the counter to track active flow cache entries
116 when entering a fresh entry or overwriting a stale one */
117 if (!is_overwrite || is_stale_overwrite)
118 clib_atomic_fetch_add_relax (&im->ipsec4_in_spd_flow_cache_entries, 1);
123 always_inline ipsec_policy_t *
124 ipsec4_input_spd_find_flow_cache_entry (ipsec_main_t *im, u32 sa, u32 da,
125 ipsec_spd_policy_type_t policy_type)
127 ipsec_policy_t *p = NULL;
128 ipsec4_hash_kv_16_8_t kv_result;
130 ipsec4_inbound_spd_tuple_t ip4_tuple = { .ip4_src_addr = (ip4_address_t) sa,
131 .ip4_dest_addr = (ip4_address_t) da,
132 .policy_type = policy_type };
134 hash = ipsec4_hash_16_8 (&ip4_tuple.kv_16_8);
135 hash &= (im->ipsec4_in_spd_hash_num_buckets - 1);
137 ipsec_spinlock_lock (&im->ipsec4_in_spd_hash_tbl[hash].bucket_lock);
138 kv_result = im->ipsec4_in_spd_hash_tbl[hash];
139 ipsec_spinlock_unlock (&im->ipsec4_in_spd_hash_tbl[hash].bucket_lock);
141 if (ipsec4_hash_key_compare_16_8 ((u64 *) &ip4_tuple.kv_16_8,
144 if (im->input_epoch_count == ((u32) (kv_result.value & 0xFFFFFFFF)))
146 /* Get the policy based on the index */
148 pool_elt_at_index (im->policies, ((u32) (kv_result.value >> 32)));
156 ipsec_fp_in_5tuple_from_ip4_range (ipsec_fp_5tuple_t *tuple, u32 sa, u32 da,
159 clib_memset (tuple->l3_zero_pad, 0, sizeof (tuple->l3_zero_pad));
160 tuple->laddr.as_u32 = da;
161 tuple->raddr.as_u32 = sa;
163 tuple->action = action;
168 ipsec_fp_in_5tuple_from_ip6_range (ipsec_fp_5tuple_t *tuple, ip6_address_t *sa,
169 ip6_address_t *da, u32 spi, u8 action)
172 clib_memcpy (&tuple->ip6_laddr, da, sizeof (ip6_address_t));
173 clib_memcpy (&tuple->ip6_raddr, sa, sizeof (ip6_address_t));
176 tuple->action = action;
180 always_inline ipsec_policy_t *
181 ipsec_input_policy_match (ipsec_spd_t *spd, u32 sa, u32 da,
182 ipsec_spd_policy_type_t policy_type)
184 ipsec_main_t *im = &ipsec_main;
188 vec_foreach (i, spd->policies[policy_type])
190 p = pool_elt_at_index (im->policies, *i);
192 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
195 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
198 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
201 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
204 if (im->input_flow_cache_flag)
206 /* Add an Entry in Flow cache */
207 ipsec4_input_spd_add_flow_cache_entry (im, sa, da, policy_type, *i);
214 always_inline ipsec_policy_t *
215 ipsec_input_protect_policy_match (ipsec_spd_t *spd, u32 sa, u32 da, u32 spi)
217 ipsec_main_t *im = &ipsec_main;
222 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT])
224 p = pool_elt_at_index (im->policies, *i);
225 s = ipsec_sa_get (p->sa_index);
230 if (ipsec_sa_is_set_IS_TUNNEL (s))
232 if (da != clib_net_to_host_u32 (s->tunnel.t_dst.ip.ip4.as_u32))
235 if (sa != clib_net_to_host_u32 (s->tunnel.t_src.ip.ip4.as_u32))
241 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
244 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
247 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
250 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
254 if (im->input_flow_cache_flag)
256 /* Add an Entry in Flow cache */
257 ipsec4_input_spd_add_flow_cache_entry (
258 im, sa, da, IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT, *i);
267 ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
270 if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
271 (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
277 ipsec_esp_packet_process (vlib_main_t *vm, ipsec_main_t *im, ip4_header_t *ip0,
278 esp_header_t *esp0, u32 thread_index,
279 ipsec_spd_t *spd0, vlib_buffer_t **b,
280 vlib_node_runtime_t *node, u64 *ipsec_bypassed,
281 u64 *ipsec_dropped, u64 *ipsec_matched,
282 u64 *ipsec_unprocessed, u16 *next)
285 ipsec_policy_t *p0 = NULL;
288 bool search_flow_cache = false;
289 ipsec_policy_t *policies[1];
290 ipsec_fp_5tuple_t tuples[1];
293 /* if flow cache is enabled, first search through flow cache for a
294 * policy match for either protect, bypass or discard rules, in that
295 * order. if no match is found search_flow_cache is set to false (1)
296 * and we revert back to linear search
299 search_flow_cache = im->input_flow_cache_flag;
302 /* SPI ID field in the ESP header MUST NOT be a zero value */
305 /* Drop the packet if SPI ID is zero */
306 *ipsec_unprocessed += 1;
307 next[0] = IPSEC_INPUT_NEXT_DROP;
311 if (im->fp_spd_ipv4_in_is_enabled &&
312 PREDICT_TRUE (INDEX_INVALID != spd0->fp_spd.ip4_in_lookup_hash_idx))
314 ipsec_fp_in_5tuple_from_ip4_range (&tuples[0], ip0->src_address.as_u32,
315 ip0->dst_address.as_u32,
316 clib_net_to_host_u32 (esp0->spi),
317 IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT);
318 ipsec_fp_in_policy_match_n (&spd0->fp_spd, !ip_v6, tuples, policies, 1);
321 else if (search_flow_cache) /* attempt to match policy in flow cache */
323 p0 = ipsec4_input_spd_find_flow_cache_entry (
324 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
325 IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT);
328 else /* linear search if flow cache is not enabled,
329 or flow cache search just failed */
331 p0 = ipsec_input_protect_policy_match (
332 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
333 clib_net_to_host_u32 (ip0->dst_address.as_u32),
334 clib_net_to_host_u32 (esp0->spi));
336 has_space0 = vlib_buffer_has_space (b[0], (clib_address_t) (esp0 + 1) -
337 (clib_address_t) ip0);
339 if (PREDICT_TRUE ((p0 != NULL) & (has_space0)))
343 pi0 = p0 - im->policies;
344 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
345 thread_index, pi0, 1,
346 clib_net_to_host_u16 (ip0->length));
348 vnet_buffer (b[0])->ipsec.sad_index = p0->sa_index;
349 next[0] = im->esp4_decrypt_next_index;
350 vlib_buffer_advance (b[0], ((u8 *) esp0 - (u8 *) ip0));
358 if (im->fp_spd_ipv4_in_is_enabled &&
359 PREDICT_TRUE (INDEX_INVALID != spd0->fp_spd.ip4_in_lookup_hash_idx))
361 tuples->action = IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS;
362 ipsec_fp_in_policy_match_n (&spd0->fp_spd, !ip_v6, tuples, policies, 1);
365 else if (search_flow_cache)
367 p0 = ipsec4_input_spd_find_flow_cache_entry (
368 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
369 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
374 p0 = ipsec_input_policy_match (
375 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
376 clib_net_to_host_u32 (ip0->dst_address.as_u32),
377 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
380 if (PREDICT_TRUE ((p0 != NULL)))
382 *ipsec_bypassed += 1;
384 pi0 = p0 - im->policies;
385 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
386 thread_index, pi0, 1,
387 clib_net_to_host_u16 (ip0->length));
396 if (im->fp_spd_ipv4_in_is_enabled &&
397 PREDICT_TRUE (INDEX_INVALID != spd0->fp_spd.ip4_in_lookup_hash_idx))
399 tuples->action = IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD;
400 ipsec_fp_in_policy_match_n (&spd0->fp_spd, !ip_v6, tuples, policies, 1);
405 if (search_flow_cache)
407 p0 = ipsec4_input_spd_find_flow_cache_entry (
408 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
409 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
414 p0 = ipsec_input_policy_match (
415 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
416 clib_net_to_host_u32 (ip0->dst_address.as_u32),
417 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
420 if (PREDICT_TRUE ((p0 != NULL)))
424 pi0 = p0 - im->policies;
425 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
426 thread_index, pi0, 1,
427 clib_net_to_host_u16 (ip0->length));
429 next[0] = IPSEC_INPUT_NEXT_DROP;
437 /* flow cache search failed, try again with linear search */
438 if (search_flow_cache && p0 == NULL)
440 search_flow_cache = false;
444 /* Drop by default if no match on PROTECT, BYPASS or DISCARD */
445 *ipsec_unprocessed += 1;
446 next[0] = IPSEC_INPUT_NEXT_DROP;
449 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
450 PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
452 ipsec_input_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
454 tr->proto = ip0->protocol;
455 tr->sa_id = p0 ? p0->sa_id : ~0;
456 tr->spi = has_space0 ? clib_net_to_host_u32 (esp0->spi) : ~0;
457 tr->seq = has_space0 ? clib_net_to_host_u32 (esp0->seq) : ~0;
459 tr->policy_index = pi0;
463 always_inline ipsec_policy_t *
464 ipsec6_input_protect_policy_match (ipsec_spd_t * spd,
466 ip6_address_t * da, u32 spi)
468 ipsec_main_t *im = &ipsec_main;
473 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT])
475 p = pool_elt_at_index (im->policies, *i);
476 s = ipsec_sa_get (p->sa_index);
481 if (ipsec_sa_is_set_IS_TUNNEL (s))
483 if (!ip6_address_is_equal (sa, &s->tunnel.t_src.ip.ip6))
486 if (!ip6_address_is_equal (da, &s->tunnel.t_dst.ip.ip6))
492 if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
495 if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
503 extern vlib_node_registration_t ipsec4_input_node;
505 VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm,
506 vlib_node_runtime_t * node,
507 vlib_frame_t * frame)
509 u32 n_left_from, *from, thread_index;
510 ipsec_main_t *im = &ipsec_main;
511 u64 ipsec_unprocessed = 0, ipsec_matched = 0;
512 u64 ipsec_dropped = 0, ipsec_bypassed = 0;
513 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
514 vlib_buffer_t **b = bufs;
515 u16 nexts[VLIB_FRAME_SIZE], *next;
517 from = vlib_frame_vector_args (frame);
518 n_left_from = frame->n_vectors;
520 vlib_get_buffers (vm, from, bufs, n_left_from);
521 thread_index = vm->thread_index;
524 while (n_left_from > 0)
528 esp_header_t *esp0 = NULL;
530 ip4_ipsec_config_t *c0;
532 ipsec_policy_t *p0 = NULL;
534 bool search_flow_cache = false;
538 vlib_prefetch_buffer_data (b[1], LOAD);
541 b[0]->flags |= VNET_BUFFER_F_IS_IP4;
542 b[0]->flags &= ~VNET_BUFFER_F_IS_IP6;
543 c0 = vnet_feature_next_with_data (&next32, b[0], sizeof (c0[0]));
544 next[0] = (u16) next32;
546 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
548 ip0 = vlib_buffer_get_current (b[0]);
550 if (ip0->protocol == IP_PROTOCOL_UDP)
552 udp_header_t *udp0 = NULL;
553 udp0 = (udp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
555 /* As per rfc3948 in UDP Encapsulated Header, UDP checksum must be
556 * Zero, and receivers must not depen upon UPD checksum.
557 * inside ESP header , SPI ID value MUST NOT be a zero value
560 if (udp0->checksum == 0)
562 esp0 = (esp_header_t *) ((u8 *) udp0 + sizeof (udp_header_t));
564 ipsec_esp_packet_process (vm, im, ip0, esp0, thread_index, spd0,
565 b, node, &ipsec_bypassed,
566 &ipsec_dropped, &ipsec_matched,
567 &ipsec_unprocessed, next);
568 if (ipsec_bypassed > 0)
572 else if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
574 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
575 ipsec_esp_packet_process (vm, im, ip0, esp0, thread_index, spd0, b,
576 node, &ipsec_bypassed, &ipsec_dropped,
577 &ipsec_matched, &ipsec_unprocessed, next);
578 if (ipsec_bypassed > 0)
581 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
583 ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
585 // if flow cache is enabled, first search through flow cache for a
586 // policy match and revert back to linear search on failure
587 search_flow_cache = im->input_flow_cache_flag;
590 if (search_flow_cache)
592 p0 = ipsec4_input_spd_find_flow_cache_entry (
593 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
594 IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT);
599 p0 = ipsec_input_protect_policy_match (
600 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
601 clib_net_to_host_u32 (ip0->dst_address.as_u32),
602 clib_net_to_host_u32 (ah0->spi));
606 vlib_buffer_has_space (b[0],
607 (clib_address_t) (ah0 + 1) -
608 (clib_address_t) ip0);
610 if (PREDICT_TRUE ((p0 != NULL) & (has_space0)))
614 pi0 = p0 - im->policies;
615 vlib_increment_combined_counter
616 (&ipsec_spd_policy_counters,
617 thread_index, pi0, 1, clib_net_to_host_u16 (ip0->length));
619 vnet_buffer (b[0])->ipsec.sad_index = p0->sa_index;
620 next[0] = im->ah4_decrypt_next_index;
629 if (search_flow_cache)
631 p0 = ipsec4_input_spd_find_flow_cache_entry (
632 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
633 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
638 p0 = ipsec_input_policy_match (
639 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
640 clib_net_to_host_u32 (ip0->dst_address.as_u32),
641 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
644 if (PREDICT_TRUE ((p0 != NULL)))
648 pi0 = p0 - im->policies;
649 vlib_increment_combined_counter (
650 &ipsec_spd_policy_counters, thread_index, pi0, 1,
651 clib_net_to_host_u16 (ip0->length));
661 if (search_flow_cache)
663 p0 = ipsec4_input_spd_find_flow_cache_entry (
664 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
665 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
670 p0 = ipsec_input_policy_match (
671 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
672 clib_net_to_host_u32 (ip0->dst_address.as_u32),
673 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
676 if (PREDICT_TRUE ((p0 != NULL)))
680 pi0 = p0 - im->policies;
681 vlib_increment_combined_counter (
682 &ipsec_spd_policy_counters, thread_index, pi0, 1,
683 clib_net_to_host_u16 (ip0->length));
685 next[0] = IPSEC_INPUT_NEXT_DROP;
694 // flow cache search failed, retry with linear search
695 if (search_flow_cache && p0 == NULL)
697 search_flow_cache = false;
701 /* Drop by default if no match on PROTECT, BYPASS or DISCARD */
702 ipsec_unprocessed += 1;
703 next[0] = IPSEC_INPUT_NEXT_DROP;
706 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
707 PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
709 ipsec_input_trace_t *tr =
710 vlib_add_trace (vm, node, b[0], sizeof (*tr));
712 tr->proto = ip0->protocol;
713 tr->sa_id = p0 ? p0->sa_id : ~0;
714 tr->spi = has_space0 ? clib_net_to_host_u32 (ah0->spi) : ~0;
715 tr->seq = has_space0 ? clib_net_to_host_u32 (ah0->seq_no) : ~0;
717 tr->policy_index = pi0;
723 ipsec_unprocessed += 1;
730 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
732 vlib_node_increment_counter (vm, ipsec4_input_node.index,
733 IPSEC_INPUT_ERROR_RX_PKTS, frame->n_vectors);
735 vlib_node_increment_counter (vm, ipsec4_input_node.index,
736 IPSEC_INPUT_ERROR_RX_POLICY_MATCH,
739 vlib_node_increment_counter (vm, ipsec4_input_node.index,
740 IPSEC_INPUT_ERROR_RX_POLICY_NO_MATCH,
743 vlib_node_increment_counter (vm, ipsec4_input_node.index,
744 IPSEC_INPUT_ERROR_RX_POLICY_DISCARD,
747 vlib_node_increment_counter (vm, ipsec4_input_node.index,
748 IPSEC_INPUT_ERROR_RX_POLICY_BYPASS,
751 return frame->n_vectors;
754 VLIB_REGISTER_NODE (ipsec4_input_node) = {
755 .name = "ipsec4-input-feature",
756 .vector_size = sizeof (u32),
757 .format_trace = format_ipsec_input_trace,
758 .type = VLIB_NODE_TYPE_INTERNAL,
759 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
760 .error_strings = ipsec_input_error_strings,
761 .n_next_nodes = IPSEC_INPUT_N_NEXT,
763 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
764 foreach_ipsec_input_next
769 extern vlib_node_registration_t ipsec6_input_node;
772 VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm,
773 vlib_node_runtime_t * node,
774 vlib_frame_t * from_frame)
776 u32 n_left_from, *from, next_index, *to_next, thread_index;
777 ipsec_main_t *im = &ipsec_main;
778 u32 ipsec_unprocessed = 0;
779 u32 ipsec_matched = 0;
780 ipsec_policy_t *policies[1];
781 ipsec_fp_5tuple_t tuples[1];
784 from = vlib_frame_vector_args (from_frame);
785 n_left_from = from_frame->n_vectors;
786 thread_index = vm->thread_index;
788 next_index = node->cached_next_index;
790 while (n_left_from > 0)
794 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
796 while (n_left_from > 0 && n_left_to_next > 0)
798 u32 bi0, next0, pi0 = ~0;
802 ip4_ipsec_config_t *c0;
804 ipsec_policy_t *p0 = 0;
806 u32 header_size = sizeof (ip0[0]);
808 bi0 = to_next[0] = from[0];
814 b0 = vlib_get_buffer (vm, bi0);
815 b0->flags |= VNET_BUFFER_F_IS_IP6;
816 b0->flags &= ~VNET_BUFFER_F_IS_IP4;
817 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
819 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
821 ip0 = vlib_buffer_get_current (b0);
822 esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
823 ah0 = (ah_header_t *) ((u8 *) ip0 + header_size);
825 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
829 ("packet received from %U to %U spi %u size %u spd_id %u",
830 format_ip6_address, &ip0->src_address, format_ip6_address,
831 &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
832 clib_net_to_host_u16 (ip0->payload_length) + header_size,
835 if (im->fp_spd_ipv6_in_is_enabled &&
836 PREDICT_TRUE (INDEX_INVALID !=
837 spd0->fp_spd.ip6_in_lookup_hash_idx))
839 ipsec_fp_in_5tuple_from_ip6_range (
840 &tuples[0], &ip0->src_address, &ip0->dst_address,
841 clib_net_to_host_u32 (esp0->spi),
842 IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT);
843 ipsec_fp_in_policy_match_n (&spd0->fp_spd, ip_v6, tuples,
848 p0 = ipsec6_input_protect_policy_match (
849 spd0, &ip0->src_address, &ip0->dst_address,
850 clib_net_to_host_u32 (esp0->spi));
852 if (PREDICT_TRUE (p0 != 0))
856 pi0 = p0 - im->policies;
857 vlib_increment_combined_counter
858 (&ipsec_spd_policy_counters,
859 thread_index, pi0, 1,
860 clib_net_to_host_u16 (ip0->payload_length) +
863 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
864 next0 = im->esp6_decrypt_next_index;
865 vlib_buffer_advance (b0, header_size);
866 /* TODO Add policy matching for bypass and discard policy
873 ipsec_unprocessed += 1;
874 next0 = IPSEC_INPUT_NEXT_DROP;
877 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
879 p0 = ipsec6_input_protect_policy_match (spd0,
885 if (PREDICT_TRUE (p0 != 0))
888 pi0 = p0 - im->policies;
889 vlib_increment_combined_counter
890 (&ipsec_spd_policy_counters,
891 thread_index, pi0, 1,
892 clib_net_to_host_u16 (ip0->payload_length) +
895 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
896 next0 = im->ah6_decrypt_next_index;
902 ipsec_unprocessed += 1;
903 next0 = IPSEC_INPUT_NEXT_DROP;
908 ipsec_unprocessed += 1;
912 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
913 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
915 ipsec_input_trace_t *tr =
916 vlib_add_trace (vm, node, b0, sizeof (*tr));
920 tr->sa_id = p0->sa_id;
921 tr->policy_type = p0->type;
924 tr->proto = ip0->protocol;
925 tr->spi = clib_net_to_host_u32 (esp0->spi);
926 tr->seq = clib_net_to_host_u32 (esp0->seq);
928 tr->policy_index = pi0;
931 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
932 n_left_to_next, bi0, next0);
934 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
937 vlib_node_increment_counter (vm, ipsec6_input_node.index,
938 IPSEC_INPUT_ERROR_RX_PKTS,
939 from_frame->n_vectors - ipsec_unprocessed);
941 vlib_node_increment_counter (vm, ipsec6_input_node.index,
942 IPSEC_INPUT_ERROR_RX_POLICY_MATCH,
945 return from_frame->n_vectors;
949 VLIB_REGISTER_NODE (ipsec6_input_node) = {
950 .name = "ipsec6-input-feature",
951 .vector_size = sizeof (u32),
952 .format_trace = format_ipsec_input_trace,
953 .type = VLIB_NODE_TYPE_INTERNAL,
954 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
955 .error_strings = ipsec_input_error_strings,
956 .n_next_nodes = IPSEC_INPUT_N_NEXT,
958 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
959 foreach_ipsec_input_next
965 * fd.io coding-style-patch-verification: ON
968 * eval: (c-set-style "gnu")