2 * decap.c : IPSec tunnel support
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/interface.h>
22 #include <vnet/fib/fib_table.h>
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/ipsec_tun.h>
26 #include <vnet/ipsec/ipsec_itf.h>
29 format_ipsec_policy_action (u8 * s, va_list * args)
31 u32 i = va_arg (*args, u32);
36 #define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
37 foreach_ipsec_policy_action
40 s = format (s, "unknown");
42 s = format (s, "%s", t);
47 format_ipsec_policy_type (u8 * s, va_list * args)
49 u32 i = va_arg (*args, u32);
54 #define _(f,str) case IPSEC_SPD_POLICY_##f: t = str; break;
55 foreach_ipsec_spd_policy_type
58 s = format (s, "unknown");
60 s = format (s, "%s", t);
65 unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
67 u32 *r = va_arg (*args, u32 *);
70 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
71 foreach_ipsec_policy_action
79 format_ipsec_crypto_alg (u8 * s, va_list * args)
81 u32 i = va_arg (*args, u32);
86 #define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
87 foreach_ipsec_crypto_alg
90 s = format (s, "unknown");
92 s = format (s, "%s", t);
97 unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
99 ipsec_crypto_alg_t *r = va_arg (*args, ipsec_crypto_alg_t *);
102 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
103 foreach_ipsec_crypto_alg
111 format_ipsec_integ_alg (u8 * s, va_list * args)
113 u32 i = va_arg (*args, u32);
118 #define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
119 foreach_ipsec_integ_alg
122 s = format (s, "unknown");
124 s = format (s, "%s", t);
129 unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
131 ipsec_integ_alg_t *r = va_arg (*args, ipsec_integ_alg_t *);
134 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
135 foreach_ipsec_integ_alg
143 format_ipsec_replay_window (u8 * s, va_list * args)
145 u64 w = va_arg (*args, u64);
148 for (i = 0; i < 64; i++)
150 s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
157 format_ipsec_policy_with_suffix (u8 *s, va_list *args, u8 *suffix)
159 u32 pi = va_arg (*args, u32);
160 ip46_type_t ip_type = IP46_TYPE_IP4;
161 ipsec_main_t *im = &ipsec_main;
163 vlib_counter_t counts;
165 p = pool_elt_at_index (im->policies, pi);
167 s = format (s, " [%d] priority %d action %U type %U protocol ",
169 format_ipsec_policy_action, p->policy,
170 format_ipsec_policy_type, p->type);
171 if (p->protocol != IPSEC_POLICY_PROTOCOL_ANY)
173 s = format (s, "%U", format_ip_protocol, p->protocol);
177 s = format (s, "any");
179 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
181 s = format (s, " sa %u", p->sa_id);
184 s = format (s, " %s", suffix);
188 ip_type = IP46_TYPE_IP6;
191 s = format (s, "\n local addr range %U - %U port range %u - %u",
192 format_ip46_address, &p->laddr.start, ip_type,
193 format_ip46_address, &p->laddr.stop, ip_type,
194 p->lport.start, p->lport.stop);
195 s = format (s, "\n remote addr range %U - %U port range %u - %u",
196 format_ip46_address, &p->raddr.start, ip_type,
197 format_ip46_address, &p->raddr.stop, ip_type,
198 p->rport.start, p->rport.stop);
200 vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
201 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
207 format_ipsec_policy (u8 *s, va_list *args)
209 return format_ipsec_policy_with_suffix (s, args, 0);
213 format_ipsec_fp_policy (u8 *s, va_list *args)
215 return format_ipsec_policy_with_suffix (s, args, (u8 *) "<fast-path>");
219 * @brief Context when walking the fp bihash table. We need to filter
220 * only those policies that are of given type as we walk the table.
222 typedef struct ipsec_spd_policy_ctx_t_
225 ipsec_spd_policy_type_t t;
226 } ipsec_fp_walk_ctx_t;
229 ipsec_fp_table_walk_ip4_cb (clib_bihash_kv_16_8_t *kvp, void *arg)
231 ipsec_fp_walk_ctx_t *ctx = (ipsec_fp_walk_ctx_t *) arg;
232 ipsec_main_t *im = &ipsec_main;
235 ipsec_fp_lookup_value_t *val = (ipsec_fp_lookup_value_t *) &kvp->value;
239 vec_foreach (policy_id, val->fp_policies_ids)
241 p = pool_elt_at_index (im->policies, *policy_id);
242 if (p->type == ctx->t)
243 vec_add1 (ctx->policies, *policy_id);
246 return BIHASH_WALK_CONTINUE;
250 ipsec_fp_table_walk_ip6_cb (clib_bihash_kv_40_8_t *kvp, void *arg)
252 ipsec_fp_walk_ctx_t *ctx = (ipsec_fp_walk_ctx_t *) arg;
253 ipsec_main_t *im = &ipsec_main;
256 ipsec_fp_lookup_value_t *val = (ipsec_fp_lookup_value_t *) &kvp->value;
260 vec_foreach (policy_id, val->fp_policies_ids)
262 p = pool_elt_at_index (im->policies, *policy_id);
263 if (p->type == ctx->t)
264 vec_add1 (ctx->policies, *policy_id);
267 return BIHASH_WALK_CONTINUE;
271 format_ipsec_fp_policies (u8 *s, va_list *args)
273 ipsec_main_t *im = &ipsec_main;
274 ipsec_spd_t *spd = va_arg (*args, ipsec_spd_t *);
275 ipsec_spd_policy_type_t t = va_arg (*args, ipsec_spd_policy_type_t);
277 ipsec_fp_walk_ctx_t ctx = {
282 u32 ip4_in_lookup_hash_idx = spd->fp_spd.ip4_in_lookup_hash_idx;
283 u32 ip4_out_lookup_hash_idx = spd->fp_spd.ip4_out_lookup_hash_idx;
284 u32 ip6_in_lookup_hash_idx = spd->fp_spd.ip6_in_lookup_hash_idx;
285 u32 ip6_out_lookup_hash_idx = spd->fp_spd.ip6_out_lookup_hash_idx;
289 case IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT:
290 case IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS:
291 case IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD:
292 if (INDEX_INVALID != ip4_in_lookup_hash_idx)
294 clib_bihash_16_8_t *bihash_table = pool_elt_at_index (
295 im->fp_ip4_lookup_hashes_pool, ip4_in_lookup_hash_idx);
297 clib_bihash_foreach_key_value_pair_16_8 (
298 bihash_table, ipsec_fp_table_walk_ip4_cb, &ctx);
303 case IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT:
304 case IPSEC_SPD_POLICY_IP6_INBOUND_BYPASS:
305 case IPSEC_SPD_POLICY_IP6_INBOUND_DISCARD:
306 if (INDEX_INVALID != ip6_in_lookup_hash_idx)
308 clib_bihash_40_8_t *bihash_table = pool_elt_at_index (
309 im->fp_ip6_lookup_hashes_pool, ip6_in_lookup_hash_idx);
311 clib_bihash_foreach_key_value_pair_40_8 (
312 bihash_table, ipsec_fp_table_walk_ip6_cb, &ctx);
316 case IPSEC_SPD_POLICY_IP4_OUTBOUND:
317 if (INDEX_INVALID != ip4_out_lookup_hash_idx)
319 clib_bihash_16_8_t *bihash_table = pool_elt_at_index (
320 im->fp_ip4_lookup_hashes_pool, ip4_out_lookup_hash_idx);
322 clib_bihash_foreach_key_value_pair_16_8 (
323 bihash_table, ipsec_fp_table_walk_ip4_cb, &ctx);
327 case IPSEC_SPD_POLICY_IP6_OUTBOUND:
328 if (INDEX_INVALID != ip6_out_lookup_hash_idx)
330 clib_bihash_40_8_t *bihash_table = pool_elt_at_index (
331 im->fp_ip6_lookup_hashes_pool, ip6_out_lookup_hash_idx);
333 clib_bihash_foreach_key_value_pair_40_8 (
334 bihash_table, ipsec_fp_table_walk_ip6_cb, &ctx);
342 vec_foreach (i, ctx.policies)
344 s = format (s, "\n %U", format_ipsec_fp_policy, *i);
347 vec_free (ctx.policies);
353 format_ipsec_spd (u8 * s, va_list * args)
355 u32 si = va_arg (*args, u32);
356 ipsec_main_t *im = &ipsec_main;
360 if (pool_is_free_index (im->spds, si))
362 s = format (s, "No such SPD index: %d", si);
366 spd = pool_elt_at_index (im->spds, si);
368 s = format (s, "spd %u", spd->id);
371 s = format (s, "\n %s:", n); \
372 vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_##v]) \
374 s = format (s, "\n %U", format_ipsec_policy, *i); \
376 s = format (s, "\n %U", format_ipsec_fp_policies, spd, IPSEC_SPD_POLICY_##v);
377 foreach_ipsec_spd_policy_type;
385 format_ipsec_out_spd_flow_cache (u8 *s, va_list *args)
387 ipsec_main_t *im = &ipsec_main;
389 s = format (s, "\nipv4-outbound-spd-flow-cache-entries: %u",
390 im->ipsec4_out_spd_flow_cache_entries);
396 format_ipsec_in_spd_flow_cache (u8 *s, va_list *args)
398 ipsec_main_t *im = &ipsec_main;
400 s = format (s, "\nipv4-inbound-spd-flow-cache-entries: %u",
401 im->ipsec4_in_spd_flow_cache_entries);
407 format_ipsec_key (u8 * s, va_list * args)
409 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
411 return (format (s, "%U", format_hex_bytes, key->data, key->len));
415 unformat_ipsec_key (unformat_input_t * input, va_list * args)
417 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
420 if (unformat (input, "%U", unformat_hex_string, &data))
422 ipsec_mk_key (key, data, vec_len (data));
431 format_ipsec_sa_flags (u8 * s, va_list * args)
433 ipsec_sa_flags_t flags = va_arg (*args, int);
435 #define _(v, f, str) if (flags & IPSEC_SA_FLAG_##f) s = format(s, "%s ", str);
436 foreach_ipsec_sa_flags
442 format_ipsec_sa (u8 * s, va_list * args)
444 u32 sai = va_arg (*args, u32);
445 ipsec_format_flags_t flags = va_arg (*args, ipsec_format_flags_t);
446 vlib_counter_t counts;
450 if (pool_is_free_index (ipsec_sa_pool, sai))
452 s = format (s, "No such SA index: %d", sai);
456 sa = ipsec_sa_get (sai);
458 s = format (s, "[%d] sa %u (0x%x) spi %u (0x%08x) protocol:%s flags:[%U]",
459 sai, sa->id, sa->id, sa->spi, sa->spi,
460 sa->protocol ? "esp" : "ah", format_ipsec_sa_flags, sa->flags);
462 if (!(flags & IPSEC_FORMAT_DETAIL))
465 s = format (s, "\n locks %d", sa->node.fn_locks);
466 s = format (s, "\n salt 0x%x", clib_net_to_host_u32 (sa->salt));
467 s = format (s, "\n thread-index:%d", sa->thread_index);
468 s = format (s, "\n seq %u seq-hi %u", sa->seq, sa->seq_hi);
469 s = format (s, "\n window %U", format_ipsec_replay_window,
471 s = format (s, "\n crypto alg %U",
472 format_ipsec_crypto_alg, sa->crypto_alg);
473 if (sa->crypto_alg && (flags & IPSEC_FORMAT_INSECURE))
474 s = format (s, " key %U", format_ipsec_key, &sa->crypto_key);
476 s = format (s, " key [redacted]");
477 s = format (s, "\n integrity alg %U",
478 format_ipsec_integ_alg, sa->integ_alg);
479 if (sa->integ_alg && (flags & IPSEC_FORMAT_INSECURE))
480 s = format (s, " key %U", format_ipsec_key, &sa->integ_key);
482 s = format (s, " key [redacted]");
483 s = format (s, "\n UDP:[src:%d dst:%d]",
484 clib_host_to_net_u16 (sa->udp_hdr.src_port),
485 clib_host_to_net_u16 (sa->udp_hdr.dst_port));
487 vlib_get_combined_counter (&ipsec_sa_counters, sai, &counts);
488 lost = vlib_get_simple_counter (&ipsec_sa_lost_counters, sai);
489 s = format (s, "\n tx/rx:[packets:%Ld bytes:%Ld], lost:[packets:%Ld]",
490 counts.packets, counts.bytes, lost);
492 if (ipsec_sa_is_set_IS_TUNNEL (sa))
493 s = format (s, "\n%U", format_tunnel, &sa->tunnel, 3);
500 format_ipsec_tun_protect_index (u8 * s, va_list * args)
502 u32 itpi = va_arg (*args, index_t);
503 ipsec_tun_protect_t *itp;
505 if (pool_is_free_index (ipsec_tun_protect_pool, itpi))
506 return (format (s, "No such tunnel index: %d", itpi));
508 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
510 return (format (s, "%U", format_ipsec_tun_protect, itp));
514 format_ipsec_tun_protect_flags (u8 * s, va_list * args)
516 ipsec_protect_flags_t flags = va_arg (*args, int);
518 if (IPSEC_PROTECT_NONE == flags)
519 s = format (s, "none");
521 else if (flags & IPSEC_PROTECT_##a) \
522 s = format (s, "%s", c); \
523 foreach_ipsec_protect_flags
530 format_ipsec_tun_protect (u8 * s, va_list * args)
532 ipsec_tun_protect_t *itp = va_arg (*args, ipsec_tun_protect_t *);
535 s = format (s, "%U flags:[%U]", format_vnet_sw_if_index_name,
536 vnet_get_main (), itp->itp_sw_if_index,
537 format_ipsec_tun_protect_flags, itp->itp_flags);
538 if (!ip_address_is_zero (itp->itp_key))
539 s = format (s, ": %U", format_ip_address, itp->itp_key);
540 s = format (s, "\n output-sa:");
541 s = format (s, "\n %U", format_ipsec_sa, itp->itp_out_sa,
544 s = format (s, "\n input-sa:");
546 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
548 s = format (s, "\n %U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
556 format_ipsec4_tunnel_kv (u8 * s, va_list * args)
558 ipsec4_tunnel_kv_t *kv = va_arg (*args, ipsec4_tunnel_kv_t *);
562 ipsec4_tunnel_extract_key (kv, &ip, &spi);
564 s = format (s, "remote:%U spi:%u (0x%08x) sa:%d tun:%d",
565 format_ip4_address, &ip,
566 clib_net_to_host_u32 (spi),
567 clib_net_to_host_u32 (spi),
568 kv->value.sa_index, kv->value.tun_index);
574 format_ipsec6_tunnel_kv (u8 * s, va_list * args)
576 ipsec6_tunnel_kv_t *kv = va_arg (*args, ipsec6_tunnel_kv_t *);
578 s = format (s, "remote:%U spi:%u (0x%08x) sa:%d tun:%d",
579 format_ip6_address, &kv->key.remote_ip,
580 clib_net_to_host_u32 (kv->key.spi),
581 clib_net_to_host_u32 (kv->key.spi),
582 kv->value.sa_index, kv->value.tun_index);
588 format_ipsec_itf (u8 * s, va_list * a)
590 index_t ii = va_arg (*a, index_t);
593 itf = ipsec_itf_get (ii);
594 s = format (s, "[%d] %U %U",
595 ii, format_vnet_sw_if_index_name, vnet_get_main (),
596 itf->ii_sw_if_index, format_tunnel_mode, itf->ii_mode);
602 * fd.io coding-style-patch-verification: ON
605 * eval: (c-set-style "gnu")