2 * decap.c : IPSec tunnel support
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/ipsec_tun.h>
29 set_interface_spd_command_fn (vlib_main_t * vm,
30 unformat_input_t * input,
31 vlib_cli_command_t * cmd)
33 unformat_input_t _line_input, *line_input = &_line_input;
34 ipsec_main_t *im = &ipsec_main;
35 u32 sw_if_index = (u32) ~ 0;
38 clib_error_t *error = NULL;
40 if (!unformat_user (input, unformat_line_input, line_input))
44 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
45 &sw_if_index, &spd_id))
47 else if (unformat (line_input, "del"))
51 error = clib_error_return (0, "parse error: '%U'",
52 format_unformat_error, line_input);
56 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
59 unformat_free (line_input);
65 VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
66 .path = "set interface ipsec spd",
68 "set interface ipsec spd <int> <id>",
69 .function = set_interface_spd_command_fn,
74 ipsec_sa_add_del_command_fn (vlib_main_t * vm,
75 unformat_input_t * input,
76 vlib_cli_command_t * cmd)
78 unformat_input_t _line_input, *line_input = &_line_input;
79 ip46_address_t tun_src = { }, tun_dst =
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;
87 ipsec_key_t ck = { 0 };
88 ipsec_key_t ik = { 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;
100 if (!unformat_user (input, unformat_line_input, line_input))
103 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
105 if (unformat (line_input, "add %u", &id))
107 else if (unformat (line_input, "del %u", &id))
109 else if (unformat (line_input, "spi %u", &spi))
111 else if (unformat (line_input, "salt 0x%x", &salt))
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))
120 else if (unformat (line_input, "crypto-alg %U",
121 unformat_ipsec_crypto_alg, &crypto_alg))
123 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
125 else if (unformat (line_input, "integ-alg %U",
126 unformat_ipsec_integ_alg, &integ_alg))
128 else if (unformat (line_input, "tunnel-src %U",
129 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
131 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
132 if (!ip46_address_is_ip4 (&tun_src))
133 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
135 else if (unformat (line_input, "tunnel-dst %U",
136 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
138 else if (unformat (line_input, "udp-encap"))
139 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
142 error = clib_error_return (0, "parse error: '%U'",
143 format_unformat_error, line_input);
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 IPSEC_UDP_PORT_NONE);
155 rv = ipsec_sa_unlock_id (id);
158 error = clib_error_return (0, "failed");
161 unformat_free (line_input);
167 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
170 "ipsec sa [add|del]",
171 .function = ipsec_sa_add_del_command_fn,
175 static clib_error_t *
176 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
177 unformat_input_t * input,
178 vlib_cli_command_t * cmd)
180 unformat_input_t _line_input, *line_input = &_line_input;
183 clib_error_t *error = NULL;
185 if (!unformat_user (input, unformat_line_input, line_input))
188 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
190 if (unformat (line_input, "add"))
192 else if (unformat (line_input, "del"))
194 else if (unformat (line_input, "%u", &spd_id))
198 error = clib_error_return (0, "parse error: '%U'",
199 format_unformat_error, line_input);
206 error = clib_error_return (0, "please specify SPD ID");
210 ipsec_add_del_spd (vm, spd_id, is_add);
213 unformat_free (line_input);
219 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
222 "ipsec spd [add|del] <id>",
223 .function = ipsec_spd_add_del_command_fn,
228 static clib_error_t *
229 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
230 unformat_input_t * input,
231 vlib_cli_command_t * cmd)
233 unformat_input_t _line_input, *line_input = &_line_input;
236 u32 tmp, tmp2, stat_index;
237 clib_error_t *error = NULL;
240 clib_memset (&p, 0, sizeof (p));
241 p.lport.stop = p.rport.stop = ~0;
244 if (!unformat_user (input, unformat_line_input, line_input))
247 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
249 if (unformat (line_input, "add"))
251 else if (unformat (line_input, "del"))
253 else if (unformat (line_input, "spd %u", &p.id))
255 else if (unformat (line_input, "inbound"))
257 else if (unformat (line_input, "outbound"))
259 else if (unformat (line_input, "priority %d", &p.priority))
261 else if (unformat (line_input, "protocol %u", &tmp))
262 p.protocol = (u8) tmp;
265 (line_input, "action %U", unformat_ipsec_policy_action,
268 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
270 error = clib_error_return (0, "unsupported action: 'resolve'");
274 else if (unformat (line_input, "sa %u", &p.sa_id))
276 else if (unformat (line_input, "local-ip-range %U - %U",
277 unformat_ip4_address, &p.laddr.start.ip4,
278 unformat_ip4_address, &p.laddr.stop.ip4))
280 else if (unformat (line_input, "remote-ip-range %U - %U",
281 unformat_ip4_address, &p.raddr.start.ip4,
282 unformat_ip4_address, &p.raddr.stop.ip4))
284 else if (unformat (line_input, "local-ip-range %U - %U",
285 unformat_ip6_address, &p.laddr.start.ip6,
286 unformat_ip6_address, &p.laddr.stop.ip6))
290 else if (unformat (line_input, "remote-ip-range %U - %U",
291 unformat_ip6_address, &p.raddr.start.ip6,
292 unformat_ip6_address, &p.raddr.stop.ip6))
296 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
302 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
309 error = clib_error_return (0, "parse error: '%U'",
310 format_unformat_error, line_input);
315 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
319 error = clib_error_return (0, "unsupported policy type for:",
320 " outboud:%s %s action:%U",
321 (is_outbound ? "yes" : "no"),
322 (p.is_ipv6 ? "IPv4" : "IPv6"),
323 format_ipsec_policy_action, p.policy);
327 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
330 vlib_cli_output (vm, "policy-index:%d", stat_index);
332 vlib_cli_output (vm, "error:%d", rv);
335 unformat_free (line_input);
341 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
342 .path = "ipsec policy",
344 "ipsec policy [add|del] spd <id> priority <n> ",
345 .function = ipsec_policy_add_del_command_fn,
350 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
355 pool_foreach_index (sai, im->sad, ({
356 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
357 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
363 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
368 pool_foreach_index (spdi, im->spds, ({
369 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
375 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
377 u32 spd_id, sw_if_index;
380 vlib_cli_output (vm, "SPD Bindings:");
383 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
384 spd = pool_elt_at_index (im->spds, spd_id);
385 vlib_cli_output (vm, " %d -> %U", spd->id,
386 format_vnet_sw_if_index_name, im->vnet_main,
393 ipsec_tun_protect_show_one (index_t itpi, void *ctx)
395 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
397 return (WALK_CONTINUE);
401 ipsec_tunnel_show_all (vlib_main_t * vm)
403 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
406 static clib_error_t *
407 show_ipsec_command_fn (vlib_main_t * vm,
408 unformat_input_t * input, vlib_cli_command_t * cmd)
410 ipsec_main_t *im = &ipsec_main;
412 ipsec_sa_show_all (vm, im, 0);
413 ipsec_spd_show_all (vm, im);
414 ipsec_spd_bindings_show_all (vm, im);
415 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
417 vlib_cli_output (vm, "IPSec async mode: %s",
418 (im->async_mode ? "on" : "off"));
424 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
425 .path = "show ipsec all",
426 .short_help = "show ipsec all",
427 .function = show_ipsec_command_fn,
431 static clib_error_t *
432 show_ipsec_sa_command_fn (vlib_main_t * vm,
433 unformat_input_t * input, vlib_cli_command_t * cmd)
435 ipsec_main_t *im = &ipsec_main;
439 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
441 if (unformat (input, "%u", &sai))
443 if (unformat (input, "detail"))
450 ipsec_sa_show_all (vm, im, detail);
452 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
453 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
458 static clib_error_t *
459 clear_ipsec_sa_command_fn (vlib_main_t * vm,
460 unformat_input_t * input, vlib_cli_command_t * cmd)
462 ipsec_main_t *im = &ipsec_main;
465 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
467 if (unformat (input, "%u", &sai))
476 pool_foreach_index (sai, im->sad, ({
483 if (pool_is_free_index (im->sad, sai))
484 return clib_error_return (0, "unknown SA index: %d", sai);
486 ipsec_sa_clear (sai);
493 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
494 .path = "show ipsec sa",
495 .short_help = "show ipsec sa [index]",
496 .function = show_ipsec_sa_command_fn,
499 VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
500 .path = "clear ipsec sa",
501 .short_help = "clear ipsec sa [index]",
502 .function = clear_ipsec_sa_command_fn,
506 static clib_error_t *
507 show_ipsec_spd_command_fn (vlib_main_t * vm,
508 unformat_input_t * input, vlib_cli_command_t * cmd)
510 ipsec_main_t *im = &ipsec_main;
511 u8 show_bindings = 0;
514 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
516 if (unformat (input, "%u", &spdi))
518 else if (unformat (input, "bindings"))
525 ipsec_spd_bindings_show_all (vm, im);
527 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
529 ipsec_spd_show_all (vm, im);
535 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
536 .path = "show ipsec spd",
537 .short_help = "show ipsec spd [index]",
538 .function = show_ipsec_spd_command_fn,
542 static clib_error_t *
543 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
544 unformat_input_t * input,
545 vlib_cli_command_t * cmd)
547 ipsec_tunnel_show_all (vm);
553 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
554 .path = "show ipsec tunnel",
555 .short_help = "show ipsec tunnel",
556 .function = show_ipsec_tunnel_command_fn,
560 static clib_error_t *
561 ipsec_show_backends_command_fn (vlib_main_t * vm,
562 unformat_input_t * input,
563 vlib_cli_command_t * cmd)
565 ipsec_main_t *im = &ipsec_main;
568 (void) unformat (input, "verbose %u", &verbose);
570 vlib_cli_output (vm, "IPsec AH backends available:");
571 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
572 ipsec_ah_backend_t *ab;
574 pool_foreach (ab, im->ah_backends, {
575 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
576 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
579 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
580 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
581 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
582 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
583 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
584 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
585 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
586 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
590 vlib_cli_output (vm, "%v", s);
592 vlib_cli_output (vm, "IPsec ESP backends available:");
593 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
594 ipsec_esp_backend_t *eb;
596 pool_foreach (eb, im->esp_backends, {
597 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
598 eb - im->esp_backends == im->esp_current_backend ? "yes"
602 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
603 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
604 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
605 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
606 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
607 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
608 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
609 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
613 vlib_cli_output (vm, "%v", s);
620 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
621 .path = "show ipsec backends",
622 .short_help = "show ipsec backends",
623 .function = ipsec_show_backends_command_fn,
627 static clib_error_t *
628 ipsec_select_backend_command_fn (vlib_main_t * vm,
629 unformat_input_t * input,
630 vlib_cli_command_t * cmd)
632 unformat_input_t _line_input, *line_input = &_line_input;
633 ipsec_main_t *im = &ipsec_main;
637 error = ipsec_rsc_in_use (im);
642 /* Get a line of input. */
643 if (!unformat_user (input, unformat_line_input, line_input))
646 if (unformat (line_input, "ah"))
648 if (unformat (line_input, "%u", &backend_index))
650 if (ipsec_select_ah_backend (im, backend_index) < 0)
652 return clib_error_return (0, "Invalid AH backend index `%u'",
658 return clib_error_return (0, "Invalid backend index `%U'",
659 format_unformat_error, line_input);
662 else if (unformat (line_input, "esp"))
664 if (unformat (line_input, "%u", &backend_index))
666 if (ipsec_select_esp_backend (im, backend_index) < 0)
668 return clib_error_return (0, "Invalid ESP backend index `%u'",
674 return clib_error_return (0, "Invalid backend index `%U'",
675 format_unformat_error, line_input);
680 return clib_error_return (0, "Unknown input `%U'",
681 format_unformat_error, line_input);
688 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
689 .path = "ipsec select backend",
690 .short_help = "ipsec select backend <ah|esp> <backend index>",
691 .function = ipsec_select_backend_command_fn,
696 static clib_error_t *
697 clear_ipsec_counters_command_fn (vlib_main_t * vm,
698 unformat_input_t * input,
699 vlib_cli_command_t * cmd)
701 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
702 vlib_clear_combined_counters (&ipsec_sa_counters);
708 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
709 .path = "clear ipsec counters",
710 .short_help = "clear ipsec counters",
711 .function = clear_ipsec_counters_command_fn,
716 ipsec_tun_mk_local_sa_id (u32 ti)
718 return (0x80000000 | ti);
722 ipsec_tun_mk_remote_sa_id (u32 ti)
724 return (0xc0000000 | ti);
727 static clib_error_t *
728 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
729 unformat_input_t * input,
730 vlib_cli_command_t * cmd)
732 unformat_input_t _line_input, *line_input = &_line_input;
733 ip46_address_t local_ip = ip46_address_initializer;
734 ip46_address_t remote_ip = ip46_address_initializer;
735 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
736 ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE;
737 ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE;
738 ipsec_sa_flags_t flags;
739 u32 local_spi, remote_spi, salt, table_id, fib_index;
746 clib_error_t *error = NULL;
747 ipsec_key_t rck = { 0 };
748 ipsec_key_t lck = { 0 };
749 ipsec_key_t lik = { 0 };
750 ipsec_key_t rik = { 0 };
753 flags = IPSEC_SA_FLAG_NONE;
755 /* Get a line of input. */
756 if (!unformat_user (input, unformat_line_input, line_input))
759 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
762 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
765 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
770 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
773 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
776 else if (unformat (line_input, "local-spi %u", &local_spi))
778 else if (unformat (line_input, "remote-spi %u", &remote_spi))
780 else if (unformat (line_input, "salt 0x%x", &salt))
782 else if (unformat (line_input, "udp-encap"))
783 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
784 else if (unformat (line_input, "use-esn"))
785 flags |= IPSEC_SA_FLAG_USE_ESN;
786 else if (unformat (line_input, "use-anti-replay"))
787 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
788 else if (unformat (line_input, "instance %u", &instance))
790 else if (unformat (line_input, "tx-table %u", &table_id))
794 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
798 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
800 else if (unformat (line_input, "crypto-alg %U",
801 unformat_ipsec_crypto_alg, &crypto_alg))
805 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
809 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
811 else if (unformat (line_input, "integ-alg %U",
812 unformat_ipsec_integ_alg, &integ_alg))
814 else if (unformat (line_input, "del"))
816 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
820 error = clib_error_return (0, "unknown input `%U'",
821 format_unformat_error, line_input);
828 error = clib_error_return (0, "mandatory argument(s) missing");
832 if (ipv4_set && ipv6_set)
833 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
835 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
839 rv = VNET_API_ERROR_NO_SUCH_FIB;
845 // remote = input, local = output
848 /* create an ip-ip tunnel, then the two SA, then bind them */
850 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
851 instance, &local_ip, &remote_ip, fib_index,
852 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
853 TUNNEL_MODE_P2P, &sw_if_index);
855 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
856 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
857 &lck, integ_alg, &lik, flags, table_id,
858 clib_host_to_net_u32 (salt), &local_ip,
859 &remote_ip, NULL, IPSEC_UDP_PORT_NONE);
861 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
862 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
863 &rck, integ_alg, &rik,
864 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
865 clib_host_to_net_u32 (salt), &remote_ip,
866 &local_ip, NULL, IPSEC_UDP_PORT_NONE);
868 ipsec_tun_protect_update_one (sw_if_index, &nh,
869 ipsec_tun_mk_local_sa_id (sw_if_index),
870 ipsec_tun_mk_remote_sa_id
880 case VNET_API_ERROR_INVALID_VALUE:
881 error = clib_error_return (0,
882 "IPSec tunnel interface already exists...");
885 error = clib_error_return (0, "ipsec_register_interface returned %d",
891 unformat_free (line_input);
897 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
898 .path = "create ipsec tunnel",
899 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
900 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
901 "[tx-table <table-id>]",
902 .function = create_ipsec_tunnel_command_fn,
906 static clib_error_t *
907 ipsec_tun_protect_cmd (vlib_main_t * vm,
908 unformat_input_t * input, vlib_cli_command_t * cmd)
910 unformat_input_t _line_input, *line_input = &_line_input;
911 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
912 ip_address_t peer = { };
917 vnm = vnet_get_main ();
919 if (!unformat_user (input, unformat_line_input, line_input))
922 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
924 if (unformat (line_input, "del"))
926 else if (unformat (line_input, "add"))
928 else if (unformat (line_input, "sa-in %d", &sa_in))
929 vec_add1 (sa_ins, sa_in);
930 else if (unformat (line_input, "sa-out %d", &sa_out))
932 else if (unformat (line_input, "%U",
933 unformat_vnet_sw_interface, vnm, &sw_if_index))
935 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
938 return (clib_error_return (0, "unknown input '%U'",
939 format_unformat_error, line_input));
943 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
945 unformat_free (line_input);
950 * Protect tunnel with IPSEC
953 VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
955 .path = "ipsec tunnel protect",
956 .function = ipsec_tun_protect_cmd,
957 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
958 // this is not MP safe
963 static clib_error_t *
964 ipsec_tun_protect_show (vlib_main_t * vm,
965 unformat_input_t * input, vlib_cli_command_t * cmd)
967 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
973 * show IPSEC tunnel protection
976 VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
978 .path = "show ipsec protect",
979 .function = ipsec_tun_protect_show,
980 .short_help = "show ipsec protect",
984 static clib_error_t *
985 ipsec_tun_protect_hash_show (vlib_main_t * vm,
986 unformat_input_t * input,
987 vlib_cli_command_t * cmd)
989 ipsec_main_t *im = &ipsec_main;
992 ipsec_tun_lkup_result_t value;
993 ipsec4_tunnel_key_t key;
995 vlib_cli_output (vm, "IPv4:");
998 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
1000 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
1001 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1007 ipsec_tun_lkup_result_t value;
1008 ipsec6_tunnel_key_t *key;
1010 vlib_cli_output (vm, "IPv6:");
1013 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1015 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1016 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1025 * show IPSEC tunnel protection hash tables
1028 VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1030 .path = "show ipsec protect-hash",
1031 .function = ipsec_tun_protect_hash_show,
1032 .short_help = "show ipsec protect-hash",
1037 ipsec_cli_init (vlib_main_t * vm)
1042 VLIB_INIT_FUNCTION (ipsec_cli_init);
1044 static clib_error_t *
1045 set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
1046 vlib_cli_command_t * cmd)
1048 unformat_input_t _line_input, *line_input = &_line_input;
1049 int async_enable = 0;
1051 if (!unformat_user (input, unformat_line_input, line_input))
1054 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1056 if (unformat (line_input, "on"))
1058 else if (unformat (line_input, "off"))
1061 return (clib_error_return (0, "unknown input '%U'",
1062 format_unformat_error, line_input));
1065 vnet_crypto_request_async_mode (async_enable);
1066 ipsec_set_async_mode (async_enable);
1068 unformat_free (line_input);
1073 VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1074 .path = "set ipsec async mode",
1075 .short_help = "set ipsec async mode on|off",
1076 .function = set_async_mode_command_fn,
1081 * fd.io coding-style-patch-verification: ON
1084 * eval: (c-set-style "gnu")