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