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))
294 p.lport.start = clib_host_to_net_u16 (p.lport.start);
295 p.lport.stop = clib_host_to_net_u16 (p.lport.stop);
298 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
302 p.rport.start = clib_host_to_net_u16 (p.rport.start);
303 p.rport.stop = clib_host_to_net_u16 (p.rport.stop);
307 error = clib_error_return (0, "parse error: '%U'",
308 format_unformat_error, line_input);
313 /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */
317 ipsec_main_t *im = &ipsec_main;
319 p1 = hash_get (im->sa_index_by_sa_id, p.sa_id);
323 clib_error_return (0, "SA with index %u not found", p.sa_id);
326 sa = pool_elt_at_index (im->sad, p1[0]);
327 if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6)
329 error = clib_error_return (0, "AH not supported for IPV6: '%U'",
330 format_unformat_error, line_input);
335 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
339 error = clib_error_return (0, "unsupported policy type for:",
340 " outboud:%s %s action:%U",
341 (is_outbound ? "yes" : "no"),
342 (p.is_ipv6 ? "IPv4" : "IPv6"),
343 format_ipsec_policy_action, p.policy);
347 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
350 vlib_cli_output (vm, "policy-index:%d", stat_index);
352 vlib_cli_output (vm, "error:%d", rv);
355 unformat_free (line_input);
361 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
362 .path = "ipsec policy",
364 "ipsec policy [add|del] spd <id> priority <n> ",
365 .function = ipsec_policy_add_del_command_fn,
369 static clib_error_t *
370 set_ipsec_sa_key_command_fn (vlib_main_t * vm,
371 unformat_input_t * input,
372 vlib_cli_command_t * cmd)
374 unformat_input_t _line_input, *line_input = &_line_input;
375 clib_error_t *error = NULL;
379 if (!unformat_user (input, unformat_line_input, line_input))
382 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
384 if (unformat (line_input, "%u", &id))
387 if (unformat (line_input, "crypto-key %U", unformat_ipsec_key, &ck))
389 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
393 error = clib_error_return (0, "parse error: '%U'",
394 format_unformat_error, line_input);
399 ipsec_set_sa_key (id, &ck, &ik);
402 unformat_free (line_input);
408 VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = {
409 .path = "set ipsec sa",
411 "set ipsec sa <id> crypto-key <key> integ-key <key>",
412 .function = set_ipsec_sa_key_command_fn,
416 static clib_error_t *
417 show_ipsec_command_fn (vlib_main_t * vm,
418 unformat_input_t * input, vlib_cli_command_t * cmd)
420 ipsec_main_t *im = &ipsec_main;
421 u32 spd_id, sw_if_index, sai;
422 vnet_hw_interface_t *hi;
423 ipsec_tunnel_if_t *t;
429 pool_foreach_index (sai, im->sad, ({
430 vlib_cli_output(vm, "%U", format_ipsec_sa, sai);
432 pool_foreach_index (i, im->spds, ({
433 vlib_cli_output(vm, "%U", format_ipsec_spd, i);
436 vlib_cli_output (vm, "SPD Bindings:");
438 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
439 vlib_cli_output (vm, " %d -> %U", spd_id,
440 format_vnet_sw_if_index_name, im->vnet_main,
445 vlib_cli_output (vm, "tunnel interfaces");
447 pool_foreach (t, im->tunnel_interfaces, ({
448 if (t->hw_if_index == ~0)
450 hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index);
452 vlib_cli_output(vm, " %s", hi->name);
454 vlib_cli_output(vm, " out-bound sa");
455 vlib_cli_output(vm, " %U", format_ipsec_sa, t->output_sa_index);
457 vlib_cli_output(vm, " in-bound sa");
458 vlib_cli_output(vm, " %U", format_ipsec_sa, t->input_sa_index);
467 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
468 .path = "show ipsec",
469 .short_help = "show ipsec [backends]",
470 .function = show_ipsec_command_fn,
474 static clib_error_t *
475 ipsec_show_backends_command_fn (vlib_main_t * vm,
476 unformat_input_t * input,
477 vlib_cli_command_t * cmd)
479 ipsec_main_t *im = &ipsec_main;
482 (void) unformat (input, "verbose %u", &verbose);
484 vlib_cli_output (vm, "IPsec AH backends available:");
485 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
486 ipsec_ah_backend_t *ab;
488 pool_foreach (ab, im->ah_backends, {
489 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
490 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
493 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
494 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
495 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
496 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
497 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
498 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
499 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
500 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
504 vlib_cli_output (vm, "%v", s);
506 vlib_cli_output (vm, "IPsec ESP backends available:");
507 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
508 ipsec_esp_backend_t *eb;
510 pool_foreach (eb, im->esp_backends, {
511 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
512 eb - im->esp_backends == im->esp_current_backend ? "yes"
516 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
517 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
518 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
519 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
520 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
521 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
522 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
523 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
527 vlib_cli_output (vm, "%v", s);
534 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
535 .path = "show ipsec backends",
536 .short_help = "show ipsec backends",
537 .function = ipsec_show_backends_command_fn,
541 static clib_error_t *
542 ipsec_select_backend_command_fn (vlib_main_t * vm,
543 unformat_input_t * input,
544 vlib_cli_command_t * cmd)
547 ipsec_main_t *im = &ipsec_main;
549 if (pool_elts (im->sad) > 0)
551 return clib_error_return (0,
552 "Cannot change IPsec backend, while %u SA entries are configured",
553 pool_elts (im->sad));
556 unformat_input_t _line_input, *line_input = &_line_input;
557 /* Get a line of input. */
558 if (!unformat_user (input, unformat_line_input, line_input))
561 if (unformat (line_input, "ah"))
563 if (unformat (line_input, "%u", &backend_index))
565 if (ipsec_select_ah_backend (im, backend_index) < 0)
567 return clib_error_return (0, "Invalid AH backend index `%u'",
573 return clib_error_return (0, "Invalid backend index `%U'",
574 format_unformat_error, line_input);
577 else if (unformat (line_input, "esp"))
579 if (unformat (line_input, "%u", &backend_index))
581 if (ipsec_select_esp_backend (im, backend_index) < 0)
583 return clib_error_return (0, "Invalid ESP backend index `%u'",
589 return clib_error_return (0, "Invalid backend index `%U'",
590 format_unformat_error, line_input);
595 return clib_error_return (0, "Unknown input `%U'",
596 format_unformat_error, line_input);
603 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
604 .path = "ipsec select backend",
605 .short_help = "ipsec select backend <ah|esp> <backend index>",
606 .function = ipsec_select_backend_command_fn,
611 static clib_error_t *
612 clear_ipsec_counters_command_fn (vlib_main_t * vm,
613 unformat_input_t * input,
614 vlib_cli_command_t * cmd)
616 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
617 vlib_clear_combined_counters (&ipsec_sa_counters);
623 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
624 .path = "clear ipsec counters",
625 .short_help = "clear ipsec counters",
626 .function = clear_ipsec_counters_command_fn,
630 static clib_error_t *
631 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
632 unformat_input_t * input,
633 vlib_cli_command_t * cmd)
635 unformat_input_t _line_input, *line_input = &_line_input;
636 ipsec_add_del_tunnel_args_t a;
641 clib_error_t *error = NULL;
642 ipsec_key_t rck = { 0 };
643 ipsec_key_t lck = { 0 };
644 ipsec_key_t lik = { 0 };
645 ipsec_key_t rik = { 0 };
647 clib_memset (&a, 0, sizeof (a));
650 /* Get a line of input. */
651 if (!unformat_user (input, unformat_line_input, line_input))
654 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
657 (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
660 ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
665 (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
668 ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
672 else if (unformat (line_input, "local-spi %u", &a.local_spi))
674 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
676 else if (unformat (line_input, "instance %u", &a.show_instance))
678 else if (unformat (line_input, "udp-encap"))
680 else if (unformat (line_input, "use-esn"))
682 else if (unformat (line_input, "use-anti-replay"))
684 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
688 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
692 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
694 else if (unformat (line_input, "crypto-alg %U",
695 unformat_ipsec_crypto_alg, &a.crypto_alg))
699 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
703 (line_input, "rmote-integ-key %U", unformat_ipsec_key, &rik))
705 else if (unformat (line_input, "integ-alg %U",
706 unformat_ipsec_integ_alg, &a.integ_alg))
708 else if (unformat (line_input, "del"))
712 error = clib_error_return (0, "unknown input `%U'",
713 format_unformat_error, line_input);
720 error = clib_error_return (0, "mandatory argument(s) missing");
725 return clib_error_return (0, "currently only IPv4 supported");
727 if (ipv4_set && ipv6_set)
728 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
730 clib_memcpy (a.local_crypto_key, lck.data, lck.len);
731 a.local_crypto_key_len = lck.len;
732 clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
733 a.remote_crypto_key_len = rck.len;
735 clib_memcpy (a.local_integ_key, lik.data, lik.len);
736 a.local_integ_key_len = lck.len;
737 clib_memcpy (a.remote_integ_key, rik.data, rik.len);
738 a.remote_integ_key_len = rck.len;
740 rv = ipsec_add_del_tunnel_if (&a);
746 case VNET_API_ERROR_INVALID_VALUE:
748 error = clib_error_return (0,
749 "IPSec tunnel interface already exists...");
751 error = clib_error_return (0, "IPSec tunnel interface not exists...");
754 error = clib_error_return (0, "ipsec_register_interface returned %d",
760 unformat_free (line_input);
766 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
767 .path = "create ipsec tunnel",
768 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
769 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
770 "[tx-table <table-id>]",
771 .function = create_ipsec_tunnel_command_fn,
775 static clib_error_t *
776 set_interface_key_command_fn (vlib_main_t * vm,
777 unformat_input_t * input,
778 vlib_cli_command_t * cmd)
780 unformat_input_t _line_input, *line_input = &_line_input;
781 ipsec_main_t *im = &ipsec_main;
782 ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
783 u32 hw_if_index = (u32) ~ 0;
786 clib_error_t *error = NULL;
788 if (!unformat_user (input, unformat_line_input, line_input))
791 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
793 if (unformat (line_input, "%U",
794 unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
798 (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
799 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
802 (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
803 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
806 (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
807 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
810 (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
811 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
812 else if (unformat (line_input, "%U", unformat_hex_string, &key))
816 error = clib_error_return (0, "parse error: '%U'",
817 format_unformat_error, line_input);
822 if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
824 error = clib_error_return (0, "unknown key type");
828 if (alg > 0 && vec_len (key) == 0)
830 error = clib_error_return (0, "key is not specified");
834 if (hw_if_index == (u32) ~ 0)
836 error = clib_error_return (0, "interface not specified");
840 ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
844 unformat_free (line_input);
850 VLIB_CLI_COMMAND (set_interface_key_command, static) = {
851 .path = "set interface ipsec key",
853 "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
854 .function = set_interface_key_command_fn,
859 ipsec_cli_init (vlib_main_t * vm)
864 VLIB_INIT_FUNCTION (ipsec_cli_init);
868 * fd.io coding-style-patch-verification: ON
871 * eval: (c-set-style "gnu")