VPP-256 - Coding style cleanup vnet/vnet/ipsec
[vpp.git] / vnet / 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
23 #include <vnet/ipsec/ipsec.h>
24
25 static clib_error_t *
26 set_interface_spd_command_fn (vlib_main_t * vm,
27                               unformat_input_t * input,
28                               vlib_cli_command_t * cmd)
29 {
30   unformat_input_t _line_input, *line_input = &_line_input;
31   ipsec_main_t *im = &ipsec_main;
32   u32 sw_if_index = (u32) ~ 0;
33   u32 spd_id;
34   int is_add = 1;
35
36   if (!unformat_user (input, unformat_line_input, line_input))
37     return 0;
38
39   if (unformat
40       (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
41        &sw_if_index, &spd_id))
42     ;
43   else if (unformat (line_input, "del"))
44     is_add = 0;
45   else
46     return clib_error_return (0, "parse error: '%U'",
47                               format_unformat_error, line_input);
48
49   unformat_free (line_input);
50
51   ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
52
53   return 0;
54 }
55
56 /* *INDENT-OFF* */
57 VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
58     .path = "set interface ipsec spd",
59     .short_help =
60     "set interface ipsec spd <int> <id>",
61     .function = set_interface_spd_command_fn,
62 };
63 /* *INDENT-ON* */
64
65 static clib_error_t *
66 ipsec_sa_add_del_command_fn (vlib_main_t * vm,
67                              unformat_input_t * input,
68                              vlib_cli_command_t * cmd)
69 {
70   unformat_input_t _line_input, *line_input = &_line_input;
71   ipsec_sa_t sa;
72   int is_add = ~0;
73   u8 *ck = 0, *ik = 0;
74
75   memset (&sa, 0, sizeof (sa));
76
77   if (!unformat_user (input, unformat_line_input, line_input))
78     return 0;
79
80   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
81     {
82       if (unformat (line_input, "add %u", &sa.id))
83         is_add = 1;
84       else if (unformat (line_input, "del %u", &sa.id))
85         is_add = 0;
86       else if (unformat (line_input, "spi %u", &sa.spi))
87         ;
88       else if (unformat (line_input, "esp"))
89         sa.protocol = IPSEC_PROTOCOL_ESP;
90       else if (unformat (line_input, "ah"))
91         //sa.protocol = IPSEC_PROTOCOL_AH;
92         return clib_error_return (0, "unsupported security protocol 'AH'");
93       else
94         if (unformat (line_input, "crypto-key %U", unformat_hex_string, &ck))
95         sa.crypto_key_len = vec_len (ck);
96       else
97         if (unformat
98             (line_input, "crypto-alg %U", unformat_ipsec_crypto_alg,
99              &sa.crypto_alg))
100         {
101           if (sa.crypto_alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
102               sa.crypto_alg > IPSEC_CRYPTO_ALG_AES_CBC_256)
103             return clib_error_return (0, "unsupported crypto-alg: '%U'",
104                                       format_ipsec_crypto_alg, sa.crypto_alg);
105         }
106       else
107         if (unformat (line_input, "integ-key %U", unformat_hex_string, &ik))
108         sa.integ_key_len = vec_len (ik);
109       else if (unformat (line_input, "integ-alg %U", unformat_ipsec_integ_alg,
110                          &sa.integ_alg))
111         {
112           if (sa.integ_alg < IPSEC_INTEG_ALG_SHA1_96 ||
113               sa.integ_alg > IPSEC_INTEG_ALG_SHA_512_256)
114             return clib_error_return (0, "unsupported integ-alg: '%U'",
115                                       format_ipsec_integ_alg, sa.integ_alg);
116         }
117       else if (unformat (line_input, "tunnel-src %U",
118                          unformat_ip4_address, &sa.tunnel_src_addr.ip4))
119         sa.is_tunnel = 1;
120       else if (unformat (line_input, "tunnel-dst %U",
121                          unformat_ip4_address, &sa.tunnel_dst_addr.ip4))
122         sa.is_tunnel = 1;
123       else if (unformat (line_input, "tunnel-src %U",
124                          unformat_ip6_address, &sa.tunnel_src_addr.ip6))
125         {
126           sa.is_tunnel = 1;
127           sa.is_tunnel_ip6 = 1;
128         }
129       else if (unformat (line_input, "tunnel-dst %U",
130                          unformat_ip6_address, &sa.tunnel_dst_addr.ip6))
131         {
132           sa.is_tunnel = 1;
133           sa.is_tunnel_ip6 = 1;
134         }
135       else
136         return clib_error_return (0, "parse error: '%U'",
137                                   format_unformat_error, line_input);
138     }
139
140   unformat_free (line_input);
141
142   if (sa.crypto_key_len > sizeof (sa.crypto_key))
143     sa.crypto_key_len = sizeof (sa.crypto_key);
144
145   if (sa.integ_key_len > sizeof (sa.integ_key))
146     sa.integ_key_len = sizeof (sa.integ_key);
147
148   if (ck)
149     strncpy ((char *) sa.crypto_key, (char *) ck, sa.crypto_key_len);
150
151   if (ik)
152     strncpy ((char *) sa.integ_key, (char *) ik, sa.integ_key_len);
153
154   ipsec_add_del_sa (vm, &sa, is_add);
155
156   return 0;
157 }
158
159 /* *INDENT-OFF* */
160 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
161     .path = "ipsec sa",
162     .short_help =
163     "ipsec sa [add|del]",
164     .function = ipsec_sa_add_del_command_fn,
165 };
166 /* *INDENT-ON* */
167
168 static clib_error_t *
169 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
170                               unformat_input_t * input,
171                               vlib_cli_command_t * cmd)
172 {
173   unformat_input_t _line_input, *line_input = &_line_input;
174   u32 spd_id;
175   int is_add = ~0;
176
177   if (!unformat_user (input, unformat_line_input, line_input))
178     return 0;
179
180   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
181     {
182       if (unformat (line_input, "add"))
183         is_add = 1;
184       else if (unformat (line_input, "del"))
185         is_add = 0;
186       else if (unformat (line_input, "%u", &spd_id))
187         ;
188       else
189         return clib_error_return (0, "parse error: '%U'",
190                                   format_unformat_error, line_input);
191     }
192
193   unformat_free (line_input);
194
195   ipsec_add_del_spd (vm, spd_id, is_add);
196
197   return 0;
198 }
199
200 /* *INDENT-OFF* */
201 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
202     .path = "ipsec spd",
203     .short_help =
204     "ipsec spd [add|del] <id>",
205     .function = ipsec_spd_add_del_command_fn,
206 };
207 /* *INDENT-ON* */
208
209
210 static clib_error_t *
211 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
212                                  unformat_input_t * input,
213                                  vlib_cli_command_t * cmd)
214 {
215   unformat_input_t _line_input, *line_input = &_line_input;
216   ipsec_policy_t p;
217   int is_add = 0;
218   int is_ip_any = 1;
219   u32 tmp, tmp2;
220
221   memset (&p, 0, sizeof (p));
222   p.lport.stop = p.rport.stop = ~0;
223   p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0;
224   p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0;
225   p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0;
226
227   if (!unformat_user (input, unformat_line_input, line_input))
228     return 0;
229
230   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
231     {
232       if (unformat (line_input, "add"))
233         is_add = 1;
234       else if (unformat (line_input, "del"))
235         is_add = 0;
236       else if (unformat (line_input, "spd %u", &p.id))
237         ;
238       else if (unformat (line_input, "inbound"))
239         p.is_outbound = 0;
240       else if (unformat (line_input, "outbound"))
241         p.is_outbound = 1;
242       else if (unformat (line_input, "priority %d", &p.priority))
243         ;
244       else if (unformat (line_input, "protocol %u", &tmp))
245         p.protocol = (u8) tmp;
246       else
247         if (unformat
248             (line_input, "action %U", unformat_ipsec_policy_action,
249              &p.policy))
250         {
251           if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
252             return clib_error_return (0, "unsupported action: 'resolve'");
253         }
254       else if (unformat (line_input, "sa %u", &p.sa_id))
255         ;
256       else if (unformat (line_input, "local-ip-range %U - %U",
257                          unformat_ip4_address, &p.laddr.start.ip4,
258                          unformat_ip4_address, &p.laddr.stop.ip4))
259         is_ip_any = 0;
260       else if (unformat (line_input, "remote-ip-range %U - %U",
261                          unformat_ip4_address, &p.raddr.start.ip4,
262                          unformat_ip4_address, &p.raddr.stop.ip4))
263         is_ip_any = 0;
264       else if (unformat (line_input, "local-ip-range %U - %U",
265                          unformat_ip6_address, &p.laddr.start.ip6,
266                          unformat_ip6_address, &p.laddr.stop.ip6))
267         {
268           p.is_ipv6 = 1;
269           is_ip_any = 0;
270         }
271       else if (unformat (line_input, "remote-ip-range %U - %U",
272                          unformat_ip6_address, &p.raddr.start.ip6,
273                          unformat_ip6_address, &p.raddr.stop.ip6))
274         {
275           p.is_ipv6 = 1;
276           is_ip_any = 0;
277         }
278       else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
279         {
280           p.lport.start = tmp;
281           p.lport.stop = tmp2;
282         }
283       else
284         if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
285         {
286           p.rport.start = tmp;
287           p.rport.stop = tmp2;
288         }
289       else
290         return clib_error_return (0, "parse error: '%U'",
291                                   format_unformat_error, line_input);
292     }
293
294   unformat_free (line_input);
295
296   ipsec_add_del_policy (vm, &p, is_add);
297   if (is_ip_any)
298     {
299       p.is_ipv6 = 1;
300       ipsec_add_del_policy (vm, &p, is_add);
301     }
302   return 0;
303 }
304
305 /* *INDENT-OFF* */
306 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
307     .path = "ipsec policy",
308     .short_help =
309     "ipsec policy [add|del] spd <id> priority <n> ",
310     .function = ipsec_policy_add_del_command_fn,
311 };
312 /* *INDENT-ON* */
313
314 static clib_error_t *
315 set_ipsec_sa_key_command_fn (vlib_main_t * vm,
316                              unformat_input_t * input,
317                              vlib_cli_command_t * cmd)
318 {
319   unformat_input_t _line_input, *line_input = &_line_input;
320   ipsec_sa_t sa;
321   u8 *ck = 0, *ik = 0;
322
323   memset (&sa, 0, sizeof (sa));
324
325   if (!unformat_user (input, unformat_line_input, line_input))
326     return 0;
327
328   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
329     {
330       if (unformat (line_input, "%u", &sa.id))
331         ;
332       else
333         if (unformat (line_input, "crypto-key %U", unformat_hex_string, &ck))
334         sa.crypto_key_len = vec_len (ck);
335       else
336         if (unformat (line_input, "integ-key %U", unformat_hex_string, &ik))
337         sa.integ_key_len = vec_len (ik);
338       else
339         return clib_error_return (0, "parse error: '%U'",
340                                   format_unformat_error, line_input);
341     }
342
343   unformat_free (line_input);
344
345   if (sa.crypto_key_len > sizeof (sa.crypto_key))
346     sa.crypto_key_len = sizeof (sa.crypto_key);
347
348   if (sa.integ_key_len > sizeof (sa.integ_key))
349     sa.integ_key_len = sizeof (sa.integ_key);
350
351   if (ck)
352     strncpy ((char *) sa.crypto_key, (char *) ck, sa.crypto_key_len);
353
354   if (ik)
355     strncpy ((char *) sa.integ_key, (char *) ik, sa.integ_key_len);
356
357   ipsec_set_sa_key (vm, &sa);
358
359   return 0;
360 }
361
362 /* *INDENT-OFF* */
363 VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = {
364     .path = "set ipsec sa",
365     .short_help =
366     "set ipsec sa <id> crypto-key <key> integ-key <key>",
367     .function = set_ipsec_sa_key_command_fn,
368 };
369 /* *INDENT-ON* */
370
371 static clib_error_t *
372 show_ipsec_command_fn (vlib_main_t * vm,
373                        unformat_input_t * input, vlib_cli_command_t * cmd)
374 {
375   ipsec_spd_t *spd;
376   ipsec_sa_t *sa;
377   ipsec_policy_t *p;
378   ipsec_main_t *im = &ipsec_main;
379   u32 *i;
380   ipsec_tunnel_if_t *t;
381   vnet_hw_interface_t *hi;
382
383   /* *INDENT-OFF* */
384   pool_foreach (sa, im->sad, ({
385     if (sa->id) {
386       vlib_cli_output(vm, "sa %u spi %u mode %s protocol %s", sa->id, sa->spi,
387                       sa->is_tunnel ? "tunnel" : "transport",
388                       sa->protocol ? "esp" : "ah");
389       if (sa->protocol == IPSEC_PROTOCOL_ESP) {
390         vlib_cli_output(vm, "  crypto alg %U%s%U integrity alg %U%s%U",
391                         format_ipsec_crypto_alg, sa->crypto_alg,
392                         sa->crypto_alg ? " key " : "",
393                         format_hex_bytes, sa->crypto_key, sa->crypto_key_len,
394                         format_ipsec_integ_alg, sa->integ_alg,
395                         sa->integ_alg ? " key " : "",
396                         format_hex_bytes, sa->integ_key, sa->integ_key_len);
397       }
398       if (sa->is_tunnel && sa->is_tunnel_ip6) {
399         vlib_cli_output(vm, "  tunnel src %U dst %U",
400                         format_ip6_address, &sa->tunnel_src_addr.ip6,
401                         format_ip6_address, &sa->tunnel_dst_addr.ip6);
402       } else if (sa->is_tunnel) {
403         vlib_cli_output(vm, "  tunnel src %U dst %U",
404                         format_ip4_address, &sa->tunnel_src_addr.ip4,
405                         format_ip4_address, &sa->tunnel_dst_addr.ip4);
406       }
407     }
408   }));
409   /* *INDENT-ON* */
410
411   /* *INDENT-OFF* */
412   pool_foreach (spd, im->spds, ({
413     vlib_cli_output(vm, "spd %u", spd->id);
414
415     vlib_cli_output(vm, " outbound policies");
416     vec_foreach(i, spd->ipv4_outbound_policies)
417       {
418         p = pool_elt_at_index(spd->policies, *i);
419         vlib_cli_output(vm, "  priority %d action %U protocol %s%s",
420                         p->priority,
421                         format_ipsec_policy_action, p->policy,
422                         p->protocol ?
423                           format(0, "%U", format_ip_protocol, p->protocol) :
424                           (u8 *) "any",
425                         p->policy == IPSEC_POLICY_ACTION_PROTECT ?
426                           format(0, " sa %u", p->sa_id) :
427                           (u8 *) "");
428         vlib_cli_output(vm, "   local addr range %U - %U port range %u - %u",
429                         format_ip4_address, &p->laddr.start.ip4,
430                         format_ip4_address, &p->laddr.stop.ip4,
431                         p->lport.start, p->lport.stop);
432         vlib_cli_output(vm, "   remte addr range %U - %U port range %u - %u",
433                         format_ip4_address, &p->raddr.start.ip4,
434                         format_ip4_address, &p->raddr.stop.ip4,
435                         p->rport.start, p->rport.stop);
436         vlib_cli_output(vm, "   packets %u bytes %u", p->counter.packets,
437                         p->counter.bytes);
438       };
439     vec_foreach(i, spd->ipv6_outbound_policies)
440       {
441         p = pool_elt_at_index(spd->policies, *i);
442         vlib_cli_output(vm, "  priority %d action %U protocol %s%s",
443                         p->priority,
444                         format_ipsec_policy_action, p->policy,
445                         p->protocol ?
446                           format(0, "%U", format_ip_protocol, p->protocol) :
447                           (u8 *) "any",
448                         p->policy == IPSEC_POLICY_ACTION_PROTECT ?
449                           format(0, " sa %u", p->sa_id) :
450                           (u8 *) "");
451         vlib_cli_output(vm, "   local addr range %U - %U port range %u - %u",
452                         format_ip6_address, &p->laddr.start.ip6,
453                         format_ip6_address, &p->laddr.stop.ip6,
454                         p->lport.start, p->lport.stop);
455         vlib_cli_output(vm, "   remote addr range %U - %U port range %u - %u",
456                         format_ip6_address, &p->raddr.start.ip6,
457                         format_ip6_address, &p->raddr.stop.ip6,
458                         p->rport.start, p->rport.stop);
459         vlib_cli_output(vm, "   packets %u bytes %u", p->counter.packets,
460                         p->counter.bytes);
461       };
462     vlib_cli_output(vm, " inbound policies");
463     vec_foreach(i, spd->ipv4_inbound_protect_policy_indices)
464       {
465         p = pool_elt_at_index(spd->policies, *i);
466         vlib_cli_output(vm, "  priority %d action %U protocol %s%s",
467                         p->priority,
468                         format_ipsec_policy_action, p->policy,
469                         p->protocol ?
470                           format(0, "%U", format_ip_protocol, p->protocol) :
471                           (u8 *) "any",
472                         p->policy == IPSEC_POLICY_ACTION_PROTECT ?
473                           format(0, " sa %u", p->sa_id) :
474                           (u8 *) "");
475         vlib_cli_output(vm, "   local addr range %U - %U port range %u - %u",
476                         format_ip4_address, &p->laddr.start.ip4,
477                         format_ip4_address, &p->laddr.stop.ip4,
478                         p->lport.start, p->lport.stop);
479         vlib_cli_output(vm, "   remte addr range %U - %U port range %u - %u",
480                         format_ip4_address, &p->raddr.start.ip4,
481                         format_ip4_address, &p->raddr.stop.ip4,
482                         p->rport.start, p->rport.stop);
483         vlib_cli_output(vm, "   packets %u bytes %u", p->counter.packets,
484                         p->counter.bytes);
485       };
486     vec_foreach(i, spd->ipv4_inbound_policy_discard_and_bypass_indices)
487       {
488         p = pool_elt_at_index(spd->policies, *i);
489         vlib_cli_output(vm, "  priority %d action %U protocol %s%s",
490                         p->priority,
491                         format_ipsec_policy_action, p->policy,
492                         p->protocol ?
493                           format(0, "%U", format_ip_protocol, p->protocol) :
494                           (u8 *) "any",
495                         p->policy == IPSEC_POLICY_ACTION_PROTECT ?
496                           format(0, " sa %u", p->sa_id) :
497                           (u8 *) "");
498         vlib_cli_output(vm, "   local addr range %U - %U port range %u - %u",
499                         format_ip4_address, &p->laddr.start.ip4,
500                         format_ip4_address, &p->laddr.stop.ip4,
501                         p->lport.start, p->lport.stop);
502         vlib_cli_output(vm, "   remte addr range %U - %U port range %u - %u",
503                         format_ip4_address, &p->raddr.start.ip4,
504                         format_ip4_address, &p->raddr.stop.ip4,
505                         p->rport.start, p->rport.stop);
506         vlib_cli_output(vm, "   packets %u bytes %u", p->counter.packets,
507                         p->counter.bytes);
508       };
509     vec_foreach(i, spd->ipv6_inbound_protect_policy_indices)
510       {
511         p = pool_elt_at_index(spd->policies, *i);
512         vlib_cli_output(vm, "  priority %d action %U protocol %s%s",
513                         p->priority,
514                         format_ipsec_policy_action, p->policy,
515                         p->protocol ?
516                           format(0, "%U", format_ip_protocol, p->protocol) :
517                           (u8 *) "any",
518                         p->policy == IPSEC_POLICY_ACTION_PROTECT ?
519                           format(0, " sa %u", p->sa_id) :
520                           (u8 *) "");
521         vlib_cli_output(vm, "   local addr range %U - %U port range %u - %u",
522                         format_ip6_address, &p->laddr.start.ip6,
523                         format_ip6_address, &p->laddr.stop.ip6,
524                         p->lport.start, p->lport.stop);
525         vlib_cli_output(vm, "   remote addr range %U - %U port range %u - %u",
526                         format_ip6_address, &p->raddr.start.ip6,
527                         format_ip6_address, &p->raddr.stop.ip6,
528                         p->rport.start, p->rport.stop);
529         vlib_cli_output(vm, "   packets %u bytes %u", p->counter.packets,
530                         p->counter.bytes);
531       };
532     vec_foreach(i, spd->ipv6_inbound_policy_discard_and_bypass_indices)
533       {
534         p = pool_elt_at_index(spd->policies, *i);
535         vlib_cli_output(vm, "  priority %d action %U protocol %s%s",
536                         p->priority,
537                         format_ipsec_policy_action, p->policy,
538                         p->protocol ?
539                           format(0, "%U", format_ip_protocol, p->protocol) :
540                           (u8 *) "any",
541                         p->policy == IPSEC_POLICY_ACTION_PROTECT ?
542                           format(0, " sa %u", p->sa_id) :
543                           (u8 *) "");
544         vlib_cli_output(vm, "   local addr range %U - %U port range %u - %u",
545                         format_ip6_address, &p->laddr.start.ip6,
546                         format_ip6_address, &p->laddr.stop.ip6,
547                         p->lport.start, p->lport.stop);
548         vlib_cli_output(vm, "   remote addr range %U - %U port range %u - %u",
549                         format_ip6_address, &p->raddr.start.ip6,
550                         format_ip6_address, &p->raddr.stop.ip6,
551                         p->rport.start, p->rport.stop);
552         vlib_cli_output(vm, "   packets %u bytes %u", p->counter.packets,
553                         p->counter.bytes);
554       };
555   }));
556   /* *INDENT-ON* */
557
558   vlib_cli_output (vm, "tunnel interfaces");
559   /* *INDENT-OFF* */
560   pool_foreach (t, im->tunnel_interfaces, ({
561     hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index);
562     vlib_cli_output(vm, "  %s seq", hi->name);
563     sa = pool_elt_at_index(im->sad, t->output_sa_index);
564     vlib_cli_output(vm, "   seq %u seq-hi %u esn %u anti-replay %u",
565                     sa->seq, sa->seq_hi, sa->use_esn, sa->use_anti_replay);
566     vlib_cli_output(vm, "   local-spi %u local-ip %U", sa->spi,
567                     format_ip4_address, &sa->tunnel_src_addr.ip4);
568     vlib_cli_output(vm, "   local-crypto %U %U",
569                     format_ipsec_crypto_alg, sa->crypto_alg,
570                     format_hex_bytes, sa->crypto_key, sa->crypto_key_len);
571     vlib_cli_output(vm, "   local-integrity %U %U",
572                     format_ipsec_integ_alg, sa->integ_alg,
573                     format_hex_bytes, sa->integ_key, sa->integ_key_len);
574     sa = pool_elt_at_index(im->sad, t->input_sa_index);
575     vlib_cli_output(vm, "   last-seq %u last-seq-hi %u esn %u anti-replay %u window %U",
576                     sa->last_seq, sa->last_seq_hi, sa->use_esn,
577                     sa->use_anti_replay,
578                     format_ipsec_replay_window, sa->replay_window);
579     vlib_cli_output(vm, "   remote-spi %u remote-ip %U", sa->spi,
580                     format_ip4_address, &sa->tunnel_src_addr.ip4);
581     vlib_cli_output(vm, "   remote-crypto %U %U",
582                     format_ipsec_crypto_alg, sa->crypto_alg,
583                     format_hex_bytes, sa->crypto_key, sa->crypto_key_len);
584     vlib_cli_output(vm, "   remote-integrity %U %U",
585                     format_ipsec_integ_alg, sa->integ_alg,
586                     format_hex_bytes, sa->integ_key, sa->integ_key_len);
587   }));
588   /* *INDENT-ON* */
589   return 0;
590 }
591
592 /* *INDENT-OFF* */
593 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
594     .path = "show ipsec",
595     .short_help = "show ipsec",
596     .function = show_ipsec_command_fn,
597 };
598 /* *INDENT-ON* */
599
600 static clib_error_t *
601 clear_ipsec_counters_command_fn (vlib_main_t * vm,
602                                  unformat_input_t * input,
603                                  vlib_cli_command_t * cmd)
604 {
605   ipsec_main_t *im = &ipsec_main;
606   ipsec_spd_t *spd;
607   ipsec_policy_t *p;
608
609   /* *INDENT-OFF* */
610   pool_foreach (spd, im->spds, ({
611     pool_foreach(p, spd->policies, ({
612       p->counter.packets = p->counter.bytes = 0;
613     }));
614   }));
615   /* *INDENT-ON* */
616
617   return 0;
618 }
619
620 /* *INDENT-OFF* */
621 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
622     .path = "clear ipsec counters",
623     .short_help = "clear ipsec counters",
624     .function = clear_ipsec_counters_command_fn,
625 };
626 /* *INDENT-ON* */
627
628 static clib_error_t *
629 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
630                                 unformat_input_t * input,
631                                 vlib_cli_command_t * cmd)
632 {
633   unformat_input_t _line_input, *line_input = &_line_input;
634   ipsec_add_del_tunnel_args_t a;
635   int rv;
636   u32 num_m_args = 0;
637
638   memset (&a, 0, sizeof (a));
639   a.is_add = 1;
640
641   /* Get a line of input. */
642   if (!unformat_user (input, unformat_line_input, line_input))
643     return 0;
644
645   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
646     {
647       if (unformat
648           (line_input, "local-ip %U", unformat_ip4_address, &a.local_ip))
649         num_m_args++;
650       else
651         if (unformat
652             (line_input, "remote-ip %U", unformat_ip4_address, &a.remote_ip))
653         num_m_args++;
654       else if (unformat (line_input, "local-spi %u", &a.local_spi))
655         num_m_args++;
656       else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
657         num_m_args++;
658       else if (unformat (line_input, "del"))
659         a.is_add = 0;
660       else
661         return clib_error_return (0, "unknown input `%U'",
662                                   format_unformat_error, input);
663     }
664   unformat_free (line_input);
665
666   if (num_m_args < 4)
667     return clib_error_return (0, "mandatory argument(s) missing");
668
669   rv = ipsec_add_del_tunnel_if (&a);
670
671   switch (rv)
672     {
673     case 0:
674       break;
675     case VNET_API_ERROR_INVALID_VALUE:
676       if (a.is_add)
677         return clib_error_return (0,
678                                   "IPSec tunnel interface already exists...");
679       else
680         return clib_error_return (0, "IPSec tunnel interface not exists...");
681     default:
682       return clib_error_return (0, "ipsec_register_interface returned %d",
683                                 rv);
684     }
685
686   return 0;
687 }
688
689 /* *INDENT-OFF* */
690 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
691   .path = "create ipsec tunnel",
692   .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> remote-ip <addr> remote-spi <spi>",
693   .function = create_ipsec_tunnel_command_fn,
694 };
695 /* *INDENT-ON* */
696
697 static clib_error_t *
698 set_interface_key_command_fn (vlib_main_t * vm,
699                               unformat_input_t * input,
700                               vlib_cli_command_t * cmd)
701 {
702   unformat_input_t _line_input, *line_input = &_line_input;
703   ipsec_main_t *im = &ipsec_main;
704   ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
705   u32 hw_if_index = (u32) ~ 0;
706   u32 alg;
707   u8 *key = 0;
708
709   if (!unformat_user (input, unformat_line_input, line_input))
710     return 0;
711
712   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
713     {
714       if (unformat (line_input, "%U",
715                     unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
716         ;
717       else
718         if (unformat
719             (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
720         type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
721       else
722         if (unformat
723             (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
724         type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
725       else
726         if (unformat
727             (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
728         type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
729       else
730         if (unformat
731             (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
732         type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
733       else if (unformat (line_input, "%U", unformat_hex_string, &key))
734         ;
735       else
736         return clib_error_return (0, "parse error: '%U'",
737                                   format_unformat_error, line_input);
738     }
739
740   unformat_free (line_input);
741
742   if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
743     return clib_error_return (0, "unknown key type");
744
745   if (alg > 0 && vec_len (key) == 0)
746     return clib_error_return (0, "key is not specified");
747
748   if (hw_if_index == (u32) ~ 0)
749     return clib_error_return (0, "interface not specified");
750
751   ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
752   vec_free (key);
753
754   return 0;
755 }
756
757 /* *INDENT-OFF* */
758 VLIB_CLI_COMMAND (set_interface_key_command, static) = {
759     .path = "set interface ipsec key",
760     .short_help =
761     "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
762     .function = set_interface_key_command_fn,
763 };
764 /* *INDENT-ON* */
765
766
767 clib_error_t *
768 ipsec_cli_init (vlib_main_t * vm)
769 {
770   return 0;
771 }
772
773 VLIB_INIT_FUNCTION (ipsec_cli_init);
774
775
776 /*
777  * fd.io coding-style-patch-verification: ON
778  *
779  * Local Variables:
780  * eval: (c-set-style "gnu")
781  * End:
782  */