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