IPSEC: show CLI improvements
[vpp.git] / src / vnet / ipsec / ipsec_cli.c
1 /*
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>
22 #include <vnet/fib/fib.h>
23
24 #include <vnet/ipsec/ipsec.h>
25
26 static clib_error_t *
27 set_interface_spd_command_fn (vlib_main_t * vm,
28                               unformat_input_t * input,
29                               vlib_cli_command_t * cmd)
30 {
31   unformat_input_t _line_input, *line_input = &_line_input;
32   ipsec_main_t *im = &ipsec_main;
33   u32 sw_if_index = (u32) ~ 0;
34   u32 spd_id;
35   int is_add = 1;
36   clib_error_t *error = NULL;
37
38   if (!unformat_user (input, unformat_line_input, line_input))
39     return 0;
40
41   if (unformat
42       (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
43        &sw_if_index, &spd_id))
44     ;
45   else if (unformat (line_input, "del"))
46     is_add = 0;
47   else
48     {
49       error = clib_error_return (0, "parse error: '%U'",
50                                  format_unformat_error, line_input);
51       goto done;
52     }
53
54   ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
55
56 done:
57   unformat_free (line_input);
58
59   return error;
60 }
61
62 /* *INDENT-OFF* */
63 VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
64     .path = "set interface ipsec spd",
65     .short_help =
66     "set interface ipsec spd <int> <id>",
67     .function = set_interface_spd_command_fn,
68 };
69 /* *INDENT-ON* */
70
71 static clib_error_t *
72 ipsec_sa_add_del_command_fn (vlib_main_t * vm,
73                              unformat_input_t * input,
74                              vlib_cli_command_t * cmd)
75 {
76   unformat_input_t _line_input, *line_input = &_line_input;
77   ip46_address_t tun_src = { }, tun_dst =
78   {
79   };
80   ipsec_crypto_alg_t crypto_alg;
81   ipsec_integ_alg_t integ_alg;
82   ipsec_protocol_t proto;
83   ipsec_sa_flags_t flags;
84   clib_error_t *error;
85   ipsec_key_t ck = { 0 };
86   ipsec_key_t ik = { 0 };
87   int is_add, rv;
88   u32 id, spi;
89
90   error = NULL;
91   is_add = 0;
92   flags = IPSEC_SA_FLAG_NONE;
93   proto = IPSEC_PROTOCOL_ESP;
94
95   if (!unformat_user (input, unformat_line_input, line_input))
96     return 0;
97
98   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
99     {
100       if (unformat (line_input, "add %u", &id))
101         is_add = 1;
102       else if (unformat (line_input, "del %u", &id))
103         is_add = 0;
104       else if (unformat (line_input, "spi %u", &spi))
105         ;
106       else if (unformat (line_input, "esp"))
107         proto = IPSEC_PROTOCOL_ESP;
108       else if (unformat (line_input, "ah"))
109         proto = IPSEC_PROTOCOL_AH;
110       else if (unformat (line_input, "crypto-key %U",
111                          unformat_ipsec_key, &ck))
112         ;
113       else if (unformat (line_input, "crypto-alg %U",
114                          unformat_ipsec_crypto_alg, &crypto_alg))
115         ;
116       else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
117         ;
118       else if (unformat (line_input, "integ-alg %U",
119                          unformat_ipsec_integ_alg, &integ_alg))
120         ;
121       else if (unformat (line_input, "tunnel-src %U",
122                          unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
123         {
124           flags |= IPSEC_SA_FLAG_IS_TUNNEL;
125           if (!ip46_address_is_ip4 (&tun_src))
126             flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
127         }
128       else if (unformat (line_input, "tunnel-dst %U",
129                          unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
130         ;
131       else if (unformat (line_input, "udp-encap"))
132         flags |= IPSEC_SA_FLAG_UDP_ENCAP;
133       else
134         {
135           error = clib_error_return (0, "parse error: '%U'",
136                                      format_unformat_error, line_input);
137           goto done;
138         }
139     }
140
141   if (is_add)
142     rv = ipsec_sa_add (id, spi, proto, crypto_alg,
143                        &ck, integ_alg, &ik, flags,
144                        0, &tun_src, &tun_dst, NULL);
145   else
146     rv = ipsec_sa_del (id);
147
148   if (rv)
149     clib_error_return (0, "failed");
150
151 done:
152   unformat_free (line_input);
153
154   return error;
155 }
156
157 /* *INDENT-OFF* */
158 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
159     .path = "ipsec sa",
160     .short_help =
161     "ipsec sa [add|del]",
162     .function = ipsec_sa_add_del_command_fn,
163 };
164 /* *INDENT-ON* */
165
166 static clib_error_t *
167 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
168                               unformat_input_t * input,
169                               vlib_cli_command_t * cmd)
170 {
171   unformat_input_t _line_input, *line_input = &_line_input;
172   u32 spd_id = ~0;
173   int is_add = ~0;
174   clib_error_t *error = NULL;
175
176   if (!unformat_user (input, unformat_line_input, line_input))
177     return 0;
178
179   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
180     {
181       if (unformat (line_input, "add"))
182         is_add = 1;
183       else if (unformat (line_input, "del"))
184         is_add = 0;
185       else if (unformat (line_input, "%u", &spd_id))
186         ;
187       else
188         {
189           error = clib_error_return (0, "parse error: '%U'",
190                                      format_unformat_error, line_input);
191           goto done;
192         }
193     }
194
195   if (spd_id == ~0)
196     {
197       error = clib_error_return (0, "please specify SPD ID");
198       goto done;
199     }
200
201   ipsec_add_del_spd (vm, spd_id, is_add);
202
203 done:
204   unformat_free (line_input);
205
206   return error;
207 }
208
209 /* *INDENT-OFF* */
210 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
211     .path = "ipsec spd",
212     .short_help =
213     "ipsec spd [add|del] <id>",
214     .function = ipsec_spd_add_del_command_fn,
215 };
216 /* *INDENT-ON* */
217
218
219 static clib_error_t *
220 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
221                                  unformat_input_t * input,
222                                  vlib_cli_command_t * cmd)
223 {
224   unformat_input_t _line_input, *line_input = &_line_input;
225   ipsec_policy_t p;
226   int rv, is_add = 0;
227   u32 tmp, tmp2, stat_index;
228   clib_error_t *error = NULL;
229   u32 is_outbound;
230
231   clib_memset (&p, 0, sizeof (p));
232   p.lport.stop = p.rport.stop = ~0;
233   p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0;
234   p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0;
235   p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0;
236   is_outbound = 0;
237
238   if (!unformat_user (input, unformat_line_input, line_input))
239     return 0;
240
241   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
242     {
243       if (unformat (line_input, "add"))
244         is_add = 1;
245       else if (unformat (line_input, "del"))
246         is_add = 0;
247       else if (unformat (line_input, "spd %u", &p.id))
248         ;
249       else if (unformat (line_input, "inbound"))
250         is_outbound = 0;
251       else if (unformat (line_input, "outbound"))
252         is_outbound = 1;
253       else if (unformat (line_input, "priority %d", &p.priority))
254         ;
255       else if (unformat (line_input, "protocol %u", &tmp))
256         p.protocol = (u8) tmp;
257       else
258         if (unformat
259             (line_input, "action %U", unformat_ipsec_policy_action,
260              &p.policy))
261         {
262           if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
263             {
264               error = clib_error_return (0, "unsupported action: 'resolve'");
265               goto done;
266             }
267         }
268       else if (unformat (line_input, "sa %u", &p.sa_id))
269         ;
270       else if (unformat (line_input, "local-ip-range %U - %U",
271                          unformat_ip4_address, &p.laddr.start.ip4,
272                          unformat_ip4_address, &p.laddr.stop.ip4))
273         ;
274       else if (unformat (line_input, "remote-ip-range %U - %U",
275                          unformat_ip4_address, &p.raddr.start.ip4,
276                          unformat_ip4_address, &p.raddr.stop.ip4))
277         ;
278       else if (unformat (line_input, "local-ip-range %U - %U",
279                          unformat_ip6_address, &p.laddr.start.ip6,
280                          unformat_ip6_address, &p.laddr.stop.ip6))
281         {
282           p.is_ipv6 = 1;
283         }
284       else if (unformat (line_input, "remote-ip-range %U - %U",
285                          unformat_ip6_address, &p.raddr.start.ip6,
286                          unformat_ip6_address, &p.raddr.stop.ip6))
287         {
288           p.is_ipv6 = 1;
289         }
290       else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
291         {
292           p.lport.start = tmp;
293           p.lport.stop = tmp2;
294           p.lport.start = clib_host_to_net_u16 (p.lport.start);
295           p.lport.stop = clib_host_to_net_u16 (p.lport.stop);
296         }
297       else
298         if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
299         {
300           p.rport.start = tmp;
301           p.rport.stop = tmp2;
302           p.rport.start = clib_host_to_net_u16 (p.rport.start);
303           p.rport.stop = clib_host_to_net_u16 (p.rport.stop);
304         }
305       else
306         {
307           error = clib_error_return (0, "parse error: '%U'",
308                                      format_unformat_error, line_input);
309           goto done;
310         }
311     }
312
313   /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */
314   if (p.sa_id)
315     {
316       uword *p1;
317       ipsec_main_t *im = &ipsec_main;
318       ipsec_sa_t *sa = 0;
319       p1 = hash_get (im->sa_index_by_sa_id, p.sa_id);
320       if (!p1)
321         {
322           error =
323             clib_error_return (0, "SA with index %u not found", p.sa_id);
324           goto done;
325         }
326       sa = pool_elt_at_index (im->sad, p1[0]);
327       if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6)
328         {
329           error = clib_error_return (0, "AH not supported for IPV6: '%U'",
330                                      format_unformat_error, line_input);
331           goto done;
332         }
333     }
334
335   rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
336
337   if (rv)
338     {
339       error = clib_error_return (0, "unsupported policy type for:",
340                                  " outboud:%s %s action:%U",
341                                  (is_outbound ? "yes" : "no"),
342                                  (p.is_ipv6 ? "IPv4" : "IPv6"),
343                                  format_ipsec_policy_action, p.policy);
344       goto done;
345     }
346
347   rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
348
349   if (!rv)
350     vlib_cli_output (vm, "policy-index:%d", stat_index);
351   else
352     vlib_cli_output (vm, "error:%d", rv);
353
354 done:
355   unformat_free (line_input);
356
357   return error;
358 }
359
360 /* *INDENT-OFF* */
361 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
362     .path = "ipsec policy",
363     .short_help =
364     "ipsec policy [add|del] spd <id> priority <n> ",
365     .function = ipsec_policy_add_del_command_fn,
366 };
367 /* *INDENT-ON* */
368
369 static clib_error_t *
370 set_ipsec_sa_key_command_fn (vlib_main_t * vm,
371                              unformat_input_t * input,
372                              vlib_cli_command_t * cmd)
373 {
374   unformat_input_t _line_input, *line_input = &_line_input;
375   clib_error_t *error = NULL;
376   ipsec_key_t ck, ik;
377   u32 id;
378
379   if (!unformat_user (input, unformat_line_input, line_input))
380     return 0;
381
382   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
383     {
384       if (unformat (line_input, "%u", &id))
385         ;
386       else
387         if (unformat (line_input, "crypto-key %U", unformat_ipsec_key, &ck))
388         ;
389       else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
390         ;
391       else
392         {
393           error = clib_error_return (0, "parse error: '%U'",
394                                      format_unformat_error, line_input);
395           goto done;
396         }
397     }
398
399   ipsec_set_sa_key (id, &ck, &ik);
400
401 done:
402   unformat_free (line_input);
403
404   return error;
405 }
406
407 /* *INDENT-OFF* */
408 VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = {
409     .path = "set ipsec sa",
410     .short_help = "set ipsec sa <id> crypto-key <key> integ-key <key>",
411     .function = set_ipsec_sa_key_command_fn,
412 };
413 /* *INDENT-ON* */
414
415 static void
416 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im)
417 {
418   u32 sai;
419
420   /* *INDENT-OFF* */
421   pool_foreach_index (sai, im->sad, ({
422     vlib_cli_output(vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
423   }));
424   /* *INDENT-ON* */
425 }
426
427 static void
428 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
429 {
430   u32 spdi;
431
432   /* *INDENT-OFF* */
433   pool_foreach_index (spdi, im->spds, ({
434     vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
435   }));
436   /* *INDENT-ON* */
437 }
438
439 static void
440 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
441 {
442   u32 spd_id, sw_if_index;
443
444   vlib_cli_output (vm, "SPD Bindings:");
445
446   /* *INDENT-OFF* */
447   hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
448     vlib_cli_output (vm, "  %d -> %U", spd_id,
449                      format_vnet_sw_if_index_name, im->vnet_main,
450                      sw_if_index);
451   }));
452   /* *INDENT-ON* */
453 }
454
455 static void
456 ipsec_tunnel_show_all (vlib_main_t * vm, ipsec_main_t * im)
457 {
458   u32 ti;
459
460   vlib_cli_output (vm, "Tunnel interfaces");
461   /* *INDENT-OFF* */
462   pool_foreach_index (ti, im->tunnel_interfaces, ({
463     vlib_cli_output(vm, "  %U", format_ipsec_tunnel, ti);
464   }));
465   /* *INDENT-ON* */
466 }
467
468 static clib_error_t *
469 show_ipsec_command_fn (vlib_main_t * vm,
470                        unformat_input_t * input, vlib_cli_command_t * cmd)
471 {
472   ipsec_main_t *im = &ipsec_main;
473
474   ipsec_sa_show_all (vm, im);
475   ipsec_spd_show_all (vm, im);
476   ipsec_spd_bindings_show_all (vm, im);
477   ipsec_tunnel_show_all (vm, im);
478
479   return 0;
480 }
481
482 /* *INDENT-OFF* */
483 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
484     .path = "show ipsec all",
485     .short_help = "show ipsec all",
486     .function = show_ipsec_command_fn,
487 };
488 /* *INDENT-ON* */
489
490 static clib_error_t *
491 show_ipsec_sa_command_fn (vlib_main_t * vm,
492                           unformat_input_t * input, vlib_cli_command_t * cmd)
493 {
494   ipsec_main_t *im = &ipsec_main;
495   u32 sai = ~0;
496
497   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
498     {
499       if (unformat (input, "%u", &sai))
500         ;
501       else
502         break;
503     }
504
505   if (~0 == sai)
506     ipsec_sa_show_all (vm, im);
507   else
508     vlib_cli_output (vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_DETAIL);
509
510   return 0;
511 }
512
513 /* *INDENT-OFF* */
514 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
515     .path = "show ipsec sa",
516     .short_help = "show ipsec sa [index]",
517     .function = show_ipsec_sa_command_fn,
518 };
519 /* *INDENT-ON* */
520
521 static clib_error_t *
522 show_ipsec_spd_command_fn (vlib_main_t * vm,
523                            unformat_input_t * input, vlib_cli_command_t * cmd)
524 {
525   ipsec_main_t *im = &ipsec_main;
526   u8 show_bindings = 0;
527   u32 spdi = ~0;
528
529   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
530     {
531       if (unformat (input, "%u", &spdi))
532         ;
533       else if (unformat (input, "bindings"))
534         show_bindings = 1;
535       else
536         break;
537     }
538
539   if (show_bindings)
540     ipsec_spd_bindings_show_all (vm, im);
541   else if (~0 != spdi)
542     vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
543   else
544     ipsec_spd_show_all (vm, im);
545
546   return 0;
547 }
548
549 /* *INDENT-OFF* */
550 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
551     .path = "show ipsec spd",
552     .short_help = "show ipsec spd [index]",
553     .function = show_ipsec_spd_command_fn,
554 };
555 /* *INDENT-ON* */
556
557 static clib_error_t *
558 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
559                               unformat_input_t * input,
560                               vlib_cli_command_t * cmd)
561 {
562   ipsec_main_t *im = &ipsec_main;
563   u32 ti = ~0;
564
565   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
566     {
567       if (unformat (input, "%u", &ti))
568         ;
569       else
570         break;
571     }
572
573   if (~0 != ti)
574     vlib_cli_output (vm, "%U", format_ipsec_tunnel, ti);
575   else
576     ipsec_tunnel_show_all (vm, im);
577
578   return 0;
579 }
580
581 /* *INDENT-OFF* */
582 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
583     .path = "show ipsec tunnel",
584     .short_help = "show ipsec tunnel [index]",
585     .function = show_ipsec_tunnel_command_fn,
586 };
587 /* *INDENT-ON* */
588
589 static clib_error_t *
590 ipsec_show_backends_command_fn (vlib_main_t * vm,
591                                 unformat_input_t * input,
592                                 vlib_cli_command_t * cmd)
593 {
594   ipsec_main_t *im = &ipsec_main;
595   u32 verbose = 0;
596
597   (void) unformat (input, "verbose %u", &verbose);
598
599   vlib_cli_output (vm, "IPsec AH backends available:");
600   u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
601   ipsec_ah_backend_t *ab;
602   /* *INDENT-OFF* */
603   pool_foreach (ab, im->ah_backends, {
604     s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
605                 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
606     if (verbose) {
607         vlib_node_t *n;
608         n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
609         s = format (s, "     enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
610         n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
611         s = format (s, "     dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
612         n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
613         s = format (s, "     enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
614         n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
615         s = format (s, "     dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
616     }
617   });
618   /* *INDENT-ON* */
619   vlib_cli_output (vm, "%v", s);
620   _vec_len (s) = 0;
621   vlib_cli_output (vm, "IPsec ESP backends available:");
622   s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
623   ipsec_esp_backend_t *eb;
624   /* *INDENT-OFF* */
625   pool_foreach (eb, im->esp_backends, {
626     s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
627                 eb - im->esp_backends == im->esp_current_backend ? "yes"
628                                                                  : "no");
629     if (verbose) {
630         vlib_node_t *n;
631         n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
632         s = format (s, "     enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
633         n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
634         s = format (s, "     dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
635         n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
636         s = format (s, "     enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
637         n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
638         s = format (s, "     dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
639     }
640   });
641   /* *INDENT-ON* */
642   vlib_cli_output (vm, "%v", s);
643
644   vec_free (s);
645   return 0;
646 }
647
648 /* *INDENT-OFF* */
649 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
650     .path = "show ipsec backends",
651     .short_help = "show ipsec backends",
652     .function = ipsec_show_backends_command_fn,
653 };
654 /* *INDENT-ON* */
655
656 static clib_error_t *
657 ipsec_select_backend_command_fn (vlib_main_t * vm,
658                                  unformat_input_t * input,
659                                  vlib_cli_command_t * cmd)
660 {
661   u32 backend_index;
662   ipsec_main_t *im = &ipsec_main;
663
664   if (pool_elts (im->sad) > 0)
665     {
666       return clib_error_return (0,
667                                 "Cannot change IPsec backend, while %u SA entries are configured",
668                                 pool_elts (im->sad));
669     }
670
671   unformat_input_t _line_input, *line_input = &_line_input;
672   /* Get a line of input. */
673   if (!unformat_user (input, unformat_line_input, line_input))
674     return 0;
675
676   if (unformat (line_input, "ah"))
677     {
678       if (unformat (line_input, "%u", &backend_index))
679         {
680           if (ipsec_select_ah_backend (im, backend_index) < 0)
681             {
682               return clib_error_return (0, "Invalid AH backend index `%u'",
683                                         backend_index);
684             }
685         }
686       else
687         {
688           return clib_error_return (0, "Invalid backend index `%U'",
689                                     format_unformat_error, line_input);
690         }
691     }
692   else if (unformat (line_input, "esp"))
693     {
694       if (unformat (line_input, "%u", &backend_index))
695         {
696           if (ipsec_select_esp_backend (im, backend_index) < 0)
697             {
698               return clib_error_return (0, "Invalid ESP backend index `%u'",
699                                         backend_index);
700             }
701         }
702       else
703         {
704           return clib_error_return (0, "Invalid backend index `%U'",
705                                     format_unformat_error, line_input);
706         }
707     }
708   else
709     {
710       return clib_error_return (0, "Unknown input `%U'",
711                                 format_unformat_error, line_input);
712     }
713
714   return 0;
715 }
716
717 /* *INDENT-OFF* */
718 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
719     .path = "ipsec select backend",
720     .short_help = "ipsec select backend <ah|esp> <backend index>",
721     .function = ipsec_select_backend_command_fn,
722 };
723
724 /* *INDENT-ON* */
725
726 static clib_error_t *
727 clear_ipsec_counters_command_fn (vlib_main_t * vm,
728                                  unformat_input_t * input,
729                                  vlib_cli_command_t * cmd)
730 {
731   vlib_clear_combined_counters (&ipsec_spd_policy_counters);
732   vlib_clear_combined_counters (&ipsec_sa_counters);
733
734   return (NULL);
735 }
736
737 /* *INDENT-OFF* */
738 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
739     .path = "clear ipsec counters",
740     .short_help = "clear ipsec counters",
741     .function = clear_ipsec_counters_command_fn,
742 };
743 /* *INDENT-ON* */
744
745 static clib_error_t *
746 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
747                                 unformat_input_t * input,
748                                 vlib_cli_command_t * cmd)
749 {
750   unformat_input_t _line_input, *line_input = &_line_input;
751   ipsec_add_del_tunnel_args_t a;
752   int rv;
753   u32 num_m_args = 0;
754   u8 ipv4_set = 0;
755   u8 ipv6_set = 0;
756   clib_error_t *error = NULL;
757   ipsec_key_t rck = { 0 };
758   ipsec_key_t lck = { 0 };
759   ipsec_key_t lik = { 0 };
760   ipsec_key_t rik = { 0 };
761
762   clib_memset (&a, 0, sizeof (a));
763   a.is_add = 1;
764
765   /* Get a line of input. */
766   if (!unformat_user (input, unformat_line_input, line_input))
767     return 0;
768
769   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
770     {
771       if (unformat
772           (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
773            IP46_TYPE_ANY))
774         {
775           ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
776           num_m_args++;
777         }
778       else
779         if (unformat
780             (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
781              IP46_TYPE_ANY))
782         {
783           ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
784                                                                  1);
785           num_m_args++;
786         }
787       else if (unformat (line_input, "local-spi %u", &a.local_spi))
788         num_m_args++;
789       else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
790         num_m_args++;
791       else if (unformat (line_input, "instance %u", &a.show_instance))
792         a.renumber = 1;
793       else if (unformat (line_input, "udp-encap"))
794         a.udp_encap = 1;
795       else if (unformat (line_input, "use-esn"))
796         a.esn = 1;
797       else if (unformat (line_input, "use-anti-replay"))
798         a.anti_replay = 1;
799       else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
800         ;
801       else
802         if (unformat
803             (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
804         ;
805       else
806         if (unformat
807             (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
808         ;
809       else if (unformat (line_input, "crypto-alg %U",
810                          unformat_ipsec_crypto_alg, &a.crypto_alg))
811         ;
812       else
813         if (unformat
814             (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
815         ;
816       else
817         if (unformat
818             (line_input, "rmote-integ-key %U", unformat_ipsec_key, &rik))
819         ;
820       else if (unformat (line_input, "integ-alg %U",
821                          unformat_ipsec_integ_alg, &a.integ_alg))
822         ;
823       else if (unformat (line_input, "del"))
824         a.is_add = 0;
825       else
826         {
827           error = clib_error_return (0, "unknown input `%U'",
828                                      format_unformat_error, line_input);
829           goto done;
830         }
831     }
832
833   if (num_m_args < 4)
834     {
835       error = clib_error_return (0, "mandatory argument(s) missing");
836       goto done;
837     }
838
839   if (ipv4_set && ipv6_set)
840     return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
841
842   a.is_ip6 = ipv6_set;
843
844   clib_memcpy (a.local_crypto_key, lck.data, lck.len);
845   a.local_crypto_key_len = lck.len;
846   clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
847   a.remote_crypto_key_len = rck.len;
848
849   clib_memcpy (a.local_integ_key, lik.data, lik.len);
850   a.local_integ_key_len = lck.len;
851   clib_memcpy (a.remote_integ_key, rik.data, rik.len);
852   a.remote_integ_key_len = rck.len;
853
854   rv = ipsec_add_del_tunnel_if (&a);
855
856   switch (rv)
857     {
858     case 0:
859       break;
860     case VNET_API_ERROR_INVALID_VALUE:
861       if (a.is_add)
862         error = clib_error_return (0,
863                                    "IPSec tunnel interface already exists...");
864       else
865         error = clib_error_return (0, "IPSec tunnel interface not exists...");
866       goto done;
867     default:
868       error = clib_error_return (0, "ipsec_register_interface returned %d",
869                                  rv);
870       goto done;
871     }
872
873 done:
874   unformat_free (line_input);
875
876   return error;
877 }
878
879 /* *INDENT-OFF* */
880 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
881   .path = "create ipsec tunnel",
882   .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
883       "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
884       "[tx-table <table-id>]",
885   .function = create_ipsec_tunnel_command_fn,
886 };
887 /* *INDENT-ON* */
888
889 static clib_error_t *
890 set_interface_key_command_fn (vlib_main_t * vm,
891                               unformat_input_t * input,
892                               vlib_cli_command_t * cmd)
893 {
894   unformat_input_t _line_input, *line_input = &_line_input;
895   ipsec_main_t *im = &ipsec_main;
896   ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
897   u32 hw_if_index = (u32) ~ 0;
898   u32 alg;
899   u8 *key = 0;
900   clib_error_t *error = NULL;
901
902   if (!unformat_user (input, unformat_line_input, line_input))
903     return 0;
904
905   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
906     {
907       if (unformat (line_input, "%U",
908                     unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
909         ;
910       else
911         if (unformat
912             (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
913         type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
914       else
915         if (unformat
916             (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
917         type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
918       else
919         if (unformat
920             (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
921         type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
922       else
923         if (unformat
924             (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
925         type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
926       else if (unformat (line_input, "%U", unformat_hex_string, &key))
927         ;
928       else
929         {
930           error = clib_error_return (0, "parse error: '%U'",
931                                      format_unformat_error, line_input);
932           goto done;
933         }
934     }
935
936   if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
937     {
938       error = clib_error_return (0, "unknown key type");
939       goto done;
940     }
941
942   if (alg > 0 && vec_len (key) == 0)
943     {
944       error = clib_error_return (0, "key is not specified");
945       goto done;
946     }
947
948   if (hw_if_index == (u32) ~ 0)
949     {
950       error = clib_error_return (0, "interface not specified");
951       goto done;
952     }
953
954   ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
955
956 done:
957   vec_free (key);
958   unformat_free (line_input);
959
960   return error;
961 }
962
963 /* *INDENT-OFF* */
964 VLIB_CLI_COMMAND (set_interface_key_command, static) = {
965     .path = "set interface ipsec key",
966     .short_help =
967     "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
968     .function = set_interface_key_command_fn,
969 };
970 /* *INDENT-ON* */
971
972 clib_error_t *
973 ipsec_cli_init (vlib_main_t * vm)
974 {
975   return 0;
976 }
977
978 VLIB_INIT_FUNCTION (ipsec_cli_init);
979
980
981 /*
982  * fd.io coding-style-patch-verification: ON
983  *
984  * Local Variables:
985  * eval: (c-set-style "gnu")
986  * End:
987  */