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;
230 clib_memset (&p, 0, sizeof (p));
231 p.lport.stop = p.rport.stop = ~0;
232 p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0;
233 p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0;
234 p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0;
236 if (!unformat_user (input, unformat_line_input, line_input))
239 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
241 if (unformat (line_input, "add"))
243 else if (unformat (line_input, "del"))
245 else if (unformat (line_input, "spd %u", &p.id))
247 else if (unformat (line_input, "inbound"))
249 else if (unformat (line_input, "outbound"))
251 else if (unformat (line_input, "priority %d", &p.priority))
253 else if (unformat (line_input, "protocol %u", &tmp))
254 p.protocol = (u8) tmp;
257 (line_input, "action %U", unformat_ipsec_policy_action,
260 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
262 error = clib_error_return (0, "unsupported action: 'resolve'");
266 else if (unformat (line_input, "sa %u", &p.sa_id))
268 else if (unformat (line_input, "local-ip-range %U - %U",
269 unformat_ip4_address, &p.laddr.start.ip4,
270 unformat_ip4_address, &p.laddr.stop.ip4))
272 else if (unformat (line_input, "remote-ip-range %U - %U",
273 unformat_ip4_address, &p.raddr.start.ip4,
274 unformat_ip4_address, &p.raddr.stop.ip4))
276 else if (unformat (line_input, "local-ip-range %U - %U",
277 unformat_ip6_address, &p.laddr.start.ip6,
278 unformat_ip6_address, &p.laddr.stop.ip6))
282 else if (unformat (line_input, "remote-ip-range %U - %U",
283 unformat_ip6_address, &p.raddr.start.ip6,
284 unformat_ip6_address, &p.raddr.stop.ip6))
288 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
294 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
301 error = clib_error_return (0, "parse error: '%U'",
302 format_unformat_error, line_input);
307 /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */
311 ipsec_main_t *im = &ipsec_main;
313 p1 = hash_get (im->sa_index_by_sa_id, p.sa_id);
317 clib_error_return (0, "SA with index %u not found", p.sa_id);
320 sa = pool_elt_at_index (im->sad, p1[0]);
321 if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6)
323 error = clib_error_return (0, "AH not supported for IPV6: '%U'",
324 format_unformat_error, line_input);
328 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
331 vlib_cli_output (vm, "policy-index:%d", stat_index);
333 vlib_cli_output (vm, "error:%d", rv);
336 unformat_free (line_input);
342 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
343 .path = "ipsec policy",
345 "ipsec policy [add|del] spd <id> priority <n> ",
346 .function = ipsec_policy_add_del_command_fn,
350 static clib_error_t *
351 set_ipsec_sa_key_command_fn (vlib_main_t * vm,
352 unformat_input_t * input,
353 vlib_cli_command_t * cmd)
355 unformat_input_t _line_input, *line_input = &_line_input;
356 clib_error_t *error = NULL;
360 if (!unformat_user (input, unformat_line_input, line_input))
363 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
365 if (unformat (line_input, "%u", &id))
368 if (unformat (line_input, "crypto-key %U", unformat_ipsec_key, &ck))
370 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
374 error = clib_error_return (0, "parse error: '%U'",
375 format_unformat_error, line_input);
380 ipsec_set_sa_key (id, &ck, &ik);
383 unformat_free (line_input);
389 VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = {
390 .path = "set ipsec sa",
392 "set ipsec sa <id> crypto-key <key> integ-key <key>",
393 .function = set_ipsec_sa_key_command_fn,
397 static clib_error_t *
398 show_ipsec_command_fn (vlib_main_t * vm,
399 unformat_input_t * input, vlib_cli_command_t * cmd)
401 ipsec_main_t *im = &ipsec_main;
402 u32 spd_id, sw_if_index, sai;
403 vnet_hw_interface_t *hi;
404 ipsec_tunnel_if_t *t;
410 pool_foreach_index (sai, im->sad, ({
411 vlib_cli_output(vm, "%U", format_ipsec_sa, sai);
413 pool_foreach_index (i, im->spds, ({
414 vlib_cli_output(vm, "%U", format_ipsec_spd, i);
417 vlib_cli_output (vm, "SPD Bindings:");
419 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
420 vlib_cli_output (vm, " %d -> %U", spd_id,
421 format_vnet_sw_if_index_name, im->vnet_main,
426 vlib_cli_output (vm, "tunnel interfaces");
428 pool_foreach (t, im->tunnel_interfaces, ({
429 if (t->hw_if_index == ~0)
431 hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index);
433 vlib_cli_output(vm, " %s", hi->name);
435 vlib_cli_output(vm, " out-bound sa");
436 vlib_cli_output(vm, " %U", format_ipsec_sa, t->output_sa_index);
438 vlib_cli_output(vm, " in-bound sa");
439 vlib_cli_output(vm, " %U", format_ipsec_sa, t->input_sa_index);
448 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
449 .path = "show ipsec",
450 .short_help = "show ipsec [backends]",
451 .function = show_ipsec_command_fn,
455 static clib_error_t *
456 ipsec_show_backends_command_fn (vlib_main_t * vm,
457 unformat_input_t * input,
458 vlib_cli_command_t * cmd)
460 ipsec_main_t *im = &ipsec_main;
463 (void) unformat (input, "verbose %u", &verbose);
465 vlib_cli_output (vm, "IPsec AH backends available:");
466 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
467 ipsec_ah_backend_t *ab;
469 pool_foreach (ab, im->ah_backends, {
470 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
471 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
474 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
475 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
476 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
477 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
478 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
479 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
480 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
481 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
485 vlib_cli_output (vm, "%v", s);
487 vlib_cli_output (vm, "IPsec ESP backends available:");
488 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
489 ipsec_esp_backend_t *eb;
491 pool_foreach (eb, im->esp_backends, {
492 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
493 eb - im->esp_backends == im->esp_current_backend ? "yes"
497 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
498 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
499 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
500 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
501 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
502 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
503 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
504 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
508 vlib_cli_output (vm, "%v", s);
515 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
516 .path = "show ipsec backends",
517 .short_help = "show ipsec backends",
518 .function = ipsec_show_backends_command_fn,
522 static clib_error_t *
523 ipsec_select_backend_command_fn (vlib_main_t * vm,
524 unformat_input_t * input,
525 vlib_cli_command_t * cmd)
528 ipsec_main_t *im = &ipsec_main;
530 if (pool_elts (im->sad) > 0)
532 return clib_error_return (0,
533 "Cannot change IPsec backend, while %u SA entries are configured",
534 pool_elts (im->sad));
537 unformat_input_t _line_input, *line_input = &_line_input;
538 /* Get a line of input. */
539 if (!unformat_user (input, unformat_line_input, line_input))
542 if (unformat (line_input, "ah"))
544 if (unformat (line_input, "%u", &backend_index))
546 if (ipsec_select_ah_backend (im, backend_index) < 0)
548 return clib_error_return (0, "Invalid AH backend index `%u'",
554 return clib_error_return (0, "Invalid backend index `%U'",
555 format_unformat_error, line_input);
558 else if (unformat (line_input, "esp"))
560 if (unformat (line_input, "%u", &backend_index))
562 if (ipsec_select_esp_backend (im, backend_index) < 0)
564 return clib_error_return (0, "Invalid ESP backend index `%u'",
570 return clib_error_return (0, "Invalid backend index `%U'",
571 format_unformat_error, line_input);
576 return clib_error_return (0, "Unknown input `%U'",
577 format_unformat_error, line_input);
584 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
585 .path = "ipsec select backend",
586 .short_help = "ipsec select backend <ah|esp> <backend index>",
587 .function = ipsec_select_backend_command_fn,
592 static clib_error_t *
593 clear_ipsec_counters_command_fn (vlib_main_t * vm,
594 unformat_input_t * input,
595 vlib_cli_command_t * cmd)
597 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
598 vlib_clear_combined_counters (&ipsec_sa_counters);
604 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
605 .path = "clear ipsec counters",
606 .short_help = "clear ipsec counters",
607 .function = clear_ipsec_counters_command_fn,
611 static clib_error_t *
612 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
613 unformat_input_t * input,
614 vlib_cli_command_t * cmd)
616 unformat_input_t _line_input, *line_input = &_line_input;
617 ipsec_add_del_tunnel_args_t a;
622 clib_error_t *error = NULL;
623 ipsec_key_t rck = { 0 };
624 ipsec_key_t lck = { 0 };
625 ipsec_key_t lik = { 0 };
626 ipsec_key_t rik = { 0 };
628 clib_memset (&a, 0, sizeof (a));
631 /* Get a line of input. */
632 if (!unformat_user (input, unformat_line_input, line_input))
635 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
638 (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
641 ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
646 (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
649 ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
653 else if (unformat (line_input, "local-spi %u", &a.local_spi))
655 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
657 else if (unformat (line_input, "instance %u", &a.show_instance))
659 else if (unformat (line_input, "udp-encap"))
661 else if (unformat (line_input, "use-esn"))
663 else if (unformat (line_input, "use-anti-replay"))
665 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
669 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
673 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
675 else if (unformat (line_input, "crypto-alg %U",
676 unformat_ipsec_crypto_alg, &a.crypto_alg))
680 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
684 (line_input, "rmote-integ-key %U", unformat_ipsec_key, &rik))
686 else if (unformat (line_input, "integ-alg %U",
687 unformat_ipsec_integ_alg, &a.integ_alg))
689 else if (unformat (line_input, "del"))
693 error = clib_error_return (0, "unknown input `%U'",
694 format_unformat_error, line_input);
701 error = clib_error_return (0, "mandatory argument(s) missing");
706 return clib_error_return (0, "currently only IPv4 supported");
708 if (ipv4_set && ipv6_set)
709 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
711 clib_memcpy (a.local_crypto_key, lck.data, lck.len);
712 a.local_crypto_key_len = lck.len;
713 clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
714 a.remote_crypto_key_len = rck.len;
716 clib_memcpy (a.local_integ_key, lik.data, lik.len);
717 a.local_integ_key_len = lck.len;
718 clib_memcpy (a.remote_integ_key, rik.data, rik.len);
719 a.remote_integ_key_len = rck.len;
721 rv = ipsec_add_del_tunnel_if (&a);
727 case VNET_API_ERROR_INVALID_VALUE:
729 error = clib_error_return (0,
730 "IPSec tunnel interface already exists...");
732 error = clib_error_return (0, "IPSec tunnel interface not exists...");
735 error = clib_error_return (0, "ipsec_register_interface returned %d",
741 unformat_free (line_input);
747 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
748 .path = "create ipsec tunnel",
749 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
750 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
751 "[tx-table <table-id>]",
752 .function = create_ipsec_tunnel_command_fn,
756 static clib_error_t *
757 set_interface_key_command_fn (vlib_main_t * vm,
758 unformat_input_t * input,
759 vlib_cli_command_t * cmd)
761 unformat_input_t _line_input, *line_input = &_line_input;
762 ipsec_main_t *im = &ipsec_main;
763 ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
764 u32 hw_if_index = (u32) ~ 0;
767 clib_error_t *error = NULL;
769 if (!unformat_user (input, unformat_line_input, line_input))
772 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
774 if (unformat (line_input, "%U",
775 unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
779 (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
780 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
783 (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
784 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
787 (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
788 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
791 (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
792 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
793 else if (unformat (line_input, "%U", unformat_hex_string, &key))
797 error = clib_error_return (0, "parse error: '%U'",
798 format_unformat_error, line_input);
803 if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
805 error = clib_error_return (0, "unknown key type");
809 if (alg > 0 && vec_len (key) == 0)
811 error = clib_error_return (0, "key is not specified");
815 if (hw_if_index == (u32) ~ 0)
817 error = clib_error_return (0, "interface not specified");
821 ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
825 unformat_free (line_input);
831 VLIB_CLI_COMMAND (set_interface_key_command, static) = {
832 .path = "set interface ipsec key",
834 "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
835 .function = set_interface_key_command_fn,
840 ipsec_cli_init (vlib_main_t * vm)
845 VLIB_INIT_FUNCTION (ipsec_cli_init);
849 * fd.io coding-style-patch-verification: ON
852 * eval: (c-set-style "gnu")