ikev2: dump state and profile name in CLI and API
[vpp.git] / src / plugins / ikev2 / ikev2_cli.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vppinfra/error.h>
18 #include <vnet/ipsec/ipsec_sa.h>
19 #include <plugins/ikev2/ikev2.h>
20 #include <plugins/ikev2/ikev2_priv.h>
21
22 u8 *
23 format_ikev2_id_type_and_data (u8 * s, va_list * args)
24 {
25   ikev2_id_t *id = va_arg (*args, ikev2_id_t *);
26
27   if (id->type == 0 || vec_len (id->data) == 0)
28     return format (s, "none");
29
30   s = format (s, "id-type %U data ", format_ikev2_id_type, id->type);
31
32   switch (id->type)
33     {
34     case IKEV2_ID_TYPE_ID_IPV4_ADDR:
35       s = format (s, "%U", format_ip4_address, id->data);
36       break;
37     case IKEV2_ID_TYPE_ID_IPV6_ADDR:
38       s = format (s, "%U", format_ip6_address, id->data);
39       break;
40     case IKEV2_ID_TYPE_ID_FQDN: /* fallthrough */
41     case IKEV2_ID_TYPE_ID_RFC822_ADDR:
42       s = format (s, "%v", id->data);
43       break;
44     default:
45       s = format (s, "0x%U", format_hex_bytes, &id->data,
46                   (uword) (vec_len (id->data)));
47       break;
48     }
49
50   return s;
51 }
52
53 static u8 *
54 format_ikev2_traffic_selector (u8 * s, va_list * va)
55 {
56   ikev2_ts_t *ts = va_arg (*va, ikev2_ts_t *);
57   u32 index = va_arg (*va, u32);
58
59   s = format (s, "%u type %u protocol_id %u addr "
60               "%U - %U port %u - %u\n",
61               index, ts->ts_type, ts->protocol_id,
62               format_ip_address, &ts->start_addr,
63               format_ip_address, &ts->end_addr,
64               clib_net_to_host_u16 (ts->start_port),
65               clib_net_to_host_u16 (ts->end_port));
66   return s;
67 }
68
69 static u8 *
70 format_ikev2_child_sa (u8 * s, va_list * va)
71 {
72   ikev2_child_sa_t *child = va_arg (*va, ikev2_child_sa_t *);
73   u32 index = va_arg (*va, u32);
74   ikev2_ts_t *ts;
75   ikev2_sa_transform_t *tr;
76   u8 *c = 0;
77
78   u32 indent = format_get_indent (s);
79   indent += 1;
80
81   s = format (s, "child sa %u:", index);
82
83   tr = ikev2_sa_get_td_for_type (child->r_proposals,
84                                  IKEV2_TRANSFORM_TYPE_ENCR);
85   c = format (c, "%U ", format_ikev2_sa_transform, tr);
86
87   tr = ikev2_sa_get_td_for_type (child->r_proposals,
88                                  IKEV2_TRANSFORM_TYPE_INTEG);
89   c = format (c, "%U ", format_ikev2_sa_transform, tr);
90
91   tr = ikev2_sa_get_td_for_type (child->r_proposals,
92                                  IKEV2_TRANSFORM_TYPE_ESN);
93   c = format (c, "%U ", format_ikev2_sa_transform, tr);
94
95   s = format (s, "%v\n", c);
96   vec_free (c);
97
98   s = format (s, "%Uspi(i) %lx spi(r) %lx\n", format_white_space, indent,
99               child->i_proposals ? child->i_proposals[0].spi : 0,
100               child->r_proposals ? child->r_proposals[0].spi : 0);
101
102   s = format (s, "%USK_e  i:%U\n%Ur:%U\n",
103               format_white_space, indent,
104               format_hex_bytes, child->sk_ei, vec_len (child->sk_ei),
105               format_white_space, indent + 6,
106               format_hex_bytes, child->sk_er, vec_len (child->sk_er));
107   if (child->sk_ai)
108     {
109       s = format (s, "%USK_a  i:%U\n%Ur:%U\n",
110                   format_white_space, indent,
111                   format_hex_bytes, child->sk_ai, vec_len (child->sk_ai),
112                   format_white_space, indent + 6,
113                   format_hex_bytes, child->sk_ar, vec_len (child->sk_ar));
114     }
115   s = format (s, "%Utraffic selectors (i):", format_white_space, indent);
116   vec_foreach (ts, child->tsi)
117     s = format (s, "%U", format_ikev2_traffic_selector, ts, ts - child->tsi);
118   s = format (s, "%Utraffic selectors (r):", format_white_space, indent);
119   vec_foreach (ts, child->tsr)
120     s = format (s, "%U", format_ikev2_traffic_selector, ts, ts - child->tsr);
121   return s;
122 }
123
124 static char *stateNames[] = {
125 #define _(v, f, s) s,
126   foreach_ikev2_state
127 #undef _
128 };
129
130 static u8 *
131 format_ikev2_sa (u8 * s, va_list * va)
132 {
133   ikev2_sa_t *sa = va_arg (*va, ikev2_sa_t *);
134   int details = va_arg (*va, int);
135   ikev2_sa_transform_t *tr;
136   ikev2_child_sa_t *child;
137   u32 indent = 1;
138
139   ikev2_main_t *km = &ikev2_main;
140   ikev2_profile_t *p;
141
142   p = pool_elt_at_index (km->profiles, sa->profile_index);
143
144   s = format (s, "iip %U ispi %lx rip %U rspi %lx",
145               format_ip_address, &sa->iaddr, sa->ispi,
146               format_ip_address, &sa->raddr, sa->rspi);
147   if (!details)
148     return s;
149
150   s = format (s, "\n%U", format_white_space, indent);
151
152   tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
153   s = format (s, "%U ", format_ikev2_sa_transform, tr);
154
155   tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
156   s = format (s, "%U ", format_ikev2_sa_transform, tr);
157
158   tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
159   s = format (s, "%U ", format_ikev2_sa_transform, tr);
160
161   tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH);
162   s = format (s, "%U", format_ikev2_sa_transform, tr);
163
164   s = format (s, "\n profile: %v", p->name);
165
166   if (sa->state <= IKEV2_STATE_NO_PROPOSAL_CHOSEN)
167     {
168       s = format (s, "\n state: %s", stateNames[sa->state]);
169     }
170
171   s = format (s, "\n%U", format_white_space, indent);
172
173   s = format (s, "nonce i:%U\n%Ur:%U\n",
174               format_hex_bytes, sa->i_nonce, vec_len (sa->i_nonce),
175               format_white_space, indent + 6,
176               format_hex_bytes, sa->r_nonce, vec_len (sa->r_nonce));
177
178   s = format (s, "%USK_d    %U\n", format_white_space, indent,
179               format_hex_bytes, sa->sk_d, vec_len (sa->sk_d));
180   if (sa->sk_ai)
181     {
182       s = format (s, "%USK_a  i:%U\n%Ur:%U\n",
183                   format_white_space, indent,
184                   format_hex_bytes, sa->sk_ai, vec_len (sa->sk_ai),
185                   format_white_space, indent + 6,
186                   format_hex_bytes, sa->sk_ar, vec_len (sa->sk_ar));
187     }
188   s = format (s, "%USK_e  i:%U\n%Ur:%U\n",
189               format_white_space, indent,
190               format_hex_bytes, sa->sk_ei, vec_len (sa->sk_ei),
191               format_white_space, indent + 6,
192               format_hex_bytes, sa->sk_er, vec_len (sa->sk_er));
193   s = format (s, "%USK_p  i:%U\n%Ur:%U\n",
194               format_white_space, indent,
195               format_hex_bytes, sa->sk_pi, vec_len (sa->sk_pi),
196               format_white_space, indent + 6,
197               format_hex_bytes, sa->sk_pr, vec_len (sa->sk_pr));
198
199   s = format (s, "%Uidentifier (i) %U\n",
200               format_white_space, indent,
201               format_ikev2_id_type_and_data, &sa->i_id);
202   s = format (s, "%Uidentifier (r) %U\n",
203               format_white_space, indent,
204               format_ikev2_id_type_and_data, &sa->r_id);
205
206   vec_foreach (child, sa->childs)
207   {
208     s = format (s, "%U%U", format_white_space, indent + 2,
209                 format_ikev2_child_sa, child, child - sa->childs);
210   }
211
212   s = format (s, "Stats:\n");
213   s = format (s, " keepalives :%u\n", sa->stats.n_keepalives);
214   s = format (s, " rekey :%u\n", sa->stats.n_rekey_req);
215   s = format (s, " SA init :%u (retransmit: %u)\n", sa->stats.n_sa_init_req,
216               sa->stats.n_init_retransmit);
217   s = format (s, " retransmit: %u\n", sa->stats.n_retransmit);
218   s = format (s, " SA auth :%u\n", sa->stats.n_sa_auth_req);
219
220   return s;
221 }
222
223 static clib_error_t *
224 show_ikev2_sa_command_fn (vlib_main_t * vm,
225                           unformat_input_t * input, vlib_cli_command_t * cmd)
226 {
227   unformat_input_t _line_input, *line_input = &_line_input;
228   ikev2_main_t *km = &ikev2_main;
229   ikev2_main_per_thread_data_t *tkm;
230   ikev2_sa_t *sa;
231   u64 rspi;
232   u8 *s = 0;
233   int details = 0, show_one = 0;
234
235   if (unformat_user (input, unformat_line_input, line_input))
236     {
237       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
238         {
239           if (unformat (line_input, "rspi %lx", &rspi))
240             {
241               show_one = 1;
242             }
243           else if (unformat (line_input, "details"))
244             details = 1;
245           else
246             break;
247         }
248       unformat_free (line_input);
249     }
250
251   vec_foreach (tkm, km->per_thread_data)
252   {
253     /* *INDENT-OFF* */
254     pool_foreach (sa, tkm->sas)  {
255       if (show_one)
256         {
257           if (sa->rspi == rspi)
258             {
259               s = format (s, "%U\n", format_ikev2_sa, sa, 1);
260               break;
261             }
262         }
263       else
264         s = format (s, "%U\n", format_ikev2_sa, sa, details);
265     }
266     /* *INDENT-ON* */
267   }
268
269   vlib_cli_output (vm, "%v", s);
270   vec_free (s);
271   return 0;
272 }
273
274 /* *INDENT-OFF* */
275 VLIB_CLI_COMMAND (show_ikev2_sa_command, static) = {
276     .path = "show ikev2 sa",
277     .short_help = "show ikev2 sa [rspi <rspi>] [details]",
278     .function = show_ikev2_sa_command_fn,
279 };
280 /* *INDENT-ON* */
281
282 static clib_error_t *
283 ikev2_disable_dpd_command_fn (vlib_main_t * vm,
284                               unformat_input_t * input,
285                               vlib_cli_command_t * cmd)
286 {
287   ikev2_disable_dpd ();
288   return 0;
289 }
290
291 /* *INDENT-OFF* */
292 VLIB_CLI_COMMAND (ikev2_cli_disable_dpd_command, static) = {
293   .path = "ikev2 dpd disable",
294   .short_help = "ikev2 dpd disable",
295   .function = ikev2_disable_dpd_command_fn,
296 };
297 /* *INDENT-ON* */
298
299 static uword
300 unformat_ikev2_token (unformat_input_t * input, va_list * va)
301 {
302   u8 **string_return = va_arg (*va, u8 **);
303   const char *token_chars = "a-zA-Z0-9_";
304   if (*string_return)
305     {
306       /* if string_return was already allocated (eg. because of a previous
307        * partial match with a successful unformat_token()), we must free it
308        * before reusing the pointer, otherwise we'll be leaking memory
309        */
310       vec_free (*string_return);
311       *string_return = 0;
312     }
313   return unformat_user (input, unformat_token, token_chars, string_return);
314 }
315
316 static clib_error_t *
317 ikev2_profile_add_del_command_fn (vlib_main_t * vm,
318                                   unformat_input_t * input,
319                                   vlib_cli_command_t * cmd)
320 {
321   vnet_main_t *vnm = vnet_get_main ();
322   unformat_input_t _line_input, *line_input = &_line_input;
323   u8 *name = 0;
324   clib_error_t *r = 0;
325   u32 id_type;
326   u8 *data = 0;
327   u32 tmp1, tmp2, tmp3;
328   u64 tmp4, tmp5;
329   ip_address_t ip, end_addr;
330   u32 responder_sw_if_index = (u32) ~ 0;
331   u32 tun_sw_if_index = (u32) ~ 0;
332   ikev2_transform_encr_type_t crypto_alg;
333   ikev2_transform_integ_type_t integ_alg;
334   ikev2_transform_dh_type_t dh_type;
335
336   if (!unformat_user (input, unformat_line_input, line_input))
337     return 0;
338
339   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
340     {
341       if (unformat (line_input, "add %U", unformat_ikev2_token, &name))
342         {
343           r = ikev2_add_del_profile (vm, name, 1);
344           goto done;
345         }
346       else if (unformat (line_input, "del %U", unformat_ikev2_token, &name))
347         {
348           r = ikev2_add_del_profile (vm, name, 0);
349           goto done;
350         }
351       else if (unformat (line_input, "set %U auth shared-key-mic string %v",
352                          unformat_ikev2_token, &name, &data))
353         {
354           r =
355             ikev2_set_profile_auth (vm, name,
356                                     IKEV2_AUTH_METHOD_SHARED_KEY_MIC, data,
357                                     0);
358           goto done;
359         }
360       else if (unformat (line_input, "set %U auth shared-key-mic hex %U",
361                          unformat_ikev2_token, &name,
362                          unformat_hex_string, &data))
363         {
364           r =
365             ikev2_set_profile_auth (vm, name,
366                                     IKEV2_AUTH_METHOD_SHARED_KEY_MIC, data,
367                                     1);
368           goto done;
369         }
370       else if (unformat (line_input, "set %U auth rsa-sig cert-file %v",
371                          unformat_ikev2_token, &name, &data))
372         {
373           r =
374             ikev2_set_profile_auth (vm, name, IKEV2_AUTH_METHOD_RSA_SIG, data,
375                                     0);
376           goto done;
377         }
378       else if (unformat (line_input, "set %U id local %U %U",
379                          unformat_ikev2_token, &name,
380                          unformat_ikev2_id_type, &id_type,
381                          unformat_ip_address, &ip))
382         {
383           data = vec_new (u8, ip_address_size (&ip));
384           clib_memcpy (data, ip_addr_bytes (&ip), ip_address_size (&ip));
385           r =
386             ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
387           goto done;
388         }
389       else if (unformat (line_input, "set %U id local %U 0x%U",
390                          unformat_ikev2_token, &name,
391                          unformat_ikev2_id_type, &id_type,
392                          unformat_hex_string, &data))
393         {
394           r =
395             ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
396           goto done;
397         }
398       else if (unformat (line_input, "set %U id local %U %v",
399                          unformat_ikev2_token, &name,
400                          unformat_ikev2_id_type, &id_type, &data))
401         {
402           r =
403             ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
404           goto done;
405         }
406       else if (unformat (line_input, "set %U id remote %U %U",
407                          unformat_ikev2_token, &name,
408                          unformat_ikev2_id_type, &id_type,
409                          unformat_ip_address, &ip))
410         {
411           data = vec_new (u8, ip_address_size (&ip));
412           clib_memcpy (data, ip_addr_bytes (&ip), ip_address_size (&ip));
413           r = ikev2_set_profile_id (vm, name, (u8) id_type, data,       /*remote */
414                                     0);
415           goto done;
416         }
417       else if (unformat (line_input, "set %U id remote %U 0x%U",
418                          unformat_ikev2_token, &name,
419                          unformat_ikev2_id_type, &id_type,
420                          unformat_hex_string, &data))
421         {
422           r = ikev2_set_profile_id (vm, name, (u8) id_type, data,       /*remote */
423                                     0);
424           goto done;
425         }
426       else if (unformat (line_input, "set %U id remote %U %v",
427                          unformat_ikev2_token, &name,
428                          unformat_ikev2_id_type, &id_type, &data))
429         {
430           r = ikev2_set_profile_id (vm, name, (u8) id_type, data,       /*remote */
431                                     0);
432           goto done;
433         }
434       else if (unformat (line_input, "set %U traffic-selector local "
435                          "ip-range %U - %U port-range %u - %u protocol %u",
436                          unformat_ikev2_token, &name,
437                          unformat_ip_address, &ip,
438                          unformat_ip_address, &end_addr, &tmp1, &tmp2, &tmp3))
439         {
440           r =
441             ikev2_set_profile_ts (vm, name, (u8) tmp3, (u16) tmp1, (u16) tmp2,
442                                   ip, end_addr, /*local */ 1);
443           goto done;
444         }
445       else if (unformat (line_input, "set %U traffic-selector remote "
446                          "ip-range %U - %U port-range %u - %u protocol %u",
447                          unformat_ikev2_token, &name,
448                          unformat_ip_address, &ip,
449                          unformat_ip_address, &end_addr, &tmp1, &tmp2, &tmp3))
450         {
451           r =
452             ikev2_set_profile_ts (vm, name, (u8) tmp3, (u16) tmp1, (u16) tmp2,
453                                   ip, end_addr, /*remote */ 0);
454           goto done;
455         }
456       else if (unformat (line_input, "set %U responder %U %U",
457                          unformat_ikev2_token, &name,
458                          unformat_vnet_sw_interface, vnm,
459                          &responder_sw_if_index, unformat_ip_address, &ip))
460         {
461           r =
462             ikev2_set_profile_responder (vm, name, responder_sw_if_index, ip);
463           goto done;
464         }
465       else if (unformat (line_input, "set %U responder %U %v",
466                          unformat_ikev2_token, &name,
467                          unformat_vnet_sw_interface, vnm,
468                          &responder_sw_if_index, &data))
469         {
470           r = ikev2_set_profile_responder_hostname (vm, name, data,
471                                                     responder_sw_if_index);
472           goto done;
473         }
474       else if (unformat (line_input, "set %U tunnel %U",
475                          unformat_ikev2_token, &name,
476                          unformat_vnet_sw_interface, vnm, &tun_sw_if_index))
477         {
478           r = ikev2_set_profile_tunnel_interface (vm, name, tun_sw_if_index);
479           goto done;
480         }
481       else
482         if (unformat
483             (line_input,
484              "set %U ike-crypto-alg %U %u ike-integ-alg %U ike-dh %U",
485              unformat_ikev2_token, &name,
486              unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
487              unformat_ikev2_transform_integ_type, &integ_alg,
488              unformat_ikev2_transform_dh_type, &dh_type))
489         {
490           r =
491             ikev2_set_profile_ike_transforms (vm, name, crypto_alg, integ_alg,
492                                               dh_type, tmp1);
493           goto done;
494         }
495       else
496         if (unformat
497             (line_input,
498              "set %U ike-crypto-alg %U %u ike-dh %U",
499              unformat_ikev2_token, &name,
500              unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
501              unformat_ikev2_transform_dh_type, &dh_type))
502         {
503           r =
504             ikev2_set_profile_ike_transforms (vm, name, crypto_alg,
505                                               IKEV2_TRANSFORM_INTEG_TYPE_NONE,
506                                               dh_type, tmp1);
507           goto done;
508         }
509       else
510         if (unformat
511             (line_input,
512              "set %U esp-crypto-alg %U %u esp-integ-alg %U",
513              unformat_ikev2_token, &name,
514              unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
515              unformat_ikev2_transform_integ_type, &integ_alg))
516         {
517           r =
518             ikev2_set_profile_esp_transforms (vm, name, crypto_alg, integ_alg,
519                                               tmp1);
520           goto done;
521         }
522       else if (unformat
523                (line_input,
524                 "set %U esp-crypto-alg %U %u",
525                 unformat_ikev2_token, &name,
526                 unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1))
527         {
528           r =
529             ikev2_set_profile_esp_transforms (vm, name, crypto_alg, 0, tmp1);
530           goto done;
531         }
532       else if (unformat (line_input, "set %U sa-lifetime %lu %u %u %lu",
533                          unformat_ikev2_token, &name,
534                          &tmp4, &tmp1, &tmp2, &tmp5))
535         {
536           r =
537             ikev2_set_profile_sa_lifetime (vm, name, tmp4, tmp1, tmp2, tmp5);
538           goto done;
539         }
540       else if (unformat (line_input, "set %U udp-encap",
541                          unformat_ikev2_token, &name))
542         {
543           r = ikev2_set_profile_udp_encap (vm, name);
544           goto done;
545         }
546       else if (unformat (line_input, "set %U ipsec-over-udp port %u",
547                          unformat_ikev2_token, &name, &tmp1))
548         {
549           int rv = ikev2_set_profile_ipsec_udp_port (vm, name, tmp1, 1);
550           if (rv)
551             r = clib_error_return (0, "Error: %U", format_vnet_api_errno, rv);
552           goto done;
553         }
554       else if (unformat (line_input, "set %U disable natt",
555                          unformat_ikev2_token, &name))
556         {
557           r = ikev2_profile_natt_disable (name);
558           goto done;
559         }
560       else
561         break;
562     }
563
564   r = clib_error_return (0, "parse error: '%U'",
565                          format_unformat_error, line_input);
566
567 done:
568   vec_free (name);
569   vec_free (data);
570   unformat_free (line_input);
571   return r;
572 }
573
574 /* *INDENT-OFF* */
575 VLIB_CLI_COMMAND (ikev2_profile_add_del_command, static) = {
576     .path = "ikev2 profile",
577     .short_help =
578     "ikev2 profile [add|del] <id>\n"
579     "ikev2 profile set <id> auth [rsa-sig|shared-key-mic] [cert-file|string|hex]"
580     " <data>\n"
581     "ikev2 profile set <id> id <local|remote> <type> <data>\n"
582     "ikev2 profile set <id> tunnel <interface>\n"
583     "ikev2 profile set <id> udp-encap\n"
584     "ikev2 profile set <id> traffic-selector <local|remote> ip-range "
585     "<start-addr> - <end-addr> port-range <start-port> - <end-port> "
586     "protocol <protocol-number>\n"
587     "ikev2 profile set <id> responder <interface> <addr>\n"
588     "ikev2 profile set <id> ike-crypto-alg <crypto alg> <key size> ike-integ-alg <integ alg> ike-dh <dh type>\n"
589     "ikev2 profile set <id> esp-crypto-alg <crypto alg> <key size> "
590       "[esp-integ-alg <integ alg>]\n"
591     "ikev2 profile set <id> sa-lifetime <seconds> <jitter> <handover> <max bytes>"
592     "ikev2 profile set <id> disable natt\n",
593     .function = ikev2_profile_add_del_command_fn,
594 };
595 /* *INDENT-ON* */
596
597 static clib_error_t *
598 show_ikev2_profile_command_fn (vlib_main_t * vm,
599                                unformat_input_t * input,
600                                vlib_cli_command_t * cmd)
601 {
602   ikev2_main_t *km = &ikev2_main;
603   ikev2_profile_t *p;
604
605   /* *INDENT-OFF* */
606   pool_foreach (p, km->profiles)  {
607     vlib_cli_output(vm, "profile %v", p->name);
608
609     if (p->auth.data)
610       {
611         if (p->auth.hex)
612           vlib_cli_output(vm, "  auth-method %U auth data 0x%U",
613                           format_ikev2_auth_method, p->auth.method,
614                           format_hex_bytes, p->auth.data, vec_len(p->auth.data));
615         else
616           vlib_cli_output(vm, "  auth-method %U auth data %v",
617                    format_ikev2_auth_method, p->auth.method, p->auth.data);
618       }
619
620     if (p->loc_id.data)
621       vlib_cli_output(vm, "  local %U", format_ikev2_id_type_and_data, &p->loc_id);
622
623     if (p->rem_id.data)
624       vlib_cli_output(vm, "  remote %U", format_ikev2_id_type_and_data, &p->rem_id);
625
626     if (!ip_address_is_zero (&p->loc_ts.start_addr))
627       vlib_cli_output(vm, "  local traffic-selector addr %U - %U port %u - %u"
628                       " protocol %u",
629                       format_ip_address, &p->loc_ts.start_addr,
630                       format_ip_address, &p->loc_ts.end_addr,
631                       p->loc_ts.start_port, p->loc_ts.end_port,
632                       p->loc_ts.protocol_id);
633
634     if (!ip_address_is_zero (&p->rem_ts.start_addr))
635       vlib_cli_output(vm, "  remote traffic-selector addr %U - %U port %u - %u"
636                       " protocol %u",
637                       format_ip_address, &p->rem_ts.start_addr,
638                       format_ip_address, &p->rem_ts.end_addr,
639                       p->rem_ts.start_port, p->rem_ts.end_port,
640                       p->rem_ts.protocol_id);
641     if (~0 != p->tun_itf)
642       vlib_cli_output(vm, "  protected tunnel %U",
643                       format_vnet_sw_if_index_name, vnet_get_main(), p->tun_itf);
644     if (~0 != p->responder.sw_if_index)
645       vlib_cli_output (vm, "  responder %U %U %v",
646                        format_vnet_sw_if_index_name, vnet_get_main (),
647                        p->responder.sw_if_index, format_ip_address,
648                        &p->responder.addr, p->responder.hostname);
649     if (p->udp_encap)
650       vlib_cli_output(vm, "  udp-encap");
651
652     if (p->natt_disabled)
653       vlib_cli_output(vm, "  NAT-T disabled");
654
655     if (p->ipsec_over_udp_port != IPSEC_UDP_PORT_NONE)
656       vlib_cli_output(vm, "  ipsec-over-udp port %d", p->ipsec_over_udp_port);
657
658     if (p->ike_ts.crypto_alg || p->ike_ts.integ_alg || p->ike_ts.dh_type || p->ike_ts.crypto_key_size)
659       vlib_cli_output(vm, "  ike-crypto-alg %U %u ike-integ-alg %U ike-dh %U",
660                     format_ikev2_transform_encr_type, p->ike_ts.crypto_alg, p->ike_ts.crypto_key_size,
661                     format_ikev2_transform_integ_type, p->ike_ts.integ_alg,
662                     format_ikev2_transform_dh_type, p->ike_ts.dh_type);
663
664     if (p->esp_ts.crypto_alg || p->esp_ts.integ_alg || p->esp_ts.dh_type)
665       vlib_cli_output(vm, "  esp-crypto-alg %U %u esp-integ-alg %U",
666                     format_ikev2_transform_encr_type, p->esp_ts.crypto_alg, p->esp_ts.crypto_key_size,
667                     format_ikev2_transform_integ_type, p->esp_ts.integ_alg);
668
669     vlib_cli_output(vm, "  lifetime %d jitter %d handover %d maxdata %d",
670                     p->lifetime, p->lifetime_jitter, p->handover, p->lifetime_maxdata);
671   }
672   /* *INDENT-ON* */
673
674   return 0;
675 }
676
677 /* *INDENT-OFF* */
678 VLIB_CLI_COMMAND (show_ikev2_profile_command, static) = {
679     .path = "show ikev2 profile",
680     .short_help = "show ikev2 profile",
681     .function = show_ikev2_profile_command_fn,
682 };
683 /* *INDENT-ON* */
684
685 static clib_error_t *
686 set_ikev2_liveness_period_fn (vlib_main_t * vm,
687                               unformat_input_t * input,
688                               vlib_cli_command_t * cmd)
689 {
690   unformat_input_t _line_input, *line_input = &_line_input;
691   clib_error_t *r = 0;
692   u32 period = 0, max_retries = 0;
693
694   if (!unformat_user (input, unformat_line_input, line_input))
695     return 0;
696
697   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
698     {
699       if (unformat (line_input, "%d %d", &period, &max_retries))
700         {
701           r = ikev2_set_liveness_params (period, max_retries);
702           goto done;
703         }
704       else
705         break;
706     }
707
708   r = clib_error_return (0, "parse error: '%U'",
709                          format_unformat_error, line_input);
710
711 done:
712   unformat_free (line_input);
713   return r;
714 }
715
716 /* *INDENT-OFF* */
717 VLIB_CLI_COMMAND (set_ikev2_liveness_command, static) = {
718   .path = "ikev2 set liveness",
719   .short_help = "ikev2 set liveness <period> <max-retires>",
720   .function = set_ikev2_liveness_period_fn,
721 };
722 /* *INDENT-ON* */
723
724 static clib_error_t *
725 set_ikev2_local_key_command_fn (vlib_main_t * vm,
726                                 unformat_input_t * input,
727                                 vlib_cli_command_t * cmd)
728 {
729   unformat_input_t _line_input, *line_input = &_line_input;
730   clib_error_t *r = 0;
731   u8 *data = 0;
732
733   if (!unformat_user (input, unformat_line_input, line_input))
734     return 0;
735
736   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
737     {
738       if (unformat (line_input, "%s", &data))
739         {
740           r = ikev2_set_local_key (vm, data);
741           goto done;
742         }
743       else
744         break;
745     }
746
747   r = clib_error_return (0, "parse error: '%U'",
748                          format_unformat_error, line_input);
749
750 done:
751   vec_free (data);
752   unformat_free (line_input);
753   return r;
754 }
755
756 /* *INDENT-OFF* */
757 VLIB_CLI_COMMAND (set_ikev2_local_key_command, static) = {
758     .path = "set ikev2 local key",
759     .short_help =
760     "set ikev2 local key <file>",
761     .function = set_ikev2_local_key_command_fn,
762 };
763 /* *INDENT-ON* */
764
765
766 static clib_error_t *
767 ikev2_initiate_command_fn (vlib_main_t * vm,
768                            unformat_input_t * input, vlib_cli_command_t * cmd)
769 {
770   unformat_input_t _line_input, *line_input = &_line_input;
771   clib_error_t *r = 0;
772   u8 *name = 0;
773   u32 tmp1;
774   u64 tmp2;
775
776   if (!unformat_user (input, unformat_line_input, line_input))
777     return 0;
778
779   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
780     {
781       if (unformat (line_input, "sa-init %U", unformat_ikev2_token, &name))
782         {
783           r = ikev2_initiate_sa_init (vm, name);
784           goto done;
785         }
786       else if (unformat (line_input, "del-child-sa %x", &tmp1))
787         {
788           r = ikev2_initiate_delete_child_sa (vm, tmp1);
789           goto done;
790         }
791       else if (unformat (line_input, "del-sa %lx", &tmp2))
792         {
793           r = ikev2_initiate_delete_ike_sa (vm, tmp2);
794           goto done;
795         }
796       else if (unformat (line_input, "rekey-child-sa %x", &tmp1))
797         {
798           r = ikev2_initiate_rekey_child_sa (vm, tmp1);
799           goto done;
800         }
801       else
802         break;
803     }
804
805   r = clib_error_return (0, "parse error: '%U'",
806                          format_unformat_error, line_input);
807
808 done:
809   vec_free (name);
810   unformat_free (line_input);
811   return r;
812 }
813
814 /* *INDENT-OFF* */
815 VLIB_CLI_COMMAND (ikev2_initiate_command, static) = {
816     .path = "ikev2 initiate",
817     .short_help =
818         "ikev2 initiate sa-init <profile id>\n"
819         "ikev2 initiate del-child-sa <child sa ispi>\n"
820         "ikev2 initiate del-sa <sa ispi>\n"
821         "ikev2 initiate rekey-child-sa <child sa ispi>\n",
822     .function = ikev2_initiate_command_fn,
823 };
824 /* *INDENT-ON* */
825
826 static clib_error_t *
827 ikev2_set_log_level_command_fn (vlib_main_t * vm,
828                                 unformat_input_t * input,
829                                 vlib_cli_command_t * cmd)
830 {
831   unformat_input_t _line_input, *line_input = &_line_input;
832   u32 log_level = IKEV2_LOG_NONE;
833   clib_error_t *error = 0;
834
835   /* Get a line of input. */
836   if (!unformat_user (input, unformat_line_input, line_input))
837     return 0;
838
839   if (!unformat (line_input, "%d", &log_level))
840     {
841       error = clib_error_return (0, "unknown input '%U'",
842                                  format_unformat_error, line_input);
843       goto done;
844     }
845   int rc = ikev2_set_log_level (log_level);
846   if (rc < 0)
847     error = clib_error_return (0, "setting log level failed!");
848
849 done:
850   unformat_free (line_input);
851   return error;
852 }
853
854 /* *INDENT-OFF* */
855 VLIB_CLI_COMMAND (ikev2_set_log_level_command, static) = {
856   .path = "ikev2 set logging level",
857   .function = ikev2_set_log_level_command_fn,
858   .short_help = "ikev2 set logging level <0-5>",
859 };
860 /* *INDENT-ON* */
861
862 /*
863  * fd.io coding-style-patch-verification: ON
864  *
865  * Local Variables:
866  * eval: (c-set-style "gnu")
867  * End:
868  */