ipsec: remove the set_key API
[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 void
350 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im)
351 {
352   u32 sai;
353
354   /* *INDENT-OFF* */
355   pool_foreach_index (sai, im->sad, ({
356     vlib_cli_output(vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
357   }));
358   /* *INDENT-ON* */
359 }
360
361 static void
362 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
363 {
364   u32 spdi;
365
366   /* *INDENT-OFF* */
367   pool_foreach_index (spdi, im->spds, ({
368     vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
369   }));
370   /* *INDENT-ON* */
371 }
372
373 static void
374 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
375 {
376   u32 spd_id, sw_if_index;
377
378   vlib_cli_output (vm, "SPD Bindings:");
379
380   /* *INDENT-OFF* */
381   hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
382     vlib_cli_output (vm, "  %d -> %U", spd_id,
383                      format_vnet_sw_if_index_name, im->vnet_main,
384                      sw_if_index);
385   }));
386   /* *INDENT-ON* */
387 }
388
389 static void
390 ipsec_tunnel_show_all (vlib_main_t * vm, ipsec_main_t * im)
391 {
392   u32 ti;
393
394   vlib_cli_output (vm, "Tunnel interfaces");
395   /* *INDENT-OFF* */
396   pool_foreach_index (ti, im->tunnel_interfaces, ({
397     vlib_cli_output(vm, "  %U", format_ipsec_tunnel, ti);
398   }));
399   /* *INDENT-ON* */
400 }
401
402 static clib_error_t *
403 show_ipsec_command_fn (vlib_main_t * vm,
404                        unformat_input_t * input, vlib_cli_command_t * cmd)
405 {
406   ipsec_main_t *im = &ipsec_main;
407
408   ipsec_sa_show_all (vm, im);
409   ipsec_spd_show_all (vm, im);
410   ipsec_spd_bindings_show_all (vm, im);
411   ipsec_tunnel_show_all (vm, im);
412
413   return 0;
414 }
415
416 /* *INDENT-OFF* */
417 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
418     .path = "show ipsec all",
419     .short_help = "show ipsec all",
420     .function = show_ipsec_command_fn,
421 };
422 /* *INDENT-ON* */
423
424 static clib_error_t *
425 show_ipsec_sa_command_fn (vlib_main_t * vm,
426                           unformat_input_t * input, vlib_cli_command_t * cmd)
427 {
428   ipsec_main_t *im = &ipsec_main;
429   u32 sai = ~0;
430
431   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
432     {
433       if (unformat (input, "%u", &sai))
434         ;
435       else
436         break;
437     }
438
439   if (~0 == sai)
440     ipsec_sa_show_all (vm, im);
441   else
442     vlib_cli_output (vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_DETAIL);
443
444   return 0;
445 }
446
447 /* *INDENT-OFF* */
448 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
449     .path = "show ipsec sa",
450     .short_help = "show ipsec sa [index]",
451     .function = show_ipsec_sa_command_fn,
452 };
453 /* *INDENT-ON* */
454
455 static clib_error_t *
456 show_ipsec_spd_command_fn (vlib_main_t * vm,
457                            unformat_input_t * input, vlib_cli_command_t * cmd)
458 {
459   ipsec_main_t *im = &ipsec_main;
460   u8 show_bindings = 0;
461   u32 spdi = ~0;
462
463   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
464     {
465       if (unformat (input, "%u", &spdi))
466         ;
467       else if (unformat (input, "bindings"))
468         show_bindings = 1;
469       else
470         break;
471     }
472
473   if (show_bindings)
474     ipsec_spd_bindings_show_all (vm, im);
475   else if (~0 != spdi)
476     vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
477   else
478     ipsec_spd_show_all (vm, im);
479
480   return 0;
481 }
482
483 /* *INDENT-OFF* */
484 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
485     .path = "show ipsec spd",
486     .short_help = "show ipsec spd [index]",
487     .function = show_ipsec_spd_command_fn,
488 };
489 /* *INDENT-ON* */
490
491 static clib_error_t *
492 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
493                               unformat_input_t * input,
494                               vlib_cli_command_t * cmd)
495 {
496   ipsec_main_t *im = &ipsec_main;
497   u32 ti = ~0;
498
499   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
500     {
501       if (unformat (input, "%u", &ti))
502         ;
503       else
504         break;
505     }
506
507   if (~0 != ti)
508     vlib_cli_output (vm, "%U", format_ipsec_tunnel, ti);
509   else
510     ipsec_tunnel_show_all (vm, im);
511
512   return 0;
513 }
514
515 /* *INDENT-OFF* */
516 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
517     .path = "show ipsec tunnel",
518     .short_help = "show ipsec tunnel [index]",
519     .function = show_ipsec_tunnel_command_fn,
520 };
521 /* *INDENT-ON* */
522
523 static clib_error_t *
524 ipsec_show_backends_command_fn (vlib_main_t * vm,
525                                 unformat_input_t * input,
526                                 vlib_cli_command_t * cmd)
527 {
528   ipsec_main_t *im = &ipsec_main;
529   u32 verbose = 0;
530
531   (void) unformat (input, "verbose %u", &verbose);
532
533   vlib_cli_output (vm, "IPsec AH backends available:");
534   u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
535   ipsec_ah_backend_t *ab;
536   /* *INDENT-OFF* */
537   pool_foreach (ab, im->ah_backends, {
538     s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
539                 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
540     if (verbose) {
541         vlib_node_t *n;
542         n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
543         s = format (s, "     enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
544         n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
545         s = format (s, "     dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
546         n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
547         s = format (s, "     enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
548         n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
549         s = format (s, "     dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
550     }
551   });
552   /* *INDENT-ON* */
553   vlib_cli_output (vm, "%v", s);
554   _vec_len (s) = 0;
555   vlib_cli_output (vm, "IPsec ESP backends available:");
556   s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
557   ipsec_esp_backend_t *eb;
558   /* *INDENT-OFF* */
559   pool_foreach (eb, im->esp_backends, {
560     s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
561                 eb - im->esp_backends == im->esp_current_backend ? "yes"
562                                                                  : "no");
563     if (verbose) {
564         vlib_node_t *n;
565         n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
566         s = format (s, "     enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
567         n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
568         s = format (s, "     dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
569         n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
570         s = format (s, "     enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
571         n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
572         s = format (s, "     dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
573     }
574   });
575   /* *INDENT-ON* */
576   vlib_cli_output (vm, "%v", s);
577
578   vec_free (s);
579   return 0;
580 }
581
582 /* *INDENT-OFF* */
583 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
584     .path = "show ipsec backends",
585     .short_help = "show ipsec backends",
586     .function = ipsec_show_backends_command_fn,
587 };
588 /* *INDENT-ON* */
589
590 static clib_error_t *
591 ipsec_select_backend_command_fn (vlib_main_t * vm,
592                                  unformat_input_t * input,
593                                  vlib_cli_command_t * cmd)
594 {
595   unformat_input_t _line_input, *line_input = &_line_input;
596   ipsec_main_t *im = &ipsec_main;
597   clib_error_t *error;
598   u32 backend_index;
599
600   error = ipsec_rsc_in_use (im);
601
602   if (error)
603     return error;
604
605   /* Get a line of input. */
606   if (!unformat_user (input, unformat_line_input, line_input))
607     return 0;
608
609   if (unformat (line_input, "ah"))
610     {
611       if (unformat (line_input, "%u", &backend_index))
612         {
613           if (ipsec_select_ah_backend (im, backend_index) < 0)
614             {
615               return clib_error_return (0, "Invalid AH backend index `%u'",
616                                         backend_index);
617             }
618         }
619       else
620         {
621           return clib_error_return (0, "Invalid backend index `%U'",
622                                     format_unformat_error, line_input);
623         }
624     }
625   else if (unformat (line_input, "esp"))
626     {
627       if (unformat (line_input, "%u", &backend_index))
628         {
629           if (ipsec_select_esp_backend (im, backend_index) < 0)
630             {
631               return clib_error_return (0, "Invalid ESP backend index `%u'",
632                                         backend_index);
633             }
634         }
635       else
636         {
637           return clib_error_return (0, "Invalid backend index `%U'",
638                                     format_unformat_error, line_input);
639         }
640     }
641   else
642     {
643       return clib_error_return (0, "Unknown input `%U'",
644                                 format_unformat_error, line_input);
645     }
646
647   return 0;
648 }
649
650 /* *INDENT-OFF* */
651 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
652     .path = "ipsec select backend",
653     .short_help = "ipsec select backend <ah|esp> <backend index>",
654     .function = ipsec_select_backend_command_fn,
655 };
656
657 /* *INDENT-ON* */
658
659 static clib_error_t *
660 clear_ipsec_counters_command_fn (vlib_main_t * vm,
661                                  unformat_input_t * input,
662                                  vlib_cli_command_t * cmd)
663 {
664   vlib_clear_combined_counters (&ipsec_spd_policy_counters);
665   vlib_clear_combined_counters (&ipsec_sa_counters);
666
667   return (NULL);
668 }
669
670 /* *INDENT-OFF* */
671 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
672     .path = "clear ipsec counters",
673     .short_help = "clear ipsec counters",
674     .function = clear_ipsec_counters_command_fn,
675 };
676 /* *INDENT-ON* */
677
678 static clib_error_t *
679 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
680                                 unformat_input_t * input,
681                                 vlib_cli_command_t * cmd)
682 {
683   unformat_input_t _line_input, *line_input = &_line_input;
684   ipsec_add_del_tunnel_args_t a;
685   int rv;
686   u32 num_m_args = 0;
687   u8 ipv4_set = 0;
688   u8 ipv6_set = 0;
689   clib_error_t *error = NULL;
690   ipsec_key_t rck = { 0 };
691   ipsec_key_t lck = { 0 };
692   ipsec_key_t lik = { 0 };
693   ipsec_key_t rik = { 0 };
694
695   clib_memset (&a, 0, sizeof (a));
696   a.is_add = 1;
697
698   /* Get a line of input. */
699   if (!unformat_user (input, unformat_line_input, line_input))
700     return 0;
701
702   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
703     {
704       if (unformat
705           (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
706            IP46_TYPE_ANY))
707         {
708           ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
709           num_m_args++;
710         }
711       else
712         if (unformat
713             (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
714              IP46_TYPE_ANY))
715         {
716           ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
717                                                                  1);
718           num_m_args++;
719         }
720       else if (unformat (line_input, "local-spi %u", &a.local_spi))
721         num_m_args++;
722       else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
723         num_m_args++;
724       else if (unformat (line_input, "instance %u", &a.show_instance))
725         a.renumber = 1;
726       else if (unformat (line_input, "salt 0x%x", &a.salt))
727         ;
728       else if (unformat (line_input, "udp-encap"))
729         a.udp_encap = 1;
730       else if (unformat (line_input, "use-esn"))
731         a.esn = 1;
732       else if (unformat (line_input, "use-anti-replay"))
733         a.anti_replay = 1;
734       else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
735         ;
736       else
737         if (unformat
738             (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
739         ;
740       else
741         if (unformat
742             (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
743         ;
744       else if (unformat (line_input, "crypto-alg %U",
745                          unformat_ipsec_crypto_alg, &a.crypto_alg))
746         ;
747       else
748         if (unformat
749             (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
750         ;
751       else
752         if (unformat
753             (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
754         ;
755       else if (unformat (line_input, "integ-alg %U",
756                          unformat_ipsec_integ_alg, &a.integ_alg))
757         ;
758       else if (unformat (line_input, "del"))
759         a.is_add = 0;
760       else
761         {
762           error = clib_error_return (0, "unknown input `%U'",
763                                      format_unformat_error, line_input);
764           goto done;
765         }
766     }
767
768   if (num_m_args < 4)
769     {
770       error = clib_error_return (0, "mandatory argument(s) missing");
771       goto done;
772     }
773
774   if (ipv4_set && ipv6_set)
775     return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
776
777   a.is_ip6 = ipv6_set;
778
779   clib_memcpy (a.local_crypto_key, lck.data, lck.len);
780   a.local_crypto_key_len = lck.len;
781   clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
782   a.remote_crypto_key_len = rck.len;
783
784   clib_memcpy (a.local_integ_key, lik.data, lik.len);
785   a.local_integ_key_len = lck.len;
786   clib_memcpy (a.remote_integ_key, rik.data, rik.len);
787   a.remote_integ_key_len = rck.len;
788
789   rv = ipsec_add_del_tunnel_if (&a);
790
791   switch (rv)
792     {
793     case 0:
794       break;
795     case VNET_API_ERROR_INVALID_VALUE:
796       if (a.is_add)
797         error = clib_error_return (0,
798                                    "IPSec tunnel interface already exists...");
799       else
800         error = clib_error_return (0, "IPSec tunnel interface not exists...");
801       goto done;
802     default:
803       error = clib_error_return (0, "ipsec_register_interface returned %d",
804                                  rv);
805       goto done;
806     }
807
808 done:
809   unformat_free (line_input);
810
811   return error;
812 }
813
814 /* *INDENT-OFF* */
815 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
816   .path = "create ipsec tunnel",
817   .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
818       "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
819       "[tx-table <table-id>]",
820   .function = create_ipsec_tunnel_command_fn,
821 };
822 /* *INDENT-ON* */
823
824 clib_error_t *
825 ipsec_cli_init (vlib_main_t * vm)
826 {
827   return 0;
828 }
829
830 VLIB_INIT_FUNCTION (ipsec_cli_init);
831
832
833 /*
834  * fd.io coding-style-patch-verification: ON
835  *
836  * Local Variables:
837  * eval: (c-set-style "gnu")
838  * End:
839  */