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>
24 #include <vnet/ipsec/ipsec.h>
27 set_interface_spd_command_fn (vlib_main_t * vm,
28 unformat_input_t * input,
29 vlib_cli_command_t * cmd)
31 unformat_input_t _line_input, *line_input = &_line_input;
32 ipsec_main_t *im = &ipsec_main;
33 u32 sw_if_index = (u32) ~ 0;
36 clib_error_t *error = NULL;
38 if (!unformat_user (input, unformat_line_input, line_input))
42 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
43 &sw_if_index, &spd_id))
45 else if (unformat (line_input, "del"))
49 error = clib_error_return (0, "parse error: '%U'",
50 format_unformat_error, line_input);
54 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
57 unformat_free (line_input);
63 VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
64 .path = "set interface ipsec spd",
66 "set interface ipsec spd <int> <id>",
67 .function = set_interface_spd_command_fn,
72 ipsec_sa_add_del_command_fn (vlib_main_t * vm,
73 unformat_input_t * input,
74 vlib_cli_command_t * cmd)
76 unformat_input_t _line_input, *line_input = &_line_input;
77 ip46_address_t tun_src = { }, tun_dst =
80 ipsec_crypto_alg_t crypto_alg;
81 ipsec_integ_alg_t integ_alg;
82 ipsec_protocol_t proto;
83 ipsec_sa_flags_t flags;
85 ipsec_key_t ck = { 0 };
86 ipsec_key_t ik = { 0 };
92 flags = IPSEC_SA_FLAG_NONE;
93 proto = IPSEC_PROTOCOL_ESP;
94 integ_alg = IPSEC_INTEG_ALG_NONE;
95 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
97 if (!unformat_user (input, unformat_line_input, line_input))
100 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
102 if (unformat (line_input, "add %u", &id))
104 else if (unformat (line_input, "del %u", &id))
106 else if (unformat (line_input, "spi %u", &spi))
108 else if (unformat (line_input, "salt %u", &salt))
110 else if (unformat (line_input, "esp"))
111 proto = IPSEC_PROTOCOL_ESP;
112 else if (unformat (line_input, "ah"))
113 proto = IPSEC_PROTOCOL_AH;
114 else if (unformat (line_input, "crypto-key %U",
115 unformat_ipsec_key, &ck))
117 else if (unformat (line_input, "crypto-alg %U",
118 unformat_ipsec_crypto_alg, &crypto_alg))
120 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
122 else if (unformat (line_input, "integ-alg %U",
123 unformat_ipsec_integ_alg, &integ_alg))
125 else if (unformat (line_input, "tunnel-src %U",
126 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
128 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
129 if (!ip46_address_is_ip4 (&tun_src))
130 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
132 else if (unformat (line_input, "tunnel-dst %U",
133 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
135 else if (unformat (line_input, "udp-encap"))
136 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
139 error = clib_error_return (0, "parse error: '%U'",
140 format_unformat_error, line_input);
146 rv = ipsec_sa_add (id, spi, proto, crypto_alg,
147 &ck, integ_alg, &ik, flags,
148 0, clib_host_to_net_u32 (salt),
149 &tun_src, &tun_dst, NULL);
151 rv = ipsec_sa_del (id);
154 error = clib_error_return (0, "failed");
157 unformat_free (line_input);
163 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
166 "ipsec sa [add|del]",
167 .function = ipsec_sa_add_del_command_fn,
171 static clib_error_t *
172 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
173 unformat_input_t * input,
174 vlib_cli_command_t * cmd)
176 unformat_input_t _line_input, *line_input = &_line_input;
179 clib_error_t *error = NULL;
181 if (!unformat_user (input, unformat_line_input, line_input))
184 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
186 if (unformat (line_input, "add"))
188 else if (unformat (line_input, "del"))
190 else if (unformat (line_input, "%u", &spd_id))
194 error = clib_error_return (0, "parse error: '%U'",
195 format_unformat_error, line_input);
202 error = clib_error_return (0, "please specify SPD ID");
206 ipsec_add_del_spd (vm, spd_id, is_add);
209 unformat_free (line_input);
215 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
218 "ipsec spd [add|del] <id>",
219 .function = ipsec_spd_add_del_command_fn,
224 static clib_error_t *
225 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
226 unformat_input_t * input,
227 vlib_cli_command_t * cmd)
229 unformat_input_t _line_input, *line_input = &_line_input;
232 u32 tmp, tmp2, stat_index;
233 clib_error_t *error = NULL;
236 clib_memset (&p, 0, sizeof (p));
237 p.lport.stop = p.rport.stop = ~0;
240 if (!unformat_user (input, unformat_line_input, line_input))
243 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
245 if (unformat (line_input, "add"))
247 else if (unformat (line_input, "del"))
249 else if (unformat (line_input, "spd %u", &p.id))
251 else if (unformat (line_input, "inbound"))
253 else if (unformat (line_input, "outbound"))
255 else if (unformat (line_input, "priority %d", &p.priority))
257 else if (unformat (line_input, "protocol %u", &tmp))
258 p.protocol = (u8) tmp;
261 (line_input, "action %U", unformat_ipsec_policy_action,
264 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
266 error = clib_error_return (0, "unsupported action: 'resolve'");
270 else if (unformat (line_input, "sa %u", &p.sa_id))
272 else if (unformat (line_input, "local-ip-range %U - %U",
273 unformat_ip4_address, &p.laddr.start.ip4,
274 unformat_ip4_address, &p.laddr.stop.ip4))
276 else if (unformat (line_input, "remote-ip-range %U - %U",
277 unformat_ip4_address, &p.raddr.start.ip4,
278 unformat_ip4_address, &p.raddr.stop.ip4))
280 else if (unformat (line_input, "local-ip-range %U - %U",
281 unformat_ip6_address, &p.laddr.start.ip6,
282 unformat_ip6_address, &p.laddr.stop.ip6))
286 else if (unformat (line_input, "remote-ip-range %U - %U",
287 unformat_ip6_address, &p.raddr.start.ip6,
288 unformat_ip6_address, &p.raddr.stop.ip6))
292 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
296 p.lport.start = clib_host_to_net_u16 (p.lport.start);
297 p.lport.stop = clib_host_to_net_u16 (p.lport.stop);
300 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
304 p.rport.start = clib_host_to_net_u16 (p.rport.start);
305 p.rport.stop = clib_host_to_net_u16 (p.rport.stop);
309 error = clib_error_return (0, "parse error: '%U'",
310 format_unformat_error, line_input);
315 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
319 error = clib_error_return (0, "unsupported policy type for:",
320 " outboud:%s %s action:%U",
321 (is_outbound ? "yes" : "no"),
322 (p.is_ipv6 ? "IPv4" : "IPv6"),
323 format_ipsec_policy_action, p.policy);
327 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
330 vlib_cli_output (vm, "policy-index:%d", stat_index);
332 vlib_cli_output (vm, "error:%d", rv);
335 unformat_free (line_input);
341 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
342 .path = "ipsec policy",
344 "ipsec policy [add|del] spd <id> priority <n> ",
345 .function = ipsec_policy_add_del_command_fn,
350 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im)
355 pool_foreach_index (sai, im->sad, ({
356 vlib_cli_output(vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
362 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
367 pool_foreach_index (spdi, im->spds, ({
368 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
374 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
376 u32 spd_id, sw_if_index;
379 vlib_cli_output (vm, "SPD Bindings:");
382 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
383 spd = pool_elt_at_index (im->spds, spd_id);
384 vlib_cli_output (vm, " %d -> %U", spd->id,
385 format_vnet_sw_if_index_name, im->vnet_main,
392 ipsec_tunnel_show_all (vlib_main_t * vm, ipsec_main_t * im)
396 vlib_cli_output (vm, "Tunnel interfaces");
398 pool_foreach_index (ti, im->tunnel_interfaces, ({
399 vlib_cli_output(vm, " %U", format_ipsec_tunnel, ti);
404 static clib_error_t *
405 show_ipsec_command_fn (vlib_main_t * vm,
406 unformat_input_t * input, vlib_cli_command_t * cmd)
408 ipsec_main_t *im = &ipsec_main;
410 ipsec_sa_show_all (vm, im);
411 ipsec_spd_show_all (vm, im);
412 ipsec_spd_bindings_show_all (vm, im);
413 ipsec_tunnel_show_all (vm, im);
419 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
420 .path = "show ipsec all",
421 .short_help = "show ipsec all",
422 .function = show_ipsec_command_fn,
426 static clib_error_t *
427 show_ipsec_sa_command_fn (vlib_main_t * vm,
428 unformat_input_t * input, vlib_cli_command_t * cmd)
430 ipsec_main_t *im = &ipsec_main;
433 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
435 if (unformat (input, "%u", &sai))
442 ipsec_sa_show_all (vm, im);
444 vlib_cli_output (vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_DETAIL);
450 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
451 .path = "show ipsec sa",
452 .short_help = "show ipsec sa [index]",
453 .function = show_ipsec_sa_command_fn,
457 static clib_error_t *
458 show_ipsec_spd_command_fn (vlib_main_t * vm,
459 unformat_input_t * input, vlib_cli_command_t * cmd)
461 ipsec_main_t *im = &ipsec_main;
462 u8 show_bindings = 0;
465 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
467 if (unformat (input, "%u", &spdi))
469 else if (unformat (input, "bindings"))
476 ipsec_spd_bindings_show_all (vm, im);
478 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
480 ipsec_spd_show_all (vm, im);
486 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
487 .path = "show ipsec spd",
488 .short_help = "show ipsec spd [index]",
489 .function = show_ipsec_spd_command_fn,
493 static clib_error_t *
494 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
495 unformat_input_t * input,
496 vlib_cli_command_t * cmd)
498 ipsec_main_t *im = &ipsec_main;
501 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
503 if (unformat (input, "%u", &ti))
510 vlib_cli_output (vm, "%U", format_ipsec_tunnel, ti);
512 ipsec_tunnel_show_all (vm, im);
518 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
519 .path = "show ipsec tunnel",
520 .short_help = "show ipsec tunnel [index]",
521 .function = show_ipsec_tunnel_command_fn,
525 static clib_error_t *
526 ipsec_show_backends_command_fn (vlib_main_t * vm,
527 unformat_input_t * input,
528 vlib_cli_command_t * cmd)
530 ipsec_main_t *im = &ipsec_main;
533 (void) unformat (input, "verbose %u", &verbose);
535 vlib_cli_output (vm, "IPsec AH backends available:");
536 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
537 ipsec_ah_backend_t *ab;
539 pool_foreach (ab, im->ah_backends, {
540 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
541 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
544 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
545 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
546 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
547 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
548 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
549 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
550 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
551 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
555 vlib_cli_output (vm, "%v", s);
557 vlib_cli_output (vm, "IPsec ESP backends available:");
558 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
559 ipsec_esp_backend_t *eb;
561 pool_foreach (eb, im->esp_backends, {
562 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
563 eb - im->esp_backends == im->esp_current_backend ? "yes"
567 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
568 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
569 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
570 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
571 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
572 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
573 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
574 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
578 vlib_cli_output (vm, "%v", s);
585 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
586 .path = "show ipsec backends",
587 .short_help = "show ipsec backends",
588 .function = ipsec_show_backends_command_fn,
592 static clib_error_t *
593 ipsec_select_backend_command_fn (vlib_main_t * vm,
594 unformat_input_t * input,
595 vlib_cli_command_t * cmd)
597 unformat_input_t _line_input, *line_input = &_line_input;
598 ipsec_main_t *im = &ipsec_main;
602 error = ipsec_rsc_in_use (im);
607 /* Get a line of input. */
608 if (!unformat_user (input, unformat_line_input, line_input))
611 if (unformat (line_input, "ah"))
613 if (unformat (line_input, "%u", &backend_index))
615 if (ipsec_select_ah_backend (im, backend_index) < 0)
617 return clib_error_return (0, "Invalid AH backend index `%u'",
623 return clib_error_return (0, "Invalid backend index `%U'",
624 format_unformat_error, line_input);
627 else if (unformat (line_input, "esp"))
629 if (unformat (line_input, "%u", &backend_index))
631 if (ipsec_select_esp_backend (im, backend_index) < 0)
633 return clib_error_return (0, "Invalid ESP backend index `%u'",
639 return clib_error_return (0, "Invalid backend index `%U'",
640 format_unformat_error, line_input);
645 return clib_error_return (0, "Unknown input `%U'",
646 format_unformat_error, line_input);
653 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
654 .path = "ipsec select backend",
655 .short_help = "ipsec select backend <ah|esp> <backend index>",
656 .function = ipsec_select_backend_command_fn,
661 static clib_error_t *
662 clear_ipsec_counters_command_fn (vlib_main_t * vm,
663 unformat_input_t * input,
664 vlib_cli_command_t * cmd)
666 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
667 vlib_clear_combined_counters (&ipsec_sa_counters);
673 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
674 .path = "clear ipsec counters",
675 .short_help = "clear ipsec counters",
676 .function = clear_ipsec_counters_command_fn,
680 static clib_error_t *
681 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
682 unformat_input_t * input,
683 vlib_cli_command_t * cmd)
685 unformat_input_t _line_input, *line_input = &_line_input;
686 ipsec_add_del_tunnel_args_t a;
691 clib_error_t *error = NULL;
692 ipsec_key_t rck = { 0 };
693 ipsec_key_t lck = { 0 };
694 ipsec_key_t lik = { 0 };
695 ipsec_key_t rik = { 0 };
697 clib_memset (&a, 0, sizeof (a));
700 /* Get a line of input. */
701 if (!unformat_user (input, unformat_line_input, line_input))
704 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
707 (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
710 ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
715 (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
718 ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
722 else if (unformat (line_input, "local-spi %u", &a.local_spi))
724 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
726 else if (unformat (line_input, "instance %u", &a.show_instance))
728 else if (unformat (line_input, "salt 0x%x", &a.salt))
730 else if (unformat (line_input, "udp-encap"))
732 else if (unformat (line_input, "use-esn"))
734 else if (unformat (line_input, "use-anti-replay"))
736 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
740 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
744 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
746 else if (unformat (line_input, "crypto-alg %U",
747 unformat_ipsec_crypto_alg, &a.crypto_alg))
751 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
755 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
757 else if (unformat (line_input, "integ-alg %U",
758 unformat_ipsec_integ_alg, &a.integ_alg))
760 else if (unformat (line_input, "del"))
764 error = clib_error_return (0, "unknown input `%U'",
765 format_unformat_error, line_input);
772 error = clib_error_return (0, "mandatory argument(s) missing");
776 if (ipv4_set && ipv6_set)
777 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
781 clib_memcpy (a.local_crypto_key, lck.data, lck.len);
782 a.local_crypto_key_len = lck.len;
783 clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
784 a.remote_crypto_key_len = rck.len;
786 clib_memcpy (a.local_integ_key, lik.data, lik.len);
787 a.local_integ_key_len = lck.len;
788 clib_memcpy (a.remote_integ_key, rik.data, rik.len);
789 a.remote_integ_key_len = rck.len;
791 rv = ipsec_add_del_tunnel_if (&a);
797 case VNET_API_ERROR_INVALID_VALUE:
799 error = clib_error_return (0,
800 "IPSec tunnel interface already exists...");
802 error = clib_error_return (0, "IPSec tunnel interface not exists...");
805 error = clib_error_return (0, "ipsec_register_interface returned %d",
811 unformat_free (line_input);
817 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
818 .path = "create ipsec tunnel",
819 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
820 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
821 "[tx-table <table-id>]",
822 .function = create_ipsec_tunnel_command_fn,
827 ipsec_cli_init (vlib_main_t * vm)
832 VLIB_INIT_FUNCTION (ipsec_cli_init);
836 * fd.io coding-style-patch-verification: ON
839 * eval: (c-set-style "gnu")