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 };
89 u32 id, spi, salt, sai;
96 flags = IPSEC_SA_FLAG_NONE;
97 proto = IPSEC_PROTOCOL_ESP;
98 integ_alg = IPSEC_INTEG_ALG_NONE;
99 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
100 udp_src = udp_dst = IPSEC_UDP_PORT_NONE;
102 if (!unformat_user (input, unformat_line_input, line_input))
105 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
107 if (unformat (line_input, "add %u", &id))
109 else if (unformat (line_input, "del %u", &id))
111 else if (unformat (line_input, "spi %u", &spi))
113 else if (unformat (line_input, "salt 0x%x", &salt))
115 else if (unformat (line_input, "esp"))
116 proto = IPSEC_PROTOCOL_ESP;
117 else if (unformat (line_input, "ah"))
118 proto = IPSEC_PROTOCOL_AH;
119 else if (unformat (line_input, "crypto-key %U",
120 unformat_ipsec_key, &ck))
122 else if (unformat (line_input, "crypto-alg %U",
123 unformat_ipsec_crypto_alg, &crypto_alg))
125 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
127 else if (unformat (line_input, "integ-alg %U",
128 unformat_ipsec_integ_alg, &integ_alg))
130 else if (unformat (line_input, "tunnel-src %U",
131 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
133 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
134 if (!ip46_address_is_ip4 (&tun_src))
135 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
137 else if (unformat (line_input, "tunnel-dst %U",
138 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
140 else if (unformat (line_input, "udp-encap"))
141 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
144 error = clib_error_return (0, "parse error: '%U'",
145 format_unformat_error, line_input);
151 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
152 &ck, integ_alg, &ik, flags,
153 0, clib_host_to_net_u32 (salt),
154 &tun_src, &tun_dst, &sai, udp_src, udp_dst);
156 rv = ipsec_sa_unlock_id (id);
159 error = clib_error_return (0, "failed");
162 unformat_free (line_input);
168 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
171 "ipsec sa [add|del]",
172 .function = ipsec_sa_add_del_command_fn,
176 static clib_error_t *
177 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
178 unformat_input_t * input,
179 vlib_cli_command_t * cmd)
181 unformat_input_t _line_input, *line_input = &_line_input;
184 clib_error_t *error = NULL;
186 if (!unformat_user (input, unformat_line_input, line_input))
189 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
191 if (unformat (line_input, "add"))
193 else if (unformat (line_input, "del"))
195 else if (unformat (line_input, "%u", &spd_id))
199 error = clib_error_return (0, "parse error: '%U'",
200 format_unformat_error, line_input);
207 error = clib_error_return (0, "please specify SPD ID");
211 ipsec_add_del_spd (vm, spd_id, is_add);
214 unformat_free (line_input);
220 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
223 "ipsec spd [add|del] <id>",
224 .function = ipsec_spd_add_del_command_fn,
229 static clib_error_t *
230 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
231 unformat_input_t * input,
232 vlib_cli_command_t * cmd)
234 unformat_input_t _line_input, *line_input = &_line_input;
237 u32 tmp, tmp2, stat_index;
238 clib_error_t *error = NULL;
241 clib_memset (&p, 0, sizeof (p));
242 p.lport.stop = p.rport.stop = ~0;
245 if (!unformat_user (input, unformat_line_input, line_input))
248 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
250 if (unformat (line_input, "add"))
252 else if (unformat (line_input, "del"))
254 else if (unformat (line_input, "spd %u", &p.id))
256 else if (unformat (line_input, "inbound"))
258 else if (unformat (line_input, "outbound"))
260 else if (unformat (line_input, "priority %d", &p.priority))
262 else if (unformat (line_input, "protocol %u", &tmp))
263 p.protocol = (u8) tmp;
266 (line_input, "action %U", unformat_ipsec_policy_action,
269 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
271 error = clib_error_return (0, "unsupported action: 'resolve'");
275 else if (unformat (line_input, "sa %u", &p.sa_id))
277 else if (unformat (line_input, "local-ip-range %U - %U",
278 unformat_ip4_address, &p.laddr.start.ip4,
279 unformat_ip4_address, &p.laddr.stop.ip4))
281 else if (unformat (line_input, "remote-ip-range %U - %U",
282 unformat_ip4_address, &p.raddr.start.ip4,
283 unformat_ip4_address, &p.raddr.stop.ip4))
285 else if (unformat (line_input, "local-ip-range %U - %U",
286 unformat_ip6_address, &p.laddr.start.ip6,
287 unformat_ip6_address, &p.laddr.stop.ip6))
291 else if (unformat (line_input, "remote-ip-range %U - %U",
292 unformat_ip6_address, &p.raddr.start.ip6,
293 unformat_ip6_address, &p.raddr.stop.ip6))
297 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
303 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
310 error = clib_error_return (0, "parse error: '%U'",
311 format_unformat_error, line_input);
316 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
320 error = clib_error_return (0, "unsupported policy type for:",
321 " outboud:%s %s action:%U",
322 (is_outbound ? "yes" : "no"),
323 (p.is_ipv6 ? "IPv4" : "IPv6"),
324 format_ipsec_policy_action, p.policy);
328 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
331 vlib_cli_output (vm, "policy-index:%d", stat_index);
333 vlib_cli_output (vm, "error:%d", rv);
336 unformat_free (line_input);
342 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
343 .path = "ipsec policy",
345 "ipsec policy [add|del] spd <id> priority <n> ",
346 .function = ipsec_policy_add_del_command_fn,
351 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
356 pool_foreach_index (sai, im->sad, ({
357 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
358 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
364 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
369 pool_foreach_index (spdi, im->spds, ({
370 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
376 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
378 u32 spd_id, sw_if_index;
381 vlib_cli_output (vm, "SPD Bindings:");
384 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
385 spd = pool_elt_at_index (im->spds, spd_id);
386 vlib_cli_output (vm, " %d -> %U", spd->id,
387 format_vnet_sw_if_index_name, im->vnet_main,
394 ipsec_tun_protect_show_one (index_t itpi, void *ctx)
396 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
398 return (WALK_CONTINUE);
402 ipsec_tunnel_show_all (vlib_main_t * vm)
404 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
407 static clib_error_t *
408 show_ipsec_command_fn (vlib_main_t * vm,
409 unformat_input_t * input, vlib_cli_command_t * cmd)
411 ipsec_main_t *im = &ipsec_main;
413 ipsec_sa_show_all (vm, im, 0);
414 ipsec_spd_show_all (vm, im);
415 ipsec_spd_bindings_show_all (vm, im);
416 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
418 vlib_cli_output (vm, "IPSec async mode: %s",
419 (im->async_mode ? "on" : "off"));
425 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
426 .path = "show ipsec all",
427 .short_help = "show ipsec all",
428 .function = show_ipsec_command_fn,
432 static clib_error_t *
433 show_ipsec_sa_command_fn (vlib_main_t * vm,
434 unformat_input_t * input, vlib_cli_command_t * cmd)
436 ipsec_main_t *im = &ipsec_main;
440 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
442 if (unformat (input, "%u", &sai))
444 if (unformat (input, "detail"))
451 ipsec_sa_show_all (vm, im, detail);
453 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
454 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
459 static clib_error_t *
460 clear_ipsec_sa_command_fn (vlib_main_t * vm,
461 unformat_input_t * input, vlib_cli_command_t * cmd)
463 ipsec_main_t *im = &ipsec_main;
466 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
468 if (unformat (input, "%u", &sai))
477 pool_foreach_index (sai, im->sad, ({
484 if (pool_is_free_index (im->sad, sai))
485 return clib_error_return (0, "unknown SA index: %d", sai);
487 ipsec_sa_clear (sai);
494 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
495 .path = "show ipsec sa",
496 .short_help = "show ipsec sa [index]",
497 .function = show_ipsec_sa_command_fn,
500 VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
501 .path = "clear ipsec sa",
502 .short_help = "clear ipsec sa [index]",
503 .function = clear_ipsec_sa_command_fn,
507 static clib_error_t *
508 show_ipsec_spd_command_fn (vlib_main_t * vm,
509 unformat_input_t * input, vlib_cli_command_t * cmd)
511 ipsec_main_t *im = &ipsec_main;
512 u8 show_bindings = 0;
515 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
517 if (unformat (input, "%u", &spdi))
519 else if (unformat (input, "bindings"))
526 ipsec_spd_bindings_show_all (vm, im);
528 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
530 ipsec_spd_show_all (vm, im);
536 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
537 .path = "show ipsec spd",
538 .short_help = "show ipsec spd [index]",
539 .function = show_ipsec_spd_command_fn,
543 static clib_error_t *
544 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
545 unformat_input_t * input,
546 vlib_cli_command_t * cmd)
548 ipsec_tunnel_show_all (vm);
554 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
555 .path = "show ipsec tunnel",
556 .short_help = "show ipsec tunnel",
557 .function = show_ipsec_tunnel_command_fn,
561 static clib_error_t *
562 ipsec_show_backends_command_fn (vlib_main_t * vm,
563 unformat_input_t * input,
564 vlib_cli_command_t * cmd)
566 ipsec_main_t *im = &ipsec_main;
569 (void) unformat (input, "verbose %u", &verbose);
571 vlib_cli_output (vm, "IPsec AH backends available:");
572 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
573 ipsec_ah_backend_t *ab;
575 pool_foreach (ab, im->ah_backends, {
576 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
577 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
580 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
581 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
582 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
583 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
584 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
585 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
586 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
587 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
591 vlib_cli_output (vm, "%v", s);
593 vlib_cli_output (vm, "IPsec ESP backends available:");
594 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
595 ipsec_esp_backend_t *eb;
597 pool_foreach (eb, im->esp_backends, {
598 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
599 eb - im->esp_backends == im->esp_current_backend ? "yes"
603 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
604 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
605 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
606 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
607 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
608 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
609 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
610 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
614 vlib_cli_output (vm, "%v", s);
621 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
622 .path = "show ipsec backends",
623 .short_help = "show ipsec backends",
624 .function = ipsec_show_backends_command_fn,
628 static clib_error_t *
629 ipsec_select_backend_command_fn (vlib_main_t * vm,
630 unformat_input_t * input,
631 vlib_cli_command_t * cmd)
633 unformat_input_t _line_input, *line_input = &_line_input;
634 ipsec_main_t *im = &ipsec_main;
638 error = ipsec_rsc_in_use (im);
643 /* Get a line of input. */
644 if (!unformat_user (input, unformat_line_input, line_input))
647 if (unformat (line_input, "ah"))
649 if (unformat (line_input, "%u", &backend_index))
651 if (ipsec_select_ah_backend (im, backend_index) < 0)
653 return clib_error_return (0, "Invalid AH backend index `%u'",
659 return clib_error_return (0, "Invalid backend index `%U'",
660 format_unformat_error, line_input);
663 else if (unformat (line_input, "esp"))
665 if (unformat (line_input, "%u", &backend_index))
667 if (ipsec_select_esp_backend (im, backend_index) < 0)
669 return clib_error_return (0, "Invalid ESP backend index `%u'",
675 return clib_error_return (0, "Invalid backend index `%U'",
676 format_unformat_error, line_input);
681 return clib_error_return (0, "Unknown input `%U'",
682 format_unformat_error, line_input);
689 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
690 .path = "ipsec select backend",
691 .short_help = "ipsec select backend <ah|esp> <backend index>",
692 .function = ipsec_select_backend_command_fn,
697 static clib_error_t *
698 clear_ipsec_counters_command_fn (vlib_main_t * vm,
699 unformat_input_t * input,
700 vlib_cli_command_t * cmd)
702 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
703 vlib_clear_combined_counters (&ipsec_sa_counters);
709 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
710 .path = "clear ipsec counters",
711 .short_help = "clear ipsec counters",
712 .function = clear_ipsec_counters_command_fn,
717 ipsec_tun_mk_local_sa_id (u32 ti)
719 return (0x80000000 | ti);
723 ipsec_tun_mk_remote_sa_id (u32 ti)
725 return (0xc0000000 | ti);
728 static clib_error_t *
729 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
730 unformat_input_t * input,
731 vlib_cli_command_t * cmd)
733 unformat_input_t _line_input, *line_input = &_line_input;
734 ip46_address_t local_ip = ip46_address_initializer;
735 ip46_address_t remote_ip = ip46_address_initializer;
736 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
737 ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE;
738 ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE;
739 ipsec_sa_flags_t flags;
740 u32 local_spi, remote_spi, salt, table_id, fib_index;
747 clib_error_t *error = NULL;
748 ipsec_key_t rck = { 0 };
749 ipsec_key_t lck = { 0 };
750 ipsec_key_t lik = { 0 };
751 ipsec_key_t rik = { 0 };
754 flags = IPSEC_SA_FLAG_NONE;
756 /* Get a line of input. */
757 if (!unformat_user (input, unformat_line_input, line_input))
760 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
763 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
766 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
771 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
774 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
777 else if (unformat (line_input, "local-spi %u", &local_spi))
779 else if (unformat (line_input, "remote-spi %u", &remote_spi))
781 else if (unformat (line_input, "salt 0x%x", &salt))
783 else if (unformat (line_input, "udp-encap"))
784 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
785 else if (unformat (line_input, "use-esn"))
786 flags |= IPSEC_SA_FLAG_USE_ESN;
787 else if (unformat (line_input, "use-anti-replay"))
788 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
789 else if (unformat (line_input, "instance %u", &instance))
791 else if (unformat (line_input, "tx-table %u", &table_id))
795 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
799 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
801 else if (unformat (line_input, "crypto-alg %U",
802 unformat_ipsec_crypto_alg, &crypto_alg))
806 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
810 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
812 else if (unformat (line_input, "integ-alg %U",
813 unformat_ipsec_integ_alg, &integ_alg))
815 else if (unformat (line_input, "del"))
817 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
821 error = clib_error_return (0, "unknown input `%U'",
822 format_unformat_error, line_input);
829 error = clib_error_return (0, "mandatory argument(s) missing");
833 if (ipv4_set && ipv6_set)
834 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
836 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
840 rv = VNET_API_ERROR_NO_SUCH_FIB;
846 // remote = input, local = output
849 /* create an ip-ip tunnel, then the two SA, then bind them */
851 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
852 instance, &local_ip, &remote_ip, fib_index,
853 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
854 TUNNEL_MODE_P2P, &sw_if_index);
856 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
857 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
858 &lck, integ_alg, &lik, flags, table_id,
859 clib_host_to_net_u32 (salt), &local_ip,
860 &remote_ip, NULL, IPSEC_UDP_PORT_NONE,
861 IPSEC_UDP_PORT_NONE);
863 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
864 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
865 &rck, integ_alg, &rik,
866 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
867 clib_host_to_net_u32 (salt), &remote_ip,
868 &local_ip, NULL, IPSEC_UDP_PORT_NONE,
869 IPSEC_UDP_PORT_NONE);
871 ipsec_tun_protect_update_one (sw_if_index, &nh,
872 ipsec_tun_mk_local_sa_id (sw_if_index),
873 ipsec_tun_mk_remote_sa_id
883 case VNET_API_ERROR_INVALID_VALUE:
884 error = clib_error_return (0,
885 "IPSec tunnel interface already exists...");
888 error = clib_error_return (0, "ipsec_register_interface returned %d",
894 unformat_free (line_input);
900 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
901 .path = "create ipsec tunnel",
902 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
903 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
904 "[tx-table <table-id>]",
905 .function = create_ipsec_tunnel_command_fn,
909 static clib_error_t *
910 ipsec_tun_protect_cmd (vlib_main_t * vm,
911 unformat_input_t * input, vlib_cli_command_t * cmd)
913 unformat_input_t _line_input, *line_input = &_line_input;
914 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
915 ip_address_t peer = { };
920 vnm = vnet_get_main ();
922 if (!unformat_user (input, unformat_line_input, line_input))
925 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
927 if (unformat (line_input, "del"))
929 else if (unformat (line_input, "add"))
931 else if (unformat (line_input, "sa-in %d", &sa_in))
932 vec_add1 (sa_ins, sa_in);
933 else if (unformat (line_input, "sa-out %d", &sa_out))
935 else if (unformat (line_input, "%U",
936 unformat_vnet_sw_interface, vnm, &sw_if_index))
938 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
941 return (clib_error_return (0, "unknown input '%U'",
942 format_unformat_error, line_input));
946 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
948 unformat_free (line_input);
953 * Protect tunnel with IPSEC
956 VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
958 .path = "ipsec tunnel protect",
959 .function = ipsec_tun_protect_cmd,
960 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
961 // this is not MP safe
966 static clib_error_t *
967 ipsec_tun_protect_show (vlib_main_t * vm,
968 unformat_input_t * input, vlib_cli_command_t * cmd)
970 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
976 * show IPSEC tunnel protection
979 VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
981 .path = "show ipsec protect",
982 .function = ipsec_tun_protect_show,
983 .short_help = "show ipsec protect",
987 static clib_error_t *
988 ipsec_tun_protect_hash_show (vlib_main_t * vm,
989 unformat_input_t * input,
990 vlib_cli_command_t * cmd)
992 ipsec_main_t *im = &ipsec_main;
995 ipsec_tun_lkup_result_t value;
996 ipsec4_tunnel_key_t key;
998 vlib_cli_output (vm, "IPv4:");
1001 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
1003 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
1004 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1010 ipsec_tun_lkup_result_t value;
1011 ipsec6_tunnel_key_t *key;
1013 vlib_cli_output (vm, "IPv6:");
1016 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1018 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1019 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1028 * show IPSEC tunnel protection hash tables
1031 VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1033 .path = "show ipsec protect-hash",
1034 .function = ipsec_tun_protect_hash_show,
1035 .short_help = "show ipsec protect-hash",
1040 ipsec_cli_init (vlib_main_t * vm)
1045 VLIB_INIT_FUNCTION (ipsec_cli_init);
1047 static clib_error_t *
1048 set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
1049 vlib_cli_command_t * cmd)
1051 unformat_input_t _line_input, *line_input = &_line_input;
1052 int async_enable = 0;
1054 if (!unformat_user (input, unformat_line_input, line_input))
1057 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1059 if (unformat (line_input, "on"))
1061 else if (unformat (line_input, "off"))
1064 return (clib_error_return (0, "unknown input '%U'",
1065 format_unformat_error, line_input));
1068 vnet_crypto_request_async_mode (async_enable);
1069 ipsec_set_async_mode (async_enable);
1071 unformat_free (line_input);
1076 VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1077 .path = "set ipsec async mode",
1078 .short_help = "set ipsec async mode on|off",
1079 .function = set_async_mode_command_fn,
1084 * fd.io coding-style-patch-verification: ON
1087 * eval: (c-set-style "gnu")