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