ipip: Tunnel flags controlling copying data to/from payload/encap
[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 #include <vnet/ipip/ipip.h>
24
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/ipsec_tun.h>
27
28 static clib_error_t *
29 set_interface_spd_command_fn (vlib_main_t * vm,
30                               unformat_input_t * input,
31                               vlib_cli_command_t * cmd)
32 {
33   unformat_input_t _line_input, *line_input = &_line_input;
34   ipsec_main_t *im = &ipsec_main;
35   u32 sw_if_index = (u32) ~ 0;
36   u32 spd_id;
37   int is_add = 1;
38   clib_error_t *error = NULL;
39
40   if (!unformat_user (input, unformat_line_input, line_input))
41     return 0;
42
43   if (unformat
44       (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
45        &sw_if_index, &spd_id))
46     ;
47   else if (unformat (line_input, "del"))
48     is_add = 0;
49   else
50     {
51       error = clib_error_return (0, "parse error: '%U'",
52                                  format_unformat_error, line_input);
53       goto done;
54     }
55
56   ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
57
58 done:
59   unformat_free (line_input);
60
61   return error;
62 }
63
64 /* *INDENT-OFF* */
65 VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
66     .path = "set interface ipsec spd",
67     .short_help =
68     "set interface ipsec spd <int> <id>",
69     .function = set_interface_spd_command_fn,
70 };
71 /* *INDENT-ON* */
72
73 static clib_error_t *
74 ipsec_sa_add_del_command_fn (vlib_main_t * vm,
75                              unformat_input_t * input,
76                              vlib_cli_command_t * cmd)
77 {
78   unformat_input_t _line_input, *line_input = &_line_input;
79   ip46_address_t tun_src = { }, tun_dst =
80   {
81   };
82   ipsec_crypto_alg_t crypto_alg;
83   ipsec_integ_alg_t integ_alg;
84   ipsec_protocol_t proto;
85   ipsec_sa_flags_t flags;
86   clib_error_t *error;
87   ipsec_key_t ck = { 0 };
88   ipsec_key_t ik = { 0 };
89   u32 id, spi, salt;
90   int is_add, rv;
91
92   error = NULL;
93   is_add = 0;
94   flags = IPSEC_SA_FLAG_NONE;
95   proto = IPSEC_PROTOCOL_ESP;
96   integ_alg = IPSEC_INTEG_ALG_NONE;
97   crypto_alg = IPSEC_CRYPTO_ALG_NONE;
98
99   if (!unformat_user (input, unformat_line_input, line_input))
100     return 0;
101
102   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (line_input, "add %u", &id))
105         is_add = 1;
106       else if (unformat (line_input, "del %u", &id))
107         is_add = 0;
108       else if (unformat (line_input, "spi %u", &spi))
109         ;
110       else if (unformat (line_input, "salt 0x%x", &salt))
111         ;
112       else if (unformat (line_input, "esp"))
113         proto = IPSEC_PROTOCOL_ESP;
114       else if (unformat (line_input, "ah"))
115         proto = IPSEC_PROTOCOL_AH;
116       else if (unformat (line_input, "crypto-key %U",
117                          unformat_ipsec_key, &ck))
118         ;
119       else if (unformat (line_input, "crypto-alg %U",
120                          unformat_ipsec_crypto_alg, &crypto_alg))
121         ;
122       else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
123         ;
124       else if (unformat (line_input, "integ-alg %U",
125                          unformat_ipsec_integ_alg, &integ_alg))
126         ;
127       else if (unformat (line_input, "tunnel-src %U",
128                          unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
129         {
130           flags |= IPSEC_SA_FLAG_IS_TUNNEL;
131           if (!ip46_address_is_ip4 (&tun_src))
132             flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
133         }
134       else if (unformat (line_input, "tunnel-dst %U",
135                          unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
136         ;
137       else if (unformat (line_input, "udp-encap"))
138         flags |= IPSEC_SA_FLAG_UDP_ENCAP;
139       else
140         {
141           error = clib_error_return (0, "parse error: '%U'",
142                                      format_unformat_error, line_input);
143           goto done;
144         }
145     }
146
147   if (is_add)
148     rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
149                                 &ck, integ_alg, &ik, flags,
150                                 0, clib_host_to_net_u32 (salt),
151                                 &tun_src, &tun_dst, NULL);
152   else
153     rv = ipsec_sa_unlock_id (id);
154
155   if (rv)
156     error = clib_error_return (0, "failed");
157
158 done:
159   unformat_free (line_input);
160
161   return error;
162 }
163
164 /* *INDENT-OFF* */
165 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
166     .path = "ipsec sa",
167     .short_help =
168     "ipsec sa [add|del]",
169     .function = ipsec_sa_add_del_command_fn,
170 };
171 /* *INDENT-ON* */
172
173 static clib_error_t *
174 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
175                               unformat_input_t * input,
176                               vlib_cli_command_t * cmd)
177 {
178   unformat_input_t _line_input, *line_input = &_line_input;
179   u32 spd_id = ~0;
180   int is_add = ~0;
181   clib_error_t *error = NULL;
182
183   if (!unformat_user (input, unformat_line_input, line_input))
184     return 0;
185
186   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
187     {
188       if (unformat (line_input, "add"))
189         is_add = 1;
190       else if (unformat (line_input, "del"))
191         is_add = 0;
192       else if (unformat (line_input, "%u", &spd_id))
193         ;
194       else
195         {
196           error = clib_error_return (0, "parse error: '%U'",
197                                      format_unformat_error, line_input);
198           goto done;
199         }
200     }
201
202   if (spd_id == ~0)
203     {
204       error = clib_error_return (0, "please specify SPD ID");
205       goto done;
206     }
207
208   ipsec_add_del_spd (vm, spd_id, is_add);
209
210 done:
211   unformat_free (line_input);
212
213   return error;
214 }
215
216 /* *INDENT-OFF* */
217 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
218     .path = "ipsec spd",
219     .short_help =
220     "ipsec spd [add|del] <id>",
221     .function = ipsec_spd_add_del_command_fn,
222 };
223 /* *INDENT-ON* */
224
225
226 static clib_error_t *
227 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
228                                  unformat_input_t * input,
229                                  vlib_cli_command_t * cmd)
230 {
231   unformat_input_t _line_input, *line_input = &_line_input;
232   ipsec_policy_t p;
233   int rv, is_add = 0;
234   u32 tmp, tmp2, stat_index;
235   clib_error_t *error = NULL;
236   u32 is_outbound;
237
238   clib_memset (&p, 0, sizeof (p));
239   p.lport.stop = p.rport.stop = ~0;
240   is_outbound = 0;
241
242   if (!unformat_user (input, unformat_line_input, line_input))
243     return 0;
244
245   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
246     {
247       if (unformat (line_input, "add"))
248         is_add = 1;
249       else if (unformat (line_input, "del"))
250         is_add = 0;
251       else if (unformat (line_input, "spd %u", &p.id))
252         ;
253       else if (unformat (line_input, "inbound"))
254         is_outbound = 0;
255       else if (unformat (line_input, "outbound"))
256         is_outbound = 1;
257       else if (unformat (line_input, "priority %d", &p.priority))
258         ;
259       else if (unformat (line_input, "protocol %u", &tmp))
260         p.protocol = (u8) tmp;
261       else
262         if (unformat
263             (line_input, "action %U", unformat_ipsec_policy_action,
264              &p.policy))
265         {
266           if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
267             {
268               error = clib_error_return (0, "unsupported action: 'resolve'");
269               goto done;
270             }
271         }
272       else if (unformat (line_input, "sa %u", &p.sa_id))
273         ;
274       else if (unformat (line_input, "local-ip-range %U - %U",
275                          unformat_ip4_address, &p.laddr.start.ip4,
276                          unformat_ip4_address, &p.laddr.stop.ip4))
277         ;
278       else if (unformat (line_input, "remote-ip-range %U - %U",
279                          unformat_ip4_address, &p.raddr.start.ip4,
280                          unformat_ip4_address, &p.raddr.stop.ip4))
281         ;
282       else if (unformat (line_input, "local-ip-range %U - %U",
283                          unformat_ip6_address, &p.laddr.start.ip6,
284                          unformat_ip6_address, &p.laddr.stop.ip6))
285         {
286           p.is_ipv6 = 1;
287         }
288       else if (unformat (line_input, "remote-ip-range %U - %U",
289                          unformat_ip6_address, &p.raddr.start.ip6,
290                          unformat_ip6_address, &p.raddr.stop.ip6))
291         {
292           p.is_ipv6 = 1;
293         }
294       else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
295         {
296           p.lport.start = tmp;
297           p.lport.stop = tmp2;
298         }
299       else
300         if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
301         {
302           p.rport.start = tmp;
303           p.rport.stop = tmp2;
304         }
305       else
306         {
307           error = clib_error_return (0, "parse error: '%U'",
308                                      format_unformat_error, line_input);
309           goto done;
310         }
311     }
312
313   rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
314
315   if (rv)
316     {
317       error = clib_error_return (0, "unsupported policy type for:",
318                                  " outboud:%s %s action:%U",
319                                  (is_outbound ? "yes" : "no"),
320                                  (p.is_ipv6 ? "IPv4" : "IPv6"),
321                                  format_ipsec_policy_action, p.policy);
322       goto done;
323     }
324
325   rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
326
327   if (!rv)
328     vlib_cli_output (vm, "policy-index:%d", stat_index);
329   else
330     vlib_cli_output (vm, "error:%d", rv);
331
332 done:
333   unformat_free (line_input);
334
335   return error;
336 }
337
338 /* *INDENT-OFF* */
339 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
340     .path = "ipsec policy",
341     .short_help =
342     "ipsec policy [add|del] spd <id> priority <n> ",
343     .function = ipsec_policy_add_del_command_fn,
344 };
345 /* *INDENT-ON* */
346
347 static void
348 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
349 {
350   u32 sai;
351
352   /* *INDENT-OFF* */
353   pool_foreach_index (sai, im->sad, ({
354     vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
355                     (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
356   }));
357   /* *INDENT-ON* */
358 }
359
360 static void
361 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
362 {
363   u32 spdi;
364
365   /* *INDENT-OFF* */
366   pool_foreach_index (spdi, im->spds, ({
367     vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
368   }));
369   /* *INDENT-ON* */
370 }
371
372 static void
373 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
374 {
375   u32 spd_id, sw_if_index;
376   ipsec_spd_t *spd;
377
378   vlib_cli_output (vm, "SPD Bindings:");
379
380   /* *INDENT-OFF* */
381   hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
382     spd = pool_elt_at_index (im->spds, spd_id);
383     vlib_cli_output (vm, "  %d -> %U", spd->id,
384                      format_vnet_sw_if_index_name, im->vnet_main,
385                      sw_if_index);
386   }));
387   /* *INDENT-ON* */
388 }
389
390 static walk_rc_t
391 ipsec_tun_protect_show_one (index_t itpi, void *ctx)
392 {
393   vlib_cli_output (ctx, "%U", format_ipsec_tun_protect, itpi);
394
395   return (WALK_CONTINUE);
396 }
397
398 static void
399 ipsec_tunnel_show_all (vlib_main_t * vm)
400 {
401   ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
402 }
403
404 static clib_error_t *
405 show_ipsec_command_fn (vlib_main_t * vm,
406                        unformat_input_t * input, vlib_cli_command_t * cmd)
407 {
408   ipsec_main_t *im = &ipsec_main;
409
410   ipsec_sa_show_all (vm, im, 0);
411   ipsec_spd_show_all (vm, im);
412   ipsec_spd_bindings_show_all (vm, im);
413   ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
414
415   return 0;
416 }
417
418 /* *INDENT-OFF* */
419 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
420     .path = "show ipsec all",
421     .short_help = "show ipsec all",
422     .function = show_ipsec_command_fn,
423 };
424 /* *INDENT-ON* */
425
426 static clib_error_t *
427 show_ipsec_sa_command_fn (vlib_main_t * vm,
428                           unformat_input_t * input, vlib_cli_command_t * cmd)
429 {
430   ipsec_main_t *im = &ipsec_main;
431   u32 sai = ~0;
432   u8 detail = 0;
433
434   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
435     {
436       if (unformat (input, "%u", &sai))
437         ;
438       if (unformat (input, "detail"))
439         detail = 1;
440       else
441         break;
442     }
443
444   if (~0 == sai)
445     ipsec_sa_show_all (vm, im, detail);
446   else
447     vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
448                      IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
449
450   return 0;
451 }
452
453 static clib_error_t *
454 clear_ipsec_sa_command_fn (vlib_main_t * vm,
455                            unformat_input_t * input, vlib_cli_command_t * cmd)
456 {
457   ipsec_main_t *im = &ipsec_main;
458   u32 sai = ~0;
459
460   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
461     {
462       if (unformat (input, "%u", &sai))
463         ;
464       else
465         break;
466     }
467
468   if (~0 == sai)
469     {
470       /* *INDENT-OFF* */
471       pool_foreach_index (sai, im->sad, ({
472         ipsec_sa_clear(sai);
473       }));
474       /* *INDENT-ON* */
475     }
476   else
477     {
478       if (pool_is_free_index (im->sad, sai))
479         return clib_error_return (0, "unknown SA index: %d", sai);
480       else
481         ipsec_sa_clear (sai);
482     }
483
484   return 0;
485 }
486
487 /* *INDENT-OFF* */
488 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
489     .path = "show ipsec sa",
490     .short_help = "show ipsec sa [index]",
491     .function = show_ipsec_sa_command_fn,
492 };
493
494 VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
495     .path = "clear ipsec sa",
496     .short_help = "clear ipsec sa [index]",
497     .function = clear_ipsec_sa_command_fn,
498 };
499 /* *INDENT-ON* */
500
501 static clib_error_t *
502 show_ipsec_spd_command_fn (vlib_main_t * vm,
503                            unformat_input_t * input, vlib_cli_command_t * cmd)
504 {
505   ipsec_main_t *im = &ipsec_main;
506   u8 show_bindings = 0;
507   u32 spdi = ~0;
508
509   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
510     {
511       if (unformat (input, "%u", &spdi))
512         ;
513       else if (unformat (input, "bindings"))
514         show_bindings = 1;
515       else
516         break;
517     }
518
519   if (show_bindings)
520     ipsec_spd_bindings_show_all (vm, im);
521   else if (~0 != spdi)
522     vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
523   else
524     ipsec_spd_show_all (vm, im);
525
526   return 0;
527 }
528
529 /* *INDENT-OFF* */
530 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
531     .path = "show ipsec spd",
532     .short_help = "show ipsec spd [index]",
533     .function = show_ipsec_spd_command_fn,
534 };
535 /* *INDENT-ON* */
536
537 static clib_error_t *
538 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
539                               unformat_input_t * input,
540                               vlib_cli_command_t * cmd)
541 {
542   ipsec_tunnel_show_all (vm);
543
544   return 0;
545 }
546
547 /* *INDENT-OFF* */
548 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
549     .path = "show ipsec tunnel",
550     .short_help = "show ipsec tunnel",
551     .function = show_ipsec_tunnel_command_fn,
552 };
553 /* *INDENT-ON* */
554
555 static clib_error_t *
556 ipsec_show_backends_command_fn (vlib_main_t * vm,
557                                 unformat_input_t * input,
558                                 vlib_cli_command_t * cmd)
559 {
560   ipsec_main_t *im = &ipsec_main;
561   u32 verbose = 0;
562
563   (void) unformat (input, "verbose %u", &verbose);
564
565   vlib_cli_output (vm, "IPsec AH backends available:");
566   u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
567   ipsec_ah_backend_t *ab;
568   /* *INDENT-OFF* */
569   pool_foreach (ab, im->ah_backends, {
570     s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
571                 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
572     if (verbose) {
573         vlib_node_t *n;
574         n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
575         s = format (s, "     enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
576         n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
577         s = format (s, "     dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
578         n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
579         s = format (s, "     enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
580         n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
581         s = format (s, "     dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
582     }
583   });
584   /* *INDENT-ON* */
585   vlib_cli_output (vm, "%v", s);
586   _vec_len (s) = 0;
587   vlib_cli_output (vm, "IPsec ESP backends available:");
588   s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
589   ipsec_esp_backend_t *eb;
590   /* *INDENT-OFF* */
591   pool_foreach (eb, im->esp_backends, {
592     s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
593                 eb - im->esp_backends == im->esp_current_backend ? "yes"
594                                                                  : "no");
595     if (verbose) {
596         vlib_node_t *n;
597         n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
598         s = format (s, "     enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
599         n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
600         s = format (s, "     dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
601         n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
602         s = format (s, "     enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
603         n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
604         s = format (s, "     dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
605     }
606   });
607   /* *INDENT-ON* */
608   vlib_cli_output (vm, "%v", s);
609
610   vec_free (s);
611   return 0;
612 }
613
614 /* *INDENT-OFF* */
615 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
616     .path = "show ipsec backends",
617     .short_help = "show ipsec backends",
618     .function = ipsec_show_backends_command_fn,
619 };
620 /* *INDENT-ON* */
621
622 static clib_error_t *
623 ipsec_select_backend_command_fn (vlib_main_t * vm,
624                                  unformat_input_t * input,
625                                  vlib_cli_command_t * cmd)
626 {
627   unformat_input_t _line_input, *line_input = &_line_input;
628   ipsec_main_t *im = &ipsec_main;
629   clib_error_t *error;
630   u32 backend_index;
631
632   error = ipsec_rsc_in_use (im);
633
634   if (error)
635     return error;
636
637   /* Get a line of input. */
638   if (!unformat_user (input, unformat_line_input, line_input))
639     return 0;
640
641   if (unformat (line_input, "ah"))
642     {
643       if (unformat (line_input, "%u", &backend_index))
644         {
645           if (ipsec_select_ah_backend (im, backend_index) < 0)
646             {
647               return clib_error_return (0, "Invalid AH backend index `%u'",
648                                         backend_index);
649             }
650         }
651       else
652         {
653           return clib_error_return (0, "Invalid backend index `%U'",
654                                     format_unformat_error, line_input);
655         }
656     }
657   else if (unformat (line_input, "esp"))
658     {
659       if (unformat (line_input, "%u", &backend_index))
660         {
661           if (ipsec_select_esp_backend (im, backend_index) < 0)
662             {
663               return clib_error_return (0, "Invalid ESP backend index `%u'",
664                                         backend_index);
665             }
666         }
667       else
668         {
669           return clib_error_return (0, "Invalid backend index `%U'",
670                                     format_unformat_error, line_input);
671         }
672     }
673   else
674     {
675       return clib_error_return (0, "Unknown input `%U'",
676                                 format_unformat_error, line_input);
677     }
678
679   return 0;
680 }
681
682 /* *INDENT-OFF* */
683 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
684     .path = "ipsec select backend",
685     .short_help = "ipsec select backend <ah|esp> <backend index>",
686     .function = ipsec_select_backend_command_fn,
687 };
688
689 /* *INDENT-ON* */
690
691 static clib_error_t *
692 clear_ipsec_counters_command_fn (vlib_main_t * vm,
693                                  unformat_input_t * input,
694                                  vlib_cli_command_t * cmd)
695 {
696   vlib_clear_combined_counters (&ipsec_spd_policy_counters);
697   vlib_clear_combined_counters (&ipsec_sa_counters);
698
699   return (NULL);
700 }
701
702 /* *INDENT-OFF* */
703 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
704     .path = "clear ipsec counters",
705     .short_help = "clear ipsec counters",
706     .function = clear_ipsec_counters_command_fn,
707 };
708 /* *INDENT-ON* */
709
710 static u32
711 ipsec_tun_mk_local_sa_id (u32 ti)
712 {
713   return (0x80000000 | ti);
714 }
715
716 static u32
717 ipsec_tun_mk_remote_sa_id (u32 ti)
718 {
719   return (0xc0000000 | ti);
720 }
721
722 static clib_error_t *
723 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
724                                 unformat_input_t * input,
725                                 vlib_cli_command_t * cmd)
726 {
727   unformat_input_t _line_input, *line_input = &_line_input;
728   ip46_address_t local_ip = ip46_address_initializer;
729   ip46_address_t remote_ip = ip46_address_initializer;
730   ipsec_crypto_alg_t crypto_alg;
731   ipsec_integ_alg_t integ_alg;
732   ipsec_sa_flags_t flags;
733   u32 local_spi, remote_spi, salt, table_id, fib_index;
734   u32 instance = ~0;
735   int rv;
736   u32 num_m_args = 0;
737   u8 ipv4_set = 0;
738   u8 ipv6_set = 0;
739   u8 is_add = 1;
740   clib_error_t *error = NULL;
741   ipsec_key_t rck = { 0 };
742   ipsec_key_t lck = { 0 };
743   ipsec_key_t lik = { 0 };
744   ipsec_key_t rik = { 0 };
745
746   table_id = 0;
747   flags = IPSEC_SA_FLAG_NONE;
748
749   /* Get a line of input. */
750   if (!unformat_user (input, unformat_line_input, line_input))
751     return 0;
752
753   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
754     {
755       if (unformat
756           (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
757            IP46_TYPE_ANY))
758         {
759           ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
760           num_m_args++;
761         }
762       else
763         if (unformat
764             (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
765              IP46_TYPE_ANY))
766         {
767           ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
768           num_m_args++;
769         }
770       else if (unformat (line_input, "local-spi %u", &local_spi))
771         num_m_args++;
772       else if (unformat (line_input, "remote-spi %u", &remote_spi))
773         num_m_args++;
774       else if (unformat (line_input, "salt 0x%x", &salt))
775         ;
776       else if (unformat (line_input, "udp-encap"))
777         flags |= IPSEC_SA_FLAG_UDP_ENCAP;
778       else if (unformat (line_input, "use-esn"))
779         flags |= IPSEC_SA_FLAG_USE_ESN;
780       else if (unformat (line_input, "use-anti-replay"))
781         flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
782       else if (unformat (line_input, "instance %u", &instance))
783         ;
784       else if (unformat (line_input, "tx-table %u", &table_id))
785         ;
786       else
787         if (unformat
788             (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
789         ;
790       else
791         if (unformat
792             (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
793         ;
794       else if (unformat (line_input, "crypto-alg %U",
795                          unformat_ipsec_crypto_alg, &crypto_alg))
796         ;
797       else
798         if (unformat
799             (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
800         ;
801       else
802         if (unformat
803             (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
804         ;
805       else if (unformat (line_input, "integ-alg %U",
806                          unformat_ipsec_integ_alg, &integ_alg))
807         ;
808       else if (unformat (line_input, "del"))
809         is_add = 0;
810       else
811         {
812           error = clib_error_return (0, "unknown input `%U'",
813                                      format_unformat_error, line_input);
814           goto done;
815         }
816     }
817
818   if (num_m_args < 4)
819     {
820       error = clib_error_return (0, "mandatory argument(s) missing");
821       goto done;
822     }
823
824   if (ipv4_set && ipv6_set)
825     return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
826
827   fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
828
829   if (~0 == fib_index)
830     {
831       rv = VNET_API_ERROR_NO_SUCH_FIB;
832       goto done;
833     }
834
835   if (is_add)
836     {
837       // remote = input, local = output
838       u32 sw_if_index;
839
840       /* create an ip-ip tunnel, then the two SA, then bind them */
841       rv =
842         ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
843                          instance, &local_ip, &remote_ip, fib_index,
844                          IPIP_TUNNEL_FLAG_NONE, IP_DSCP_CS0, &sw_if_index);
845       rv |=
846         ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
847                                local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
848                                &lck, integ_alg, &lik, flags, table_id,
849                                clib_host_to_net_u32 (salt), &local_ip,
850                                &remote_ip, NULL);
851       rv |=
852         ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
853                                remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
854                                &rck, integ_alg, &rik,
855                                (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
856                                clib_host_to_net_u32 (salt), &remote_ip,
857                                &local_ip, NULL);
858       rv |=
859         ipsec_tun_protect_update_one (sw_if_index,
860                                       ipsec_tun_mk_local_sa_id (sw_if_index),
861                                       ipsec_tun_mk_remote_sa_id
862                                       (sw_if_index));
863     }
864   else
865     rv = 0;
866
867   switch (rv)
868     {
869     case 0:
870       break;
871     case VNET_API_ERROR_INVALID_VALUE:
872       error = clib_error_return (0,
873                                  "IPSec tunnel interface already exists...");
874       goto done;
875     default:
876       error = clib_error_return (0, "ipsec_register_interface returned %d",
877                                  rv);
878       goto done;
879     }
880
881 done:
882   unformat_free (line_input);
883
884   return error;
885 }
886
887 /* *INDENT-OFF* */
888 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
889   .path = "create ipsec tunnel",
890   .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
891       "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
892       "[tx-table <table-id>]",
893   .function = create_ipsec_tunnel_command_fn,
894 };
895 /* *INDENT-ON* */
896
897 static clib_error_t *
898 ipsec_tun_protect_cmd (vlib_main_t * vm,
899                        unformat_input_t * input, vlib_cli_command_t * cmd)
900 {
901   unformat_input_t _line_input, *line_input = &_line_input;
902   u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
903   vnet_main_t *vnm;
904
905   is_del = 0;
906   sw_if_index = ~0;
907   vnm = vnet_get_main ();
908
909   if (!unformat_user (input, unformat_line_input, line_input))
910     return 0;
911
912   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
913     {
914       if (unformat (line_input, "del"))
915         is_del = 1;
916       else if (unformat (line_input, "add"))
917         is_del = 0;
918       else if (unformat (line_input, "sa-in %d", &sa_in))
919         vec_add1 (sa_ins, sa_in);
920       else if (unformat (line_input, "sa-out %d", &sa_out))
921         ;
922       else if (unformat (line_input, "%U",
923                          unformat_vnet_sw_interface, vnm, &sw_if_index))
924         ;
925       else
926         return (clib_error_return (0, "unknown input '%U'",
927                                    format_unformat_error, line_input));
928     }
929
930   if (!is_del)
931     ipsec_tun_protect_update (sw_if_index, sa_out, sa_ins);
932
933   unformat_free (line_input);
934   return NULL;
935 }
936
937 /**
938  * Protect tunnel with IPSEC
939  */
940 /* *INDENT-OFF* */
941 VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
942 {
943   .path = "ipsec tunnel protect",
944   .function = ipsec_tun_protect_cmd,
945   .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
946     // this is not MP safe
947 };
948 /* *INDENT-ON* */
949
950
951 static clib_error_t *
952 ipsec_tun_protect_show (vlib_main_t * vm,
953                         unformat_input_t * input, vlib_cli_command_t * cmd)
954 {
955   ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
956
957   return NULL;
958 }
959
960 /**
961  * show IPSEC tunnel protection
962  */
963 /* *INDENT-OFF* */
964 VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
965 {
966   .path = "show ipsec protect",
967   .function = ipsec_tun_protect_show,
968   .short_help =  "show ipsec protect",
969 };
970 /* *INDENT-ON* */
971
972 static clib_error_t *
973 ipsec_tun_protect_hash_show (vlib_main_t * vm,
974                              unformat_input_t * input,
975                              vlib_cli_command_t * cmd)
976 {
977   ipsec_main_t *im = &ipsec_main;
978
979   {
980     ipsec_tun_lkup_result_t value;
981     ipsec4_tunnel_key_t key;
982
983     vlib_cli_output (vm, "IPv4:");
984
985     /* *INDENT-OFF* */
986     hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
987     ({
988       vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
989       vlib_cli_output (vm, "  tun:%d sa:%d", value.tun_index, value.sa_index);
990     }));
991     /* *INDENT-ON* */
992   }
993
994   {
995     ipsec_tun_lkup_result_t value;
996     ipsec6_tunnel_key_t *key;
997
998     vlib_cli_output (vm, "IPv6:");
999
1000     /* *INDENT-OFF* */
1001     hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1002     ({
1003       vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1004       vlib_cli_output (vm, "  tun:%d sa:%d", value.tun_index, value.sa_index);
1005     }));
1006     /* *INDENT-ON* */
1007   }
1008
1009   return NULL;
1010 }
1011
1012 /**
1013  * show IPSEC tunnel protection hash tables
1014  */
1015 /* *INDENT-OFF* */
1016 VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1017 {
1018   .path = "show ipsec protect-hash",
1019   .function = ipsec_tun_protect_hash_show,
1020   .short_help =  "show ipsec protect-hash",
1021 };
1022 /* *INDENT-ON* */
1023
1024 clib_error_t *
1025 ipsec_cli_init (vlib_main_t * vm)
1026 {
1027   return 0;
1028 }
1029
1030 VLIB_INIT_FUNCTION (ipsec_cli_init);
1031
1032
1033 /*
1034  * fd.io coding-style-patch-verification: ON
1035  *
1036  * Local Variables:
1037  * eval: (c-set-style "gnu")
1038  * End:
1039  */