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, local_range_set, remote_range_set;
238 clib_error_t *error = NULL;
241 clib_memset (&p, 0, sizeof (p));
242 p.lport.stop = p.rport.stop = ~0;
243 remote_range_set = local_range_set = is_outbound = 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, "ip6"))
256 else if (unformat (line_input, "spd %u", &p.id))
258 else if (unformat (line_input, "inbound"))
260 else if (unformat (line_input, "outbound"))
262 else if (unformat (line_input, "priority %d", &p.priority))
264 else if (unformat (line_input, "protocol %u", &tmp))
265 p.protocol = (u8) tmp;
268 (line_input, "action %U", unformat_ipsec_policy_action,
271 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
273 error = clib_error_return (0, "unsupported action: 'resolve'");
277 else if (unformat (line_input, "sa %u", &p.sa_id))
279 else if (unformat (line_input, "local-ip-range %U - %U",
280 unformat_ip4_address, &p.laddr.start.ip4,
281 unformat_ip4_address, &p.laddr.stop.ip4))
283 else if (unformat (line_input, "remote-ip-range %U - %U",
284 unformat_ip4_address, &p.raddr.start.ip4,
285 unformat_ip4_address, &p.raddr.stop.ip4))
286 remote_range_set = 1;
287 else if (unformat (line_input, "local-ip-range %U - %U",
288 unformat_ip6_address, &p.laddr.start.ip6,
289 unformat_ip6_address, &p.laddr.stop.ip6))
294 else if (unformat (line_input, "remote-ip-range %U - %U",
295 unformat_ip6_address, &p.raddr.start.ip6,
296 unformat_ip6_address, &p.raddr.stop.ip6))
299 remote_range_set = 1;
301 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
307 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
314 error = clib_error_return (0, "parse error: '%U'",
315 format_unformat_error, line_input);
320 if (!remote_range_set)
323 clib_memset (&p.raddr.stop.ip6, 0xff, 16);
325 clib_memset (&p.raddr.stop.ip4, 0xff, 4);
327 if (!local_range_set)
330 clib_memset (&p.laddr.stop.ip6, 0xff, 16);
332 clib_memset (&p.laddr.stop.ip4, 0xff, 4);
335 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
339 error = clib_error_return (0, "unsupported policy type for:",
340 " outboud:%s %s action:%U",
341 (is_outbound ? "yes" : "no"),
342 (p.is_ipv6 ? "IPv4" : "IPv6"),
343 format_ipsec_policy_action, p.policy);
347 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
350 vlib_cli_output (vm, "policy-index:%d", stat_index);
352 vlib_cli_output (vm, "error:%d", rv);
355 unformat_free (line_input);
361 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
362 .path = "ipsec policy",
364 "ipsec policy [add|del] spd <id> priority <n> ",
365 .function = ipsec_policy_add_del_command_fn,
370 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
375 pool_foreach_index (sai, im->sad, ({
376 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
377 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
383 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
388 pool_foreach_index (spdi, im->spds, ({
389 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
395 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
397 u32 spd_id, sw_if_index;
400 vlib_cli_output (vm, "SPD Bindings:");
403 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
404 spd = pool_elt_at_index (im->spds, spd_id);
405 vlib_cli_output (vm, " %d -> %U", spd->id,
406 format_vnet_sw_if_index_name, im->vnet_main,
413 ipsec_tun_protect_show_one (index_t itpi, void *ctx)
415 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
417 return (WALK_CONTINUE);
421 ipsec_tunnel_show_all (vlib_main_t * vm)
423 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
426 static clib_error_t *
427 show_ipsec_command_fn (vlib_main_t * vm,
428 unformat_input_t * input, vlib_cli_command_t * cmd)
430 ipsec_main_t *im = &ipsec_main;
432 ipsec_sa_show_all (vm, im, 0);
433 ipsec_spd_show_all (vm, im);
434 ipsec_spd_bindings_show_all (vm, im);
435 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
437 vlib_cli_output (vm, "IPSec async mode: %s",
438 (im->async_mode ? "on" : "off"));
444 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
445 .path = "show ipsec all",
446 .short_help = "show ipsec all",
447 .function = show_ipsec_command_fn,
451 static clib_error_t *
452 show_ipsec_sa_command_fn (vlib_main_t * vm,
453 unformat_input_t * input, vlib_cli_command_t * cmd)
455 ipsec_main_t *im = &ipsec_main;
459 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
461 if (unformat (input, "%u", &sai))
463 if (unformat (input, "detail"))
470 ipsec_sa_show_all (vm, im, detail);
472 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
473 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
478 static clib_error_t *
479 clear_ipsec_sa_command_fn (vlib_main_t * vm,
480 unformat_input_t * input, vlib_cli_command_t * cmd)
482 ipsec_main_t *im = &ipsec_main;
485 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
487 if (unformat (input, "%u", &sai))
496 pool_foreach_index (sai, im->sad, ({
503 if (pool_is_free_index (im->sad, sai))
504 return clib_error_return (0, "unknown SA index: %d", sai);
506 ipsec_sa_clear (sai);
513 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
514 .path = "show ipsec sa",
515 .short_help = "show ipsec sa [index]",
516 .function = show_ipsec_sa_command_fn,
519 VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
520 .path = "clear ipsec sa",
521 .short_help = "clear ipsec sa [index]",
522 .function = clear_ipsec_sa_command_fn,
526 static clib_error_t *
527 show_ipsec_spd_command_fn (vlib_main_t * vm,
528 unformat_input_t * input, vlib_cli_command_t * cmd)
530 ipsec_main_t *im = &ipsec_main;
531 u8 show_bindings = 0;
534 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
536 if (unformat (input, "%u", &spdi))
538 else if (unformat (input, "bindings"))
545 ipsec_spd_bindings_show_all (vm, im);
547 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
549 ipsec_spd_show_all (vm, im);
555 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
556 .path = "show ipsec spd",
557 .short_help = "show ipsec spd [index]",
558 .function = show_ipsec_spd_command_fn,
562 static clib_error_t *
563 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
564 unformat_input_t * input,
565 vlib_cli_command_t * cmd)
567 ipsec_tunnel_show_all (vm);
573 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
574 .path = "show ipsec tunnel",
575 .short_help = "show ipsec tunnel",
576 .function = show_ipsec_tunnel_command_fn,
580 static clib_error_t *
581 ipsec_show_backends_command_fn (vlib_main_t * vm,
582 unformat_input_t * input,
583 vlib_cli_command_t * cmd)
585 ipsec_main_t *im = &ipsec_main;
588 (void) unformat (input, "verbose %u", &verbose);
590 vlib_cli_output (vm, "IPsec AH backends available:");
591 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
592 ipsec_ah_backend_t *ab;
594 pool_foreach (ab, im->ah_backends, {
595 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
596 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
599 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
600 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
601 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
602 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
603 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
604 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
605 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
606 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
610 vlib_cli_output (vm, "%v", s);
612 vlib_cli_output (vm, "IPsec ESP backends available:");
613 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
614 ipsec_esp_backend_t *eb;
616 pool_foreach (eb, im->esp_backends, {
617 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
618 eb - im->esp_backends == im->esp_current_backend ? "yes"
622 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
623 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
624 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
625 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
626 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
627 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
628 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
629 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
633 vlib_cli_output (vm, "%v", s);
640 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
641 .path = "show ipsec backends",
642 .short_help = "show ipsec backends",
643 .function = ipsec_show_backends_command_fn,
647 static clib_error_t *
648 ipsec_select_backend_command_fn (vlib_main_t * vm,
649 unformat_input_t * input,
650 vlib_cli_command_t * cmd)
652 unformat_input_t _line_input, *line_input = &_line_input;
653 ipsec_main_t *im = &ipsec_main;
657 error = ipsec_rsc_in_use (im);
662 /* Get a line of input. */
663 if (!unformat_user (input, unformat_line_input, line_input))
666 if (unformat (line_input, "ah"))
668 if (unformat (line_input, "%u", &backend_index))
670 if (ipsec_select_ah_backend (im, backend_index) < 0)
672 return clib_error_return (0, "Invalid AH backend index `%u'",
678 return clib_error_return (0, "Invalid backend index `%U'",
679 format_unformat_error, line_input);
682 else if (unformat (line_input, "esp"))
684 if (unformat (line_input, "%u", &backend_index))
686 if (ipsec_select_esp_backend (im, backend_index) < 0)
688 return clib_error_return (0, "Invalid ESP backend index `%u'",
694 return clib_error_return (0, "Invalid backend index `%U'",
695 format_unformat_error, line_input);
700 return clib_error_return (0, "Unknown input `%U'",
701 format_unformat_error, line_input);
708 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
709 .path = "ipsec select backend",
710 .short_help = "ipsec select backend <ah|esp> <backend index>",
711 .function = ipsec_select_backend_command_fn,
716 static clib_error_t *
717 clear_ipsec_counters_command_fn (vlib_main_t * vm,
718 unformat_input_t * input,
719 vlib_cli_command_t * cmd)
721 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
722 vlib_clear_combined_counters (&ipsec_sa_counters);
728 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
729 .path = "clear ipsec counters",
730 .short_help = "clear ipsec counters",
731 .function = clear_ipsec_counters_command_fn,
736 ipsec_tun_mk_local_sa_id (u32 ti)
738 return (0x80000000 | ti);
742 ipsec_tun_mk_remote_sa_id (u32 ti)
744 return (0xc0000000 | ti);
747 static clib_error_t *
748 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
749 unformat_input_t * input,
750 vlib_cli_command_t * cmd)
752 unformat_input_t _line_input, *line_input = &_line_input;
753 ip46_address_t local_ip = ip46_address_initializer;
754 ip46_address_t remote_ip = ip46_address_initializer;
755 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
756 ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE;
757 ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE;
758 ipsec_sa_flags_t flags;
759 u32 local_spi, remote_spi, salt, table_id, fib_index;
766 clib_error_t *error = NULL;
767 ipsec_key_t rck = { 0 };
768 ipsec_key_t lck = { 0 };
769 ipsec_key_t lik = { 0 };
770 ipsec_key_t rik = { 0 };
773 flags = IPSEC_SA_FLAG_NONE;
775 /* Get a line of input. */
776 if (!unformat_user (input, unformat_line_input, line_input))
779 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
782 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
785 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
790 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
793 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
796 else if (unformat (line_input, "local-spi %u", &local_spi))
798 else if (unformat (line_input, "remote-spi %u", &remote_spi))
800 else if (unformat (line_input, "salt 0x%x", &salt))
802 else if (unformat (line_input, "udp-encap"))
803 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
804 else if (unformat (line_input, "use-esn"))
805 flags |= IPSEC_SA_FLAG_USE_ESN;
806 else if (unformat (line_input, "use-anti-replay"))
807 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
808 else if (unformat (line_input, "instance %u", &instance))
810 else if (unformat (line_input, "tx-table %u", &table_id))
814 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
818 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
820 else if (unformat (line_input, "crypto-alg %U",
821 unformat_ipsec_crypto_alg, &crypto_alg))
825 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
829 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
831 else if (unformat (line_input, "integ-alg %U",
832 unformat_ipsec_integ_alg, &integ_alg))
834 else if (unformat (line_input, "del"))
836 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
840 error = clib_error_return (0, "unknown input `%U'",
841 format_unformat_error, line_input);
848 error = clib_error_return (0, "mandatory argument(s) missing");
852 if (ipv4_set && ipv6_set)
853 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
855 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
859 rv = VNET_API_ERROR_NO_SUCH_FIB;
865 // remote = input, local = output
868 /* create an ip-ip tunnel, then the two SA, then bind them */
870 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
871 instance, &local_ip, &remote_ip, fib_index,
872 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
873 TUNNEL_MODE_P2P, &sw_if_index);
875 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
876 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
877 &lck, integ_alg, &lik, flags, table_id,
878 clib_host_to_net_u32 (salt), &local_ip,
879 &remote_ip, NULL, IPSEC_UDP_PORT_NONE,
880 IPSEC_UDP_PORT_NONE);
882 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
883 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
884 &rck, integ_alg, &rik,
885 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
886 clib_host_to_net_u32 (salt), &remote_ip,
887 &local_ip, NULL, IPSEC_UDP_PORT_NONE,
888 IPSEC_UDP_PORT_NONE);
890 ipsec_tun_protect_update_one (sw_if_index, &nh,
891 ipsec_tun_mk_local_sa_id (sw_if_index),
892 ipsec_tun_mk_remote_sa_id
902 case VNET_API_ERROR_INVALID_VALUE:
903 error = clib_error_return (0,
904 "IPSec tunnel interface already exists...");
907 error = clib_error_return (0, "ipsec_register_interface returned %d",
913 unformat_free (line_input);
919 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
920 .path = "create ipsec tunnel",
921 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
922 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
923 "[tx-table <table-id>]",
924 .function = create_ipsec_tunnel_command_fn,
928 static clib_error_t *
929 ipsec_tun_protect_cmd (vlib_main_t * vm,
930 unformat_input_t * input, vlib_cli_command_t * cmd)
932 unformat_input_t _line_input, *line_input = &_line_input;
933 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
934 ip_address_t peer = { };
939 vnm = vnet_get_main ();
941 if (!unformat_user (input, unformat_line_input, line_input))
944 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
946 if (unformat (line_input, "del"))
948 else if (unformat (line_input, "add"))
950 else if (unformat (line_input, "sa-in %d", &sa_in))
951 vec_add1 (sa_ins, sa_in);
952 else if (unformat (line_input, "sa-out %d", &sa_out))
954 else if (unformat (line_input, "%U",
955 unformat_vnet_sw_interface, vnm, &sw_if_index))
957 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
960 return (clib_error_return (0, "unknown input '%U'",
961 format_unformat_error, line_input));
965 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
967 unformat_free (line_input);
972 * Protect tunnel with IPSEC
975 VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
977 .path = "ipsec tunnel protect",
978 .function = ipsec_tun_protect_cmd,
979 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
980 // this is not MP safe
985 static clib_error_t *
986 ipsec_tun_protect_show (vlib_main_t * vm,
987 unformat_input_t * input, vlib_cli_command_t * cmd)
989 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
995 * show IPSEC tunnel protection
998 VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
1000 .path = "show ipsec protect",
1001 .function = ipsec_tun_protect_show,
1002 .short_help = "show ipsec protect",
1006 static clib_error_t *
1007 ipsec_tun_protect_hash_show (vlib_main_t * vm,
1008 unformat_input_t * input,
1009 vlib_cli_command_t * cmd)
1011 ipsec_main_t *im = &ipsec_main;
1014 ipsec_tun_lkup_result_t value;
1015 ipsec4_tunnel_key_t key;
1017 vlib_cli_output (vm, "IPv4:");
1020 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
1022 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
1023 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1029 ipsec_tun_lkup_result_t value;
1030 ipsec6_tunnel_key_t *key;
1032 vlib_cli_output (vm, "IPv6:");
1035 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1037 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1038 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1047 * show IPSEC tunnel protection hash tables
1050 VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1052 .path = "show ipsec protect-hash",
1053 .function = ipsec_tun_protect_hash_show,
1054 .short_help = "show ipsec protect-hash",
1059 ipsec_cli_init (vlib_main_t * vm)
1064 VLIB_INIT_FUNCTION (ipsec_cli_init);
1066 static clib_error_t *
1067 set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
1068 vlib_cli_command_t * cmd)
1070 unformat_input_t _line_input, *line_input = &_line_input;
1071 int async_enable = 0;
1073 if (!unformat_user (input, unformat_line_input, line_input))
1076 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1078 if (unformat (line_input, "on"))
1080 else if (unformat (line_input, "off"))
1083 return (clib_error_return (0, "unknown input '%U'",
1084 format_unformat_error, line_input));
1087 vnet_crypto_request_async_mode (async_enable);
1088 ipsec_set_async_mode (async_enable);
1090 unformat_free (line_input);
1095 VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1096 .path = "set ipsec async mode",
1097 .short_help = "set ipsec async mode on|off",
1098 .function = set_async_mode_command_fn,
1103 * fd.io coding-style-patch-verification: ON
1106 * eval: (c-set-style "gnu")