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