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>
26 #include <vnet/ipsec/ipsec_io.h>
28 #define foreach_ipsec_input_error \
29 _(RX_PKTS, "IPSEC pkts received") \
30 _(RX_MATCH_PKTS, "IPSEC pkts matched")
34 #define _(sym,str) IPSEC_INPUT_ERROR_##sym,
35 foreach_ipsec_input_error
38 } ipsec_input_error_t;
40 static char *ipsec_input_error_strings[] = {
41 #define _(sym,string) string,
42 foreach_ipsec_input_error
54 } ipsec_input_trace_t;
56 /* packet trace format function */
58 format_ipsec_input_trace (u8 * s, va_list * args)
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 *);
62 ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
64 s = format (s, "%U: sa_id %u spd %u policy %d spi %u seq %u",
65 format_ip_protocol, t->proto, t->sa_id,
66 t->spd, t->policy_index, t->spi, t->seq);
71 always_inline ipsec_policy_t *
72 ipsec_input_protect_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, u32 spi)
74 ipsec_main_t *im = &ipsec_main;
79 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT])
81 p = pool_elt_at_index (im->policies, *i);
82 s = pool_elt_at_index (im->sad, p->sa_index);
89 if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32))
92 if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32))
98 if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
101 if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
104 if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
107 if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
116 ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
119 if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
120 (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
125 always_inline ipsec_policy_t *
126 ipsec6_input_protect_policy_match (ipsec_spd_t * spd,
128 ip6_address_t * da, u32 spi)
130 ipsec_main_t *im = &ipsec_main;
135 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT])
137 p = pool_elt_at_index (im->policies, *i);
138 s = pool_elt_at_index (im->sad, p->sa_index);
145 if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6))
148 if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6))
154 if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
157 if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
165 static vlib_node_registration_t ipsec4_input_node;
167 VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm,
168 vlib_node_runtime_t * node,
169 vlib_frame_t * from_frame)
171 u32 n_left_from, *from, next_index, *to_next, thread_index;
172 ipsec_main_t *im = &ipsec_main;
173 u32 ipsec_unprocessed = 0;
174 u32 ipsec_matched = 0;
176 from = vlib_frame_vector_args (from_frame);
177 n_left_from = from_frame->n_vectors;
178 thread_index = vm->thread_index;
180 next_index = node->cached_next_index;
182 while (n_left_from > 0)
186 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
188 while (n_left_from > 0 && n_left_to_next > 0)
195 ip4_ipsec_config_t *c0;
197 ipsec_policy_t *p0 = 0;
199 bi0 = to_next[0] = from[0];
205 b0 = vlib_get_buffer (vm, bi0);
206 b0->flags |= VNET_BUFFER_F_IS_IP4;
207 b0->flags &= ~VNET_BUFFER_F_IS_IP6;
208 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
210 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
212 ip0 = vlib_buffer_get_current (b0);
215 (ip0->protocol == IP_PROTOCOL_IPSEC_ESP
216 || ip0->protocol == IP_PROTOCOL_UDP))
220 ("packet received from %U to %U spi %u size %u spd_id %u",
221 format_ip4_address, ip0->src_address.as_u8,
222 format_ip4_address, ip0->dst_address.as_u8,
223 clib_net_to_host_u32 (esp0->spi),
224 clib_net_to_host_u16 (ip0->length), spd0->id);
227 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
228 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP))
231 (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t));
233 /* FIXME TODO missing check whether there is enough data inside
234 * IP/UDP to contain ESP header & stuff ? */
235 p0 = ipsec_input_protect_policy_match (spd0,
245 if (PREDICT_TRUE (p0 != NULL))
249 pi0 = p0 - im->policies;
250 vlib_increment_combined_counter
251 (&ipsec_spd_policy_counters,
252 thread_index, pi0, 1,
253 clib_net_to_host_u16 (ip0->length));
255 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
256 vnet_buffer (b0)->ipsec.flags = 0;
257 next0 = im->esp4_decrypt_next_index;
258 vlib_buffer_advance (b0, ((u8 *) esp0 - (u8 *) ip0));
266 /* FIXME bypass and discard */
268 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
269 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
271 ipsec_input_trace_t *tr =
272 vlib_add_trace (vm, node, b0, sizeof (*tr));
274 tr->proto = ip0->protocol;
276 tr->sa_id = p0->sa_id;
277 tr->spi = clib_net_to_host_u32 (esp0->spi);
278 tr->seq = clib_net_to_host_u32 (esp0->seq);
280 tr->policy_index = pi0;
283 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
285 ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
286 p0 = ipsec_input_protect_policy_match (spd0,
296 if (PREDICT_TRUE (p0 != 0))
300 pi0 = p0 - im->policies;
301 vlib_increment_combined_counter
302 (&ipsec_spd_policy_counters,
303 thread_index, pi0, 1,
304 clib_net_to_host_u16 (ip0->length));
306 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
307 vnet_buffer (b0)->ipsec.flags = 0;
308 next0 = im->ah4_decrypt_next_index;
315 /* FIXME bypass and discard */
317 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
318 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
320 ipsec_input_trace_t *tr =
321 vlib_add_trace (vm, node, b0, sizeof (*tr));
323 tr->proto = ip0->protocol;
325 tr->sa_id = p0->sa_id;
326 tr->spi = clib_net_to_host_u32 (ah0->spi);
327 tr->seq = clib_net_to_host_u32 (ah0->seq_no);
329 tr->policy_index = pi0;
334 ipsec_unprocessed += 1;
337 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
338 to_next, n_left_to_next, bi0,
341 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
344 vlib_node_increment_counter (vm, ipsec4_input_node.index,
345 IPSEC_INPUT_ERROR_RX_PKTS,
346 from_frame->n_vectors - ipsec_unprocessed);
348 vlib_node_increment_counter (vm, ipsec4_input_node.index,
349 IPSEC_INPUT_ERROR_RX_MATCH_PKTS,
351 return from_frame->n_vectors;
356 VLIB_REGISTER_NODE (ipsec4_input_node,static) = {
357 .name = "ipsec4-input-feature",
358 .vector_size = sizeof (u32),
359 .format_trace = format_ipsec_input_trace,
360 .type = VLIB_NODE_TYPE_INTERNAL,
361 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
362 .error_strings = ipsec_input_error_strings,
363 .n_next_nodes = IPSEC_INPUT_N_NEXT,
365 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
366 foreach_ipsec_input_next
372 static vlib_node_registration_t ipsec6_input_node;
375 VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm,
376 vlib_node_runtime_t * node,
377 vlib_frame_t * from_frame)
379 u32 n_left_from, *from, next_index, *to_next, thread_index;
380 ipsec_main_t *im = &ipsec_main;
381 u32 ipsec_unprocessed = 0;
382 u32 ipsec_matched = 0;
384 from = vlib_frame_vector_args (from_frame);
385 n_left_from = from_frame->n_vectors;
386 thread_index = vm->thread_index;
388 next_index = node->cached_next_index;
390 while (n_left_from > 0)
394 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
396 while (n_left_from > 0 && n_left_to_next > 0)
402 ip4_ipsec_config_t *c0;
404 ipsec_policy_t *p0 = 0;
406 u32 header_size = sizeof (ip0[0]);
408 bi0 = to_next[0] = from[0];
414 b0 = vlib_get_buffer (vm, bi0);
415 b0->flags |= VNET_BUFFER_F_IS_IP6;
416 b0->flags &= ~VNET_BUFFER_F_IS_IP4;
417 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
419 spd0 = pool_elt_at_index (im->spds, c0->spd_index);
421 ip0 = vlib_buffer_get_current (b0);
422 esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
423 ah0 = (ah_header_t *) ((u8 *) ip0 + header_size);
425 if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
429 ("packet received from %U to %U spi %u size %u spd_id %u",
430 format_ip6_address, &ip0->src_address, format_ip6_address,
431 &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
432 clib_net_to_host_u16 (ip0->payload_length) + header_size,
435 p0 = ipsec6_input_protect_policy_match (spd0,
441 if (PREDICT_TRUE (p0 != 0))
445 pi0 = p0 - im->policies;
446 vlib_increment_combined_counter
447 (&ipsec_spd_policy_counters,
448 thread_index, pi0, 1,
449 clib_net_to_host_u16 (ip0->payload_length) +
452 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
453 vnet_buffer (b0)->ipsec.flags = 0;
454 next0 = im->esp6_decrypt_next_index;
455 vlib_buffer_advance (b0, header_size);
463 else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
465 p0 = ipsec6_input_protect_policy_match (spd0,
471 if (PREDICT_TRUE (p0 != 0))
474 pi0 = p0 - im->policies;
475 vlib_increment_combined_counter
476 (&ipsec_spd_policy_counters,
477 thread_index, pi0, 1,
478 clib_net_to_host_u16 (ip0->payload_length) +
481 vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
482 vnet_buffer (b0)->ipsec.flags = 0;
483 next0 = im->ah6_decrypt_next_index;
493 ipsec_unprocessed += 1;
497 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
498 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
500 ipsec_input_trace_t *tr =
501 vlib_add_trace (vm, node, b0, sizeof (*tr));
504 tr->sa_id = p0->sa_id;
505 tr->proto = ip0->protocol;
506 tr->spi = clib_net_to_host_u32 (esp0->spi);
507 tr->seq = clib_net_to_host_u32 (esp0->seq);
511 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
512 n_left_to_next, bi0, next0);
514 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
517 vlib_node_increment_counter (vm, ipsec6_input_node.index,
518 IPSEC_INPUT_ERROR_RX_PKTS,
519 from_frame->n_vectors - ipsec_unprocessed);
521 vlib_node_increment_counter (vm, ipsec6_input_node.index,
522 IPSEC_INPUT_ERROR_RX_MATCH_PKTS,
525 return from_frame->n_vectors;
530 VLIB_REGISTER_NODE (ipsec6_input_node,static) = {
531 .name = "ipsec6-input-feature",
532 .vector_size = sizeof (u32),
533 .format_trace = format_ipsec_input_trace,
534 .type = VLIB_NODE_TYPE_INTERNAL,
535 .n_errors = ARRAY_LEN(ipsec_input_error_strings),
536 .error_strings = ipsec_input_error_strings,
537 .n_next_nodes = IPSEC_INPUT_N_NEXT,
539 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
540 foreach_ipsec_input_next
547 * fd.io coding-style-patch-verification: ON
550 * eval: (c-set-style "gnu")