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;
97 flags = IPSEC_SA_FLAG_NONE;
98 proto = IPSEC_PROTOCOL_ESP;
99 integ_alg = IPSEC_INTEG_ALG_NONE;
100 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
101 udp_src = udp_dst = IPSEC_UDP_PORT_NONE;
103 if (!unformat_user (input, unformat_line_input, line_input))
106 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
108 if (unformat (line_input, "add %u", &id))
113 else if (unformat (line_input, "del %u", &id))
118 else if (unformat (line_input, "spi %u", &spi))
120 else if (unformat (line_input, "salt 0x%x", &salt))
122 else if (unformat (line_input, "esp"))
123 proto = IPSEC_PROTOCOL_ESP;
124 else if (unformat (line_input, "ah"))
125 proto = IPSEC_PROTOCOL_AH;
126 else if (unformat (line_input, "crypto-key %U",
127 unformat_ipsec_key, &ck))
129 else if (unformat (line_input, "crypto-alg %U",
130 unformat_ipsec_crypto_alg, &crypto_alg))
132 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
134 else if (unformat (line_input, "integ-alg %U",
135 unformat_ipsec_integ_alg, &integ_alg))
137 else if (unformat (line_input, "tunnel-src %U",
138 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
140 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
141 if (!ip46_address_is_ip4 (&tun_src))
142 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
144 else if (unformat (line_input, "tunnel-dst %U",
145 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
147 else if (unformat (line_input, "udp-encap"))
148 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
151 error = clib_error_return (0, "parse error: '%U'",
152 format_unformat_error, line_input);
159 error = clib_error_return (0, "missing id");
167 error = clib_error_return (0, "missing spi");
170 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
171 &ck, integ_alg, &ik, flags,
172 0, clib_host_to_net_u32 (salt),
173 &tun_src, &tun_dst, &sai, udp_src, udp_dst);
177 rv = ipsec_sa_unlock_id (id);
181 error = clib_error_return (0, "failed");
184 unformat_free (line_input);
190 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
193 "ipsec sa [add|del]",
194 .function = ipsec_sa_add_del_command_fn,
198 static clib_error_t *
199 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
200 unformat_input_t * input,
201 vlib_cli_command_t * cmd)
203 unformat_input_t _line_input, *line_input = &_line_input;
206 clib_error_t *error = NULL;
208 if (!unformat_user (input, unformat_line_input, line_input))
211 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
213 if (unformat (line_input, "add"))
215 else if (unformat (line_input, "del"))
217 else if (unformat (line_input, "%u", &spd_id))
221 error = clib_error_return (0, "parse error: '%U'",
222 format_unformat_error, line_input);
229 error = clib_error_return (0, "please specify SPD ID");
233 ipsec_add_del_spd (vm, spd_id, is_add);
236 unformat_free (line_input);
242 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
245 "ipsec spd [add|del] <id>",
246 .function = ipsec_spd_add_del_command_fn,
251 static clib_error_t *
252 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
253 unformat_input_t * input,
254 vlib_cli_command_t * cmd)
256 unformat_input_t _line_input, *line_input = &_line_input;
259 u32 tmp, tmp2, stat_index, local_range_set, remote_range_set;
260 clib_error_t *error = NULL;
263 clib_memset (&p, 0, sizeof (p));
264 p.lport.stop = p.rport.stop = ~0;
265 remote_range_set = local_range_set = is_outbound = 0;
267 if (!unformat_user (input, unformat_line_input, line_input))
270 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
272 if (unformat (line_input, "add"))
274 else if (unformat (line_input, "del"))
276 else if (unformat (line_input, "ip6"))
278 else if (unformat (line_input, "spd %u", &p.id))
280 else if (unformat (line_input, "inbound"))
282 else if (unformat (line_input, "outbound"))
284 else if (unformat (line_input, "priority %d", &p.priority))
286 else if (unformat (line_input, "protocol %u", &tmp))
287 p.protocol = (u8) tmp;
290 (line_input, "action %U", unformat_ipsec_policy_action,
293 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
295 error = clib_error_return (0, "unsupported action: 'resolve'");
299 else if (unformat (line_input, "sa %u", &p.sa_id))
301 else if (unformat (line_input, "local-ip-range %U - %U",
302 unformat_ip4_address, &p.laddr.start.ip4,
303 unformat_ip4_address, &p.laddr.stop.ip4))
305 else if (unformat (line_input, "remote-ip-range %U - %U",
306 unformat_ip4_address, &p.raddr.start.ip4,
307 unformat_ip4_address, &p.raddr.stop.ip4))
308 remote_range_set = 1;
309 else if (unformat (line_input, "local-ip-range %U - %U",
310 unformat_ip6_address, &p.laddr.start.ip6,
311 unformat_ip6_address, &p.laddr.stop.ip6))
316 else if (unformat (line_input, "remote-ip-range %U - %U",
317 unformat_ip6_address, &p.raddr.start.ip6,
318 unformat_ip6_address, &p.raddr.stop.ip6))
321 remote_range_set = 1;
323 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
329 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
336 error = clib_error_return (0, "parse error: '%U'",
337 format_unformat_error, line_input);
342 if (!remote_range_set)
345 clib_memset (&p.raddr.stop.ip6, 0xff, 16);
347 clib_memset (&p.raddr.stop.ip4, 0xff, 4);
349 if (!local_range_set)
352 clib_memset (&p.laddr.stop.ip6, 0xff, 16);
354 clib_memset (&p.laddr.stop.ip4, 0xff, 4);
357 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
361 error = clib_error_return (0, "unsupported policy type for:",
362 " outboud:%s %s action:%U",
363 (is_outbound ? "yes" : "no"),
364 (p.is_ipv6 ? "IPv4" : "IPv6"),
365 format_ipsec_policy_action, p.policy);
369 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
372 vlib_cli_output (vm, "policy-index:%d", stat_index);
374 vlib_cli_output (vm, "error:%d", rv);
377 unformat_free (line_input);
383 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
384 .path = "ipsec policy",
386 "ipsec policy [add|del] spd <id> priority <n> ",
387 .function = ipsec_policy_add_del_command_fn,
392 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
397 pool_foreach_index (sai, im->sad, ({
398 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
399 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
405 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
410 pool_foreach_index (spdi, im->spds, ({
411 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
417 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
419 u32 spd_id, sw_if_index;
422 vlib_cli_output (vm, "SPD Bindings:");
425 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
426 spd = pool_elt_at_index (im->spds, spd_id);
427 vlib_cli_output (vm, " %d -> %U", spd->id,
428 format_vnet_sw_if_index_name, im->vnet_main,
435 ipsec_tun_protect_show_one (index_t itpi, void *ctx)
437 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
439 return (WALK_CONTINUE);
443 ipsec_tunnel_show_all (vlib_main_t * vm)
445 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
448 static clib_error_t *
449 show_ipsec_command_fn (vlib_main_t * vm,
450 unformat_input_t * input, vlib_cli_command_t * cmd)
452 ipsec_main_t *im = &ipsec_main;
454 ipsec_sa_show_all (vm, im, 0);
455 ipsec_spd_show_all (vm, im);
456 ipsec_spd_bindings_show_all (vm, im);
457 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
459 vlib_cli_output (vm, "IPSec async mode: %s",
460 (im->async_mode ? "on" : "off"));
466 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
467 .path = "show ipsec all",
468 .short_help = "show ipsec all",
469 .function = show_ipsec_command_fn,
473 static clib_error_t *
474 show_ipsec_sa_command_fn (vlib_main_t * vm,
475 unformat_input_t * input, vlib_cli_command_t * cmd)
477 ipsec_main_t *im = &ipsec_main;
481 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
483 if (unformat (input, "%u", &sai))
485 if (unformat (input, "detail"))
492 ipsec_sa_show_all (vm, im, detail);
494 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
495 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
500 static clib_error_t *
501 clear_ipsec_sa_command_fn (vlib_main_t * vm,
502 unformat_input_t * input, vlib_cli_command_t * cmd)
504 ipsec_main_t *im = &ipsec_main;
507 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
509 if (unformat (input, "%u", &sai))
518 pool_foreach_index (sai, im->sad, ({
525 if (pool_is_free_index (im->sad, sai))
526 return clib_error_return (0, "unknown SA index: %d", sai);
528 ipsec_sa_clear (sai);
535 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
536 .path = "show ipsec sa",
537 .short_help = "show ipsec sa [index]",
538 .function = show_ipsec_sa_command_fn,
541 VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
542 .path = "clear ipsec sa",
543 .short_help = "clear ipsec sa [index]",
544 .function = clear_ipsec_sa_command_fn,
548 static clib_error_t *
549 show_ipsec_spd_command_fn (vlib_main_t * vm,
550 unformat_input_t * input, vlib_cli_command_t * cmd)
552 ipsec_main_t *im = &ipsec_main;
553 u8 show_bindings = 0;
556 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
558 if (unformat (input, "%u", &spdi))
560 else if (unformat (input, "bindings"))
567 ipsec_spd_bindings_show_all (vm, im);
569 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
571 ipsec_spd_show_all (vm, im);
577 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
578 .path = "show ipsec spd",
579 .short_help = "show ipsec spd [index]",
580 .function = show_ipsec_spd_command_fn,
584 static clib_error_t *
585 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
586 unformat_input_t * input,
587 vlib_cli_command_t * cmd)
589 ipsec_tunnel_show_all (vm);
595 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
596 .path = "show ipsec tunnel",
597 .short_help = "show ipsec tunnel",
598 .function = show_ipsec_tunnel_command_fn,
602 static clib_error_t *
603 ipsec_show_backends_command_fn (vlib_main_t * vm,
604 unformat_input_t * input,
605 vlib_cli_command_t * cmd)
607 ipsec_main_t *im = &ipsec_main;
610 (void) unformat (input, "verbose %u", &verbose);
612 vlib_cli_output (vm, "IPsec AH backends available:");
613 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
614 ipsec_ah_backend_t *ab;
616 pool_foreach (ab, im->ah_backends, {
617 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
618 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
621 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
622 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
623 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
624 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
625 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
626 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
627 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
628 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
632 vlib_cli_output (vm, "%v", s);
634 vlib_cli_output (vm, "IPsec ESP backends available:");
635 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
636 ipsec_esp_backend_t *eb;
638 pool_foreach (eb, im->esp_backends, {
639 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
640 eb - im->esp_backends == im->esp_current_backend ? "yes"
644 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
645 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
646 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
647 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
648 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
649 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
650 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
651 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
655 vlib_cli_output (vm, "%v", s);
662 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
663 .path = "show ipsec backends",
664 .short_help = "show ipsec backends",
665 .function = ipsec_show_backends_command_fn,
669 static clib_error_t *
670 ipsec_select_backend_command_fn (vlib_main_t * vm,
671 unformat_input_t * input,
672 vlib_cli_command_t * cmd)
674 unformat_input_t _line_input, *line_input = &_line_input;
675 ipsec_main_t *im = &ipsec_main;
679 error = ipsec_rsc_in_use (im);
684 /* Get a line of input. */
685 if (!unformat_user (input, unformat_line_input, line_input))
688 if (unformat (line_input, "ah"))
690 if (unformat (line_input, "%u", &backend_index))
692 if (ipsec_select_ah_backend (im, backend_index) < 0)
694 return clib_error_return (0, "Invalid AH backend index `%u'",
700 return clib_error_return (0, "Invalid backend index `%U'",
701 format_unformat_error, line_input);
704 else if (unformat (line_input, "esp"))
706 if (unformat (line_input, "%u", &backend_index))
708 if (ipsec_select_esp_backend (im, backend_index) < 0)
710 return clib_error_return (0, "Invalid ESP backend index `%u'",
716 return clib_error_return (0, "Invalid backend index `%U'",
717 format_unformat_error, line_input);
722 return clib_error_return (0, "Unknown input `%U'",
723 format_unformat_error, line_input);
730 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
731 .path = "ipsec select backend",
732 .short_help = "ipsec select backend <ah|esp> <backend index>",
733 .function = ipsec_select_backend_command_fn,
738 static clib_error_t *
739 clear_ipsec_counters_command_fn (vlib_main_t * vm,
740 unformat_input_t * input,
741 vlib_cli_command_t * cmd)
743 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
744 vlib_clear_combined_counters (&ipsec_sa_counters);
750 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
751 .path = "clear ipsec counters",
752 .short_help = "clear ipsec counters",
753 .function = clear_ipsec_counters_command_fn,
758 ipsec_tun_mk_local_sa_id (u32 ti)
760 return (0x80000000 | ti);
764 ipsec_tun_mk_remote_sa_id (u32 ti)
766 return (0xc0000000 | ti);
769 static clib_error_t *
770 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
771 unformat_input_t * input,
772 vlib_cli_command_t * cmd)
774 unformat_input_t _line_input, *line_input = &_line_input;
775 ip46_address_t local_ip = ip46_address_initializer;
776 ip46_address_t remote_ip = ip46_address_initializer;
777 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
778 ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE;
779 ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE;
780 ipsec_sa_flags_t flags;
781 u32 local_spi, remote_spi, salt = 0, table_id, fib_index;
788 clib_error_t *error = NULL;
789 ipsec_key_t rck = { 0 };
790 ipsec_key_t lck = { 0 };
791 ipsec_key_t lik = { 0 };
792 ipsec_key_t rik = { 0 };
795 flags = IPSEC_SA_FLAG_NONE;
797 /* Get a line of input. */
798 if (!unformat_user (input, unformat_line_input, line_input))
801 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
804 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
807 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
812 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
815 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
818 else if (unformat (line_input, "local-spi %u", &local_spi))
820 else if (unformat (line_input, "remote-spi %u", &remote_spi))
822 else if (unformat (line_input, "salt 0x%x", &salt))
824 else if (unformat (line_input, "udp-encap"))
825 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
826 else if (unformat (line_input, "use-esn"))
827 flags |= IPSEC_SA_FLAG_USE_ESN;
828 else if (unformat (line_input, "use-anti-replay"))
829 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
830 else if (unformat (line_input, "instance %u", &instance))
832 else if (unformat (line_input, "tx-table %u", &table_id))
836 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
840 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
842 else if (unformat (line_input, "crypto-alg %U",
843 unformat_ipsec_crypto_alg, &crypto_alg))
847 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
851 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
853 else if (unformat (line_input, "integ-alg %U",
854 unformat_ipsec_integ_alg, &integ_alg))
856 else if (unformat (line_input, "del"))
858 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
862 error = clib_error_return (0, "unknown input `%U'",
863 format_unformat_error, line_input);
870 error = clib_error_return (0, "mandatory argument(s) missing");
874 if (ipv4_set && ipv6_set)
875 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
877 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
881 rv = VNET_API_ERROR_NO_SUCH_FIB;
887 // remote = input, local = output
890 /* create an ip-ip tunnel, then the two SA, then bind them */
892 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
893 instance, &local_ip, &remote_ip, fib_index,
894 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
895 TUNNEL_MODE_P2P, &sw_if_index);
897 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
898 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
899 &lck, integ_alg, &lik, flags, table_id,
900 clib_host_to_net_u32 (salt), &local_ip,
901 &remote_ip, NULL, IPSEC_UDP_PORT_NONE,
902 IPSEC_UDP_PORT_NONE);
904 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
905 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
906 &rck, integ_alg, &rik,
907 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
908 clib_host_to_net_u32 (salt), &remote_ip,
909 &local_ip, NULL, IPSEC_UDP_PORT_NONE,
910 IPSEC_UDP_PORT_NONE);
912 ipsec_tun_protect_update_one (sw_if_index, &nh,
913 ipsec_tun_mk_local_sa_id (sw_if_index),
914 ipsec_tun_mk_remote_sa_id
924 case VNET_API_ERROR_INVALID_VALUE:
925 error = clib_error_return (0,
926 "IPSec tunnel interface already exists...");
929 error = clib_error_return (0, "ipsec_register_interface returned %d",
935 unformat_free (line_input);
941 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
942 .path = "create ipsec tunnel",
943 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
944 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
945 "[tx-table <table-id>]",
946 .function = create_ipsec_tunnel_command_fn,
950 static clib_error_t *
951 ipsec_tun_protect_cmd (vlib_main_t * vm,
952 unformat_input_t * input, vlib_cli_command_t * cmd)
954 unformat_input_t _line_input, *line_input = &_line_input;
955 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
956 ip_address_t peer = { };
961 vnm = vnet_get_main ();
963 if (!unformat_user (input, unformat_line_input, line_input))
966 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
968 if (unformat (line_input, "del"))
970 else if (unformat (line_input, "add"))
972 else if (unformat (line_input, "sa-in %d", &sa_in))
973 vec_add1 (sa_ins, sa_in);
974 else if (unformat (line_input, "sa-out %d", &sa_out))
976 else if (unformat (line_input, "%U",
977 unformat_vnet_sw_interface, vnm, &sw_if_index))
979 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
982 return (clib_error_return (0, "unknown input '%U'",
983 format_unformat_error, line_input));
987 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
989 unformat_free (line_input);
994 * Protect tunnel with IPSEC
997 VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
999 .path = "ipsec tunnel protect",
1000 .function = ipsec_tun_protect_cmd,
1001 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
1002 // this is not MP safe
1007 static clib_error_t *
1008 ipsec_tun_protect_show (vlib_main_t * vm,
1009 unformat_input_t * input, vlib_cli_command_t * cmd)
1011 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
1017 * show IPSEC tunnel protection
1020 VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
1022 .path = "show ipsec protect",
1023 .function = ipsec_tun_protect_show,
1024 .short_help = "show ipsec protect",
1028 static clib_error_t *
1029 ipsec_tun_protect_hash_show (vlib_main_t * vm,
1030 unformat_input_t * input,
1031 vlib_cli_command_t * cmd)
1033 ipsec_main_t *im = &ipsec_main;
1036 ipsec_tun_lkup_result_t value;
1037 ipsec4_tunnel_key_t key;
1039 vlib_cli_output (vm, "IPv4:");
1042 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
1044 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
1045 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1051 ipsec_tun_lkup_result_t value;
1052 ipsec6_tunnel_key_t *key;
1054 vlib_cli_output (vm, "IPv6:");
1057 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1059 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1060 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1069 * show IPSEC tunnel protection hash tables
1072 VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1074 .path = "show ipsec protect-hash",
1075 .function = ipsec_tun_protect_hash_show,
1076 .short_help = "show ipsec protect-hash",
1081 ipsec_cli_init (vlib_main_t * vm)
1086 VLIB_INIT_FUNCTION (ipsec_cli_init);
1088 static clib_error_t *
1089 set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
1090 vlib_cli_command_t * cmd)
1092 unformat_input_t _line_input, *line_input = &_line_input;
1093 int async_enable = 0;
1095 if (!unformat_user (input, unformat_line_input, line_input))
1098 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1100 if (unformat (line_input, "on"))
1102 else if (unformat (line_input, "off"))
1105 return (clib_error_return (0, "unknown input '%U'",
1106 format_unformat_error, line_input));
1109 vnet_crypto_request_async_mode (async_enable);
1110 ipsec_set_async_mode (async_enable);
1112 unformat_free (line_input);
1117 VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1118 .path = "set ipsec async mode",
1119 .short_help = "set ipsec async mode on|off",
1120 .function = set_async_mode_command_fn,
1125 * fd.io coding-style-patch-verification: ON
1128 * eval: (c-set-style "gnu")