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