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