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