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;
95 if (!unformat_user (input, unformat_line_input, line_input))
98 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
100 if (unformat (line_input, "add %u", &id))
102 else if (unformat (line_input, "del %u", &id))
104 else if (unformat (line_input, "spi %u", &spi))
106 else if (unformat (line_input, "esp"))
107 proto = IPSEC_PROTOCOL_ESP;
108 else if (unformat (line_input, "ah"))
109 proto = IPSEC_PROTOCOL_AH;
110 else if (unformat (line_input, "crypto-key %U",
111 unformat_ipsec_key, &ck))
113 else if (unformat (line_input, "crypto-alg %U",
114 unformat_ipsec_crypto_alg, &crypto_alg))
116 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
118 else if (unformat (line_input, "integ-alg %U",
119 unformat_ipsec_integ_alg, &integ_alg))
121 else if (unformat (line_input, "tunnel-src %U",
122 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
124 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
125 if (!ip46_address_is_ip4 (&tun_src))
126 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
128 else if (unformat (line_input, "tunnel-dst %U",
129 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
131 else if (unformat (line_input, "udp-encap"))
132 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
135 error = clib_error_return (0, "parse error: '%U'",
136 format_unformat_error, line_input);
142 rv = ipsec_sa_add (id, spi, proto, crypto_alg,
143 &ck, integ_alg, &ik, flags,
144 0, &tun_src, &tun_dst, NULL);
146 rv = ipsec_sa_del (id);
149 clib_error_return (0, "failed");
152 unformat_free (line_input);
158 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
161 "ipsec sa [add|del]",
162 .function = ipsec_sa_add_del_command_fn,
166 static clib_error_t *
167 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
168 unformat_input_t * input,
169 vlib_cli_command_t * cmd)
171 unformat_input_t _line_input, *line_input = &_line_input;
174 clib_error_t *error = NULL;
176 if (!unformat_user (input, unformat_line_input, line_input))
179 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
181 if (unformat (line_input, "add"))
183 else if (unformat (line_input, "del"))
185 else if (unformat (line_input, "%u", &spd_id))
189 error = clib_error_return (0, "parse error: '%U'",
190 format_unformat_error, line_input);
197 error = clib_error_return (0, "please specify SPD ID");
201 ipsec_add_del_spd (vm, spd_id, is_add);
204 unformat_free (line_input);
210 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
213 "ipsec spd [add|del] <id>",
214 .function = ipsec_spd_add_del_command_fn,
219 static clib_error_t *
220 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
221 unformat_input_t * input,
222 vlib_cli_command_t * cmd)
224 unformat_input_t _line_input, *line_input = &_line_input;
227 u32 tmp, tmp2, stat_index;
228 clib_error_t *error = NULL;
231 clib_memset (&p, 0, sizeof (p));
232 p.lport.stop = p.rport.stop = ~0;
233 p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0;
234 p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0;
235 p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0;
238 if (!unformat_user (input, unformat_line_input, line_input))
241 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
243 if (unformat (line_input, "add"))
245 else if (unformat (line_input, "del"))
247 else if (unformat (line_input, "spd %u", &p.id))
249 else if (unformat (line_input, "inbound"))
251 else if (unformat (line_input, "outbound"))
253 else if (unformat (line_input, "priority %d", &p.priority))
255 else if (unformat (line_input, "protocol %u", &tmp))
256 p.protocol = (u8) tmp;
259 (line_input, "action %U", unformat_ipsec_policy_action,
262 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
264 error = clib_error_return (0, "unsupported action: 'resolve'");
268 else if (unformat (line_input, "sa %u", &p.sa_id))
270 else if (unformat (line_input, "local-ip-range %U - %U",
271 unformat_ip4_address, &p.laddr.start.ip4,
272 unformat_ip4_address, &p.laddr.stop.ip4))
274 else if (unformat (line_input, "remote-ip-range %U - %U",
275 unformat_ip4_address, &p.raddr.start.ip4,
276 unformat_ip4_address, &p.raddr.stop.ip4))
278 else if (unformat (line_input, "local-ip-range %U - %U",
279 unformat_ip6_address, &p.laddr.start.ip6,
280 unformat_ip6_address, &p.laddr.stop.ip6))
284 else if (unformat (line_input, "remote-ip-range %U - %U",
285 unformat_ip6_address, &p.raddr.start.ip6,
286 unformat_ip6_address, &p.raddr.stop.ip6))
290 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
296 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
303 error = clib_error_return (0, "parse error: '%U'",
304 format_unformat_error, line_input);
309 /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */
313 ipsec_main_t *im = &ipsec_main;
315 p1 = hash_get (im->sa_index_by_sa_id, p.sa_id);
319 clib_error_return (0, "SA with index %u not found", p.sa_id);
322 sa = pool_elt_at_index (im->sad, p1[0]);
323 if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6)
325 error = clib_error_return (0, "AH not supported for IPV6: '%U'",
326 format_unformat_error, line_input);
331 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
335 error = clib_error_return (0, "unsupported policy type for:",
336 " outboud:%s %s action:%U",
337 (is_outbound ? "yes" : "no"),
338 (p.is_ipv6 ? "IPv4" : "IPv6"),
339 format_ipsec_policy_action, p.policy);
343 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
346 vlib_cli_output (vm, "policy-index:%d", stat_index);
348 vlib_cli_output (vm, "error:%d", rv);
351 unformat_free (line_input);
357 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
358 .path = "ipsec policy",
360 "ipsec policy [add|del] spd <id> priority <n> ",
361 .function = ipsec_policy_add_del_command_fn,
365 static clib_error_t *
366 set_ipsec_sa_key_command_fn (vlib_main_t * vm,
367 unformat_input_t * input,
368 vlib_cli_command_t * cmd)
370 unformat_input_t _line_input, *line_input = &_line_input;
371 clib_error_t *error = NULL;
375 if (!unformat_user (input, unformat_line_input, line_input))
378 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
380 if (unformat (line_input, "%u", &id))
383 if (unformat (line_input, "crypto-key %U", unformat_ipsec_key, &ck))
385 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
389 error = clib_error_return (0, "parse error: '%U'",
390 format_unformat_error, line_input);
395 ipsec_set_sa_key (id, &ck, &ik);
398 unformat_free (line_input);
404 VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = {
405 .path = "set ipsec sa",
407 "set ipsec sa <id> crypto-key <key> integ-key <key>",
408 .function = set_ipsec_sa_key_command_fn,
412 static clib_error_t *
413 show_ipsec_command_fn (vlib_main_t * vm,
414 unformat_input_t * input, vlib_cli_command_t * cmd)
416 ipsec_main_t *im = &ipsec_main;
417 u32 spd_id, sw_if_index, sai;
418 vnet_hw_interface_t *hi;
419 ipsec_tunnel_if_t *t;
425 pool_foreach_index (sai, im->sad, ({
426 vlib_cli_output(vm, "%U", format_ipsec_sa, sai);
428 pool_foreach_index (i, im->spds, ({
429 vlib_cli_output(vm, "%U", format_ipsec_spd, i);
432 vlib_cli_output (vm, "SPD Bindings:");
434 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
435 vlib_cli_output (vm, " %d -> %U", spd_id,
436 format_vnet_sw_if_index_name, im->vnet_main,
441 vlib_cli_output (vm, "tunnel interfaces");
443 pool_foreach (t, im->tunnel_interfaces, ({
444 if (t->hw_if_index == ~0)
446 hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index);
448 vlib_cli_output(vm, " %s", hi->name);
450 vlib_cli_output(vm, " out-bound sa");
451 vlib_cli_output(vm, " %U", format_ipsec_sa, t->output_sa_index);
453 vlib_cli_output(vm, " in-bound sa");
454 vlib_cli_output(vm, " %U", format_ipsec_sa, t->input_sa_index);
463 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
464 .path = "show ipsec",
465 .short_help = "show ipsec [backends]",
466 .function = show_ipsec_command_fn,
470 static clib_error_t *
471 ipsec_show_backends_command_fn (vlib_main_t * vm,
472 unformat_input_t * input,
473 vlib_cli_command_t * cmd)
475 ipsec_main_t *im = &ipsec_main;
478 (void) unformat (input, "verbose %u", &verbose);
480 vlib_cli_output (vm, "IPsec AH backends available:");
481 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
482 ipsec_ah_backend_t *ab;
484 pool_foreach (ab, im->ah_backends, {
485 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
486 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
489 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
490 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
491 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
492 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
493 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
494 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
495 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
496 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
500 vlib_cli_output (vm, "%v", s);
502 vlib_cli_output (vm, "IPsec ESP backends available:");
503 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
504 ipsec_esp_backend_t *eb;
506 pool_foreach (eb, im->esp_backends, {
507 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
508 eb - im->esp_backends == im->esp_current_backend ? "yes"
512 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
513 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
514 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
515 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
516 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
517 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
518 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
519 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
523 vlib_cli_output (vm, "%v", s);
530 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
531 .path = "show ipsec backends",
532 .short_help = "show ipsec backends",
533 .function = ipsec_show_backends_command_fn,
537 static clib_error_t *
538 ipsec_select_backend_command_fn (vlib_main_t * vm,
539 unformat_input_t * input,
540 vlib_cli_command_t * cmd)
543 ipsec_main_t *im = &ipsec_main;
545 if (pool_elts (im->sad) > 0)
547 return clib_error_return (0,
548 "Cannot change IPsec backend, while %u SA entries are configured",
549 pool_elts (im->sad));
552 unformat_input_t _line_input, *line_input = &_line_input;
553 /* Get a line of input. */
554 if (!unformat_user (input, unformat_line_input, line_input))
557 if (unformat (line_input, "ah"))
559 if (unformat (line_input, "%u", &backend_index))
561 if (ipsec_select_ah_backend (im, backend_index) < 0)
563 return clib_error_return (0, "Invalid AH backend index `%u'",
569 return clib_error_return (0, "Invalid backend index `%U'",
570 format_unformat_error, line_input);
573 else if (unformat (line_input, "esp"))
575 if (unformat (line_input, "%u", &backend_index))
577 if (ipsec_select_esp_backend (im, backend_index) < 0)
579 return clib_error_return (0, "Invalid ESP backend index `%u'",
585 return clib_error_return (0, "Invalid backend index `%U'",
586 format_unformat_error, line_input);
591 return clib_error_return (0, "Unknown input `%U'",
592 format_unformat_error, line_input);
599 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
600 .path = "ipsec select backend",
601 .short_help = "ipsec select backend <ah|esp> <backend index>",
602 .function = ipsec_select_backend_command_fn,
607 static clib_error_t *
608 clear_ipsec_counters_command_fn (vlib_main_t * vm,
609 unformat_input_t * input,
610 vlib_cli_command_t * cmd)
612 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
613 vlib_clear_combined_counters (&ipsec_sa_counters);
619 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
620 .path = "clear ipsec counters",
621 .short_help = "clear ipsec counters",
622 .function = clear_ipsec_counters_command_fn,
626 static clib_error_t *
627 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
628 unformat_input_t * input,
629 vlib_cli_command_t * cmd)
631 unformat_input_t _line_input, *line_input = &_line_input;
632 ipsec_add_del_tunnel_args_t a;
637 clib_error_t *error = NULL;
638 ipsec_key_t rck = { 0 };
639 ipsec_key_t lck = { 0 };
640 ipsec_key_t lik = { 0 };
641 ipsec_key_t rik = { 0 };
643 clib_memset (&a, 0, sizeof (a));
646 /* Get a line of input. */
647 if (!unformat_user (input, unformat_line_input, line_input))
650 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
653 (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
656 ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
661 (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
664 ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
668 else if (unformat (line_input, "local-spi %u", &a.local_spi))
670 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
672 else if (unformat (line_input, "instance %u", &a.show_instance))
674 else if (unformat (line_input, "udp-encap"))
676 else if (unformat (line_input, "use-esn"))
678 else if (unformat (line_input, "use-anti-replay"))
680 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
684 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
688 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
690 else if (unformat (line_input, "crypto-alg %U",
691 unformat_ipsec_crypto_alg, &a.crypto_alg))
695 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
699 (line_input, "rmote-integ-key %U", unformat_ipsec_key, &rik))
701 else if (unformat (line_input, "integ-alg %U",
702 unformat_ipsec_integ_alg, &a.integ_alg))
704 else if (unformat (line_input, "del"))
708 error = clib_error_return (0, "unknown input `%U'",
709 format_unformat_error, line_input);
716 error = clib_error_return (0, "mandatory argument(s) missing");
721 return clib_error_return (0, "currently only IPv4 supported");
723 if (ipv4_set && ipv6_set)
724 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
726 clib_memcpy (a.local_crypto_key, lck.data, lck.len);
727 a.local_crypto_key_len = lck.len;
728 clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
729 a.remote_crypto_key_len = rck.len;
731 clib_memcpy (a.local_integ_key, lik.data, lik.len);
732 a.local_integ_key_len = lck.len;
733 clib_memcpy (a.remote_integ_key, rik.data, rik.len);
734 a.remote_integ_key_len = rck.len;
736 rv = ipsec_add_del_tunnel_if (&a);
742 case VNET_API_ERROR_INVALID_VALUE:
744 error = clib_error_return (0,
745 "IPSec tunnel interface already exists...");
747 error = clib_error_return (0, "IPSec tunnel interface not exists...");
750 error = clib_error_return (0, "ipsec_register_interface returned %d",
756 unformat_free (line_input);
762 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
763 .path = "create ipsec tunnel",
764 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
765 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
766 "[tx-table <table-id>]",
767 .function = create_ipsec_tunnel_command_fn,
771 static clib_error_t *
772 set_interface_key_command_fn (vlib_main_t * vm,
773 unformat_input_t * input,
774 vlib_cli_command_t * cmd)
776 unformat_input_t _line_input, *line_input = &_line_input;
777 ipsec_main_t *im = &ipsec_main;
778 ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
779 u32 hw_if_index = (u32) ~ 0;
782 clib_error_t *error = NULL;
784 if (!unformat_user (input, unformat_line_input, line_input))
787 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
789 if (unformat (line_input, "%U",
790 unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
794 (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
795 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
798 (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
799 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
802 (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
803 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
806 (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
807 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
808 else if (unformat (line_input, "%U", unformat_hex_string, &key))
812 error = clib_error_return (0, "parse error: '%U'",
813 format_unformat_error, line_input);
818 if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
820 error = clib_error_return (0, "unknown key type");
824 if (alg > 0 && vec_len (key) == 0)
826 error = clib_error_return (0, "key is not specified");
830 if (hw_if_index == (u32) ~ 0)
832 error = clib_error_return (0, "interface not specified");
836 ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
840 unformat_free (line_input);
846 VLIB_CLI_COMMAND (set_interface_key_command, static) = {
847 .path = "set interface ipsec key",
849 "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
850 .function = set_interface_key_command_fn,
855 ipsec_cli_init (vlib_main_t * vm)
860 VLIB_INIT_FUNCTION (ipsec_cli_init);
864 * fd.io coding-style-patch-verification: ON
867 * eval: (c-set-style "gnu")