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>
23 #include <vnet/ipip/ipip.h>
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/ipsec_tun.h>
29 set_interface_spd_command_fn (vlib_main_t * vm,
30 unformat_input_t * input,
31 vlib_cli_command_t * cmd)
33 unformat_input_t _line_input, *line_input = &_line_input;
34 ipsec_main_t *im = &ipsec_main;
35 u32 sw_if_index = (u32) ~ 0;
38 clib_error_t *error = NULL;
40 if (!unformat_user (input, unformat_line_input, line_input))
44 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
45 &sw_if_index, &spd_id))
47 else if (unformat (line_input, "del"))
51 error = clib_error_return (0, "parse error: '%U'",
52 format_unformat_error, line_input);
56 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
59 unformat_free (line_input);
65 VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
66 .path = "set interface ipsec spd",
68 "set interface ipsec spd <int> <id>",
69 .function = set_interface_spd_command_fn,
74 ipsec_sa_add_del_command_fn (vlib_main_t * vm,
75 unformat_input_t * input,
76 vlib_cli_command_t * cmd)
78 unformat_input_t _line_input, *line_input = &_line_input;
79 ip46_address_t tun_src = { }, tun_dst =
82 ipsec_crypto_alg_t crypto_alg;
83 ipsec_integ_alg_t integ_alg;
84 ipsec_protocol_t proto;
85 ipsec_sa_flags_t flags;
87 ipsec_key_t ck = { 0 };
88 ipsec_key_t ik = { 0 };
95 flags = IPSEC_SA_FLAG_NONE;
96 proto = IPSEC_PROTOCOL_ESP;
97 integ_alg = IPSEC_INTEG_ALG_NONE;
98 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
100 if (!unformat_user (input, unformat_line_input, line_input))
103 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
105 if (unformat (line_input, "add %u", &id))
107 else if (unformat (line_input, "del %u", &id))
109 else if (unformat (line_input, "spi %u", &spi))
111 else if (unformat (line_input, "salt 0x%x", &salt))
113 else if (unformat (line_input, "esp"))
114 proto = IPSEC_PROTOCOL_ESP;
115 else if (unformat (line_input, "ah"))
116 proto = IPSEC_PROTOCOL_AH;
117 else if (unformat (line_input, "crypto-key %U",
118 unformat_ipsec_key, &ck))
120 else if (unformat (line_input, "crypto-alg %U",
121 unformat_ipsec_crypto_alg, &crypto_alg))
123 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
125 else if (unformat (line_input, "integ-alg %U",
126 unformat_ipsec_integ_alg, &integ_alg))
128 else if (unformat (line_input, "tunnel-src %U",
129 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
131 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
132 if (!ip46_address_is_ip4 (&tun_src))
133 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
135 else if (unformat (line_input, "tunnel-dst %U",
136 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
138 else if (unformat (line_input, "udp-encap"))
139 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
142 error = clib_error_return (0, "parse error: '%U'",
143 format_unformat_error, line_input);
149 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
150 &ck, integ_alg, &ik, flags,
151 0, clib_host_to_net_u32 (salt),
152 &tun_src, &tun_dst, NULL);
154 rv = ipsec_sa_unlock_id (id);
157 error = clib_error_return (0, "failed");
160 unformat_free (line_input);
166 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
169 "ipsec sa [add|del]",
170 .function = ipsec_sa_add_del_command_fn,
174 static clib_error_t *
175 ipsec_spd_add_del_command_fn (vlib_main_t * vm,
176 unformat_input_t * input,
177 vlib_cli_command_t * cmd)
179 unformat_input_t _line_input, *line_input = &_line_input;
182 clib_error_t *error = NULL;
184 if (!unformat_user (input, unformat_line_input, line_input))
187 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
189 if (unformat (line_input, "add"))
191 else if (unformat (line_input, "del"))
193 else if (unformat (line_input, "%u", &spd_id))
197 error = clib_error_return (0, "parse error: '%U'",
198 format_unformat_error, line_input);
205 error = clib_error_return (0, "please specify SPD ID");
209 ipsec_add_del_spd (vm, spd_id, is_add);
212 unformat_free (line_input);
218 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
221 "ipsec spd [add|del] <id>",
222 .function = ipsec_spd_add_del_command_fn,
227 static clib_error_t *
228 ipsec_policy_add_del_command_fn (vlib_main_t * vm,
229 unformat_input_t * input,
230 vlib_cli_command_t * cmd)
232 unformat_input_t _line_input, *line_input = &_line_input;
235 u32 tmp, tmp2, stat_index;
236 clib_error_t *error = NULL;
239 clib_memset (&p, 0, sizeof (p));
240 p.lport.stop = p.rport.stop = ~0;
243 if (!unformat_user (input, unformat_line_input, line_input))
246 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
248 if (unformat (line_input, "add"))
250 else if (unformat (line_input, "del"))
252 else if (unformat (line_input, "spd %u", &p.id))
254 else if (unformat (line_input, "inbound"))
256 else if (unformat (line_input, "outbound"))
258 else if (unformat (line_input, "priority %d", &p.priority))
260 else if (unformat (line_input, "protocol %u", &tmp))
261 p.protocol = (u8) tmp;
264 (line_input, "action %U", unformat_ipsec_policy_action,
267 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
269 error = clib_error_return (0, "unsupported action: 'resolve'");
273 else if (unformat (line_input, "sa %u", &p.sa_id))
275 else if (unformat (line_input, "local-ip-range %U - %U",
276 unformat_ip4_address, &p.laddr.start.ip4,
277 unformat_ip4_address, &p.laddr.stop.ip4))
279 else if (unformat (line_input, "remote-ip-range %U - %U",
280 unformat_ip4_address, &p.raddr.start.ip4,
281 unformat_ip4_address, &p.raddr.stop.ip4))
283 else if (unformat (line_input, "local-ip-range %U - %U",
284 unformat_ip6_address, &p.laddr.start.ip6,
285 unformat_ip6_address, &p.laddr.stop.ip6))
289 else if (unformat (line_input, "remote-ip-range %U - %U",
290 unformat_ip6_address, &p.raddr.start.ip6,
291 unformat_ip6_address, &p.raddr.stop.ip6))
295 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
301 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
308 error = clib_error_return (0, "parse error: '%U'",
309 format_unformat_error, line_input);
314 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
318 error = clib_error_return (0, "unsupported policy type for:",
319 " outboud:%s %s action:%U",
320 (is_outbound ? "yes" : "no"),
321 (p.is_ipv6 ? "IPv4" : "IPv6"),
322 format_ipsec_policy_action, p.policy);
326 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
329 vlib_cli_output (vm, "policy-index:%d", stat_index);
331 vlib_cli_output (vm, "error:%d", rv);
334 unformat_free (line_input);
340 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
341 .path = "ipsec policy",
343 "ipsec policy [add|del] spd <id> priority <n> ",
344 .function = ipsec_policy_add_del_command_fn,
349 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
354 pool_foreach_index (sai, im->sad, ({
355 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
356 (detail ? IPSEC_FORMAT_DETAIL : 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_tun_protect_show_one (index_t itpi, void *ctx)
394 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect, itpi);
396 return (WALK_CONTINUE);
400 ipsec_tunnel_show_all (vlib_main_t * vm)
402 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
405 static clib_error_t *
406 show_ipsec_command_fn (vlib_main_t * vm,
407 unformat_input_t * input, vlib_cli_command_t * cmd)
409 ipsec_main_t *im = &ipsec_main;
411 ipsec_sa_show_all (vm, im, 0);
412 ipsec_spd_show_all (vm, im);
413 ipsec_spd_bindings_show_all (vm, im);
414 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
420 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
421 .path = "show ipsec all",
422 .short_help = "show ipsec all",
423 .function = show_ipsec_command_fn,
427 static clib_error_t *
428 show_ipsec_sa_command_fn (vlib_main_t * vm,
429 unformat_input_t * input, vlib_cli_command_t * cmd)
431 ipsec_main_t *im = &ipsec_main;
435 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
437 if (unformat (input, "%u", &sai))
439 if (unformat (input, "detail"))
446 ipsec_sa_show_all (vm, im, detail);
448 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
449 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
454 static clib_error_t *
455 clear_ipsec_sa_command_fn (vlib_main_t * vm,
456 unformat_input_t * input, vlib_cli_command_t * cmd)
458 ipsec_main_t *im = &ipsec_main;
461 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
463 if (unformat (input, "%u", &sai))
472 pool_foreach_index (sai, im->sad, ({
479 if (pool_is_free_index (im->sad, sai))
480 return clib_error_return (0, "unknown SA index: %d", sai);
482 ipsec_sa_clear (sai);
489 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
490 .path = "show ipsec sa",
491 .short_help = "show ipsec sa [index]",
492 .function = show_ipsec_sa_command_fn,
495 VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
496 .path = "clear ipsec sa",
497 .short_help = "clear ipsec sa [index]",
498 .function = clear_ipsec_sa_command_fn,
502 static clib_error_t *
503 show_ipsec_spd_command_fn (vlib_main_t * vm,
504 unformat_input_t * input, vlib_cli_command_t * cmd)
506 ipsec_main_t *im = &ipsec_main;
507 u8 show_bindings = 0;
510 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
512 if (unformat (input, "%u", &spdi))
514 else if (unformat (input, "bindings"))
521 ipsec_spd_bindings_show_all (vm, im);
523 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
525 ipsec_spd_show_all (vm, im);
531 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
532 .path = "show ipsec spd",
533 .short_help = "show ipsec spd [index]",
534 .function = show_ipsec_spd_command_fn,
538 static clib_error_t *
539 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
540 unformat_input_t * input,
541 vlib_cli_command_t * cmd)
543 ipsec_tunnel_show_all (vm);
549 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
550 .path = "show ipsec tunnel",
551 .short_help = "show ipsec tunnel",
552 .function = show_ipsec_tunnel_command_fn,
556 static clib_error_t *
557 ipsec_show_backends_command_fn (vlib_main_t * vm,
558 unformat_input_t * input,
559 vlib_cli_command_t * cmd)
561 ipsec_main_t *im = &ipsec_main;
564 (void) unformat (input, "verbose %u", &verbose);
566 vlib_cli_output (vm, "IPsec AH backends available:");
567 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
568 ipsec_ah_backend_t *ab;
570 pool_foreach (ab, im->ah_backends, {
571 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
572 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
575 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
576 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
577 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
578 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
579 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
580 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
581 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
582 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
586 vlib_cli_output (vm, "%v", s);
588 vlib_cli_output (vm, "IPsec ESP backends available:");
589 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
590 ipsec_esp_backend_t *eb;
592 pool_foreach (eb, im->esp_backends, {
593 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
594 eb - im->esp_backends == im->esp_current_backend ? "yes"
598 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
599 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
600 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
601 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
602 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
603 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
604 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
605 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
609 vlib_cli_output (vm, "%v", s);
616 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
617 .path = "show ipsec backends",
618 .short_help = "show ipsec backends",
619 .function = ipsec_show_backends_command_fn,
623 static clib_error_t *
624 ipsec_select_backend_command_fn (vlib_main_t * vm,
625 unformat_input_t * input,
626 vlib_cli_command_t * cmd)
628 unformat_input_t _line_input, *line_input = &_line_input;
629 ipsec_main_t *im = &ipsec_main;
633 error = ipsec_rsc_in_use (im);
638 /* Get a line of input. */
639 if (!unformat_user (input, unformat_line_input, line_input))
642 if (unformat (line_input, "ah"))
644 if (unformat (line_input, "%u", &backend_index))
646 if (ipsec_select_ah_backend (im, backend_index) < 0)
648 return clib_error_return (0, "Invalid AH backend index `%u'",
654 return clib_error_return (0, "Invalid backend index `%U'",
655 format_unformat_error, line_input);
658 else if (unformat (line_input, "esp"))
660 if (unformat (line_input, "%u", &backend_index))
662 if (ipsec_select_esp_backend (im, backend_index) < 0)
664 return clib_error_return (0, "Invalid ESP backend index `%u'",
670 return clib_error_return (0, "Invalid backend index `%U'",
671 format_unformat_error, line_input);
676 return clib_error_return (0, "Unknown input `%U'",
677 format_unformat_error, line_input);
684 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
685 .path = "ipsec select backend",
686 .short_help = "ipsec select backend <ah|esp> <backend index>",
687 .function = ipsec_select_backend_command_fn,
692 static clib_error_t *
693 clear_ipsec_counters_command_fn (vlib_main_t * vm,
694 unformat_input_t * input,
695 vlib_cli_command_t * cmd)
697 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
698 vlib_clear_combined_counters (&ipsec_sa_counters);
704 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
705 .path = "clear ipsec counters",
706 .short_help = "clear ipsec counters",
707 .function = clear_ipsec_counters_command_fn,
712 ipsec_tun_mk_local_sa_id (u32 ti)
714 return (0x80000000 | ti);
718 ipsec_tun_mk_remote_sa_id (u32 ti)
720 return (0xc0000000 | ti);
723 static clib_error_t *
724 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
725 unformat_input_t * input,
726 vlib_cli_command_t * cmd)
728 unformat_input_t _line_input, *line_input = &_line_input;
729 ip46_address_t local_ip = ip46_address_initializer;
730 ip46_address_t remote_ip = ip46_address_initializer;
731 ipsec_crypto_alg_t crypto_alg;
732 ipsec_integ_alg_t integ_alg;
733 ipsec_sa_flags_t flags;
734 u32 local_spi, remote_spi, salt, table_id, fib_index;
741 clib_error_t *error = NULL;
742 ipsec_key_t rck = { 0 };
743 ipsec_key_t lck = { 0 };
744 ipsec_key_t lik = { 0 };
745 ipsec_key_t rik = { 0 };
748 flags = IPSEC_SA_FLAG_NONE;
750 /* Get a line of input. */
751 if (!unformat_user (input, unformat_line_input, line_input))
754 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
757 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
760 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
765 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
768 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
771 else if (unformat (line_input, "local-spi %u", &local_spi))
773 else if (unformat (line_input, "remote-spi %u", &remote_spi))
775 else if (unformat (line_input, "salt 0x%x", &salt))
777 else if (unformat (line_input, "udp-encap"))
778 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
779 else if (unformat (line_input, "use-esn"))
780 flags |= IPSEC_SA_FLAG_USE_ESN;
781 else if (unformat (line_input, "use-anti-replay"))
782 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
783 else if (unformat (line_input, "instance %u", &instance))
785 else if (unformat (line_input, "tx-table %u", &table_id))
789 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
793 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
795 else if (unformat (line_input, "crypto-alg %U",
796 unformat_ipsec_crypto_alg, &crypto_alg))
800 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
804 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
806 else if (unformat (line_input, "integ-alg %U",
807 unformat_ipsec_integ_alg, &integ_alg))
809 else if (unformat (line_input, "del"))
813 error = clib_error_return (0, "unknown input `%U'",
814 format_unformat_error, line_input);
821 error = clib_error_return (0, "mandatory argument(s) missing");
825 if (ipv4_set && ipv6_set)
826 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
828 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
832 rv = VNET_API_ERROR_NO_SUCH_FIB;
838 // remote = input, local = output
841 /* create an ip-ip tunnel, then the two SA, then bind them */
843 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
844 instance, &local_ip, &remote_ip, fib_index,
845 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
846 TUNNEL_MODE_P2P, &sw_if_index);
848 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
849 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
850 &lck, integ_alg, &lik, flags, table_id,
851 clib_host_to_net_u32 (salt), &local_ip,
854 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
855 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
856 &rck, integ_alg, &rik,
857 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
858 clib_host_to_net_u32 (salt), &remote_ip,
861 ipsec_tun_protect_update_one (sw_if_index,
862 ipsec_tun_mk_local_sa_id (sw_if_index),
863 ipsec_tun_mk_remote_sa_id
873 case VNET_API_ERROR_INVALID_VALUE:
874 error = clib_error_return (0,
875 "IPSec tunnel interface already exists...");
878 error = clib_error_return (0, "ipsec_register_interface returned %d",
884 unformat_free (line_input);
890 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
891 .path = "create ipsec tunnel",
892 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
893 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
894 "[tx-table <table-id>]",
895 .function = create_ipsec_tunnel_command_fn,
899 static clib_error_t *
900 ipsec_tun_protect_cmd (vlib_main_t * vm,
901 unformat_input_t * input, vlib_cli_command_t * cmd)
903 unformat_input_t _line_input, *line_input = &_line_input;
904 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
909 vnm = vnet_get_main ();
911 if (!unformat_user (input, unformat_line_input, line_input))
914 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
916 if (unformat (line_input, "del"))
918 else if (unformat (line_input, "add"))
920 else if (unformat (line_input, "sa-in %d", &sa_in))
921 vec_add1 (sa_ins, sa_in);
922 else if (unformat (line_input, "sa-out %d", &sa_out))
924 else if (unformat (line_input, "%U",
925 unformat_vnet_sw_interface, vnm, &sw_if_index))
928 return (clib_error_return (0, "unknown input '%U'",
929 format_unformat_error, line_input));
933 ipsec_tun_protect_update (sw_if_index, sa_out, sa_ins);
935 unformat_free (line_input);
940 * Protect tunnel with IPSEC
943 VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
945 .path = "ipsec tunnel protect",
946 .function = ipsec_tun_protect_cmd,
947 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
948 // this is not MP safe
953 static clib_error_t *
954 ipsec_tun_protect_show (vlib_main_t * vm,
955 unformat_input_t * input, vlib_cli_command_t * cmd)
957 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
963 * show IPSEC tunnel protection
966 VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
968 .path = "show ipsec protect",
969 .function = ipsec_tun_protect_show,
970 .short_help = "show ipsec protect",
974 static clib_error_t *
975 ipsec_tun_protect_hash_show (vlib_main_t * vm,
976 unformat_input_t * input,
977 vlib_cli_command_t * cmd)
979 ipsec_main_t *im = &ipsec_main;
982 ipsec_tun_lkup_result_t value;
983 ipsec4_tunnel_key_t key;
985 vlib_cli_output (vm, "IPv4:");
988 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
990 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
991 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
997 ipsec_tun_lkup_result_t value;
998 ipsec6_tunnel_key_t *key;
1000 vlib_cli_output (vm, "IPv6:");
1003 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1005 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1006 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1015 * show IPSEC tunnel protection hash tables
1018 VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1020 .path = "show ipsec protect-hash",
1021 .function = ipsec_tun_protect_hash_show,
1022 .short_help = "show ipsec protect-hash",
1027 ipsec_cli_init (vlib_main_t * vm)
1032 VLIB_INIT_FUNCTION (ipsec_cli_init);
1036 * fd.io coding-style-patch-verification: ON
1039 * eval: (c-set-style "gnu")