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