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;
98 flags = IPSEC_SA_FLAG_NONE;
99 proto = IPSEC_PROTOCOL_ESP;
100 integ_alg = IPSEC_INTEG_ALG_NONE;
101 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
102 udp_src = udp_dst = IPSEC_UDP_PORT_NONE;
105 if (!unformat_user (input, unformat_line_input, line_input))
108 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
110 if (unformat (line_input, "add %u", &id))
115 else if (unformat (line_input, "del %u", &id))
120 else if (unformat (line_input, "spi %u", &spi))
122 else if (unformat (line_input, "salt 0x%x", &salt))
124 else if (unformat (line_input, "esp"))
125 proto = IPSEC_PROTOCOL_ESP;
126 else if (unformat (line_input, "ah"))
127 proto = IPSEC_PROTOCOL_AH;
128 else if (unformat (line_input, "crypto-key %U",
129 unformat_ipsec_key, &ck))
131 else if (unformat (line_input, "crypto-alg %U",
132 unformat_ipsec_crypto_alg, &crypto_alg))
134 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
136 else if (unformat (line_input, "integ-alg %U",
137 unformat_ipsec_integ_alg, &integ_alg))
139 else if (unformat (line_input, "tunnel-src %U",
140 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
142 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
143 if (!ip46_address_is_ip4 (&tun_src))
144 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
146 else if (unformat (line_input, "tunnel-dst %U",
147 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
149 else if (unformat (line_input, "inbound"))
150 flags |= IPSEC_SA_FLAG_IS_INBOUND;
151 else if (unformat (line_input, "use-anti-replay"))
152 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
153 else if (unformat (line_input, "use-esn"))
154 flags |= IPSEC_SA_FLAG_USE_ESN;
155 else if (unformat (line_input, "udp-encap"))
156 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
159 error = clib_error_return (0, "parse error: '%U'",
160 format_unformat_error, line_input);
164 if ((flags & IPSEC_SA_FLAG_IS_INBOUND)
165 && !(flags & IPSEC_SA_FLAG_IS_TUNNEL))
167 error = clib_error_return (0, "inbound specified on non-tunnel SA");
173 error = clib_error_return (0, "missing id");
181 error = clib_error_return (0, "missing spi");
184 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
185 &ck, integ_alg, &ik, flags,
186 0, clib_host_to_net_u32 (salt),
188 TUNNEL_ENCAP_DECAP_FLAG_NONE, dscp,
189 &sai, udp_src, udp_dst);
193 rv = ipsec_sa_unlock_id (id);
197 error = clib_error_return (0, "failed");
200 unformat_free (line_input);
206 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
209 "ipsec sa [add|del]",
210 .function = ipsec_sa_add_del_command_fn,
214 static clib_error_t *
215 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
216 unformat_input_t * input,
217 vlib_cli_command_t * cmd)
219 unformat_input_t _line_input, *line_input = &_line_input;
222 clib_error_t *error = NULL;
224 if (!unformat_user (input, unformat_line_input, line_input))
227 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
229 if (unformat (line_input, "add"))
231 else if (unformat (line_input, "del"))
233 else if (unformat (line_input, "%u", &spd_id))
237 error = clib_error_return (0, "parse error: '%U'",
238 format_unformat_error, line_input);
245 error = clib_error_return (0, "please specify SPD ID");
249 ipsec_add_del_spd (vm, spd_id, is_add);
252 unformat_free (line_input);
258 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
261 "ipsec spd [add|del] <id>",
262 .function = ipsec_spd_add_del_command_fn,
267 static clib_error_t *
268 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
269 unformat_input_t * input,
270 vlib_cli_command_t * cmd)
272 unformat_input_t _line_input, *line_input = &_line_input;
275 u32 tmp, tmp2, stat_index, local_range_set, remote_range_set;
276 clib_error_t *error = NULL;
279 clib_memset (&p, 0, sizeof (p));
280 p.lport.stop = p.rport.stop = ~0;
281 remote_range_set = local_range_set = is_outbound = 0;
283 if (!unformat_user (input, unformat_line_input, line_input))
286 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
288 if (unformat (line_input, "add"))
290 else if (unformat (line_input, "del"))
292 else if (unformat (line_input, "ip6"))
294 else if (unformat (line_input, "spd %u", &p.id))
296 else if (unformat (line_input, "inbound"))
298 else if (unformat (line_input, "outbound"))
300 else if (unformat (line_input, "priority %d", &p.priority))
302 else if (unformat (line_input, "protocol %u", &tmp))
303 p.protocol = (u8) tmp;
306 (line_input, "action %U", unformat_ipsec_policy_action,
309 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
311 error = clib_error_return (0, "unsupported action: 'resolve'");
315 else if (unformat (line_input, "sa %u", &p.sa_id))
317 else if (unformat (line_input, "local-ip-range %U - %U",
318 unformat_ip4_address, &p.laddr.start.ip4,
319 unformat_ip4_address, &p.laddr.stop.ip4))
321 else if (unformat (line_input, "remote-ip-range %U - %U",
322 unformat_ip4_address, &p.raddr.start.ip4,
323 unformat_ip4_address, &p.raddr.stop.ip4))
324 remote_range_set = 1;
325 else if (unformat (line_input, "local-ip-range %U - %U",
326 unformat_ip6_address, &p.laddr.start.ip6,
327 unformat_ip6_address, &p.laddr.stop.ip6))
332 else if (unformat (line_input, "remote-ip-range %U - %U",
333 unformat_ip6_address, &p.raddr.start.ip6,
334 unformat_ip6_address, &p.raddr.stop.ip6))
337 remote_range_set = 1;
339 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
345 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
352 error = clib_error_return (0, "parse error: '%U'",
353 format_unformat_error, line_input);
358 if (!remote_range_set)
361 clib_memset (&p.raddr.stop.ip6, 0xff, 16);
363 clib_memset (&p.raddr.stop.ip4, 0xff, 4);
365 if (!local_range_set)
368 clib_memset (&p.laddr.stop.ip6, 0xff, 16);
370 clib_memset (&p.laddr.stop.ip4, 0xff, 4);
373 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
377 error = clib_error_return (0, "unsupported policy type for:",
378 " outboud:%s %s action:%U",
379 (is_outbound ? "yes" : "no"),
380 (p.is_ipv6 ? "IPv4" : "IPv6"),
381 format_ipsec_policy_action, p.policy);
385 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
388 vlib_cli_output (vm, "policy-index:%d", stat_index);
390 vlib_cli_output (vm, "error:%d", rv);
393 unformat_free (line_input);
399 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
400 .path = "ipsec policy",
402 "ipsec policy [add|del] spd <id> priority <n> ",
403 .function = ipsec_policy_add_del_command_fn,
408 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
413 pool_foreach_index (sai, im->sad, ({
414 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
415 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
421 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
426 pool_foreach_index (spdi, im->spds, ({
427 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
433 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
435 u32 spd_id, sw_if_index;
438 vlib_cli_output (vm, "SPD Bindings:");
441 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
442 spd = pool_elt_at_index (im->spds, spd_id);
443 vlib_cli_output (vm, " %d -> %U", spd->id,
444 format_vnet_sw_if_index_name, im->vnet_main,
451 ipsec_tun_protect_show_one (index_t itpi, void *ctx)
453 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
455 return (WALK_CONTINUE);
459 ipsec_tunnel_show_all (vlib_main_t * vm)
461 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
464 static clib_error_t *
465 show_ipsec_command_fn (vlib_main_t * vm,
466 unformat_input_t * input, vlib_cli_command_t * cmd)
468 ipsec_main_t *im = &ipsec_main;
470 ipsec_sa_show_all (vm, im, 0);
471 ipsec_spd_show_all (vm, im);
472 ipsec_spd_bindings_show_all (vm, im);
473 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
475 vlib_cli_output (vm, "IPSec async mode: %s",
476 (im->async_mode ? "on" : "off"));
482 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
483 .path = "show ipsec all",
484 .short_help = "show ipsec all",
485 .function = show_ipsec_command_fn,
489 static clib_error_t *
490 show_ipsec_sa_command_fn (vlib_main_t * vm,
491 unformat_input_t * input, vlib_cli_command_t * cmd)
493 ipsec_main_t *im = &ipsec_main;
497 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
499 if (unformat (input, "%u", &sai))
501 if (unformat (input, "detail"))
508 ipsec_sa_show_all (vm, im, detail);
510 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
511 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
516 static clib_error_t *
517 clear_ipsec_sa_command_fn (vlib_main_t * vm,
518 unformat_input_t * input, vlib_cli_command_t * cmd)
520 ipsec_main_t *im = &ipsec_main;
523 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
525 if (unformat (input, "%u", &sai))
534 pool_foreach_index (sai, im->sad, ({
541 if (pool_is_free_index (im->sad, sai))
542 return clib_error_return (0, "unknown SA index: %d", sai);
544 ipsec_sa_clear (sai);
551 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
552 .path = "show ipsec sa",
553 .short_help = "show ipsec sa [index]",
554 .function = show_ipsec_sa_command_fn,
557 VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
558 .path = "clear ipsec sa",
559 .short_help = "clear ipsec sa [index]",
560 .function = clear_ipsec_sa_command_fn,
564 static clib_error_t *
565 show_ipsec_spd_command_fn (vlib_main_t * vm,
566 unformat_input_t * input, vlib_cli_command_t * cmd)
568 ipsec_main_t *im = &ipsec_main;
569 u8 show_bindings = 0;
572 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
574 if (unformat (input, "%u", &spdi))
576 else if (unformat (input, "bindings"))
583 ipsec_spd_bindings_show_all (vm, im);
585 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
587 ipsec_spd_show_all (vm, im);
593 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
594 .path = "show ipsec spd",
595 .short_help = "show ipsec spd [index]",
596 .function = show_ipsec_spd_command_fn,
600 static clib_error_t *
601 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
602 unformat_input_t * input,
603 vlib_cli_command_t * cmd)
605 ipsec_tunnel_show_all (vm);
611 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
612 .path = "show ipsec tunnel",
613 .short_help = "show ipsec tunnel",
614 .function = show_ipsec_tunnel_command_fn,
618 static clib_error_t *
619 ipsec_show_backends_command_fn (vlib_main_t * vm,
620 unformat_input_t * input,
621 vlib_cli_command_t * cmd)
623 ipsec_main_t *im = &ipsec_main;
626 (void) unformat (input, "verbose %u", &verbose);
628 vlib_cli_output (vm, "IPsec AH backends available:");
629 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
630 ipsec_ah_backend_t *ab;
632 pool_foreach (ab, im->ah_backends, {
633 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
634 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
637 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
638 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
639 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
640 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
641 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
642 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
643 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
644 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
648 vlib_cli_output (vm, "%v", s);
650 vlib_cli_output (vm, "IPsec ESP backends available:");
651 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
652 ipsec_esp_backend_t *eb;
654 pool_foreach (eb, im->esp_backends, {
655 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
656 eb - im->esp_backends == im->esp_current_backend ? "yes"
660 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
661 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
662 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
663 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
664 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
665 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
666 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
667 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
671 vlib_cli_output (vm, "%v", s);
678 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
679 .path = "show ipsec backends",
680 .short_help = "show ipsec backends",
681 .function = ipsec_show_backends_command_fn,
685 static clib_error_t *
686 ipsec_select_backend_command_fn (vlib_main_t * vm,
687 unformat_input_t * input,
688 vlib_cli_command_t * cmd)
690 unformat_input_t _line_input, *line_input = &_line_input;
691 ipsec_main_t *im = &ipsec_main;
695 error = ipsec_rsc_in_use (im);
700 /* Get a line of input. */
701 if (!unformat_user (input, unformat_line_input, line_input))
704 if (unformat (line_input, "ah"))
706 if (unformat (line_input, "%u", &backend_index))
708 if (ipsec_select_ah_backend (im, backend_index) < 0)
710 return clib_error_return (0, "Invalid AH backend index `%u'",
716 return clib_error_return (0, "Invalid backend index `%U'",
717 format_unformat_error, line_input);
720 else if (unformat (line_input, "esp"))
722 if (unformat (line_input, "%u", &backend_index))
724 if (ipsec_select_esp_backend (im, backend_index) < 0)
726 return clib_error_return (0, "Invalid ESP backend index `%u'",
732 return clib_error_return (0, "Invalid backend index `%U'",
733 format_unformat_error, line_input);
738 return clib_error_return (0, "Unknown input `%U'",
739 format_unformat_error, line_input);
746 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
747 .path = "ipsec select backend",
748 .short_help = "ipsec select backend <ah|esp> <backend index>",
749 .function = ipsec_select_backend_command_fn,
754 static clib_error_t *
755 clear_ipsec_counters_command_fn (vlib_main_t * vm,
756 unformat_input_t * input,
757 vlib_cli_command_t * cmd)
759 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
760 vlib_clear_combined_counters (&ipsec_sa_counters);
766 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
767 .path = "clear ipsec counters",
768 .short_help = "clear ipsec counters",
769 .function = clear_ipsec_counters_command_fn,
774 ipsec_tun_mk_local_sa_id (u32 ti)
776 return (0x80000000 | ti);
780 ipsec_tun_mk_remote_sa_id (u32 ti)
782 return (0xc0000000 | ti);
785 static clib_error_t *
786 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
787 unformat_input_t * input,
788 vlib_cli_command_t * cmd)
790 unformat_input_t _line_input, *line_input = &_line_input;
791 ip46_address_t local_ip = ip46_address_initializer;
792 ip46_address_t remote_ip = ip46_address_initializer;
793 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
794 ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE;
795 ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE;
796 ipsec_sa_flags_t flags;
797 u32 local_spi, remote_spi, salt = 0, table_id, fib_index;
804 clib_error_t *error = NULL;
805 ipsec_key_t rck = { 0 };
806 ipsec_key_t lck = { 0 };
807 ipsec_key_t lik = { 0 };
808 ipsec_key_t rik = { 0 };
811 flags = IPSEC_SA_FLAG_NONE;
813 /* Get a line of input. */
814 if (!unformat_user (input, unformat_line_input, line_input))
817 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
820 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
823 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
828 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
831 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
834 else if (unformat (line_input, "local-spi %u", &local_spi))
836 else if (unformat (line_input, "remote-spi %u", &remote_spi))
838 else if (unformat (line_input, "salt 0x%x", &salt))
840 else if (unformat (line_input, "udp-encap"))
841 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
842 else if (unformat (line_input, "use-esn"))
843 flags |= IPSEC_SA_FLAG_USE_ESN;
844 else if (unformat (line_input, "use-anti-replay"))
845 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
846 else if (unformat (line_input, "instance %u", &instance))
848 else if (unformat (line_input, "tx-table %u", &table_id))
852 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
856 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
858 else if (unformat (line_input, "crypto-alg %U",
859 unformat_ipsec_crypto_alg, &crypto_alg))
863 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
867 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
869 else if (unformat (line_input, "integ-alg %U",
870 unformat_ipsec_integ_alg, &integ_alg))
872 else if (unformat (line_input, "del"))
874 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
878 error = clib_error_return (0, "unknown input `%U'",
879 format_unformat_error, line_input);
886 error = clib_error_return (0, "mandatory argument(s) missing");
890 if (ipv4_set && ipv6_set)
891 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
893 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
897 rv = VNET_API_ERROR_NO_SUCH_FIB;
903 // remote = input, local = output
906 /* create an ip-ip tunnel, then the two SA, then bind them */
908 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
909 instance, &local_ip, &remote_ip, fib_index,
910 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
911 TUNNEL_MODE_P2P, &sw_if_index);
913 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
914 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
915 &lck, integ_alg, &lik, flags, table_id,
916 clib_host_to_net_u32 (salt), &local_ip,
917 &remote_ip, TUNNEL_ENCAP_DECAP_FLAG_NONE,
919 IPSEC_UDP_PORT_NONE, IPSEC_UDP_PORT_NONE);
921 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
922 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
923 &rck, integ_alg, &rik,
924 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
925 clib_host_to_net_u32 (salt), &remote_ip,
926 &local_ip, TUNNEL_ENCAP_DECAP_FLAG_NONE,
928 IPSEC_UDP_PORT_NONE, IPSEC_UDP_PORT_NONE);
930 ipsec_tun_protect_update_one (sw_if_index, &nh,
931 ipsec_tun_mk_local_sa_id (sw_if_index),
932 ipsec_tun_mk_remote_sa_id
942 case VNET_API_ERROR_INVALID_VALUE:
943 error = clib_error_return (0,
944 "IPSec tunnel interface already exists...");
947 error = clib_error_return (0, "ipsec_register_interface returned %d",
953 unformat_free (line_input);
959 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
960 .path = "create ipsec tunnel",
961 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
962 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
963 "[tx-table <table-id>]",
964 .function = create_ipsec_tunnel_command_fn,
968 static clib_error_t *
969 ipsec_tun_protect_cmd (vlib_main_t * vm,
970 unformat_input_t * input, vlib_cli_command_t * cmd)
972 unformat_input_t _line_input, *line_input = &_line_input;
973 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
974 ip_address_t peer = { };
979 vnm = vnet_get_main ();
981 if (!unformat_user (input, unformat_line_input, line_input))
984 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
986 if (unformat (line_input, "del"))
988 else if (unformat (line_input, "add"))
990 else if (unformat (line_input, "sa-in %d", &sa_in))
991 vec_add1 (sa_ins, sa_in);
992 else if (unformat (line_input, "sa-out %d", &sa_out))
994 else if (unformat (line_input, "%U",
995 unformat_vnet_sw_interface, vnm, &sw_if_index))
997 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
1000 return (clib_error_return (0, "unknown input '%U'",
1001 format_unformat_error, line_input));
1005 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
1007 ipsec_tun_protect_del (sw_if_index, &peer);
1009 unformat_free (line_input);
1014 * Protect tunnel with IPSEC
1017 VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
1019 .path = "ipsec tunnel protect",
1020 .function = ipsec_tun_protect_cmd,
1021 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA> [add|del]",
1022 // this is not MP safe
1027 static clib_error_t *
1028 ipsec_tun_protect_show (vlib_main_t * vm,
1029 unformat_input_t * input, vlib_cli_command_t * cmd)
1031 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
1037 * show IPSEC tunnel protection
1040 VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
1042 .path = "show ipsec protect",
1043 .function = ipsec_tun_protect_show,
1044 .short_help = "show ipsec protect",
1049 ipsec_tun_protect4_hash_show_one (clib_bihash_kv_8_16_t * kv, void *arg)
1051 ipsec4_tunnel_kv_t *ikv = (ipsec4_tunnel_kv_t *) kv;
1052 vlib_main_t *vm = arg;
1054 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_kv, ikv);
1056 return (BIHASH_WALK_CONTINUE);
1060 ipsec_tun_protect6_hash_show_one (clib_bihash_kv_24_16_t * kv, void *arg)
1062 ipsec6_tunnel_kv_t *ikv = (ipsec6_tunnel_kv_t *) kv;
1063 vlib_main_t *vm = arg;
1065 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_kv, ikv);
1067 return (BIHASH_WALK_CONTINUE);
1070 static clib_error_t *
1071 ipsec_tun_protect_hash_show (vlib_main_t * vm,
1072 unformat_input_t * input,
1073 vlib_cli_command_t * cmd)
1075 ipsec_main_t *im = &ipsec_main;
1078 vlib_cli_output (vm, "IPv4:");
1080 clib_bihash_foreach_key_value_pair_8_16
1081 (&im->tun4_protect_by_key, ipsec_tun_protect4_hash_show_one, vm);
1083 vlib_cli_output (vm, "IPv6:");
1085 clib_bihash_foreach_key_value_pair_24_16
1086 (&im->tun6_protect_by_key, ipsec_tun_protect6_hash_show_one, vm);
1093 * show IPSEC tunnel protection hash tables
1096 VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1098 .path = "show ipsec protect-hash",
1099 .function = ipsec_tun_protect_hash_show,
1100 .short_help = "show ipsec protect-hash",
1105 ipsec_cli_init (vlib_main_t * vm)
1110 VLIB_INIT_FUNCTION (ipsec_cli_init);
1112 static clib_error_t *
1113 set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input,
1114 vlib_cli_command_t * cmd)
1116 unformat_input_t _line_input, *line_input = &_line_input;
1117 int async_enable = 0;
1119 if (!unformat_user (input, unformat_line_input, line_input))
1122 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1124 if (unformat (line_input, "on"))
1126 else if (unformat (line_input, "off"))
1129 return (clib_error_return (0, "unknown input '%U'",
1130 format_unformat_error, line_input));
1133 vnet_crypto_request_async_mode (async_enable);
1134 ipsec_set_async_mode (async_enable);
1136 unformat_free (line_input);
1141 VLIB_CLI_COMMAND (set_async_mode_command, static) = {
1142 .path = "set ipsec async mode",
1143 .short_help = "set ipsec async mode on|off",
1144 .function = set_async_mode_command_fn,
1149 * fd.io coding-style-patch-verification: ON
1152 * eval: (c-set-style "gnu")