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