blob: 61b10fb9db65a74d7d757e61e2e65f6dd1387d7b [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
Kingwel Xiec69ac312019-02-04 01:49:29 -080028#define foreach_ipsec_input_error \
29_(RX_PKTS, "IPSEC pkts received") \
30_(RX_MATCH_PKTS, "IPSEC pkts matched")
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070032typedef enum
33{
Ed Warnickecb9cada2015-12-08 15:45:58 -070034#define _(sym,str) IPSEC_INPUT_ERROR_##sym,
35 foreach_ipsec_input_error
36#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070037 IPSEC_INPUT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070038} ipsec_input_error_t;
39
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040static char *ipsec_input_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070041#define _(sym,string) string,
42 foreach_ipsec_input_error
43#undef _
44};
45
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070046typedef struct
47{
Neale Rannsa09c1ff2019-02-04 01:10:30 -080048 ip_protocol_t proto;
Pierre Pfister62219272018-11-26 09:29:00 +010049 u32 spd;
Neale Rannsa09c1ff2019-02-04 01:10:30 -080050 u32 policy_index;
Matus Fabian5539a072016-09-07 05:57:09 -070051 u32 sa_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 u32 spi;
53 u32 seq;
54} ipsec_input_trace_t;
55
56/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070057static u8 *
58format_ipsec_input_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070062 ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020064 s = format (s, "%U: sa_id %u spd %u policy %d spi %u (0x%08x) seq %u",
Neale Rannsa09c1ff2019-02-04 01:10:30 -080065 format_ip_protocol, t->proto, t->sa_id,
Guillaume Solignac8f818cc2019-05-15 12:02:33 +020066 t->spd, t->policy_index, t->spi, t->spi, t->seq);
Matus Fabian5539a072016-09-07 05:57:09 -070067
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 return s;
69}
70
71always_inline ipsec_policy_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070072ipsec_input_protect_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, u32 spi)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
74 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070075 ipsec_policy_t *p;
76 ipsec_sa_t *s;
77 u32 *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Neale Rannsa09c1ff2019-02-04 01:10:30 -080079 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT])
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070080 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -080081 p = pool_elt_at_index (im->policies, *i);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070082 s = pool_elt_at_index (im->sad, p->sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070084 if (spi != s->spi)
85 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Damjan Mariond709cbc2019-03-26 13:16:42 +010087 if (ipsec_sa_is_set_IS_TUNNEL (s))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070088 {
Neale Rannsd2029bc2019-07-11 09:31:19 +000089 if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070090 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Neale Rannsd2029bc2019-07-11 09:31:19 +000092 if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070093 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070095 return p;
96 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Neale Rannsd2029bc2019-07-11 09:31:19 +000098 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070099 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
Neale Rannsd2029bc2019-07-11 09:31:19 +0000101 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700102 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
Neale Rannsd2029bc2019-07-11 09:31:19 +0000104 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700105 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Neale Rannsd2029bc2019-07-11 09:31:19 +0000107 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700108 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110 return p;
111 }
112 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113}
114
115always_inline uword
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700116ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
117 ip6_address_t * ua)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700119 if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
120 (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 return 1;
122 return 0;
123}
124
125always_inline ipsec_policy_t *
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200126ipsec6_input_protect_policy_match (ipsec_spd_t * spd,
127 ip6_address_t * sa,
128 ip6_address_t * da, u32 spi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129{
130 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700131 ipsec_policy_t *p;
132 ipsec_sa_t *s;
133 u32 *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800135 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT])
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700136 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800137 p = pool_elt_at_index (im->policies, *i);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700138 s = pool_elt_at_index (im->sad, p->sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700140 if (spi != s->spi)
141 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
Damjan Mariond709cbc2019-03-26 13:16:42 +0100143 if (ipsec_sa_is_set_IS_TUNNEL (s))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700144 {
145 if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6))
146 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700148 if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6))
149 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700151 return p;
152 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700154 if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
155 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700157 if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
158 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700160 return p;
161 }
162 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163}
164
Damjan Mariond770cfc2019-09-02 19:00:33 +0200165extern vlib_node_registration_t ipsec4_input_node;
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +0100166
Klement Sekerab8f35442018-10-29 13:38:19 +0100167VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm,
168 vlib_node_runtime_t * node,
169 vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800171 u32 n_left_from, *from, next_index, *to_next, thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 ipsec_main_t *im = &ipsec_main;
Kingwel Xiec69ac312019-02-04 01:49:29 -0800173 u32 ipsec_unprocessed = 0;
174 u32 ipsec_matched = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176 from = vlib_frame_vector_args (from_frame);
177 n_left_from = from_frame->n_vectors;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800178 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
180 next_index = node->cached_next_index;
181
182 while (n_left_from > 0)
183 {
184 u32 n_left_to_next;
185
186 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
187
188 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700189 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800190 u32 bi0, next0, pi0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700191 vlib_buffer_t *b0;
192 ip4_header_t *ip0;
193 esp_header_t *esp0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800194 ah_header_t *ah0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700195 ip4_ipsec_config_t *c0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700196 ipsec_spd_t *spd0;
Matus Fabian5539a072016-09-07 05:57:09 -0700197 ipsec_policy_t *p0 = 0;
Benoît Gannef7f49642019-10-25 15:26:27 +0200198 u8 has_space0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700200 bi0 = to_next[0] = from[0];
201 from += 1;
202 n_left_from -= 1;
203 to_next += 1;
204 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700206 b0 = vlib_get_buffer (vm, bi0);
Szymon Sliwa65a27272018-05-09 14:28:08 +0200207 b0->flags |= VNET_BUFFER_F_IS_IP4;
208 b0->flags &= ~VNET_BUFFER_F_IS_IP6;
Damjan Marion7d98a122018-07-19 20:42:08 +0200209 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700211 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700213 ip0 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
Klement Sekera4b089f22018-04-17 18:04:57 +0200215 if (PREDICT_TRUE
216 (ip0->protocol == IP_PROTOCOL_IPSEC_ESP
217 || ip0->protocol == IP_PROTOCOL_UDP))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700218 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219#if 0
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700220 clib_warning
221 ("packet received from %U to %U spi %u size %u spd_id %u",
222 format_ip4_address, ip0->src_address.as_u8,
223 format_ip4_address, ip0->dst_address.as_u8,
224 clib_net_to_host_u32 (esp0->spi),
225 clib_net_to_host_u16 (ip0->length), spd0->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226#endif
Matus Fabian5539a072016-09-07 05:57:09 -0700227
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800228 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
Klement Sekera4b089f22018-04-17 18:04:57 +0200229 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP))
230 {
231 esp0 =
232 (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t));
233 }
Benoît Gannef7f49642019-10-25 15:26:27 +0200234
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700235 p0 = ipsec_input_protect_policy_match (spd0,
Neale Rannsd2029bc2019-07-11 09:31:19 +0000236 clib_net_to_host_u32
237 (ip0->src_address.
238 as_u32),
239 clib_net_to_host_u32
240 (ip0->dst_address.
241 as_u32),
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700242 clib_net_to_host_u32
243 (esp0->spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
Benoît Gannef7f49642019-10-25 15:26:27 +0200245 has_space0 =
246 vlib_buffer_has_space (b0,
247 (clib_address_t) (esp0 + 1) -
248 (clib_address_t) ip0);
249
250 if (PREDICT_TRUE ((p0 != NULL) & (has_space0)))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700251 {
Kingwel Xiec69ac312019-02-04 01:49:29 -0800252 ipsec_matched += 1;
253
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800254 pi0 = p0 - im->policies;
255 vlib_increment_combined_counter
256 (&ipsec_spd_policy_counters,
257 thread_index, pi0, 1,
258 clib_net_to_host_u16 (ip0->length));
259
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100260 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200261 next0 = im->esp4_decrypt_next_index;
Klement Sekera4b089f22018-04-17 18:04:57 +0200262 vlib_buffer_advance (b0, ((u8 *) esp0 - (u8 *) ip0));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700263 goto trace0;
264 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800265 else
266 {
Benoît Gannef7f49642019-10-25 15:26:27 +0200267 p0 = 0;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800268 pi0 = ~0;
269 };
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800270
271 /* FIXME bypass and discard */
272 trace0:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800273 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
274 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800275 {
276 ipsec_input_trace_t *tr =
277 vlib_add_trace (vm, node, b0, sizeof (*tr));
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800278
279 tr->proto = ip0->protocol;
Benoît Gannef7f49642019-10-25 15:26:27 +0200280 tr->sa_id = p0 ? p0->sa_id : ~0;
281 tr->spi =
282 has_space0 ? clib_net_to_host_u32 (esp0->spi) : ~0;
283 tr->seq =
284 has_space0 ? clib_net_to_host_u32 (esp0->seq) : ~0;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800285 tr->spd = spd0->id;
286 tr->policy_index = pi0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800287 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700288 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800289 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700290 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800291 ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
292 p0 = ipsec_input_protect_policy_match (spd0,
Neale Rannsd2029bc2019-07-11 09:31:19 +0000293 clib_net_to_host_u32
294 (ip0->src_address.
295 as_u32),
296 clib_net_to_host_u32
297 (ip0->dst_address.
298 as_u32),
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800299 clib_net_to_host_u32
300 (ah0->spi));
301
Benoît Gannef7f49642019-10-25 15:26:27 +0200302 has_space0 =
303 vlib_buffer_has_space (b0,
304 (clib_address_t) (ah0 + 1) -
305 (clib_address_t) ip0);
306
307 if (PREDICT_TRUE ((p0 != NULL) & (has_space0)))
Matus Fabian5539a072016-09-07 05:57:09 -0700308 {
Kingwel Xiec69ac312019-02-04 01:49:29 -0800309 ipsec_matched += 1;
310
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800311 pi0 = p0 - im->policies;
312 vlib_increment_combined_counter
313 (&ipsec_spd_policy_counters,
314 thread_index, pi0, 1,
315 clib_net_to_host_u16 (ip0->length));
Kingwel Xiec69ac312019-02-04 01:49:29 -0800316
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800317 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200318 next0 = im->ah4_decrypt_next_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800319 goto trace1;
320 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800321 else
322 {
Benoît Gannef7f49642019-10-25 15:26:27 +0200323 p0 = 0;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800324 pi0 = ~0;
325 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800326 /* FIXME bypass and discard */
327 trace1:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800328 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
329 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800330 {
331 ipsec_input_trace_t *tr =
332 vlib_add_trace (vm, node, b0, sizeof (*tr));
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800333
334 tr->proto = ip0->protocol;
Benoît Gannef7f49642019-10-25 15:26:27 +0200335 tr->sa_id = p0 ? p0->sa_id : ~0;
336 tr->spi = has_space0 ? clib_net_to_host_u32 (ah0->spi) : ~0;
337 tr->seq =
338 has_space0 ? clib_net_to_host_u32 (ah0->seq_no) : ~0;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800339 tr->spd = spd0->id;
340 tr->policy_index = pi0;
Matus Fabian5539a072016-09-07 05:57:09 -0700341 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700342 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800343 else
344 {
345 ipsec_unprocessed += 1;
346 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700348 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
349 to_next, n_left_to_next, bi0,
350 next0);
351 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
353 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800354
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200355 vlib_node_increment_counter (vm, ipsec4_input_node.index,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700356 IPSEC_INPUT_ERROR_RX_PKTS,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800357 from_frame->n_vectors - ipsec_unprocessed);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
Kingwel Xiec69ac312019-02-04 01:49:29 -0800359 vlib_node_increment_counter (vm, ipsec4_input_node.index,
360 IPSEC_INPUT_ERROR_RX_MATCH_PKTS,
361 ipsec_matched);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 return from_frame->n_vectors;
363}
364
365
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700366/* *INDENT-OFF* */
Damjan Mariond770cfc2019-09-02 19:00:33 +0200367VLIB_REGISTER_NODE (ipsec4_input_node) = {
Pierre Pfister057b3562018-12-10 17:01:01 +0100368 .name = "ipsec4-input-feature",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 .vector_size = sizeof (u32),
370 .format_trace = format_ipsec_input_trace,
371 .type = VLIB_NODE_TYPE_INTERNAL,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
373 .error_strings = ipsec_input_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374 .n_next_nodes = IPSEC_INPUT_N_NEXT,
375 .next_nodes = {
376#define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
377 foreach_ipsec_input_next
378#undef _
379 },
380};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700381/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382
Damjan Mariond770cfc2019-09-02 19:00:33 +0200383extern vlib_node_registration_t ipsec6_input_node;
Damjan Marion1c80e832016-05-11 23:07:18 +0200384
Klement Sekerab8f35442018-10-29 13:38:19 +0100385
386VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm,
387 vlib_node_runtime_t * node,
388 vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800390 u32 n_left_from, *from, next_index, *to_next, thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 ipsec_main_t *im = &ipsec_main;
Kingwel Xiec69ac312019-02-04 01:49:29 -0800392 u32 ipsec_unprocessed = 0;
393 u32 ipsec_matched = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
395 from = vlib_frame_vector_args (from_frame);
396 n_left_from = from_frame->n_vectors;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800397 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
399 next_index = node->cached_next_index;
400
401 while (n_left_from > 0)
402 {
403 u32 n_left_to_next;
404
405 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
406
407 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700408 {
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800409 u32 bi0, next0, pi0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700410 vlib_buffer_t *b0;
411 ip6_header_t *ip0;
412 esp_header_t *esp0;
413 ip4_ipsec_config_t *c0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700414 ipsec_spd_t *spd0;
Matus Fabian5539a072016-09-07 05:57:09 -0700415 ipsec_policy_t *p0 = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200416 ah_header_t *ah0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700417 u32 header_size = sizeof (ip0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700419 bi0 = to_next[0] = from[0];
420 from += 1;
421 n_left_from -= 1;
422 to_next += 1;
423 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700425 b0 = vlib_get_buffer (vm, bi0);
Szymon Sliwa65a27272018-05-09 14:28:08 +0200426 b0->flags |= VNET_BUFFER_F_IS_IP6;
427 b0->flags &= ~VNET_BUFFER_F_IS_IP4;
Damjan Marion7d98a122018-07-19 20:42:08 +0200428 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700430 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700432 ip0 = vlib_buffer_get_current (b0);
433 esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
Klement Sekera611864f2018-09-26 11:19:00 +0200434 ah0 = (ah_header_t *) ((u8 *) ip0 + header_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700436 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
437 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438#if 0
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700439 clib_warning
440 ("packet received from %U to %U spi %u size %u spd_id %u",
441 format_ip6_address, &ip0->src_address, format_ip6_address,
442 &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
443 clib_net_to_host_u16 (ip0->payload_length) + header_size,
444 spd0->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445#endif
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200446 p0 = ipsec6_input_protect_policy_match (spd0,
447 &ip0->src_address,
448 &ip0->dst_address,
449 clib_net_to_host_u32
450 (esp0->spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700452 if (PREDICT_TRUE (p0 != 0))
453 {
Kingwel Xiec69ac312019-02-04 01:49:29 -0800454 ipsec_matched += 1;
455
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800456 pi0 = p0 - im->policies;
457 vlib_increment_combined_counter
458 (&ipsec_spd_policy_counters,
459 thread_index, pi0, 1,
460 clib_net_to_host_u16 (ip0->payload_length) +
461 header_size);
Kingwel Xiec69ac312019-02-04 01:49:29 -0800462
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100463 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200464 next0 = im->esp6_decrypt_next_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700465 vlib_buffer_advance (b0, header_size);
466 goto trace0;
467 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800468 else
469 {
470 pi0 = ~0;
471 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700472 }
Klement Sekera611864f2018-09-26 11:19:00 +0200473 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
474 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200475 p0 = ipsec6_input_protect_policy_match (spd0,
476 &ip0->src_address,
477 &ip0->dst_address,
478 clib_net_to_host_u32
479 (ah0->spi));
Klement Sekera611864f2018-09-26 11:19:00 +0200480
481 if (PREDICT_TRUE (p0 != 0))
482 {
Kingwel Xiec69ac312019-02-04 01:49:29 -0800483 ipsec_matched += 1;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800484 pi0 = p0 - im->policies;
485 vlib_increment_combined_counter
486 (&ipsec_spd_policy_counters,
487 thread_index, pi0, 1,
488 clib_net_to_host_u16 (ip0->payload_length) +
489 header_size);
Kingwel Xiec69ac312019-02-04 01:49:29 -0800490
Klement Sekera611864f2018-09-26 11:19:00 +0200491 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200492 next0 = im->ah6_decrypt_next_index;
Klement Sekera611864f2018-09-26 11:19:00 +0200493 goto trace0;
494 }
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800495 else
496 {
497 pi0 = ~0;
498 }
Klement Sekera611864f2018-09-26 11:19:00 +0200499 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800500 else
501 {
502 ipsec_unprocessed += 1;
503 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700505 trace0:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800506 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
507 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700508 {
509 ipsec_input_trace_t *tr =
510 vlib_add_trace (vm, node, b0, sizeof (*tr));
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800511
512 if (p0)
513 tr->sa_id = p0->sa_id;
514 tr->proto = ip0->protocol;
515 tr->spi = clib_net_to_host_u32 (esp0->spi);
516 tr->seq = clib_net_to_host_u32 (esp0->seq);
517 tr->spd = spd0->id;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700518 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700520 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
521 n_left_to_next, bi0, next0);
522 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
524 }
Kingwel Xiec69ac312019-02-04 01:49:29 -0800525
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200526 vlib_node_increment_counter (vm, ipsec6_input_node.index,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700527 IPSEC_INPUT_ERROR_RX_PKTS,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800528 from_frame->n_vectors - ipsec_unprocessed);
529
530 vlib_node_increment_counter (vm, ipsec6_input_node.index,
531 IPSEC_INPUT_ERROR_RX_MATCH_PKTS,
532 ipsec_matched);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
534 return from_frame->n_vectors;
535}
536
537
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700538/* *INDENT-OFF* */
Damjan Mariond770cfc2019-09-02 19:00:33 +0200539VLIB_REGISTER_NODE (ipsec6_input_node) = {
Pierre Pfister057b3562018-12-10 17:01:01 +0100540 .name = "ipsec6-input-feature",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 .vector_size = sizeof (u32),
542 .format_trace = format_ipsec_input_trace,
543 .type = VLIB_NODE_TYPE_INTERNAL,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
545 .error_strings = ipsec_input_error_strings,
Kingwel Xiec69ac312019-02-04 01:49:29 -0800546 .n_next_nodes = IPSEC_INPUT_N_NEXT,
547 .next_nodes = {
548#define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
549 foreach_ipsec_input_next
550#undef _
551 },
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700553/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +0200554
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700555/*
556 * fd.io coding-style-patch-verification: ON
557 *
558 * Local Variables:
559 * eval: (c-set-style "gnu")
560 * End:
561 */