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