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>
28 format_ipsec_policy_action (u8 * s, va_list * args)
30 u32 i = va_arg (*args, u32);
35 #define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
36 foreach_ipsec_policy_action
39 s = format (s, "unknown");
41 s = format (s, "%s", t);
46 format_ipsec_policy_type (u8 * s, va_list * args)
48 u32 i = va_arg (*args, u32);
53 #define _(f,str) case IPSEC_SPD_POLICY_##f: t = str; break;
54 foreach_ipsec_spd_policy_type
57 s = format (s, "unknown");
59 s = format (s, "%s", t);
64 unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
66 u32 *r = va_arg (*args, u32 *);
69 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
70 foreach_ipsec_policy_action
78 format_ipsec_crypto_alg (u8 * s, va_list * args)
80 u32 i = va_arg (*args, u32);
85 #define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
86 foreach_ipsec_crypto_alg
89 s = format (s, "unknown");
91 s = format (s, "%s", t);
96 unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
98 u32 *r = va_arg (*args, u32 *);
101 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
102 foreach_ipsec_crypto_alg
110 format_ipsec_integ_alg (u8 * s, va_list * args)
112 u32 i = va_arg (*args, u32);
117 #define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
118 foreach_ipsec_integ_alg
121 s = format (s, "unknown");
123 s = format (s, "%s", t);
128 unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
130 u32 *r = va_arg (*args, u32 *);
133 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
134 foreach_ipsec_integ_alg
142 format_ipsec_replay_window (u8 * s, va_list * args)
144 u64 w = va_arg (*args, u64);
147 for (i = 0; i < 64; i++)
149 s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
156 format_ipsec_policy (u8 * s, va_list * args)
158 u32 pi = va_arg (*args, u32);
159 ip46_type_t ip_type = IP46_TYPE_IP4;
160 ipsec_main_t *im = &ipsec_main;
162 vlib_counter_t counts;
164 p = pool_elt_at_index (im->policies, pi);
166 s = format (s, " [%d] priority %d action %U type %U protocol ",
168 format_ipsec_policy_action, p->policy,
169 format_ipsec_policy_type, p->type);
172 s = format (s, "%U", format_ip_protocol, p->protocol);
176 s = format (s, "any");
178 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
180 s = format (s, " sa %u", p->sa_id);
184 ip_type = IP46_TYPE_IP6;
187 s = format (s, "\n local addr range %U - %U port range %u - %u",
188 format_ip46_address, &p->laddr.start, ip_type,
189 format_ip46_address, &p->laddr.stop, ip_type,
190 p->lport.start, p->lport.stop);
191 s = format (s, "\n remote addr range %U - %U port range %u - %u",
192 format_ip46_address, &p->raddr.start, ip_type,
193 format_ip46_address, &p->raddr.stop, ip_type,
194 p->rport.start, p->rport.stop);
196 vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
197 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
203 format_ipsec_spd (u8 * s, va_list * args)
205 u32 si = va_arg (*args, u32);
206 ipsec_main_t *im = &ipsec_main;
210 if (pool_is_free_index (im->spds, si))
212 s = format (s, "No such SPD index: %d", si);
216 spd = pool_elt_at_index (im->spds, si);
218 s = format (s, "spd %u", spd->id);
221 s = format (s, "\n %s:", n); \
222 vec_foreach(i, spd->policies[IPSEC_SPD_POLICY_##v]) \
224 s = format (s, "\n %U", format_ipsec_policy, *i); \
226 foreach_ipsec_spd_policy_type;
234 format_ipsec_key (u8 * s, va_list * args)
236 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
238 return (format (s, "%U", format_hex_bytes, key->data, key->len));
242 unformat_ipsec_key (unformat_input_t * input, va_list * args)
244 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
247 if (unformat (input, "%U", unformat_hex_string, &data))
249 ipsec_mk_key (key, data, vec_len (data));
258 format_ipsec_sa_flags (u8 * s, va_list * args)
260 ipsec_sa_flags_t flags = va_arg (*args, int);
262 #define _(v, f, str) if (flags & IPSEC_SA_FLAG_##f) s = format(s, "%s ", str);
263 foreach_ipsec_sa_flags
269 format_ipsec_sa (u8 * s, va_list * args)
271 u32 sai = va_arg (*args, u32);
272 ipsec_format_flags_t flags = va_arg (*args, ipsec_format_flags_t);
273 ipsec_main_t *im = &ipsec_main;
274 vlib_counter_t counts;
278 if (pool_is_free_index (im->sad, sai))
280 s = format (s, "No such SA index: %d", sai);
284 sa = pool_elt_at_index (im->sad, sai);
286 s = format (s, "[%d] sa %u (0x%x) spi %u (0x%08x) protocol:%s flags:[%U]",
287 sai, sa->id, sa->id, sa->spi, sa->spi,
288 sa->protocol ? "esp" : "ah", format_ipsec_sa_flags, sa->flags);
290 if (!(flags & IPSEC_FORMAT_DETAIL))
293 s = format (s, "\n locks %d", sa->node.fn_locks);
294 s = format (s, "\n salt 0x%x", clib_net_to_host_u32 (sa->salt));
295 s = format (s, "\n seq %u seq-hi %u", sa->seq, sa->seq_hi);
296 s = format (s, "\n last-seq %u last-seq-hi %u window %U",
297 sa->last_seq, sa->last_seq_hi,
298 format_ipsec_replay_window, sa->replay_window);
299 s = format (s, "\n crypto alg %U",
300 format_ipsec_crypto_alg, sa->crypto_alg);
301 if (sa->crypto_alg && (flags & IPSEC_FORMAT_INSECURE))
302 s = format (s, " key %U", format_ipsec_key, &sa->crypto_key);
304 s = format (s, " key [redacted]");
305 s = format (s, "\n integrity alg %U",
306 format_ipsec_integ_alg, sa->integ_alg);
307 if (sa->integ_alg && (flags & IPSEC_FORMAT_INSECURE))
308 s = format (s, " key %U", format_ipsec_key, &sa->integ_key);
310 s = format (s, " key [redacted]");
312 vlib_get_combined_counter (&ipsec_sa_counters, sai, &counts);
313 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
315 if (ipsec_sa_is_set_IS_TUNNEL (sa))
317 tx_table_id = fib_table_get_table_id (sa->tx_fib_index,
319 s = format (s, "\n table-ID %d tunnel src %U dst %U",
321 format_ip46_address, &sa->tunnel_src_addr, IP46_TYPE_ANY,
322 format_ip46_address, &sa->tunnel_dst_addr, IP46_TYPE_ANY);
323 if (!ipsec_sa_is_set_IS_INBOUND (sa))
326 format (s, "\n resovle via fib-entry: %d",
327 sa->fib_entry_index);
328 s = format (s, "\n stacked on:");
329 s = format (s, "\n %U", format_dpo_id, &sa->dpo, 6);
338 format_ipsec_tunnel (u8 * s, va_list * args)
340 ipsec_main_t *im = &ipsec_main;
341 u32 ti = va_arg (*args, u32);
342 ipsec_tunnel_if_t *t;
344 if (pool_is_free_index (im->tunnel_interfaces, ti))
346 s = format (s, "No such tunnel index: %d", ti);
350 t = pool_elt_at_index (im->tunnel_interfaces, ti);
352 if (t->hw_if_index == ~0)
356 format (s, "%U\n", format_vnet_hw_if_index_name, im->vnet_main,
359 s = format (s, " out-bound sa: ");
360 s = format (s, "%U\n", format_ipsec_sa, t->output_sa_index,
363 s = format (s, " in-bound sa: ");
364 s = format (s, "%U\n", format_ipsec_sa, t->input_sa_index,
372 format_ipsec_tun_protect (u8 * s, va_list * args)
374 u32 itpi = va_arg (*args, u32);
375 ipsec_tun_protect_t *itp;
378 if (pool_is_free_index (ipsec_protect_pool, itpi))
380 s = format (s, "No such tunnel index: %d", itpi);
384 itp = pool_elt_at_index (ipsec_protect_pool, itpi);
386 s = format (s, "%U", format_vnet_sw_if_index_name,
387 vnet_get_main (), itp->itp_sw_if_index);
388 s = format (s, "\n output-sa:");
390 format (s, "\n %U", format_ipsec_sa, itp->itp_out_sa,
393 s = format (s, "\n input-sa:");
395 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
397 s = format (s, "\n %U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
406 format_ipsec4_tunnel_key (u8 * s, va_list * args)
408 ipsec4_tunnel_key_t *key = va_arg (*args, ipsec4_tunnel_key_t *);
410 s = format (s, "remote:%U spi:%u (0x%08x)",
411 format_ip4_address, &key->remote_ip,
412 clib_net_to_host_u32 (key->spi),
413 clib_net_to_host_u32 (key->spi));
419 format_ipsec6_tunnel_key (u8 * s, va_list * args)
421 ipsec6_tunnel_key_t *key = va_arg (*args, ipsec6_tunnel_key_t *);
423 s = format (s, "remote:%U spi:%u (0x%08x)",
424 format_ip6_address, &key->remote_ip,
425 clib_net_to_host_u32 (key->spi),
426 clib_net_to_host_u32 (key->spi));
432 * fd.io coding-style-patch-verification: ON
435 * eval: (c-set-style "gnu")