blob: 5bcfcdbeff1192db4fa50e1a06e65c3df5cb80f8 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * decap.c : IPSec tunnel support
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
21#include <vnet/interface.h>
Neale Ranns8d7c5022019-02-06 01:41:05 -080022#include <vnet/fib/fib_table.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
25
26u8 *
27format_ipsec_policy_action (u8 * s, va_list * args)
28{
29 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070030 char *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
32 switch (i)
33 {
34#define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070035 foreach_ipsec_policy_action
Ed Warnickecb9cada2015-12-08 15:45:58 -070036#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070037 default:
38 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -070039 }
40 s = format (s, "%s", t);
41 return s;
42}
43
Neale Ranns9f231d42019-03-19 10:06:00 +000044u8 *
45format_ipsec_policy_type (u8 * s, va_list * args)
46{
47 u32 i = va_arg (*args, u32);
48 char *t = 0;
49
50 switch (i)
51 {
52#define _(f,str) case IPSEC_SPD_POLICY_##f: t = str; break;
53 foreach_ipsec_spd_policy_type
54#undef _
55 default:
56 s = format (s, "unknown");
57 }
58 s = format (s, "%s", t);
59 return s;
60}
61
Ed Warnickecb9cada2015-12-08 15:45:58 -070062uword
63unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
64{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070065 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070067 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
69 foreach_ipsec_policy_action
70#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071 else
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 return 0;
73 return 1;
74}
75
76u8 *
77format_ipsec_crypto_alg (u8 * s, va_list * args)
78{
79 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070080 u8 *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
82 switch (i)
83 {
84#define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070085 foreach_ipsec_crypto_alg
Ed Warnickecb9cada2015-12-08 15:45:58 -070086#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070087 default:
88 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 }
90 s = format (s, "%s", t);
91 return s;
92}
93
94uword
95unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
96{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070097 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070099 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
101 foreach_ipsec_crypto_alg
102#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700103 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 return 0;
105 return 1;
106}
107
108u8 *
109format_ipsec_integ_alg (u8 * s, va_list * args)
110{
111 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700112 u8 *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
114 switch (i)
115 {
116#define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700117 foreach_ipsec_integ_alg
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700119 default:
120 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 }
122 s = format (s, "%s", t);
123 return s;
124}
125
126uword
127unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
128{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700129 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700131 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
133 foreach_ipsec_integ_alg
134#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700135 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 return 0;
137 return 1;
138}
139
140u8 *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700141format_ipsec_replay_window (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142{
143 u64 w = va_arg (*args, u64);
144 u8 i;
145
146 for (i = 0; i < 64; i++)
147 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700148 s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 }
150
151 return s;
152}
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700153
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800154u8 *
155format_ipsec_policy (u8 * s, va_list * args)
156{
157 u32 pi = va_arg (*args, u32);
Guillaume Solignac5f08ab62019-05-20 15:58:46 +0200158 ip46_type_t ip_type = IP46_TYPE_IP4;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800159 ipsec_main_t *im = &ipsec_main;
160 ipsec_policy_t *p;
161 vlib_counter_t counts;
162
163 p = pool_elt_at_index (im->policies, pi);
164
Neale Ranns9f231d42019-03-19 10:06:00 +0000165 s = format (s, " [%d] priority %d action %U type %U protocol ",
166 pi, p->priority,
167 format_ipsec_policy_action, p->policy,
168 format_ipsec_policy_type, p->type);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800169 if (p->protocol)
170 {
171 s = format (s, "%U", format_ip_protocol, p->protocol);
172 }
173 else
174 {
175 s = format (s, "any");
176 }
177 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
178 {
179 s = format (s, " sa %u", p->sa_id);
180 }
Guillaume Solignac5f08ab62019-05-20 15:58:46 +0200181 if (p->is_ipv6)
182 {
183 ip_type = IP46_TYPE_IP6;
184 }
Neale Ranns231c4692019-03-18 17:11:28 +0000185
186 s = format (s, "\n local addr range %U - %U port range %u - %u",
Guillaume Solignac5f08ab62019-05-20 15:58:46 +0200187 format_ip46_address, &p->laddr.start, ip_type,
188 format_ip46_address, &p->laddr.stop, ip_type,
Neale Ranns231c4692019-03-18 17:11:28 +0000189 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",
Guillaume Solignac5f08ab62019-05-20 15:58:46 +0200192 format_ip46_address, &p->raddr.start, ip_type,
193 format_ip46_address, &p->raddr.stop, ip_type,
Neale Ranns231c4692019-03-18 17:11:28 +0000194 clib_net_to_host_u16 (p->rport.start),
195 clib_net_to_host_u16 (p->rport.stop));
196
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800197 vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
198 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
199
200 return (s);
201}
202
203u8 *
204format_ipsec_spd (u8 * s, va_list * args)
205{
206 u32 si = va_arg (*args, u32);
207 ipsec_main_t *im = &ipsec_main;
208 ipsec_spd_t *spd;
209 u32 *i;
210
Neale Rannsb294f102019-04-03 13:17:50 +0000211 if (pool_is_free_index (im->spds, si))
212 {
213 s = format (s, "No such SPD index: %d", si);
214 goto done;
215 }
216
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800217 spd = pool_elt_at_index (im->spds, si);
218
219 s = format (s, "spd %u", spd->id);
220
221#define _(v, n) \
222 s = format (s, "\n %s:", n); \
223 vec_foreach(i, spd->policies[IPSEC_SPD_POLICY_##v]) \
224 { \
225 s = format (s, "\n %U", format_ipsec_policy, *i); \
226 }
227 foreach_ipsec_spd_policy_type;
228#undef _
229
Neale Rannsb294f102019-04-03 13:17:50 +0000230done:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800231 return (s);
232}
233
Neale Ranns8d7c5022019-02-06 01:41:05 -0800234u8 *
235format_ipsec_key (u8 * s, va_list * args)
236{
237 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
238
239 return (format (s, "%U", format_hex_bytes, key->data, key->len));
240}
241
242uword
243unformat_ipsec_key (unformat_input_t * input, va_list * args)
244{
245 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
246 u8 *data;
247
248 if (unformat (input, "%U", unformat_hex_string, &data))
249 {
250 ipsec_mk_key (key, data, vec_len (data));
251 vec_free (data);
252 }
253 else
254 return 0;
255 return 1;
256}
257
258u8 *
Neale Rannse524d452019-02-19 15:22:46 +0000259format_ipsec_sa_flags (u8 * s, va_list * args)
260{
261 ipsec_sa_flags_t flags = va_arg (*args, int);
262
263 if (0)
264 ;
265#define _(v, f, str) else if (flags & IPSEC_SA_FLAG_##f) s = format(s, "%s ", str);
266 foreach_ipsec_sa_flags
267#undef _
268 return (s);
269}
270
271u8 *
Neale Ranns8d7c5022019-02-06 01:41:05 -0800272format_ipsec_sa (u8 * s, va_list * args)
273{
274 u32 sai = va_arg (*args, u32);
Neale Rannsb294f102019-04-03 13:17:50 +0000275 ipsec_format_flags_t flags = va_arg (*args, ipsec_format_flags_t);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800276 ipsec_main_t *im = &ipsec_main;
Neale Rannseba31ec2019-02-17 18:04:27 +0000277 vlib_counter_t counts;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800278 u32 tx_table_id;
279 ipsec_sa_t *sa;
280
Neale Rannsb294f102019-04-03 13:17:50 +0000281 if (pool_is_free_index (im->sad, sai))
282 {
283 s = format (s, "No such SA index: %d", sai);
284 goto done;
285 }
286
Neale Ranns8d7c5022019-02-06 01:41:05 -0800287 sa = pool_elt_at_index (im->sad, sai);
288
Neale Rannse524d452019-02-19 15:22:46 +0000289 s = format (s, "[%d] sa 0x%x spi %u mode %s%s protocol %s %U",
Neale Ranns8d7c5022019-02-06 01:41:05 -0800290 sai, sa->id, sa->spi,
Damjan Mariond709cbc2019-03-26 13:16:42 +0100291 ipsec_sa_is_set_IS_TUNNEL (sa) ? "tunnel" : "transport",
292 ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ? "-ip6" : "",
Neale Rannse524d452019-02-19 15:22:46 +0000293 sa->protocol ? "esp" : "ah", format_ipsec_sa_flags, sa->flags);
Neale Rannsb294f102019-04-03 13:17:50 +0000294
295 if (!(flags & IPSEC_FORMAT_DETAIL))
296 goto done;
297
Neale Ranns80f6fd52019-04-16 02:41:34 +0000298 s = format (s, "\n salt 0x%x", clib_net_to_host_u32 (sa->salt));
Neale Ranns00a44202019-03-21 16:36:28 +0000299 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",
Neale Ranns8d7c5022019-02-06 01:41:05 -0800301 sa->last_seq, sa->last_seq_hi,
302 format_ipsec_replay_window, sa->replay_window);
Neale Rannsb64cd2c2019-04-16 16:21:57 -0700303 s = format (s, "\n crypto alg %U",
304 format_ipsec_crypto_alg, sa->crypto_alg);
305 if (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);
309 if (sa->integ_alg)
310 s = format (s, " key %U", format_ipsec_key, &sa->integ_key);
Neale Ranns80f6fd52019-04-16 02:41:34 +0000311
Neale Rannseba31ec2019-02-17 18:04:27 +0000312 vlib_get_combined_counter (&ipsec_sa_counters, sai, &counts);
313 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800314
Damjan Mariond709cbc2019-03-26 13:16:42 +0100315 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800316 {
317 tx_table_id = fib_table_get_table_id (sa->tx_fib_index,
318 FIB_PROTOCOL_IP4);
319 s = format (s, "\n table-ID %d tunnel src %U dst %U",
320 tx_table_id,
321 format_ip46_address, &sa->tunnel_src_addr, IP46_TYPE_ANY,
322 format_ip46_address, &sa->tunnel_dst_addr, IP46_TYPE_ANY);
Neale Ranns2b5ba952019-04-02 10:15:40 +0000323 if (!ipsec_sa_is_set_IS_INBOUND (sa))
324 {
325 s =
326 format (s, "\n resovle via fib-entry: %d",
327 sa->fib_entry_index);
328 s = format (s, "\n stacked on:");
329 s =
330 format (s, "\n %U", format_dpo_id,
331 &sa->dpo[IPSEC_PROTOCOL_ESP], 6);
332 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800333 }
334
Neale Rannsb294f102019-04-03 13:17:50 +0000335done:
336 return (s);
337}
338
339u8 *
340format_ipsec_tunnel (u8 * s, va_list * args)
341{
342 ipsec_main_t *im = &ipsec_main;
343 u32 ti = va_arg (*args, u32);
Neale Rannsb294f102019-04-03 13:17:50 +0000344 ipsec_tunnel_if_t *t;
345
346 if (pool_is_free_index (im->tunnel_interfaces, ti))
347 {
348 s = format (s, "No such tunnel index: %d", ti);
349 goto done;
350 }
351
352 t = pool_elt_at_index (im->tunnel_interfaces, ti);
353
354 if (t->hw_if_index == ~0)
355 goto done;
356
BenoƮt Gannee7a527f2019-04-29 16:31:24 +0200357 s =
358 format (s, "%U\n", format_vnet_hw_if_index_name, im->vnet_main,
359 t->hw_if_index);
Neale Rannsb294f102019-04-03 13:17:50 +0000360
361 s = format (s, " out-bound sa: ");
362 s = format (s, "%U\n", format_ipsec_sa, t->output_sa_index,
363 IPSEC_FORMAT_BRIEF);
364
365 s = format (s, " in-bound sa: ");
366 s = format (s, "%U\n", format_ipsec_sa, t->input_sa_index,
367 IPSEC_FORMAT_BRIEF);
368
369done:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800370 return (s);
371}
372
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700373/*
374 * fd.io coding-style-patch-verification: ON
375 *
376 * Local Variables:
377 * eval: (c-set-style "gnu")
378 * End:
379 */