blob: 19c3b5bcef1809d05fc3b6e294442806c953bca4 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
Ed Warnickecb9cada2015-12-08 15:45:58 -070027#define foreach_ipsec_input_error \
28 _(RX_PKTS, "IPSEC pkts received") \
29 _(DECRYPTION_FAILED, "IPSEC decryption failed")
30
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070031typedef enum
32{
Ed Warnickecb9cada2015-12-08 15:45:58 -070033#define _(sym,str) IPSEC_INPUT_ERROR_##sym,
34 foreach_ipsec_input_error
35#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070036 IPSEC_INPUT_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070037} ipsec_input_error_t;
38
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039static char *ipsec_input_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070040#define _(sym,string) string,
41 foreach_ipsec_input_error
42#undef _
43};
44
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070045typedef struct
46{
Matus Fabian5539a072016-09-07 05:57:09 -070047 u32 sa_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 u32 spi;
49 u32 seq;
50} ipsec_input_trace_t;
51
52/* packet trace format function */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070053static u8 *
54format_ipsec_input_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070055{
56 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
57 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070058 ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
Matus Fabian5539a072016-09-07 05:57:09 -070060 if (t->spi == 0 && t->seq == 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070061 {
Matus Fabian5539a072016-09-07 05:57:09 -070062 s = format (s, "esp: no esp packet");
63 return s;
64 }
65
66 if (t->sa_id != 0)
67 {
68 s = format (s, "esp: sa_id %u spi %u seq %u", t->sa_id, t->spi, t->seq);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070069 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071 {
Matus Fabian5539a072016-09-07 05:57:09 -070072 s = format (s, "esp: no sa spi %u seq %u", t->spi, t->seq);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070073 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 return s;
75}
76
77always_inline ipsec_policy_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070078ipsec_input_protect_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, u32 spi)
Ed Warnickecb9cada2015-12-08 15:45:58 -070079{
80 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070081 ipsec_policy_t *p;
82 ipsec_sa_t *s;
83 u32 *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070085 vec_foreach (i, spd->ipv4_inbound_protect_policy_indices)
86 {
87 p = pool_elt_at_index (spd->policies, *i);
88 s = pool_elt_at_index (im->sad, p->sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070090 if (spi != s->spi)
91 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070093 if (s->is_tunnel)
94 {
95 if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32))
96 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070098 if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32))
99 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700101 return p;
102 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700104 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
105 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700107 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
108 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
111 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700113 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
114 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700116 return p;
117 }
118 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119}
120
121always_inline uword
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700122ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
123 ip6_address_t * ua)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700125 if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
126 (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 return 1;
128 return 0;
129}
130
131always_inline ipsec_policy_t *
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200132ipsec6_input_protect_policy_match (ipsec_spd_t * spd,
133 ip6_address_t * sa,
134 ip6_address_t * da, u32 spi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135{
136 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700137 ipsec_policy_t *p;
138 ipsec_sa_t *s;
139 u32 *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700141 vec_foreach (i, spd->ipv6_inbound_protect_policy_indices)
142 {
143 p = pool_elt_at_index (spd->policies, *i);
144 s = pool_elt_at_index (im->sad, p->sa_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700146 if (spi != s->spi)
147 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700149 if (s->is_tunnel)
150 {
151 if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6))
152 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700154 if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6))
155 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700157 return p;
158 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700160 if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
161 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700163 if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
164 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700166 return p;
167 }
168 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169}
170
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200171static vlib_node_registration_t ipsec4_input_node;
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +0100172
Klement Sekerab8f35442018-10-29 13:38:19 +0100173VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm,
174 vlib_node_runtime_t * node,
175 vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 u32 n_left_from, *from, next_index, *to_next;
178 ipsec_main_t *im = &ipsec_main;
179
180 from = vlib_frame_vector_args (from_frame);
181 n_left_from = from_frame->n_vectors;
182
183 next_index = node->cached_next_index;
184
185 while (n_left_from > 0)
186 {
187 u32 n_left_to_next;
188
189 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
190
191 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700192 {
193 u32 bi0, next0;
194 vlib_buffer_t *b0;
195 ip4_header_t *ip0;
196 esp_header_t *esp0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800197 ah_header_t *ah0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700198 ip4_ipsec_config_t *c0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700199 ipsec_spd_t *spd0;
Matus Fabian5539a072016-09-07 05:57:09 -0700200 ipsec_policy_t *p0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700202 bi0 = to_next[0] = from[0];
203 from += 1;
204 n_left_from -= 1;
205 to_next += 1;
206 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700208 b0 = vlib_get_buffer (vm, bi0);
Szymon Sliwa65a27272018-05-09 14:28:08 +0200209 b0->flags |= VNET_BUFFER_F_IS_IP4;
210 b0->flags &= ~VNET_BUFFER_F_IS_IP6;
Damjan Marion7d98a122018-07-19 20:42:08 +0200211 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700213 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700215 ip0 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Klement Sekera4b089f22018-04-17 18:04:57 +0200217 if (PREDICT_TRUE
218 (ip0->protocol == IP_PROTOCOL_IPSEC_ESP
219 || ip0->protocol == IP_PROTOCOL_UDP))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700220 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221#if 0
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700222 clib_warning
223 ("packet received from %U to %U spi %u size %u spd_id %u",
224 format_ip4_address, ip0->src_address.as_u8,
225 format_ip4_address, ip0->dst_address.as_u8,
226 clib_net_to_host_u32 (esp0->spi),
227 clib_net_to_host_u16 (ip0->length), spd0->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228#endif
Matus Fabian5539a072016-09-07 05:57:09 -0700229
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800230 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
Klement Sekera4b089f22018-04-17 18:04:57 +0200231 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP))
232 {
233 esp0 =
234 (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t));
235 }
236 /* FIXME TODO missing check whether there is enough data inside
237 * IP/UDP to contain ESP header & stuff ? */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700238 p0 = ipsec_input_protect_policy_match (spd0,
239 clib_net_to_host_u32
Matus Fabian5539a072016-09-07 05:57:09 -0700240 (ip0->src_address.
241 as_u32),
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700242 clib_net_to_host_u32
Matus Fabian5539a072016-09-07 05:57:09 -0700243 (ip0->dst_address.
244 as_u32),
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700245 clib_net_to_host_u32
246 (esp0->spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700248 if (PREDICT_TRUE (p0 != 0))
249 {
250 p0->counter.packets++;
251 p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100252 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
253 vnet_buffer (b0)->ipsec.flags = 0;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200254 next0 = im->esp4_decrypt_next_index;
Klement Sekera4b089f22018-04-17 18:04:57 +0200255 vlib_buffer_advance (b0, ((u8 *) esp0 - (u8 *) ip0));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700256 goto trace0;
257 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800258
259 /* FIXME bypass and discard */
260 trace0:
261 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
262 {
263 ipsec_input_trace_t *tr =
264 vlib_add_trace (vm, node, b0, sizeof (*tr));
Klement Sekera4b089f22018-04-17 18:04:57 +0200265 if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP ||
266 ip0->protocol == IP_PROTOCOL_UDP)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800267 {
268 if (p0)
269 tr->sa_id = p0->sa_id;
270 tr->spi = clib_host_to_net_u32 (esp0->spi);
271 tr->seq = clib_host_to_net_u32 (esp0->seq);
272 }
273 }
274
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700275 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800278 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_AH))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700279 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800280 ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
281 p0 = ipsec_input_protect_policy_match (spd0,
282 clib_net_to_host_u32
283 (ip0->src_address.
284 as_u32),
285 clib_net_to_host_u32
286 (ip0->dst_address.
287 as_u32),
288 clib_net_to_host_u32
289 (ah0->spi));
290
291 if (PREDICT_TRUE (p0 != 0))
Matus Fabian5539a072016-09-07 05:57:09 -0700292 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800293 p0->counter.packets++;
294 p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
295 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
296 vnet_buffer (b0)->ipsec.flags = 0;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200297 next0 = im->ah4_decrypt_next_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800298 goto trace1;
299 }
300 /* FIXME bypass and discard */
301 trace1:
302 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
303 {
304 ipsec_input_trace_t *tr =
305 vlib_add_trace (vm, node, b0, sizeof (*tr));
306 if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
307 {
308 if (p0)
309 tr->sa_id = p0->sa_id;
310 tr->spi = clib_host_to_net_u32 (ah0->spi);
311 tr->seq = clib_host_to_net_u32 (ah0->seq_no);
312 }
Matus Fabian5539a072016-09-07 05:57:09 -0700313 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700314 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700316 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
317 to_next, n_left_to_next, bi0,
318 next0);
319 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
321 }
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200322 vlib_node_increment_counter (vm, ipsec4_input_node.index,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700323 IPSEC_INPUT_ERROR_RX_PKTS,
324 from_frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
326 return from_frame->n_vectors;
327}
328
329
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700330/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200331VLIB_REGISTER_NODE (ipsec4_input_node,static) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200332 .name = "ipsec4-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 .vector_size = sizeof (u32),
334 .format_trace = format_ipsec_input_trace,
335 .type = VLIB_NODE_TYPE_INTERNAL,
336
337 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
338 .error_strings = ipsec_input_error_strings,
339
340 .n_next_nodes = IPSEC_INPUT_N_NEXT,
341 .next_nodes = {
342#define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
343 foreach_ipsec_input_next
344#undef _
345 },
346};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700347/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200349static vlib_node_registration_t ipsec6_input_node;
Damjan Marion1c80e832016-05-11 23:07:18 +0200350
Klement Sekerab8f35442018-10-29 13:38:19 +0100351
352VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm,
353 vlib_node_runtime_t * node,
354 vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 u32 n_left_from, *from, next_index, *to_next;
357 ipsec_main_t *im = &ipsec_main;
358
359 from = vlib_frame_vector_args (from_frame);
360 n_left_from = from_frame->n_vectors;
361
362 next_index = node->cached_next_index;
363
364 while (n_left_from > 0)
365 {
366 u32 n_left_to_next;
367
368 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
369
370 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700371 {
372 u32 bi0, next0;
373 vlib_buffer_t *b0;
374 ip6_header_t *ip0;
375 esp_header_t *esp0;
376 ip4_ipsec_config_t *c0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700377 ipsec_spd_t *spd0;
Matus Fabian5539a072016-09-07 05:57:09 -0700378 ipsec_policy_t *p0 = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200379 ah_header_t *ah0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700380 u32 header_size = sizeof (ip0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700382 bi0 = to_next[0] = from[0];
383 from += 1;
384 n_left_from -= 1;
385 to_next += 1;
386 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700388 b0 = vlib_get_buffer (vm, bi0);
Szymon Sliwa65a27272018-05-09 14:28:08 +0200389 b0->flags |= VNET_BUFFER_F_IS_IP6;
390 b0->flags &= ~VNET_BUFFER_F_IS_IP4;
Damjan Marion7d98a122018-07-19 20:42:08 +0200391 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700393 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700395 ip0 = vlib_buffer_get_current (b0);
396 esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
Klement Sekera611864f2018-09-26 11:19:00 +0200397 ah0 = (ah_header_t *) ((u8 *) ip0 + header_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700399 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
400 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401#if 0
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700402 clib_warning
403 ("packet received from %U to %U spi %u size %u spd_id %u",
404 format_ip6_address, &ip0->src_address, format_ip6_address,
405 &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
406 clib_net_to_host_u16 (ip0->payload_length) + header_size,
407 spd0->id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408#endif
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200409 p0 = ipsec6_input_protect_policy_match (spd0,
410 &ip0->src_address,
411 &ip0->dst_address,
412 clib_net_to_host_u32
413 (esp0->spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700415 if (PREDICT_TRUE (p0 != 0))
416 {
417 p0->counter.packets++;
418 p0->counter.bytes +=
419 clib_net_to_host_u16 (ip0->payload_length);
420 p0->counter.bytes += header_size;
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100421 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
422 vnet_buffer (b0)->ipsec.flags = 0;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200423 next0 = im->esp6_decrypt_next_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700424 vlib_buffer_advance (b0, header_size);
425 goto trace0;
426 }
427 }
Klement Sekera611864f2018-09-26 11:19:00 +0200428 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
429 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200430 p0 = ipsec6_input_protect_policy_match (spd0,
431 &ip0->src_address,
432 &ip0->dst_address,
433 clib_net_to_host_u32
434 (ah0->spi));
Klement Sekera611864f2018-09-26 11:19:00 +0200435
436 if (PREDICT_TRUE (p0 != 0))
437 {
438 p0->counter.packets++;
439 p0->counter.bytes +=
440 clib_net_to_host_u16 (ip0->payload_length);
441 p0->counter.bytes += header_size;
442 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
443 vnet_buffer (b0)->ipsec.flags = 0;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200444 next0 = im->ah6_decrypt_next_index;
Klement Sekera611864f2018-09-26 11:19:00 +0200445 goto trace0;
446 }
447 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700449 trace0:
450 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
451 {
452 ipsec_input_trace_t *tr =
453 vlib_add_trace (vm, node, b0, sizeof (*tr));
Matus Fabian5539a072016-09-07 05:57:09 -0700454 if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
455 {
456 if (p0)
457 tr->sa_id = p0->sa_id;
458 tr->spi = clib_host_to_net_u32 (esp0->spi);
459 tr->seq = clib_host_to_net_u32 (esp0->seq);
460 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700461 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700463 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
464 n_left_to_next, bi0, next0);
465 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
467 }
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200468 vlib_node_increment_counter (vm, ipsec6_input_node.index,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700469 IPSEC_INPUT_ERROR_RX_PKTS,
470 from_frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
472 return from_frame->n_vectors;
473}
474
475
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700476/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200477VLIB_REGISTER_NODE (ipsec6_input_node,static) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200478 .name = "ipsec6-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 .vector_size = sizeof (u32),
480 .format_trace = format_ipsec_input_trace,
481 .type = VLIB_NODE_TYPE_INTERNAL,
482
483 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
484 .error_strings = ipsec_input_error_strings,
485
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200486 .sibling_of = "ipsec4-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700488/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +0200489
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700490/*
491 * fd.io coding-style-patch-verification: ON
492 *
493 * Local Variables:
494 * eval: (c-set-style "gnu")
495 * End:
496 */