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