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;
378 vlib_cli_output (vm, "SPD Bindings:");
381 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
382 vlib_cli_output (vm, " %d -> %U", spd_id,
383 format_vnet_sw_if_index_name, im->vnet_main,
390 ipsec_tunnel_show_all (vlib_main_t * vm, ipsec_main_t * im)
394 vlib_cli_output (vm, "Tunnel interfaces");
396 pool_foreach_index (ti, im->tunnel_interfaces, ({
397 vlib_cli_output(vm, " %U", format_ipsec_tunnel, ti);
402 static clib_error_t *
403 show_ipsec_command_fn (vlib_main_t * vm,
404 unformat_input_t * input, vlib_cli_command_t * cmd)
406 ipsec_main_t *im = &ipsec_main;
408 ipsec_sa_show_all (vm, im);
409 ipsec_spd_show_all (vm, im);
410 ipsec_spd_bindings_show_all (vm, im);
411 ipsec_tunnel_show_all (vm, im);
417 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
418 .path = "show ipsec all",
419 .short_help = "show ipsec all",
420 .function = show_ipsec_command_fn,
424 static clib_error_t *
425 show_ipsec_sa_command_fn (vlib_main_t * vm,
426 unformat_input_t * input, vlib_cli_command_t * cmd)
428 ipsec_main_t *im = &ipsec_main;
431 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
433 if (unformat (input, "%u", &sai))
440 ipsec_sa_show_all (vm, im);
442 vlib_cli_output (vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_DETAIL);
448 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
449 .path = "show ipsec sa",
450 .short_help = "show ipsec sa [index]",
451 .function = show_ipsec_sa_command_fn,
455 static clib_error_t *
456 show_ipsec_spd_command_fn (vlib_main_t * vm,
457 unformat_input_t * input, vlib_cli_command_t * cmd)
459 ipsec_main_t *im = &ipsec_main;
460 u8 show_bindings = 0;
463 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
465 if (unformat (input, "%u", &spdi))
467 else if (unformat (input, "bindings"))
474 ipsec_spd_bindings_show_all (vm, im);
476 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
478 ipsec_spd_show_all (vm, im);
484 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
485 .path = "show ipsec spd",
486 .short_help = "show ipsec spd [index]",
487 .function = show_ipsec_spd_command_fn,
491 static clib_error_t *
492 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
493 unformat_input_t * input,
494 vlib_cli_command_t * cmd)
496 ipsec_main_t *im = &ipsec_main;
499 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
501 if (unformat (input, "%u", &ti))
508 vlib_cli_output (vm, "%U", format_ipsec_tunnel, ti);
510 ipsec_tunnel_show_all (vm, im);
516 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
517 .path = "show ipsec tunnel",
518 .short_help = "show ipsec tunnel [index]",
519 .function = show_ipsec_tunnel_command_fn,
523 static clib_error_t *
524 ipsec_show_backends_command_fn (vlib_main_t * vm,
525 unformat_input_t * input,
526 vlib_cli_command_t * cmd)
528 ipsec_main_t *im = &ipsec_main;
531 (void) unformat (input, "verbose %u", &verbose);
533 vlib_cli_output (vm, "IPsec AH backends available:");
534 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
535 ipsec_ah_backend_t *ab;
537 pool_foreach (ab, im->ah_backends, {
538 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
539 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
542 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
543 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
544 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
545 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
546 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
547 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
548 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
549 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
553 vlib_cli_output (vm, "%v", s);
555 vlib_cli_output (vm, "IPsec ESP backends available:");
556 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
557 ipsec_esp_backend_t *eb;
559 pool_foreach (eb, im->esp_backends, {
560 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
561 eb - im->esp_backends == im->esp_current_backend ? "yes"
565 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
566 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
567 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
568 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
569 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
570 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
571 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
572 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
576 vlib_cli_output (vm, "%v", s);
583 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
584 .path = "show ipsec backends",
585 .short_help = "show ipsec backends",
586 .function = ipsec_show_backends_command_fn,
590 static clib_error_t *
591 ipsec_select_backend_command_fn (vlib_main_t * vm,
592 unformat_input_t * input,
593 vlib_cli_command_t * cmd)
595 unformat_input_t _line_input, *line_input = &_line_input;
596 ipsec_main_t *im = &ipsec_main;
600 error = ipsec_rsc_in_use (im);
605 /* Get a line of input. */
606 if (!unformat_user (input, unformat_line_input, line_input))
609 if (unformat (line_input, "ah"))
611 if (unformat (line_input, "%u", &backend_index))
613 if (ipsec_select_ah_backend (im, backend_index) < 0)
615 return clib_error_return (0, "Invalid AH backend index `%u'",
621 return clib_error_return (0, "Invalid backend index `%U'",
622 format_unformat_error, line_input);
625 else if (unformat (line_input, "esp"))
627 if (unformat (line_input, "%u", &backend_index))
629 if (ipsec_select_esp_backend (im, backend_index) < 0)
631 return clib_error_return (0, "Invalid ESP backend index `%u'",
637 return clib_error_return (0, "Invalid backend index `%U'",
638 format_unformat_error, line_input);
643 return clib_error_return (0, "Unknown input `%U'",
644 format_unformat_error, line_input);
651 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
652 .path = "ipsec select backend",
653 .short_help = "ipsec select backend <ah|esp> <backend index>",
654 .function = ipsec_select_backend_command_fn,
659 static clib_error_t *
660 clear_ipsec_counters_command_fn (vlib_main_t * vm,
661 unformat_input_t * input,
662 vlib_cli_command_t * cmd)
664 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
665 vlib_clear_combined_counters (&ipsec_sa_counters);
671 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
672 .path = "clear ipsec counters",
673 .short_help = "clear ipsec counters",
674 .function = clear_ipsec_counters_command_fn,
678 static clib_error_t *
679 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
680 unformat_input_t * input,
681 vlib_cli_command_t * cmd)
683 unformat_input_t _line_input, *line_input = &_line_input;
684 ipsec_add_del_tunnel_args_t a;
689 clib_error_t *error = NULL;
690 ipsec_key_t rck = { 0 };
691 ipsec_key_t lck = { 0 };
692 ipsec_key_t lik = { 0 };
693 ipsec_key_t rik = { 0 };
695 clib_memset (&a, 0, sizeof (a));
698 /* Get a line of input. */
699 if (!unformat_user (input, unformat_line_input, line_input))
702 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
705 (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
708 ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
713 (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
716 ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
720 else if (unformat (line_input, "local-spi %u", &a.local_spi))
722 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
724 else if (unformat (line_input, "instance %u", &a.show_instance))
726 else if (unformat (line_input, "salt 0x%x", &a.salt))
728 else if (unformat (line_input, "udp-encap"))
730 else if (unformat (line_input, "use-esn"))
732 else if (unformat (line_input, "use-anti-replay"))
734 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
738 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
742 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
744 else if (unformat (line_input, "crypto-alg %U",
745 unformat_ipsec_crypto_alg, &a.crypto_alg))
749 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
753 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
755 else if (unformat (line_input, "integ-alg %U",
756 unformat_ipsec_integ_alg, &a.integ_alg))
758 else if (unformat (line_input, "del"))
762 error = clib_error_return (0, "unknown input `%U'",
763 format_unformat_error, line_input);
770 error = clib_error_return (0, "mandatory argument(s) missing");
774 if (ipv4_set && ipv6_set)
775 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
779 clib_memcpy (a.local_crypto_key, lck.data, lck.len);
780 a.local_crypto_key_len = lck.len;
781 clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
782 a.remote_crypto_key_len = rck.len;
784 clib_memcpy (a.local_integ_key, lik.data, lik.len);
785 a.local_integ_key_len = lck.len;
786 clib_memcpy (a.remote_integ_key, rik.data, rik.len);
787 a.remote_integ_key_len = rck.len;
789 rv = ipsec_add_del_tunnel_if (&a);
795 case VNET_API_ERROR_INVALID_VALUE:
797 error = clib_error_return (0,
798 "IPSec tunnel interface already exists...");
800 error = clib_error_return (0, "IPSec tunnel interface not exists...");
803 error = clib_error_return (0, "ipsec_register_interface returned %d",
809 unformat_free (line_input);
815 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
816 .path = "create ipsec tunnel",
817 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
818 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
819 "[tx-table <table-id>]",
820 .function = create_ipsec_tunnel_command_fn,
825 ipsec_cli_init (vlib_main_t * vm)
830 VLIB_INIT_FUNCTION (ipsec_cli_init);
834 * fd.io coding-style-patch-verification: ON
837 * eval: (c-set-style "gnu")