blob: 48f7deadda3a493ad3faa367196237c58f9eda06 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * decap.c : IPSec tunnel decapsulation
3 *
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
Damjan Marion8b3191e2016-11-09 19:54:20 +010021#include <vnet/feature/feature.h>
Piotr Bronowski993b6be2022-08-31 13:48:14 +000022#include <vnet/ipsec/ipsec_spd_fp_lookup.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
25#include <vnet/ipsec/esp.h>
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080026#include <vnet/ipsec/ah.h>
Neale Ranns918c1612019-02-21 23:34:59 -080027#include <vnet/ipsec/ipsec_io.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
ShivaShankarK05464832020-04-14 14:01:03 +053029#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")
Ed Warnickecb9cada2015-12-08 15:45:58 -070035
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070036typedef enum
37{
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#define _(sym,str) IPSEC_INPUT_ERROR_##sym,
39 foreach_ipsec_input_error
40#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070041 IPSEC_INPUT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070042} ipsec_input_error_t;
43
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070044static char *ipsec_input_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070045#define _(sym,string) string,
46 foreach_ipsec_input_error
47#undef _
48};
49
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070050typedef struct
51{
Neale Rannsa09c1ff2019-02-04 01:10:30 -080052 ip_protocol_t proto;
Pierre Pfister62219272018-11-26 09:29:00 +010053 u32 spd;
Neale Rannsa09c1ff2019-02-04 01:10:30 -080054 u32 policy_index;
Piotr Bronowski06abf232022-09-20 14:44:36 +000055 u32 policy_type;
Matus Fabian5539a072016-09-07 05:57:09 -070056 u32 sa_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 u32 spi;
58 u32 seq;
59} ipsec_input_trace_t;
60
61/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070062static u8 *
63format_ipsec_input_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070064{
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 *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070067 ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
Piotr Bronowski06abf232022-09-20 14:44:36 +000069 s =
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);
Matus Fabian5539a072016-09-07 05:57:09 -070073
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 return s;
75}
76
Zachary Leaf7cd35f52021-06-25 08:11:15 -050077always_inline void
78ipsec4_input_spd_add_flow_cache_entry (ipsec_main_t *im, u32 sa, u32 da,
79 ipsec_spd_policy_type_t policy_type,
80 u32 pol_id)
81{
82 u64 hash;
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
89 };
90
91 ip4_tuple.kv_16_8.value =
92 (((u64) pol_id) << 32) | ((u64) im->input_epoch_count);
93
94 hash = ipsec4_hash_16_8 (&ip4_tuple.kv_16_8);
95 hash &= (im->ipsec4_in_spd_hash_num_buckets - 1);
96
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))
108 is_stale_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);
114
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);
119
120 return;
121}
122
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123always_inline ipsec_policy_t *
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500124ipsec4_input_spd_find_flow_cache_entry (ipsec_main_t *im, u32 sa, u32 da,
125 ipsec_spd_policy_type_t policy_type)
126{
127 ipsec_policy_t *p = NULL;
128 ipsec4_hash_kv_16_8_t kv_result;
129 u64 hash;
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 };
133
134 hash = ipsec4_hash_16_8 (&ip4_tuple.kv_16_8);
135 hash &= (im->ipsec4_in_spd_hash_num_buckets - 1);
136
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);
140
141 if (ipsec4_hash_key_compare_16_8 ((u64 *) &ip4_tuple.kv_16_8,
142 (u64 *) &kv_result))
143 {
144 if (im->input_epoch_count == ((u32) (kv_result.value & 0xFFFFFFFF)))
145 {
146 /* Get the policy based on the index */
147 p =
148 pool_elt_at_index (im->policies, ((u32) (kv_result.value >> 32)));
149 }
150 }
151
152 return p;
153}
154
Piotr Bronowski993b6be2022-08-31 13:48:14 +0000155always_inline void
Piotr Bronowski1d9780a2022-10-21 15:48:55 +0000156ipsec_fp_in_5tuple_from_ip4_range (ipsec_fp_5tuple_t *tuple, u32 sa, u32 da,
Piotr Bronowski993b6be2022-08-31 13:48:14 +0000157 u32 spi, u8 action)
158{
159 clib_memset (tuple->l3_zero_pad, 0, sizeof (tuple->l3_zero_pad));
Piotr Bronowski1d9780a2022-10-21 15:48:55 +0000160 tuple->laddr.as_u32 = da;
161 tuple->raddr.as_u32 = sa;
Piotr Bronowski993b6be2022-08-31 13:48:14 +0000162 tuple->spi = spi;
163 tuple->action = action;
164 tuple->is_ipv6 = 0;
165}
166
Piotr Bronowski06abf232022-09-20 14:44:36 +0000167always_inline void
Piotr Bronowski1d9780a2022-10-21 15:48:55 +0000168ipsec_fp_in_5tuple_from_ip6_range (ipsec_fp_5tuple_t *tuple, ip6_address_t *sa,
169 ip6_address_t *da, u32 spi, u8 action)
Piotr Bronowski06abf232022-09-20 14:44:36 +0000170
171{
Piotr Bronowski1d9780a2022-10-21 15:48:55 +0000172 clib_memcpy (&tuple->ip6_laddr, da, sizeof (ip6_address_t));
173 clib_memcpy (&tuple->ip6_raddr, sa, sizeof (ip6_address_t));
Piotr Bronowski06abf232022-09-20 14:44:36 +0000174
175 tuple->spi = spi;
176 tuple->action = action;
177 tuple->is_ipv6 = 1;
178}
179
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500180always_inline ipsec_policy_t *
181ipsec_input_policy_match (ipsec_spd_t *spd, u32 sa, u32 da,
ShivaShankarK05464832020-04-14 14:01:03 +0530182 ipsec_spd_policy_type_t policy_type)
183{
184 ipsec_main_t *im = &ipsec_main;
185 ipsec_policy_t *p;
186 u32 *i;
187
188 vec_foreach (i, spd->policies[policy_type])
189 {
190 p = pool_elt_at_index (im->policies, *i);
191
192 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
193 continue;
194
195 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
196 continue;
197
198 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
199 continue;
200
201 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
202 continue;
203
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500204 if (im->input_flow_cache_flag)
205 {
206 /* Add an Entry in Flow cache */
207 ipsec4_input_spd_add_flow_cache_entry (im, sa, da, policy_type, *i);
208 }
ShivaShankarK05464832020-04-14 14:01:03 +0530209 return p;
210 }
211 return 0;
212}
213
214always_inline ipsec_policy_t *
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500215ipsec_input_protect_policy_match (ipsec_spd_t *spd, u32 sa, u32 da, u32 spi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216{
217 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700218 ipsec_policy_t *p;
219 ipsec_sa_t *s;
220 u32 *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800222 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT])
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700223 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800224 p = pool_elt_at_index (im->policies, *i);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000225 s = ipsec_sa_get (p->sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227 if (spi != s->spi)
228 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
Damjan Mariond709cbc2019-03-26 13:16:42 +0100230 if (ipsec_sa_is_set_IS_TUNNEL (s))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700231 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000232 if (da != clib_net_to_host_u32 (s->tunnel.t_dst.ip.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700233 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
Neale Ranns9ec846c2021-02-09 14:04:02 +0000235 if (sa != clib_net_to_host_u32 (s->tunnel.t_src.ip.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700236 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500238 goto return_policy;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700239 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240
Neale Rannsd2029bc2019-07-11 09:31:19 +0000241 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700242 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
Neale Rannsd2029bc2019-07-11 09:31:19 +0000244 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700245 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Neale Rannsd2029bc2019-07-11 09:31:19 +0000247 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700248 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
Neale Rannsd2029bc2019-07-11 09:31:19 +0000250 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700251 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500253 return_policy:
254 if (im->input_flow_cache_flag)
255 {
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);
259 }
260
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700261 return p;
262 }
263 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264}
265
266always_inline uword
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700267ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
268 ip6_address_t * ua)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700270 if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
271 (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 return 1;
273 return 0;
274}
275
vinay tripathi75069ce2023-05-31 14:41:45 +0530276always_inline void
vinay tripathi518bcc12024-01-12 10:28:00 +0000277ipsec_collect_ah_trace (vlib_buffer_t **b, vlib_node_runtime_t *node,
278 vlib_main_t *vm, ip4_header_t *ip0, ah_header_t *ah0,
279 u8 has_space0, ipsec_spd_t *spd0, ipsec_policy_t *p0,
280 u32 pi0)
281{
282 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
283 PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
284 {
285 ipsec_input_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
286
287 tr->proto = ip0->protocol;
288 tr->sa_id = p0 ? p0->sa_id : ~0;
289 tr->spi = has_space0 ? clib_net_to_host_u32 (ah0->spi) : ~0;
290 tr->seq = has_space0 ? clib_net_to_host_u32 (ah0->seq_no) : ~0;
291 tr->spd = spd0->id;
292 tr->policy_index = pi0;
293 }
294}
295
296always_inline void
297ipsec_ah_packet_process (vlib_main_t *vm, ipsec_main_t *im, ip4_header_t *ip0,
298 ah_header_t *ah0, u32 thread_index, ipsec_spd_t *spd0,
299 vlib_buffer_t **b, vlib_node_runtime_t *node,
300 u64 *ipsec_bypassed, u64 *ipsec_dropped,
301 u64 *ipsec_matched, u64 *ipsec_unprocessed, u16 *next)
302
303{
304 ipsec_policy_t *p0 = NULL;
305 u32 pi0 = ~0;
306 u8 has_space0;
307 /* if flow cache is enabled, first search through flow cache for a
308 * policy match and revert back to linear search on failure */
309 bool search_flow_cache = im->input_flow_cache_flag;
310
311 while (1)
312 {
313 if (search_flow_cache)
314 {
315 p0 = ipsec4_input_spd_find_flow_cache_entry (
316 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
317 IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT);
318 }
319 else
320 {
321 p0 = ipsec_input_protect_policy_match (
322 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
323 clib_net_to_host_u32 (ip0->dst_address.as_u32),
324 clib_net_to_host_u32 (ah0->spi));
325 }
326
327 has_space0 = vlib_buffer_has_space (b[0], (clib_address_t) (ah0 + 1) -
328 (clib_address_t) ip0);
329
330 if (PREDICT_TRUE ((p0 != NULL) & (has_space0)))
331 {
332 *ipsec_matched += 1;
333 pi0 = p0 - im->policies;
334 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
335 thread_index, pi0, 1,
336 clib_net_to_host_u16 (ip0->length));
337
338 vnet_buffer (b[0])->ipsec.sad_index = p0->sa_index;
339 next[0] = im->ah4_decrypt_next_index;
340 ipsec_collect_ah_trace (b, node, vm, ip0, ah0, has_space0, spd0, p0,
341 pi0);
342 return;
343 }
344 else
345 {
346 p0 = 0;
347 pi0 = ~0;
348 }
349 if (search_flow_cache)
350 {
351 p0 = ipsec4_input_spd_find_flow_cache_entry (
352 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
353 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
354 }
355
356 else
357 {
358 p0 = ipsec_input_policy_match (
359 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
360 clib_net_to_host_u32 (ip0->dst_address.as_u32),
361 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
362 }
363
364 if (PREDICT_TRUE ((p0 != NULL)))
365 {
366 *ipsec_bypassed += 1;
367 pi0 = p0 - im->policies;
368 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
369 thread_index, pi0, 1,
370 clib_net_to_host_u16 (ip0->length));
371 ipsec_collect_ah_trace (b, node, vm, ip0, ah0, has_space0, spd0, p0,
372 pi0);
373 return;
374 }
375 else
376 {
377 p0 = 0;
378 pi0 = ~0;
379 };
380
381 if (search_flow_cache)
382 {
383 p0 = ipsec4_input_spd_find_flow_cache_entry (
384 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
385 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
386 }
387
388 else
389 {
390 p0 = ipsec_input_policy_match (
391 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
392 clib_net_to_host_u32 (ip0->dst_address.as_u32),
393 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
394 }
395
396 if (PREDICT_TRUE ((p0 != NULL)))
397 {
398 *ipsec_dropped += 1;
399 pi0 = p0 - im->policies;
400 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
401 thread_index, pi0, 1,
402 clib_net_to_host_u16 (ip0->length));
403
404 next[0] = IPSEC_INPUT_NEXT_DROP;
405 ipsec_collect_ah_trace (b, node, vm, ip0, ah0, has_space0, spd0, p0,
406 pi0);
407 return;
408 }
409 else
410 {
411 p0 = 0;
412 pi0 = ~0;
413 };
414 /* flow cache search failed, retry with linear search */
415 if (search_flow_cache && p0 == NULL)
416 {
417 search_flow_cache = false;
418 }
419 else if (search_flow_cache == false && p0 == NULL)
420 {
421 /* Drop by default if no match on PROTECT, BYPASS or DISCARD */
422 *ipsec_unprocessed += 1;
423 next[0] = IPSEC_INPUT_NEXT_DROP;
424 return;
425 }
426 }
427}
428
429always_inline void
vinay tripathi75069ce2023-05-31 14:41:45 +0530430ipsec_esp_packet_process (vlib_main_t *vm, ipsec_main_t *im, ip4_header_t *ip0,
431 esp_header_t *esp0, u32 thread_index,
432 ipsec_spd_t *spd0, vlib_buffer_t **b,
433 vlib_node_runtime_t *node, u64 *ipsec_bypassed,
434 u64 *ipsec_dropped, u64 *ipsec_matched,
435 u64 *ipsec_unprocessed, u16 *next)
436
437{
438 ipsec_policy_t *p0 = NULL;
439 u32 pi0;
440 u8 has_space0;
441 bool search_flow_cache = false;
442 ipsec_policy_t *policies[1];
443 ipsec_fp_5tuple_t tuples[1];
444 bool ip_v6 = true;
445
446 /* if flow cache is enabled, first search through flow cache for a
447 * policy match for either protect, bypass or discard rules, in that
448 * order. if no match is found search_flow_cache is set to false (1)
449 * and we revert back to linear search
450 */
451
452 search_flow_cache = im->input_flow_cache_flag;
453udp_or_esp:
454
vinay tripathi2d7988d2023-06-06 12:57:55 +0530455 if (esp0->spi == 0)
456 {
Fan Zhange7901e82024-05-24 16:46:00 +0100457 /* RFC 4303, section 2.1: The SPI value of zero (0 is reserved for
458 * local, implementation-specific use and MUST NOT be sent on the wire.
459 */
vinay tripathi2d7988d2023-06-06 12:57:55 +0530460 *ipsec_unprocessed += 1;
461 next[0] = IPSEC_INPUT_NEXT_DROP;
462 return;
463 }
464
vinay tripathi75069ce2023-05-31 14:41:45 +0530465 if (im->fp_spd_ipv4_in_is_enabled &&
466 PREDICT_TRUE (INDEX_INVALID != spd0->fp_spd.ip4_in_lookup_hash_idx))
467 {
468 ipsec_fp_in_5tuple_from_ip4_range (&tuples[0], ip0->src_address.as_u32,
469 ip0->dst_address.as_u32,
470 clib_net_to_host_u32 (esp0->spi),
471 IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT);
472 ipsec_fp_in_policy_match_n (&spd0->fp_spd, !ip_v6, tuples, policies, 1);
473 p0 = policies[0];
474 }
475 else if (search_flow_cache) /* attempt to match policy in flow cache */
476 {
477 p0 = ipsec4_input_spd_find_flow_cache_entry (
478 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
479 IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT);
480 }
481
482 else /* linear search if flow cache is not enabled,
483 or flow cache search just failed */
484 {
485 p0 = ipsec_input_protect_policy_match (
486 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
487 clib_net_to_host_u32 (ip0->dst_address.as_u32),
488 clib_net_to_host_u32 (esp0->spi));
489 }
490 has_space0 = vlib_buffer_has_space (b[0], (clib_address_t) (esp0 + 1) -
491 (clib_address_t) ip0);
492
493 if (PREDICT_TRUE ((p0 != NULL) & (has_space0)))
494 {
495 *ipsec_matched += 1;
496
497 pi0 = p0 - im->policies;
498 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
499 thread_index, pi0, 1,
500 clib_net_to_host_u16 (ip0->length));
501
502 vnet_buffer (b[0])->ipsec.sad_index = p0->sa_index;
503 next[0] = im->esp4_decrypt_next_index;
504 vlib_buffer_advance (b[0], ((u8 *) esp0 - (u8 *) ip0));
505 goto trace0;
506 }
507 else
508 {
509 p0 = 0;
510 pi0 = ~0;
511 }
512 if (im->fp_spd_ipv4_in_is_enabled &&
513 PREDICT_TRUE (INDEX_INVALID != spd0->fp_spd.ip4_in_lookup_hash_idx))
514 {
515 tuples->action = IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS;
516 ipsec_fp_in_policy_match_n (&spd0->fp_spd, !ip_v6, tuples, policies, 1);
517 p0 = policies[0];
518 }
519 else if (search_flow_cache)
520 {
521 p0 = ipsec4_input_spd_find_flow_cache_entry (
522 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
523 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
524 }
525
526 else
527 {
528 p0 = ipsec_input_policy_match (
529 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
530 clib_net_to_host_u32 (ip0->dst_address.as_u32),
531 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
532 }
533
534 if (PREDICT_TRUE ((p0 != NULL)))
535 {
536 *ipsec_bypassed += 1;
537
538 pi0 = p0 - im->policies;
539 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
540 thread_index, pi0, 1,
541 clib_net_to_host_u16 (ip0->length));
542
543 goto trace0;
544 }
545 else
546 {
547 p0 = 0;
548 pi0 = ~0;
549 };
550 if (im->fp_spd_ipv4_in_is_enabled &&
551 PREDICT_TRUE (INDEX_INVALID != spd0->fp_spd.ip4_in_lookup_hash_idx))
552 {
553 tuples->action = IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD;
554 ipsec_fp_in_policy_match_n (&spd0->fp_spd, !ip_v6, tuples, policies, 1);
555 p0 = policies[0];
556 }
557 else
558
559 if (search_flow_cache)
560 {
561 p0 = ipsec4_input_spd_find_flow_cache_entry (
562 im, ip0->src_address.as_u32, ip0->dst_address.as_u32,
563 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
564 }
565
566 else
567 {
568 p0 = ipsec_input_policy_match (
569 spd0, clib_net_to_host_u32 (ip0->src_address.as_u32),
570 clib_net_to_host_u32 (ip0->dst_address.as_u32),
571 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
572 }
573
574 if (PREDICT_TRUE ((p0 != NULL)))
575 {
576 *ipsec_dropped += 1;
577
578 pi0 = p0 - im->policies;
579 vlib_increment_combined_counter (&ipsec_spd_policy_counters,
580 thread_index, pi0, 1,
581 clib_net_to_host_u16 (ip0->length));
582
583 next[0] = IPSEC_INPUT_NEXT_DROP;
584 goto trace0;
585 }
586 else
587 {
588 p0 = 0;
589 pi0 = ~0;
590 };
591 /* flow cache search failed, try again with linear search */
592 if (search_flow_cache && p0 == NULL)
593 {
594 search_flow_cache = false;
595 goto udp_or_esp;
596 }
597
598 /* Drop by default if no match on PROTECT, BYPASS or DISCARD */
599 *ipsec_unprocessed += 1;
600 next[0] = IPSEC_INPUT_NEXT_DROP;
601
602trace0:
603 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
604 PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
605 {
606 ipsec_input_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
607
608 tr->proto = ip0->protocol;
609 tr->sa_id = p0 ? p0->sa_id : ~0;
610 tr->spi = has_space0 ? clib_net_to_host_u32 (esp0->spi) : ~0;
611 tr->seq = has_space0 ? clib_net_to_host_u32 (esp0->seq) : ~0;
612 tr->spd = spd0->id;
613 tr->policy_index = pi0;
614 }
615}
616
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617always_inline ipsec_policy_t *
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200618ipsec6_input_protect_policy_match (ipsec_spd_t * spd,
619 ip6_address_t * sa,
620 ip6_address_t * da, u32 spi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621{
622 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700623 ipsec_policy_t *p;
624 ipsec_sa_t *s;
625 u32 *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800627 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT])
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700628 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800629 p = pool_elt_at_index (im->policies, *i);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000630 s = ipsec_sa_get (p->sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700632 if (spi != s->spi)
633 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
Damjan Mariond709cbc2019-03-26 13:16:42 +0100635 if (ipsec_sa_is_set_IS_TUNNEL (s))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700636 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000637 if (!ip6_address_is_equal (sa, &s->tunnel.t_src.ip.ip6))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700638 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639
Neale Ranns9ec846c2021-02-09 14:04:02 +0000640 if (!ip6_address_is_equal (da, &s->tunnel.t_dst.ip.ip6))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700641 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700643 return p;
644 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700646 if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
647 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700649 if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
650 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700652 return p;
653 }
654 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655}
656
Damjan Mariond770cfc2019-09-02 19:00:33 +0200657extern vlib_node_registration_t ipsec4_input_node;
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +0100658
Klement Sekerab8f35442018-10-29 13:38:19 +0100659VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm,
660 vlib_node_runtime_t * node,
ShivaShankarK05464832020-04-14 14:01:03 +0530661 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662{
ShivaShankarK05464832020-04-14 14:01:03 +0530663 u32 n_left_from, *from, thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 ipsec_main_t *im = &ipsec_main;
ShivaShankarK05464832020-04-14 14:01:03 +0530665 u64 ipsec_unprocessed = 0, ipsec_matched = 0;
666 u64 ipsec_dropped = 0, ipsec_bypassed = 0;
667 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
668 vlib_buffer_t **b = bufs;
669 u16 nexts[VLIB_FRAME_SIZE], *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670
ShivaShankarK05464832020-04-14 14:01:03 +0530671 from = vlib_frame_vector_args (frame);
672 n_left_from = frame->n_vectors;
673 next = nexts;
674 vlib_get_buffers (vm, from, bufs, n_left_from);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800675 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677
678 while (n_left_from > 0)
679 {
vinay tripathi518bcc12024-01-12 10:28:00 +0000680 u32 next32;
ShivaShankarK05464832020-04-14 14:01:03 +0530681 ip4_header_t *ip0;
682 esp_header_t *esp0 = NULL;
683 ah_header_t *ah0;
684 ip4_ipsec_config_t *c0;
685 ipsec_spd_t *spd0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686
ShivaShankarK05464832020-04-14 14:01:03 +0530687 if (n_left_from > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700688 {
ShivaShankarK05464832020-04-14 14:01:03 +0530689 vlib_prefetch_buffer_data (b[1], LOAD);
690 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
ShivaShankarK05464832020-04-14 14:01:03 +0530692 b[0]->flags |= VNET_BUFFER_F_IS_IP4;
693 b[0]->flags &= ~VNET_BUFFER_F_IS_IP6;
694 c0 = vnet_feature_next_with_data (&next32, b[0], sizeof (c0[0]));
695 next[0] = (u16) next32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696
ShivaShankarK05464832020-04-14 14:01:03 +0530697 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
ShivaShankarK05464832020-04-14 14:01:03 +0530699 ip0 = vlib_buffer_get_current (b[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
vinay tripathi2d7988d2023-06-06 12:57:55 +0530701 if (ip0->protocol == IP_PROTOCOL_UDP)
ShivaShankarK05464832020-04-14 14:01:03 +0530702 {
vinay tripathi2d7988d2023-06-06 12:57:55 +0530703 udp_header_t *udp0 = NULL;
704 udp0 = (udp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Fan Zhange7901e82024-05-24 16:46:00 +0100706 /* RFC5996 Section 2.23 "Port 4500 is reserved for
707 * UDP-encapsulated ESP and IKE."
708 */
709 if (clib_host_to_net_u16 (4500) == udp0->dst_port)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700710 {
vinay tripathi2d7988d2023-06-06 12:57:55 +0530711 esp0 = (esp_header_t *) ((u8 *) udp0 + sizeof (udp_header_t));
712
713 ipsec_esp_packet_process (vm, im, ip0, esp0, thread_index, spd0,
714 b, node, &ipsec_bypassed,
715 &ipsec_dropped, &ipsec_matched,
716 &ipsec_unprocessed, next);
717 if (ipsec_bypassed > 0)
718 goto ipsec_bypassed;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700719 }
vinay tripathi2d7988d2023-06-06 12:57:55 +0530720 }
721 else if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
722 {
723 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
vinay tripathi75069ce2023-05-31 14:41:45 +0530724 ipsec_esp_packet_process (vm, im, ip0, esp0, thread_index, spd0, b,
725 node, &ipsec_bypassed, &ipsec_dropped,
726 &ipsec_matched, &ipsec_unprocessed, next);
vinay tripathi2d7988d2023-06-06 12:57:55 +0530727 if (ipsec_bypassed > 0)
728 goto ipsec_bypassed;
ShivaShankarK05464832020-04-14 14:01:03 +0530729 }
730 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
731 {
732 ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
Zachary Leaf7cd35f52021-06-25 08:11:15 -0500733
vinay tripathi518bcc12024-01-12 10:28:00 +0000734 ipsec_ah_packet_process (vm, im, ip0, ah0, thread_index, spd0, b,
735 node, &ipsec_bypassed, &ipsec_dropped,
736 &ipsec_matched, &ipsec_unprocessed, next);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700737 }
ShivaShankarK05464832020-04-14 14:01:03 +0530738 else
739 {
vinay tripathi2d7988d2023-06-06 12:57:55 +0530740 ipsec_bypassed:
ShivaShankarK05464832020-04-14 14:01:03 +0530741 ipsec_unprocessed += 1;
742 }
743 n_left_from -= 1;
744 b += 1;
745 next += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800747
ShivaShankarK05464832020-04-14 14:01:03 +0530748 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
Kingwel Xiec69ac312019-02-04 01:49:29 -0800750 vlib_node_increment_counter (vm, ipsec4_input_node.index,
ShivaShankarK05464832020-04-14 14:01:03 +0530751 IPSEC_INPUT_ERROR_RX_PKTS, frame->n_vectors);
752
753 vlib_node_increment_counter (vm, ipsec4_input_node.index,
754 IPSEC_INPUT_ERROR_RX_POLICY_MATCH,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800755 ipsec_matched);
ShivaShankarK05464832020-04-14 14:01:03 +0530756
757 vlib_node_increment_counter (vm, ipsec4_input_node.index,
758 IPSEC_INPUT_ERROR_RX_POLICY_NO_MATCH,
759 ipsec_unprocessed);
760
761 vlib_node_increment_counter (vm, ipsec4_input_node.index,
762 IPSEC_INPUT_ERROR_RX_POLICY_DISCARD,
763 ipsec_dropped);
764
765 vlib_node_increment_counter (vm, ipsec4_input_node.index,
766 IPSEC_INPUT_ERROR_RX_POLICY_BYPASS,
767 ipsec_bypassed);
768
769 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770}
771
Damjan Mariond770cfc2019-09-02 19:00:33 +0200772VLIB_REGISTER_NODE (ipsec4_input_node) = {
Pierre Pfister057b3562018-12-10 17:01:01 +0100773 .name = "ipsec4-input-feature",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774 .vector_size = sizeof (u32),
775 .format_trace = format_ipsec_input_trace,
776 .type = VLIB_NODE_TYPE_INTERNAL,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
778 .error_strings = ipsec_input_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779 .n_next_nodes = IPSEC_INPUT_N_NEXT,
780 .next_nodes = {
781#define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
782 foreach_ipsec_input_next
783#undef _
784 },
785};
786
Damjan Mariond770cfc2019-09-02 19:00:33 +0200787extern vlib_node_registration_t ipsec6_input_node;
Damjan Marion1c80e832016-05-11 23:07:18 +0200788
Klement Sekerab8f35442018-10-29 13:38:19 +0100789
790VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm,
791 vlib_node_runtime_t * node,
792 vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800794 u32 n_left_from, *from, next_index, *to_next, thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795 ipsec_main_t *im = &ipsec_main;
Kingwel Xiec69ac312019-02-04 01:49:29 -0800796 u32 ipsec_unprocessed = 0;
797 u32 ipsec_matched = 0;
Piotr Bronowski06abf232022-09-20 14:44:36 +0000798 ipsec_policy_t *policies[1];
799 ipsec_fp_5tuple_t tuples[1];
800 bool ip_v6 = true;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801
802 from = vlib_frame_vector_args (from_frame);
803 n_left_from = from_frame->n_vectors;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800804 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
806 next_index = node->cached_next_index;
807
808 while (n_left_from > 0)
809 {
810 u32 n_left_to_next;
811
812 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
813
814 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700815 {
Piotr Bronowski06abf232022-09-20 14:44:36 +0000816 u32 bi0, next0, pi0 = ~0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700817 vlib_buffer_t *b0;
818 ip6_header_t *ip0;
819 esp_header_t *esp0;
820 ip4_ipsec_config_t *c0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700821 ipsec_spd_t *spd0;
Matus Fabian5539a072016-09-07 05:57:09 -0700822 ipsec_policy_t *p0 = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200823 ah_header_t *ah0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700824 u32 header_size = sizeof (ip0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700826 bi0 = to_next[0] = from[0];
827 from += 1;
828 n_left_from -= 1;
829 to_next += 1;
830 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700832 b0 = vlib_get_buffer (vm, bi0);
Szymon Sliwa65a27272018-05-09 14:28:08 +0200833 b0->flags |= VNET_BUFFER_F_IS_IP6;
834 b0->flags &= ~VNET_BUFFER_F_IS_IP4;
Damjan Marion7d98a122018-07-19 20:42:08 +0200835 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700837 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700839 ip0 = vlib_buffer_get_current (b0);
840 esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
Klement Sekera611864f2018-09-26 11:19:00 +0200841 ah0 = (ah_header_t *) ((u8 *) ip0 + header_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700843 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
844 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845#if 0
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700846 clib_warning
847 ("packet received from %U to %U spi %u size %u spd_id %u",
848 format_ip6_address, &ip0->src_address, format_ip6_address,
849 &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
850 clib_net_to_host_u16 (ip0->payload_length) + header_size,
851 spd0->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852#endif
Piotr Bronowski06abf232022-09-20 14:44:36 +0000853 if (im->fp_spd_ipv6_in_is_enabled &&
854 PREDICT_TRUE (INDEX_INVALID !=
855 spd0->fp_spd.ip6_in_lookup_hash_idx))
856 {
857 ipsec_fp_in_5tuple_from_ip6_range (
858 &tuples[0], &ip0->src_address, &ip0->dst_address,
859 clib_net_to_host_u32 (esp0->spi),
860 IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT);
861 ipsec_fp_in_policy_match_n (&spd0->fp_spd, ip_v6, tuples,
862 policies, 1);
863 p0 = policies[0];
864 }
865 else
866 p0 = ipsec6_input_protect_policy_match (
867 spd0, &ip0->src_address, &ip0->dst_address,
868 clib_net_to_host_u32 (esp0->spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700870 if (PREDICT_TRUE (p0 != 0))
871 {
Kingwel Xiec69ac312019-02-04 01:49:29 -0800872 ipsec_matched += 1;
873
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800874 pi0 = p0 - im->policies;
875 vlib_increment_combined_counter
876 (&ipsec_spd_policy_counters,
877 thread_index, pi0, 1,
878 clib_net_to_host_u16 (ip0->payload_length) +
879 header_size);
Kingwel Xiec69ac312019-02-04 01:49:29 -0800880
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100881 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200882 next0 = im->esp6_decrypt_next_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700883 vlib_buffer_advance (b0, header_size);
Piotr Bronowski06abf232022-09-20 14:44:36 +0000884 /* TODO Add policy matching for bypass and discard policy
885 * type */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700886 goto trace0;
887 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800888 else
889 {
890 pi0 = ~0;
Zachary Leaf26fec712021-10-26 10:05:58 -0500891 ipsec_unprocessed += 1;
892 next0 = IPSEC_INPUT_NEXT_DROP;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800893 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700894 }
Klement Sekera611864f2018-09-26 11:19:00 +0200895 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
896 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200897 p0 = ipsec6_input_protect_policy_match (spd0,
898 &ip0->src_address,
899 &ip0->dst_address,
900 clib_net_to_host_u32
901 (ah0->spi));
Klement Sekera611864f2018-09-26 11:19:00 +0200902
903 if (PREDICT_TRUE (p0 != 0))
904 {
Kingwel Xiec69ac312019-02-04 01:49:29 -0800905 ipsec_matched += 1;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800906 pi0 = p0 - im->policies;
907 vlib_increment_combined_counter
908 (&ipsec_spd_policy_counters,
909 thread_index, pi0, 1,
910 clib_net_to_host_u16 (ip0->payload_length) +
911 header_size);
Kingwel Xiec69ac312019-02-04 01:49:29 -0800912
Klement Sekera611864f2018-09-26 11:19:00 +0200913 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200914 next0 = im->ah6_decrypt_next_index;
Klement Sekera611864f2018-09-26 11:19:00 +0200915 goto trace0;
916 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800917 else
918 {
919 pi0 = ~0;
Zachary Leaf26fec712021-10-26 10:05:58 -0500920 ipsec_unprocessed += 1;
921 next0 = IPSEC_INPUT_NEXT_DROP;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800922 }
Klement Sekera611864f2018-09-26 11:19:00 +0200923 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800924 else
925 {
926 ipsec_unprocessed += 1;
927 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700929 trace0:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800930 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
931 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700932 {
933 ipsec_input_trace_t *tr =
934 vlib_add_trace (vm, node, b0, sizeof (*tr));
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800935
936 if (p0)
Piotr Bronowski06abf232022-09-20 14:44:36 +0000937 {
938 tr->sa_id = p0->sa_id;
939 tr->policy_type = p0->type;
940 }
941
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800942 tr->proto = ip0->protocol;
943 tr->spi = clib_net_to_host_u32 (esp0->spi);
944 tr->seq = clib_net_to_host_u32 (esp0->seq);
945 tr->spd = spd0->id;
Piotr Bronowski06abf232022-09-20 14:44:36 +0000946 tr->policy_index = pi0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700947 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700949 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
950 n_left_to_next, bi0, next0);
951 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
953 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800954
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200955 vlib_node_increment_counter (vm, ipsec6_input_node.index,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700956 IPSEC_INPUT_ERROR_RX_PKTS,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800957 from_frame->n_vectors - ipsec_unprocessed);
958
959 vlib_node_increment_counter (vm, ipsec6_input_node.index,
ShivaShankarK05464832020-04-14 14:01:03 +0530960 IPSEC_INPUT_ERROR_RX_POLICY_MATCH,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800961 ipsec_matched);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962
963 return from_frame->n_vectors;
964}
965
966
Damjan Mariond770cfc2019-09-02 19:00:33 +0200967VLIB_REGISTER_NODE (ipsec6_input_node) = {
Pierre Pfister057b3562018-12-10 17:01:01 +0100968 .name = "ipsec6-input-feature",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700969 .vector_size = sizeof (u32),
970 .format_trace = format_ipsec_input_trace,
971 .type = VLIB_NODE_TYPE_INTERNAL,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
973 .error_strings = ipsec_input_error_strings,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800974 .n_next_nodes = IPSEC_INPUT_N_NEXT,
975 .next_nodes = {
976#define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
977 foreach_ipsec_input_next
978#undef _
979 },
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980};
Damjan Marion1c80e832016-05-11 23:07:18 +0200981
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700982/*
983 * fd.io coding-style-patch-verification: ON
984 *
985 * Local Variables:
986 * eval: (c-set-style "gnu")
987 * End:
988 */