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>
27 format_ipsec_policy_action (u8 * s, va_list * args)
29 u32 i = va_arg (*args, u32);
34 #define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
35 foreach_ipsec_policy_action
38 s = format (s, "unknown");
40 s = format (s, "%s", t);
45 format_ipsec_policy_type (u8 * s, va_list * args)
47 u32 i = va_arg (*args, u32);
52 #define _(f,str) case IPSEC_SPD_POLICY_##f: t = str; break;
53 foreach_ipsec_spd_policy_type
56 s = format (s, "unknown");
58 s = format (s, "%s", t);
63 unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
65 u32 *r = va_arg (*args, u32 *);
68 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
69 foreach_ipsec_policy_action
77 format_ipsec_crypto_alg (u8 * s, va_list * args)
79 u32 i = va_arg (*args, u32);
84 #define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
85 foreach_ipsec_crypto_alg
88 s = format (s, "unknown");
90 s = format (s, "%s", t);
95 unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
97 u32 *r = va_arg (*args, u32 *);
100 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
101 foreach_ipsec_crypto_alg
109 format_ipsec_integ_alg (u8 * s, va_list * args)
111 u32 i = va_arg (*args, u32);
116 #define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
117 foreach_ipsec_integ_alg
120 s = format (s, "unknown");
122 s = format (s, "%s", t);
127 unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
129 u32 *r = va_arg (*args, u32 *);
132 #define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
133 foreach_ipsec_integ_alg
141 format_ipsec_replay_window (u8 * s, va_list * args)
143 u64 w = va_arg (*args, u64);
146 for (i = 0; i < 64; i++)
148 s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
155 format_ipsec_policy (u8 * s, va_list * args)
157 u32 pi = va_arg (*args, u32);
158 ip46_type_t ip_type = IP46_TYPE_IP4;
159 ipsec_main_t *im = &ipsec_main;
161 vlib_counter_t counts;
163 p = pool_elt_at_index (im->policies, pi);
165 s = format (s, " [%d] priority %d action %U type %U protocol ",
167 format_ipsec_policy_action, p->policy,
168 format_ipsec_policy_type, p->type);
171 s = format (s, "%U", format_ip_protocol, p->protocol);
175 s = format (s, "any");
177 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
179 s = format (s, " sa %u", p->sa_id);
183 ip_type = IP46_TYPE_IP6;
186 s = format (s, "\n local addr range %U - %U port range %u - %u",
187 format_ip46_address, &p->laddr.start, ip_type,
188 format_ip46_address, &p->laddr.stop, ip_type,
189 clib_net_to_host_u16 (p->lport.start),
190 clib_net_to_host_u16 (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 clib_net_to_host_u16 (p->rport.start),
195 clib_net_to_host_u16 (p->rport.stop));
197 vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
198 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
204 format_ipsec_spd (u8 * s, va_list * args)
206 u32 si = va_arg (*args, u32);
207 ipsec_main_t *im = &ipsec_main;
211 if (pool_is_free_index (im->spds, si))
213 s = format (s, "No such SPD index: %d", si);
217 spd = pool_elt_at_index (im->spds, si);
219 s = format (s, "spd %u", spd->id);
222 s = format (s, "\n %s:", n); \
223 vec_foreach(i, spd->policies[IPSEC_SPD_POLICY_##v]) \
225 s = format (s, "\n %U", format_ipsec_policy, *i); \
227 foreach_ipsec_spd_policy_type;
235 format_ipsec_key (u8 * s, va_list * args)
237 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
239 return (format (s, "%U", format_hex_bytes, key->data, key->len));
243 unformat_ipsec_key (unformat_input_t * input, va_list * args)
245 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
248 if (unformat (input, "%U", unformat_hex_string, &data))
250 ipsec_mk_key (key, data, vec_len (data));
259 format_ipsec_sa_flags (u8 * s, va_list * args)
261 ipsec_sa_flags_t flags = va_arg (*args, int);
265 #define _(v, f, str) else if (flags & IPSEC_SA_FLAG_##f) s = format(s, "%s ", str);
266 foreach_ipsec_sa_flags
272 format_ipsec_sa (u8 * s, va_list * args)
274 u32 sai = va_arg (*args, u32);
275 ipsec_format_flags_t flags = va_arg (*args, ipsec_format_flags_t);
276 ipsec_main_t *im = &ipsec_main;
277 vlib_counter_t counts;
281 if (pool_is_free_index (im->sad, sai))
283 s = format (s, "No such SA index: %d", sai);
287 sa = pool_elt_at_index (im->sad, sai);
289 s = format (s, "[%d] sa 0x%x spi %u mode %s%s protocol %s %U",
290 sai, sa->id, sa->spi,
291 ipsec_sa_is_set_IS_TUNNEL (sa) ? "tunnel" : "transport",
292 ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ? "-ip6" : "",
293 sa->protocol ? "esp" : "ah", format_ipsec_sa_flags, sa->flags);
295 if (!(flags & IPSEC_FORMAT_DETAIL))
298 s = format (s, "\n salt 0x%x", clib_net_to_host_u32 (sa->salt));
299 s = format (s, "\n seq %u seq-hi %u", sa->seq, sa->seq_hi);
300 s = format (s, "\n last-seq %u last-seq-hi %u window %U",
301 sa->last_seq, sa->last_seq_hi,
302 format_ipsec_replay_window, sa->replay_window);
303 s = format (s, "\n crypto alg %U",
304 format_ipsec_crypto_alg, sa->crypto_alg);
306 s = format (s, " key %U", format_ipsec_key, &sa->crypto_key);
307 s = format (s, "\n integrity alg %U",
308 format_ipsec_integ_alg, sa->integ_alg);
310 s = format (s, " key %U", format_ipsec_key, &sa->integ_key);
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:");
330 format (s, "\n %U", format_dpo_id,
331 &sa->dpo[IPSEC_PROTOCOL_ESP], 6);
340 format_ipsec_tunnel (u8 * s, va_list * args)
342 ipsec_main_t *im = &ipsec_main;
343 u32 ti = va_arg (*args, u32);
344 ipsec_tunnel_if_t *t;
346 if (pool_is_free_index (im->tunnel_interfaces, ti))
348 s = format (s, "No such tunnel index: %d", ti);
352 t = pool_elt_at_index (im->tunnel_interfaces, ti);
354 if (t->hw_if_index == ~0)
358 format (s, "%U\n", format_vnet_hw_if_index_name, im->vnet_main,
361 s = format (s, " out-bound sa: ");
362 s = format (s, "%U\n", format_ipsec_sa, t->output_sa_index,
365 s = format (s, " in-bound sa: ");
366 s = format (s, "%U\n", format_ipsec_sa, t->input_sa_index,
374 * fd.io coding-style-patch-verification: ON
377 * eval: (c-set-style "gnu")