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