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