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