2 * decap.c : IPSec tunnel decapsulation
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/feature/feature.h>
23 #include <vnet/ipsec/ipsec.h>
24 #include <vnet/ipsec/esp.h>
25 #include <vnet/ipsec/ah.h>
27 #define foreach_ipsec_input_error \
28 _(RX_PKTS, "IPSEC pkts received") \
29 _(DECRYPTION_FAILED, "IPSEC decryption failed")
33 #define _(sym,str) IPSEC_INPUT_ERROR_##sym,
34 foreach_ipsec_input_error
37 } ipsec_input_error_t;
39 static char *ipsec_input_error_strings[] = {
40 #define _(sym,string) string,
41 foreach_ipsec_input_error
50 } ipsec_input_trace_t;
52 /* packet trace format function */
54 format_ipsec_input_trace (u8 * s, va_list * args)
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 *);
58 ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
60 if (t->spi == 0 && t->seq == 0)
62 s = format (s, "esp: no esp packet");
68 s = format (s, "esp: sa_id %u spi %u seq %u", t->sa_id, t->spi, t->seq);
72 s = format (s, "esp: no sa spi %u seq %u", t->spi, t->seq);
77 always_inline ipsec_policy_t *
78 ipsec_input_protect_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, u32 spi)
80 ipsec_main_t *im = &ipsec_main;
85 vec_foreach (i, spd->ipv4_inbound_protect_policy_indices)
87 p = pool_elt_at_index (spd->policies, *i);
88 s = pool_elt_at_index (im->sad, p->sa_index);
95 if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32))
98 if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32))
104 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
107 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
110 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
113 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
122 ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
125 if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
126 (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
131 always_inline ipsec_policy_t *
132 ipsec_input_ip6_protect_policy_match (ipsec_spd_t * spd,
134 ip6_address_t * da, u32 spi)
136 ipsec_main_t *im = &ipsec_main;
141 vec_foreach (i, spd->ipv6_inbound_protect_policy_indices)
143 p = pool_elt_at_index (spd->policies, *i);
144 s = pool_elt_at_index (im->sad, p->sa_index);
151 if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6))
154 if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6))
160 if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
163 if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
171 static vlib_node_registration_t ipsec_input_ip4_node;
174 ipsec_input_ip4_node_fn (vlib_main_t * vm,
175 vlib_node_runtime_t * node,
176 vlib_frame_t * from_frame)
178 u32 n_left_from, *from, next_index, *to_next;
179 ipsec_main_t *im = &ipsec_main;
181 from = vlib_frame_vector_args (from_frame);
182 n_left_from = from_frame->n_vectors;
184 next_index = node->cached_next_index;
186 while (n_left_from > 0)
190 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
192 while (n_left_from > 0 && n_left_to_next > 0)
199 ip4_ipsec_config_t *c0;
201 ipsec_policy_t *p0 = 0;
203 bi0 = to_next[0] = from[0];
209 b0 = vlib_get_buffer (vm, bi0);
210 b0->flags |= VNET_BUFFER_F_IS_IP4;
211 b0->flags &= ~VNET_BUFFER_F_IS_IP6;
212 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
214 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
216 ip0 = vlib_buffer_get_current (b0);
219 (ip0->protocol == IP_PROTOCOL_IPSEC_ESP
220 || ip0->protocol == IP_PROTOCOL_UDP))
224 ("packet received from %U to %U spi %u size %u spd_id %u",
225 format_ip4_address, ip0->src_address.as_u8,
226 format_ip4_address, ip0->dst_address.as_u8,
227 clib_net_to_host_u32 (esp0->spi),
228 clib_net_to_host_u16 (ip0->length), spd0->id);
231 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
232 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP))
235 (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t));
237 /* FIXME TODO missing check whether there is enough data inside
238 * IP/UDP to contain ESP header & stuff ? */
239 p0 = ipsec_input_protect_policy_match (spd0,
249 if (PREDICT_TRUE (p0 != 0))
251 p0->counter.packets++;
252 p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
253 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
254 vnet_buffer (b0)->ipsec.flags = 0;
255 next0 = im->esp_decrypt_next_index;
256 vlib_buffer_advance (b0, ((u8 *) esp0 - (u8 *) ip0));
260 /* FIXME bypass and discard */
262 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
264 ipsec_input_trace_t *tr =
265 vlib_add_trace (vm, node, b0, sizeof (*tr));
266 if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP ||
267 ip0->protocol == IP_PROTOCOL_UDP)
270 tr->sa_id = p0->sa_id;
271 tr->spi = clib_host_to_net_u32 (esp0->spi);
272 tr->seq = clib_host_to_net_u32 (esp0->seq);
279 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_AH))
281 ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
282 p0 = ipsec_input_protect_policy_match (spd0,
292 if (PREDICT_TRUE (p0 != 0))
294 p0->counter.packets++;
295 p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
296 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
297 vnet_buffer (b0)->ipsec.flags = 0;
298 next0 = im->ah_decrypt_next_index;
301 /* FIXME bypass and discard */
303 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
305 ipsec_input_trace_t *tr =
306 vlib_add_trace (vm, node, b0, sizeof (*tr));
307 if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
310 tr->sa_id = p0->sa_id;
311 tr->spi = clib_host_to_net_u32 (ah0->spi);
312 tr->seq = clib_host_to_net_u32 (ah0->seq_no);
317 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
318 to_next, n_left_to_next, bi0,
321 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
323 vlib_node_increment_counter (vm, ipsec_input_ip4_node.index,
324 IPSEC_INPUT_ERROR_RX_PKTS,
325 from_frame->n_vectors);
327 return from_frame->n_vectors;
332 VLIB_REGISTER_NODE (ipsec_input_ip4_node,static) = {
333 .function = ipsec_input_ip4_node_fn,
334 .name = "ipsec-input-ip4",
335 .vector_size = sizeof (u32),
336 .format_trace = format_ipsec_input_trace,
337 .type = VLIB_NODE_TYPE_INTERNAL,
339 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
340 .error_strings = ipsec_input_error_strings,
342 .n_next_nodes = IPSEC_INPUT_N_NEXT,
344 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
345 foreach_ipsec_input_next
351 VLIB_NODE_FUNCTION_MULTIARCH (ipsec_input_ip4_node, ipsec_input_ip4_node_fn)
352 static vlib_node_registration_t ipsec_input_ip6_node;
355 ipsec_input_ip6_node_fn (vlib_main_t * vm,
356 vlib_node_runtime_t * node,
357 vlib_frame_t * from_frame)
359 u32 n_left_from, *from, next_index, *to_next;
360 ipsec_main_t *im = &ipsec_main;
362 from = vlib_frame_vector_args (from_frame);
363 n_left_from = from_frame->n_vectors;
365 next_index = node->cached_next_index;
367 while (n_left_from > 0)
371 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
373 while (n_left_from > 0 && n_left_to_next > 0)
379 ip4_ipsec_config_t *c0;
381 ipsec_policy_t *p0 = 0;
382 u32 header_size = sizeof (ip0[0]);
384 bi0 = to_next[0] = from[0];
390 b0 = vlib_get_buffer (vm, bi0);
391 b0->flags |= VNET_BUFFER_F_IS_IP6;
392 b0->flags &= ~VNET_BUFFER_F_IS_IP4;
393 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
395 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
397 ip0 = vlib_buffer_get_current (b0);
398 esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
400 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
404 ("packet received from %U to %U spi %u size %u spd_id %u",
405 format_ip6_address, &ip0->src_address, format_ip6_address,
406 &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
407 clib_net_to_host_u16 (ip0->payload_length) + header_size,
410 p0 = ipsec_input_ip6_protect_policy_match (spd0,
416 if (PREDICT_TRUE (p0 != 0))
418 p0->counter.packets++;
420 clib_net_to_host_u16 (ip0->payload_length);
421 p0->counter.bytes += header_size;
422 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
423 vnet_buffer (b0)->ipsec.flags = 0;
424 next0 = im->esp_decrypt_next_index;
425 vlib_buffer_advance (b0, header_size);
431 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
433 ipsec_input_trace_t *tr =
434 vlib_add_trace (vm, node, b0, sizeof (*tr));
435 if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
438 tr->sa_id = p0->sa_id;
439 tr->spi = clib_host_to_net_u32 (esp0->spi);
440 tr->seq = clib_host_to_net_u32 (esp0->seq);
444 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
445 n_left_to_next, bi0, next0);
447 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
449 vlib_node_increment_counter (vm, ipsec_input_ip6_node.index,
450 IPSEC_INPUT_ERROR_RX_PKTS,
451 from_frame->n_vectors);
453 return from_frame->n_vectors;
458 VLIB_REGISTER_NODE (ipsec_input_ip6_node,static) = {
459 .function = ipsec_input_ip6_node_fn,
460 .name = "ipsec-input-ip6",
461 .vector_size = sizeof (u32),
462 .format_trace = format_ipsec_input_trace,
463 .type = VLIB_NODE_TYPE_INTERNAL,
465 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
466 .error_strings = ipsec_input_error_strings,
468 .sibling_of = "ipsec-input-ip4",
472 VLIB_NODE_FUNCTION_MULTIARCH (ipsec_input_ip6_node, ipsec_input_ip6_node_fn)
474 * fd.io coding-style-patch-verification: ON
477 * eval: (c-set-style "gnu")