blob: 96bad28c2b5f179f8ef091b682ca5df342bff793 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
23#include <vnet/ipsec/ipsec.h>
24#include <vnet/ipsec/esp.h>
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080025#include <vnet/ipsec/ah.h>
Neale Ranns918c1612019-02-21 23:34:59 -080026#include <vnet/ipsec/ipsec_io.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
ShivaShankarK05464832020-04-14 14:01:03 +053028#define foreach_ipsec_input_error \
29_(RX_PKTS, "IPSec pkts received") \
30_(RX_POLICY_MATCH, "IPSec policy match") \
31_(RX_POLICY_NO_MATCH, "IPSec policy not matched") \
32_(RX_POLICY_BYPASS, "IPSec policy bypass") \
33_(RX_POLICY_DISCARD, "IPSec policy discard")
Ed Warnickecb9cada2015-12-08 15:45:58 -070034
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070035typedef enum
36{
Ed Warnickecb9cada2015-12-08 15:45:58 -070037#define _(sym,str) IPSEC_INPUT_ERROR_##sym,
38 foreach_ipsec_input_error
39#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040 IPSEC_INPUT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070041} ipsec_input_error_t;
42
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070043static char *ipsec_input_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#define _(sym,string) string,
45 foreach_ipsec_input_error
46#undef _
47};
48
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070049typedef struct
50{
Neale Rannsa09c1ff2019-02-04 01:10:30 -080051 ip_protocol_t proto;
Pierre Pfister62219272018-11-26 09:29:00 +010052 u32 spd;
Neale Rannsa09c1ff2019-02-04 01:10:30 -080053 u32 policy_index;
Matus Fabian5539a072016-09-07 05:57:09 -070054 u32 sa_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 u32 spi;
56 u32 seq;
57} ipsec_input_trace_t;
58
59/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070060static u8 *
61format_ipsec_input_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070065 ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020067 s = format (s, "%U: sa_id %u spd %u policy %d spi %u (0x%08x) seq %u",
Neale Rannsa09c1ff2019-02-04 01:10:30 -080068 format_ip_protocol, t->proto, t->sa_id,
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020069 t->spd, t->policy_index, t->spi, t->spi, t->seq);
Matus Fabian5539a072016-09-07 05:57:09 -070070
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 return s;
72}
73
74always_inline ipsec_policy_t *
ShivaShankarK05464832020-04-14 14:01:03 +053075ipsec_input_policy_match (ipsec_spd_t * spd, u32 sa, u32 da,
76 ipsec_spd_policy_type_t policy_type)
77{
78 ipsec_main_t *im = &ipsec_main;
79 ipsec_policy_t *p;
80 u32 *i;
81
82 vec_foreach (i, spd->policies[policy_type])
83 {
84 p = pool_elt_at_index (im->policies, *i);
85
86 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
87 continue;
88
89 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
90 continue;
91
92 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
93 continue;
94
95 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
96 continue;
97
98 return p;
99 }
100 return 0;
101}
102
103always_inline ipsec_policy_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700104ipsec_input_protect_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, u32 spi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105{
106 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700107 ipsec_policy_t *p;
108 ipsec_sa_t *s;
109 u32 *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800111 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT])
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700112 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800113 p = pool_elt_at_index (im->policies, *i);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000114 s = ipsec_sa_get (p->sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700116 if (spi != s->spi)
117 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Damjan Mariond709cbc2019-03-26 13:16:42 +0100119 if (ipsec_sa_is_set_IS_TUNNEL (s))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700120 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000121 if (da != clib_net_to_host_u32 (s->tunnel.t_dst.ip.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700122 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
Neale Ranns9ec846c2021-02-09 14:04:02 +0000124 if (sa != clib_net_to_host_u32 (s->tunnel.t_src.ip.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700125 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700127 return p;
128 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Neale Rannsd2029bc2019-07-11 09:31:19 +0000130 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700131 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Neale Rannsd2029bc2019-07-11 09:31:19 +0000133 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700134 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Neale Rannsd2029bc2019-07-11 09:31:19 +0000136 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700137 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
Neale Rannsd2029bc2019-07-11 09:31:19 +0000139 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700140 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700142 return p;
143 }
144 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145}
146
147always_inline uword
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700148ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
149 ip6_address_t * ua)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700151 if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
152 (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 return 1;
154 return 0;
155}
156
157always_inline ipsec_policy_t *
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200158ipsec6_input_protect_policy_match (ipsec_spd_t * spd,
159 ip6_address_t * sa,
160 ip6_address_t * da, u32 spi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161{
162 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700163 ipsec_policy_t *p;
164 ipsec_sa_t *s;
165 u32 *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800167 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT])
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700168 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800169 p = pool_elt_at_index (im->policies, *i);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000170 s = ipsec_sa_get (p->sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700172 if (spi != s->spi)
173 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Damjan Mariond709cbc2019-03-26 13:16:42 +0100175 if (ipsec_sa_is_set_IS_TUNNEL (s))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700176 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000177 if (!ip6_address_is_equal (sa, &s->tunnel.t_src.ip.ip6))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700178 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
Neale Ranns9ec846c2021-02-09 14:04:02 +0000180 if (!ip6_address_is_equal (da, &s->tunnel.t_dst.ip.ip6))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700181 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700183 return p;
184 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700186 if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
187 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700189 if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
190 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700192 return p;
193 }
194 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195}
196
Damjan Mariond770cfc2019-09-02 19:00:33 +0200197extern vlib_node_registration_t ipsec4_input_node;
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +0100198
Klement Sekerab8f35442018-10-29 13:38:19 +0100199VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm,
200 vlib_node_runtime_t * node,
ShivaShankarK05464832020-04-14 14:01:03 +0530201 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202{
ShivaShankarK05464832020-04-14 14:01:03 +0530203 u32 n_left_from, *from, thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 ipsec_main_t *im = &ipsec_main;
ShivaShankarK05464832020-04-14 14:01:03 +0530205 u64 ipsec_unprocessed = 0, ipsec_matched = 0;
206 u64 ipsec_dropped = 0, ipsec_bypassed = 0;
207 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
208 vlib_buffer_t **b = bufs;
209 u16 nexts[VLIB_FRAME_SIZE], *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
ShivaShankarK05464832020-04-14 14:01:03 +0530211 from = vlib_frame_vector_args (frame);
212 n_left_from = frame->n_vectors;
213 next = nexts;
214 vlib_get_buffers (vm, from, bufs, n_left_from);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800215 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217
218 while (n_left_from > 0)
219 {
ShivaShankarK05464832020-04-14 14:01:03 +0530220 u32 next32, pi0;
221 ip4_header_t *ip0;
222 esp_header_t *esp0 = NULL;
223 ah_header_t *ah0;
224 ip4_ipsec_config_t *c0;
225 ipsec_spd_t *spd0;
226 ipsec_policy_t *p0 = NULL;
227 u8 has_space0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
ShivaShankarK05464832020-04-14 14:01:03 +0530229 if (n_left_from > 2)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700230 {
ShivaShankarK05464832020-04-14 14:01:03 +0530231 vlib_prefetch_buffer_data (b[1], LOAD);
232 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
ShivaShankarK05464832020-04-14 14:01:03 +0530234 b[0]->flags |= VNET_BUFFER_F_IS_IP4;
235 b[0]->flags &= ~VNET_BUFFER_F_IS_IP6;
236 c0 = vnet_feature_next_with_data (&next32, b[0], sizeof (c0[0]));
237 next[0] = (u16) next32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
ShivaShankarK05464832020-04-14 14:01:03 +0530239 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240
ShivaShankarK05464832020-04-14 14:01:03 +0530241 ip0 = vlib_buffer_get_current (b[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
ShivaShankarK05464832020-04-14 14:01:03 +0530243 if (PREDICT_TRUE
244 (ip0->protocol == IP_PROTOCOL_IPSEC_ESP
245 || ip0->protocol == IP_PROTOCOL_UDP))
246 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
ShivaShankarK05464832020-04-14 14:01:03 +0530248 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
249 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700250 {
ShivaShankarK05464832020-04-14 14:01:03 +0530251 /* FIXME Skip, if not a UDP encapsulated packet */
252 esp0 = (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700253 }
ShivaShankarK05464832020-04-14 14:01:03 +0530254
255 p0 = ipsec_input_protect_policy_match (spd0,
256 clib_net_to_host_u32
257 (ip0->src_address.as_u32),
258 clib_net_to_host_u32
259 (ip0->dst_address.as_u32),
260 clib_net_to_host_u32
261 (esp0->spi));
262
263 has_space0 =
264 vlib_buffer_has_space (b[0],
265 (clib_address_t) (esp0 + 1) -
266 (clib_address_t) ip0);
267
268 if (PREDICT_TRUE ((p0 != NULL) & (has_space0)))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700269 {
ShivaShankarK05464832020-04-14 14:01:03 +0530270 ipsec_matched += 1;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800271
ShivaShankarK05464832020-04-14 14:01:03 +0530272 pi0 = p0 - im->policies;
273 vlib_increment_combined_counter
274 (&ipsec_spd_policy_counters,
275 thread_index, pi0, 1, clib_net_to_host_u16 (ip0->length));
Benoît Gannef7f49642019-10-25 15:26:27 +0200276
ShivaShankarK05464832020-04-14 14:01:03 +0530277 vnet_buffer (b[0])->ipsec.sad_index = p0->sa_index;
278 next[0] = im->esp4_decrypt_next_index;
279 vlib_buffer_advance (b[0], ((u8 *) esp0 - (u8 *) ip0));
280 goto trace0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700281 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800282 else
283 {
ShivaShankarK05464832020-04-14 14:01:03 +0530284 p0 = 0;
285 pi0 = ~0;
286 };
287
288 p0 = ipsec_input_policy_match (spd0,
289 clib_net_to_host_u32
290 (ip0->src_address.as_u32),
291 clib_net_to_host_u32
292 (ip0->dst_address.as_u32),
293 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
294 if (PREDICT_TRUE ((p0 != NULL)))
295 {
296 ipsec_bypassed += 1;
Zachary Leaffbab65b2021-06-07 03:01:07 -0500297
ShivaShankarK05464832020-04-14 14:01:03 +0530298 pi0 = p0 - im->policies;
Zachary Leaffbab65b2021-06-07 03:01:07 -0500299 vlib_increment_combined_counter (
300 &ipsec_spd_policy_counters, thread_index, pi0, 1,
301 clib_net_to_host_u16 (ip0->length));
302
ShivaShankarK05464832020-04-14 14:01:03 +0530303 goto trace0;
304 }
305 else
306 {
307 p0 = 0;
308 pi0 = ~0;
309 };
310
311 p0 = ipsec_input_policy_match (spd0,
312 clib_net_to_host_u32
313 (ip0->src_address.as_u32),
314 clib_net_to_host_u32
315 (ip0->dst_address.as_u32),
316 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
317 if (PREDICT_TRUE ((p0 != NULL)))
318 {
319 ipsec_dropped += 1;
Zachary Leaffbab65b2021-06-07 03:01:07 -0500320
ShivaShankarK05464832020-04-14 14:01:03 +0530321 pi0 = p0 - im->policies;
Zachary Leaffbab65b2021-06-07 03:01:07 -0500322 vlib_increment_combined_counter (
323 &ipsec_spd_policy_counters, thread_index, pi0, 1,
324 clib_net_to_host_u16 (ip0->length));
325
ShivaShankarK05464832020-04-14 14:01:03 +0530326 next[0] = IPSEC_INPUT_NEXT_DROP;
327 goto trace0;
328 }
329 else
330 {
331 p0 = 0;
332 pi0 = ~0;
333 };
334 trace0:
335 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
336 PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
337 {
338 ipsec_input_trace_t *tr =
339 vlib_add_trace (vm, node, b[0], sizeof (*tr));
340
341 tr->proto = ip0->protocol;
342 tr->sa_id = p0 ? p0->sa_id : ~0;
343 tr->spi = has_space0 ? clib_net_to_host_u32 (esp0->spi) : ~0;
344 tr->seq = has_space0 ? clib_net_to_host_u32 (esp0->seq) : ~0;
345 tr->spd = spd0->id;
346 tr->policy_index = pi0;
347 }
348 }
349 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
350 {
351 ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
352 p0 = ipsec_input_protect_policy_match (spd0,
353 clib_net_to_host_u32
354 (ip0->src_address.as_u32),
355 clib_net_to_host_u32
356 (ip0->dst_address.as_u32),
357 clib_net_to_host_u32
358 (ah0->spi));
359
360 has_space0 =
361 vlib_buffer_has_space (b[0],
362 (clib_address_t) (ah0 + 1) -
363 (clib_address_t) ip0);
364
365 if (PREDICT_TRUE ((p0 != NULL) & (has_space0)))
366 {
367 ipsec_matched += 1;
368
369 pi0 = p0 - im->policies;
370 vlib_increment_combined_counter
371 (&ipsec_spd_policy_counters,
372 thread_index, pi0, 1, clib_net_to_host_u16 (ip0->length));
373
374 vnet_buffer (b[0])->ipsec.sad_index = p0->sa_index;
375 next[0] = im->ah4_decrypt_next_index;
376 goto trace1;
377 }
378 else
379 {
380 p0 = 0;
381 pi0 = ~0;
Kingwel Xiec69ac312019-02-04 01:49:29 -0800382 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383
ShivaShankarK05464832020-04-14 14:01:03 +0530384 p0 = ipsec_input_policy_match (spd0,
385 clib_net_to_host_u32
386 (ip0->src_address.as_u32),
387 clib_net_to_host_u32
388 (ip0->dst_address.as_u32),
389 IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS);
390 if (PREDICT_TRUE ((p0 != NULL)))
391 {
392 ipsec_bypassed += 1;
Zachary Leaffbab65b2021-06-07 03:01:07 -0500393
ShivaShankarK05464832020-04-14 14:01:03 +0530394 pi0 = p0 - im->policies;
Zachary Leaffbab65b2021-06-07 03:01:07 -0500395 vlib_increment_combined_counter (
396 &ipsec_spd_policy_counters, thread_index, pi0, 1,
397 clib_net_to_host_u16 (ip0->length));
398
ShivaShankarK05464832020-04-14 14:01:03 +0530399 goto trace1;
400 }
401 else
402 {
403 p0 = 0;
404 pi0 = ~0;
405 };
406
407 p0 = ipsec_input_policy_match (spd0,
408 clib_net_to_host_u32
409 (ip0->src_address.as_u32),
410 clib_net_to_host_u32
411 (ip0->dst_address.as_u32),
412 IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD);
413 if (PREDICT_TRUE ((p0 != NULL)))
414 {
415 ipsec_dropped += 1;
Zachary Leaffbab65b2021-06-07 03:01:07 -0500416
ShivaShankarK05464832020-04-14 14:01:03 +0530417 pi0 = p0 - im->policies;
Zachary Leaffbab65b2021-06-07 03:01:07 -0500418 vlib_increment_combined_counter (
419 &ipsec_spd_policy_counters, thread_index, pi0, 1,
420 clib_net_to_host_u16 (ip0->length));
421
ShivaShankarK05464832020-04-14 14:01:03 +0530422 next[0] = IPSEC_INPUT_NEXT_DROP;
423 goto trace1;
424 }
425 else
426 {
427 p0 = 0;
428 pi0 = ~0;
429 };
430 trace1:
431 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
432 PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
433 {
434 ipsec_input_trace_t *tr =
435 vlib_add_trace (vm, node, b[0], sizeof (*tr));
436
437 tr->proto = ip0->protocol;
438 tr->sa_id = p0 ? p0->sa_id : ~0;
439 tr->spi = has_space0 ? clib_net_to_host_u32 (ah0->spi) : ~0;
440 tr->seq = has_space0 ? clib_net_to_host_u32 (ah0->seq_no) : ~0;
441 tr->spd = spd0->id;
442 tr->policy_index = pi0;
443 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700444 }
ShivaShankarK05464832020-04-14 14:01:03 +0530445 else
446 {
447 ipsec_unprocessed += 1;
448 }
449 n_left_from -= 1;
450 b += 1;
451 next += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800453
ShivaShankarK05464832020-04-14 14:01:03 +0530454 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455
Kingwel Xiec69ac312019-02-04 01:49:29 -0800456 vlib_node_increment_counter (vm, ipsec4_input_node.index,
ShivaShankarK05464832020-04-14 14:01:03 +0530457 IPSEC_INPUT_ERROR_RX_PKTS, frame->n_vectors);
458
459 vlib_node_increment_counter (vm, ipsec4_input_node.index,
460 IPSEC_INPUT_ERROR_RX_POLICY_MATCH,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800461 ipsec_matched);
ShivaShankarK05464832020-04-14 14:01:03 +0530462
463 vlib_node_increment_counter (vm, ipsec4_input_node.index,
464 IPSEC_INPUT_ERROR_RX_POLICY_NO_MATCH,
465 ipsec_unprocessed);
466
467 vlib_node_increment_counter (vm, ipsec4_input_node.index,
468 IPSEC_INPUT_ERROR_RX_POLICY_DISCARD,
469 ipsec_dropped);
470
471 vlib_node_increment_counter (vm, ipsec4_input_node.index,
472 IPSEC_INPUT_ERROR_RX_POLICY_BYPASS,
473 ipsec_bypassed);
474
475 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476}
477
478
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700479/* *INDENT-OFF* */
Damjan Mariond770cfc2019-09-02 19:00:33 +0200480VLIB_REGISTER_NODE (ipsec4_input_node) = {
Pierre Pfister057b3562018-12-10 17:01:01 +0100481 .name = "ipsec4-input-feature",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482 .vector_size = sizeof (u32),
483 .format_trace = format_ipsec_input_trace,
484 .type = VLIB_NODE_TYPE_INTERNAL,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
486 .error_strings = ipsec_input_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 .n_next_nodes = IPSEC_INPUT_N_NEXT,
488 .next_nodes = {
489#define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
490 foreach_ipsec_input_next
491#undef _
492 },
493};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700494/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495
Damjan Mariond770cfc2019-09-02 19:00:33 +0200496extern vlib_node_registration_t ipsec6_input_node;
Damjan Marion1c80e832016-05-11 23:07:18 +0200497
Klement Sekerab8f35442018-10-29 13:38:19 +0100498
499VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm,
500 vlib_node_runtime_t * node,
501 vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800503 u32 n_left_from, *from, next_index, *to_next, thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 ipsec_main_t *im = &ipsec_main;
Kingwel Xiec69ac312019-02-04 01:49:29 -0800505 u32 ipsec_unprocessed = 0;
506 u32 ipsec_matched = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507
508 from = vlib_frame_vector_args (from_frame);
509 n_left_from = from_frame->n_vectors;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800510 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
512 next_index = node->cached_next_index;
513
514 while (n_left_from > 0)
515 {
516 u32 n_left_to_next;
517
518 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
519
520 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700521 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800522 u32 bi0, next0, pi0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700523 vlib_buffer_t *b0;
524 ip6_header_t *ip0;
525 esp_header_t *esp0;
526 ip4_ipsec_config_t *c0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700527 ipsec_spd_t *spd0;
Matus Fabian5539a072016-09-07 05:57:09 -0700528 ipsec_policy_t *p0 = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200529 ah_header_t *ah0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700530 u32 header_size = sizeof (ip0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700532 bi0 = to_next[0] = from[0];
533 from += 1;
534 n_left_from -= 1;
535 to_next += 1;
536 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700538 b0 = vlib_get_buffer (vm, bi0);
Szymon Sliwa65a27272018-05-09 14:28:08 +0200539 b0->flags |= VNET_BUFFER_F_IS_IP6;
540 b0->flags &= ~VNET_BUFFER_F_IS_IP4;
Damjan Marion7d98a122018-07-19 20:42:08 +0200541 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700543 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700545 ip0 = vlib_buffer_get_current (b0);
546 esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
Klement Sekera611864f2018-09-26 11:19:00 +0200547 ah0 = (ah_header_t *) ((u8 *) ip0 + header_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700549 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
550 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551#if 0
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700552 clib_warning
553 ("packet received from %U to %U spi %u size %u spd_id %u",
554 format_ip6_address, &ip0->src_address, format_ip6_address,
555 &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
556 clib_net_to_host_u16 (ip0->payload_length) + header_size,
557 spd0->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558#endif
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200559 p0 = ipsec6_input_protect_policy_match (spd0,
560 &ip0->src_address,
561 &ip0->dst_address,
562 clib_net_to_host_u32
563 (esp0->spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700565 if (PREDICT_TRUE (p0 != 0))
566 {
Kingwel Xiec69ac312019-02-04 01:49:29 -0800567 ipsec_matched += 1;
568
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800569 pi0 = p0 - im->policies;
570 vlib_increment_combined_counter
571 (&ipsec_spd_policy_counters,
572 thread_index, pi0, 1,
573 clib_net_to_host_u16 (ip0->payload_length) +
574 header_size);
Kingwel Xiec69ac312019-02-04 01:49:29 -0800575
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100576 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200577 next0 = im->esp6_decrypt_next_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700578 vlib_buffer_advance (b0, header_size);
579 goto trace0;
580 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800581 else
582 {
583 pi0 = ~0;
584 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700585 }
Klement Sekera611864f2018-09-26 11:19:00 +0200586 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
587 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200588 p0 = ipsec6_input_protect_policy_match (spd0,
589 &ip0->src_address,
590 &ip0->dst_address,
591 clib_net_to_host_u32
592 (ah0->spi));
Klement Sekera611864f2018-09-26 11:19:00 +0200593
594 if (PREDICT_TRUE (p0 != 0))
595 {
Kingwel Xiec69ac312019-02-04 01:49:29 -0800596 ipsec_matched += 1;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800597 pi0 = p0 - im->policies;
598 vlib_increment_combined_counter
599 (&ipsec_spd_policy_counters,
600 thread_index, pi0, 1,
601 clib_net_to_host_u16 (ip0->payload_length) +
602 header_size);
Kingwel Xiec69ac312019-02-04 01:49:29 -0800603
Klement Sekera611864f2018-09-26 11:19:00 +0200604 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200605 next0 = im->ah6_decrypt_next_index;
Klement Sekera611864f2018-09-26 11:19:00 +0200606 goto trace0;
607 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800608 else
609 {
610 pi0 = ~0;
611 }
Klement Sekera611864f2018-09-26 11:19:00 +0200612 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800613 else
614 {
615 ipsec_unprocessed += 1;
616 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700618 trace0:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800619 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
620 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700621 {
622 ipsec_input_trace_t *tr =
623 vlib_add_trace (vm, node, b0, sizeof (*tr));
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800624
625 if (p0)
626 tr->sa_id = p0->sa_id;
627 tr->proto = ip0->protocol;
628 tr->spi = clib_net_to_host_u32 (esp0->spi);
629 tr->seq = clib_net_to_host_u32 (esp0->seq);
630 tr->spd = spd0->id;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700631 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700633 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
634 n_left_to_next, bi0, next0);
635 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
637 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800638
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200639 vlib_node_increment_counter (vm, ipsec6_input_node.index,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700640 IPSEC_INPUT_ERROR_RX_PKTS,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800641 from_frame->n_vectors - ipsec_unprocessed);
642
643 vlib_node_increment_counter (vm, ipsec6_input_node.index,
ShivaShankarK05464832020-04-14 14:01:03 +0530644 IPSEC_INPUT_ERROR_RX_POLICY_MATCH,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800645 ipsec_matched);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
647 return from_frame->n_vectors;
648}
649
650
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700651/* *INDENT-OFF* */
Damjan Mariond770cfc2019-09-02 19:00:33 +0200652VLIB_REGISTER_NODE (ipsec6_input_node) = {
Pierre Pfister057b3562018-12-10 17:01:01 +0100653 .name = "ipsec6-input-feature",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 .vector_size = sizeof (u32),
655 .format_trace = format_ipsec_input_trace,
656 .type = VLIB_NODE_TYPE_INTERNAL,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
658 .error_strings = ipsec_input_error_strings,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800659 .n_next_nodes = IPSEC_INPUT_N_NEXT,
660 .next_nodes = {
661#define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
662 foreach_ipsec_input_next
663#undef _
664 },
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700666/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +0200667
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700668/*
669 * fd.io coding-style-patch-verification: ON
670 *
671 * Local Variables:
672 * eval: (c-set-style "gnu")
673 * End:
674 */