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