blob: 1c3c5f95262c754f2a467809810036ec3d11104f [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>
Neale Rannsc87b66c2019-02-07 07:26:12 -080025#include <vnet/ipsec/ipsec_tun.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
27u8 *
28format_ipsec_policy_action (u8 * s, va_list * args)
29{
30 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070031 char *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070032
33 switch (i)
34 {
35#define _(v,f,str) case IPSEC_POLICY_ACTION_##f: t = str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070036 foreach_ipsec_policy_action
Ed Warnickecb9cada2015-12-08 15:45:58 -070037#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070038 default:
39 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 }
41 s = format (s, "%s", t);
42 return s;
43}
44
Neale Ranns9f231d42019-03-19 10:06:00 +000045u8 *
46format_ipsec_policy_type (u8 * s, va_list * args)
47{
48 u32 i = va_arg (*args, u32);
49 char *t = 0;
50
51 switch (i)
52 {
53#define _(f,str) case IPSEC_SPD_POLICY_##f: t = str; break;
54 foreach_ipsec_spd_policy_type
55#undef _
56 default:
57 s = format (s, "unknown");
58 }
59 s = format (s, "%s", t);
60 return s;
61}
62
Ed Warnickecb9cada2015-12-08 15:45:58 -070063uword
64unformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
65{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070066 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070068 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070069#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
70 foreach_ipsec_policy_action
71#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070072 else
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 return 0;
74 return 1;
75}
76
77u8 *
78format_ipsec_crypto_alg (u8 * s, va_list * args)
79{
80 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070081 u8 *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 switch (i)
84 {
85#define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070086 foreach_ipsec_crypto_alg
Ed Warnickecb9cada2015-12-08 15:45:58 -070087#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070088 default:
89 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 }
91 s = format (s, "%s", t);
92 return s;
93}
94
95uword
96unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args)
97{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070098 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700100 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_CRYPTO_ALG_##f;
102 foreach_ipsec_crypto_alg
103#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700104 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 return 0;
106 return 1;
107}
108
109u8 *
110format_ipsec_integ_alg (u8 * s, va_list * args)
111{
112 u32 i = va_arg (*args, u32);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700113 u8 *t = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
115 switch (i)
116 {
117#define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700118 foreach_ipsec_integ_alg
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700120 default:
121 s = format (s, "unknown");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 }
123 s = format (s, "%s", t);
124 return s;
125}
126
127uword
128unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args)
129{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700130 u32 *r = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700132 if (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_INTEG_ALG_##f;
134 foreach_ipsec_integ_alg
135#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700136 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 return 0;
138 return 1;
139}
140
141u8 *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700142format_ipsec_replay_window (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
144 u64 w = va_arg (*args, u64);
145 u8 i;
146
147 for (i = 0; i < 64; i++)
148 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700149 s = format (s, "%u", w & (1ULL << i) ? 1 : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 }
151
152 return s;
153}
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700154
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800155u8 *
156format_ipsec_policy (u8 * s, va_list * args)
157{
158 u32 pi = va_arg (*args, u32);
Guillaume Solignac5f08ab62019-05-20 15:58:46 +0200159 ip46_type_t ip_type = IP46_TYPE_IP4;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800160 ipsec_main_t *im = &ipsec_main;
161 ipsec_policy_t *p;
162 vlib_counter_t counts;
163
164 p = pool_elt_at_index (im->policies, pi);
165
Neale Ranns9f231d42019-03-19 10:06:00 +0000166 s = format (s, " [%d] priority %d action %U type %U protocol ",
167 pi, p->priority,
168 format_ipsec_policy_action, p->policy,
169 format_ipsec_policy_type, p->type);
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800170 if (p->protocol)
171 {
172 s = format (s, "%U", format_ip_protocol, p->protocol);
173 }
174 else
175 {
176 s = format (s, "any");
177 }
178 if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
179 {
180 s = format (s, " sa %u", p->sa_id);
181 }
Guillaume Solignac5f08ab62019-05-20 15:58:46 +0200182 if (p->is_ipv6)
183 {
184 ip_type = IP46_TYPE_IP6;
185 }
Neale Ranns231c4692019-03-18 17:11:28 +0000186
187 s = format (s, "\n local addr range %U - %U port range %u - %u",
Guillaume Solignac5f08ab62019-05-20 15:58:46 +0200188 format_ip46_address, &p->laddr.start, ip_type,
189 format_ip46_address, &p->laddr.stop, ip_type,
Neale Ranns231c4692019-03-18 17:11:28 +0000190 clib_net_to_host_u16 (p->lport.start),
191 clib_net_to_host_u16 (p->lport.stop));
192 s = format (s, "\n remote addr range %U - %U port range %u - %u",
Guillaume Solignac5f08ab62019-05-20 15:58:46 +0200193 format_ip46_address, &p->raddr.start, ip_type,
194 format_ip46_address, &p->raddr.stop, ip_type,
Neale Ranns231c4692019-03-18 17:11:28 +0000195 clib_net_to_host_u16 (p->rport.start),
196 clib_net_to_host_u16 (p->rport.stop));
197
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800198 vlib_get_combined_counter (&ipsec_spd_policy_counters, pi, &counts);
199 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
200
201 return (s);
202}
203
204u8 *
205format_ipsec_spd (u8 * s, va_list * args)
206{
207 u32 si = va_arg (*args, u32);
208 ipsec_main_t *im = &ipsec_main;
209 ipsec_spd_t *spd;
210 u32 *i;
211
Neale Rannsb294f102019-04-03 13:17:50 +0000212 if (pool_is_free_index (im->spds, si))
213 {
214 s = format (s, "No such SPD index: %d", si);
215 goto done;
216 }
217
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800218 spd = pool_elt_at_index (im->spds, si);
219
220 s = format (s, "spd %u", spd->id);
221
222#define _(v, n) \
223 s = format (s, "\n %s:", n); \
224 vec_foreach(i, spd->policies[IPSEC_SPD_POLICY_##v]) \
225 { \
226 s = format (s, "\n %U", format_ipsec_policy, *i); \
227 }
228 foreach_ipsec_spd_policy_type;
229#undef _
230
Neale Rannsb294f102019-04-03 13:17:50 +0000231done:
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800232 return (s);
233}
234
Neale Ranns8d7c5022019-02-06 01:41:05 -0800235u8 *
236format_ipsec_key (u8 * s, va_list * args)
237{
238 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
239
240 return (format (s, "%U", format_hex_bytes, key->data, key->len));
241}
242
243uword
244unformat_ipsec_key (unformat_input_t * input, va_list * args)
245{
246 ipsec_key_t *key = va_arg (*args, ipsec_key_t *);
247 u8 *data;
248
249 if (unformat (input, "%U", unformat_hex_string, &data))
250 {
251 ipsec_mk_key (key, data, vec_len (data));
252 vec_free (data);
253 }
254 else
255 return 0;
256 return 1;
257}
258
259u8 *
Neale Rannse524d452019-02-19 15:22:46 +0000260format_ipsec_sa_flags (u8 * s, va_list * args)
261{
262 ipsec_sa_flags_t flags = va_arg (*args, int);
263
264 if (0)
265 ;
266#define _(v, f, str) else if (flags & IPSEC_SA_FLAG_##f) s = format(s, "%s ", str);
267 foreach_ipsec_sa_flags
268#undef _
269 return (s);
270}
271
272u8 *
Neale Ranns8d7c5022019-02-06 01:41:05 -0800273format_ipsec_sa (u8 * s, va_list * args)
274{
275 u32 sai = va_arg (*args, u32);
Neale Rannsb294f102019-04-03 13:17:50 +0000276 ipsec_format_flags_t flags = va_arg (*args, ipsec_format_flags_t);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800277 ipsec_main_t *im = &ipsec_main;
Neale Rannseba31ec2019-02-17 18:04:27 +0000278 vlib_counter_t counts;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800279 u32 tx_table_id;
280 ipsec_sa_t *sa;
281
Neale Rannsb294f102019-04-03 13:17:50 +0000282 if (pool_is_free_index (im->sad, sai))
283 {
284 s = format (s, "No such SA index: %d", sai);
285 goto done;
286 }
287
Neale Ranns8d7c5022019-02-06 01:41:05 -0800288 sa = pool_elt_at_index (im->sad, sai);
289
Guillaume Solignac8f818cc2019-05-15 12:02:33 +0200290 s = format (s, "[%d] sa 0x%x spi %u (0x%08x) mode %s%s protocol %s %U",
291 sai, sa->id, sa->spi, sa->spi,
Damjan Mariond709cbc2019-03-26 13:16:42 +0100292 ipsec_sa_is_set_IS_TUNNEL (sa) ? "tunnel" : "transport",
293 ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ? "-ip6" : "",
Neale Rannse524d452019-02-19 15:22:46 +0000294 sa->protocol ? "esp" : "ah", format_ipsec_sa_flags, sa->flags);
Neale Rannsb294f102019-04-03 13:17:50 +0000295
296 if (!(flags & IPSEC_FORMAT_DETAIL))
297 goto done;
298
Neale Ranns80f6fd52019-04-16 02:41:34 +0000299 s = format (s, "\n salt 0x%x", clib_net_to_host_u32 (sa->salt));
Neale Ranns00a44202019-03-21 16:36:28 +0000300 s = format (s, "\n seq %u seq-hi %u", sa->seq, sa->seq_hi);
301 s = format (s, "\n last-seq %u last-seq-hi %u window %U",
Neale Ranns8d7c5022019-02-06 01:41:05 -0800302 sa->last_seq, sa->last_seq_hi,
303 format_ipsec_replay_window, sa->replay_window);
Neale Rannsb64cd2c2019-04-16 16:21:57 -0700304 s = format (s, "\n crypto alg %U",
305 format_ipsec_crypto_alg, sa->crypto_alg);
306 if (sa->crypto_alg)
307 s = format (s, " key %U", format_ipsec_key, &sa->crypto_key);
308 s = format (s, "\n integrity alg %U",
309 format_ipsec_integ_alg, sa->integ_alg);
310 if (sa->integ_alg)
311 s = format (s, " key %U", format_ipsec_key, &sa->integ_key);
Neale Ranns80f6fd52019-04-16 02:41:34 +0000312
Neale Rannseba31ec2019-02-17 18:04:27 +0000313 vlib_get_combined_counter (&ipsec_sa_counters, sai, &counts);
314 s = format (s, "\n packets %u bytes %u", counts.packets, counts.bytes);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800315
Damjan Mariond709cbc2019-03-26 13:16:42 +0100316 if (ipsec_sa_is_set_IS_TUNNEL (sa))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800317 {
318 tx_table_id = fib_table_get_table_id (sa->tx_fib_index,
319 FIB_PROTOCOL_IP4);
320 s = format (s, "\n table-ID %d tunnel src %U dst %U",
321 tx_table_id,
322 format_ip46_address, &sa->tunnel_src_addr, IP46_TYPE_ANY,
323 format_ip46_address, &sa->tunnel_dst_addr, IP46_TYPE_ANY);
Neale Ranns2b5ba952019-04-02 10:15:40 +0000324 if (!ipsec_sa_is_set_IS_INBOUND (sa))
325 {
326 s =
327 format (s, "\n resovle via fib-entry: %d",
328 sa->fib_entry_index);
329 s = format (s, "\n stacked on:");
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000330 s = format (s, "\n %U", format_dpo_id, &sa->dpo, 6);
Neale Ranns2b5ba952019-04-02 10:15:40 +0000331 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800332 }
333
Neale Rannsb294f102019-04-03 13:17:50 +0000334done:
335 return (s);
336}
337
338u8 *
339format_ipsec_tunnel (u8 * s, va_list * args)
340{
341 ipsec_main_t *im = &ipsec_main;
342 u32 ti = va_arg (*args, u32);
Neale Rannsb294f102019-04-03 13:17:50 +0000343 ipsec_tunnel_if_t *t;
344
345 if (pool_is_free_index (im->tunnel_interfaces, ti))
346 {
347 s = format (s, "No such tunnel index: %d", ti);
348 goto done;
349 }
350
351 t = pool_elt_at_index (im->tunnel_interfaces, ti);
352
353 if (t->hw_if_index == ~0)
354 goto done;
355
BenoƮt Gannee7a527f2019-04-29 16:31:24 +0200356 s =
357 format (s, "%U\n", format_vnet_hw_if_index_name, im->vnet_main,
358 t->hw_if_index);
Neale Rannsb294f102019-04-03 13:17:50 +0000359
360 s = format (s, " out-bound sa: ");
361 s = format (s, "%U\n", format_ipsec_sa, t->output_sa_index,
362 IPSEC_FORMAT_BRIEF);
363
364 s = format (s, " in-bound sa: ");
365 s = format (s, "%U\n", format_ipsec_sa, t->input_sa_index,
366 IPSEC_FORMAT_BRIEF);
367
368done:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800369 return (s);
370}
371
Neale Rannsc87b66c2019-02-07 07:26:12 -0800372u8 *
373format_ipsec_tun_protect (u8 * s, va_list * args)
374{
375 u32 itpi = va_arg (*args, u32);
376 ipsec_tun_protect_t *itp;
377 u32 sai;
378
379 if (pool_is_free_index (ipsec_protect_pool, itpi))
380 {
381 s = format (s, "No such tunnel index: %d", itpi);
382 goto done;
383 }
384
385 itp = pool_elt_at_index (ipsec_protect_pool, itpi);
386
387 s = format (s, "%U", format_vnet_sw_if_index_name,
388 vnet_get_main (), itp->itp_sw_if_index);
389 s = format (s, "\n output-sa:");
390 s =
391 format (s, "\n %U", format_ipsec_sa, itp->itp_out_sa,
392 IPSEC_FORMAT_BRIEF);
393
394 s = format (s, "\n input-sa:");
395 /* *INDENT-OFF* */
396 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
397 ({
398 s = format (s, "\n %U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
399 }));
400 /* *INDENT-ON* */
401
402done:
403 return (s);
404}
405
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700406/*
407 * fd.io coding-style-patch-verification: ON
408 *
409 * Local Variables:
410 * eval: (c-set-style "gnu")
411 * End:
412 */