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, 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",
410 .short_help = "set ipsec sa <id> crypto-key <key> integ-key <key>",
411 .function = set_ipsec_sa_key_command_fn,
416 ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im)
421 pool_foreach_index (sai, im->sad, ({
422 vlib_cli_output(vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
428 ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
433 pool_foreach_index (spdi, im->spds, ({
434 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
440 ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
442 u32 spd_id, sw_if_index;
444 vlib_cli_output (vm, "SPD Bindings:");
447 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
448 vlib_cli_output (vm, " %d -> %U", spd_id,
449 format_vnet_sw_if_index_name, im->vnet_main,
456 ipsec_tunnel_show_all (vlib_main_t * vm, ipsec_main_t * im)
460 vlib_cli_output (vm, "Tunnel interfaces");
462 pool_foreach_index (ti, im->tunnel_interfaces, ({
463 vlib_cli_output(vm, " %U", format_ipsec_tunnel, ti);
468 static clib_error_t *
469 show_ipsec_command_fn (vlib_main_t * vm,
470 unformat_input_t * input, vlib_cli_command_t * cmd)
472 ipsec_main_t *im = &ipsec_main;
474 ipsec_sa_show_all (vm, im);
475 ipsec_spd_show_all (vm, im);
476 ipsec_spd_bindings_show_all (vm, im);
477 ipsec_tunnel_show_all (vm, im);
483 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
484 .path = "show ipsec all",
485 .short_help = "show ipsec all",
486 .function = show_ipsec_command_fn,
490 static clib_error_t *
491 show_ipsec_sa_command_fn (vlib_main_t * vm,
492 unformat_input_t * input, vlib_cli_command_t * cmd)
494 ipsec_main_t *im = &ipsec_main;
497 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
499 if (unformat (input, "%u", &sai))
506 ipsec_sa_show_all (vm, im);
508 vlib_cli_output (vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_DETAIL);
514 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
515 .path = "show ipsec sa",
516 .short_help = "show ipsec sa [index]",
517 .function = show_ipsec_sa_command_fn,
521 static clib_error_t *
522 show_ipsec_spd_command_fn (vlib_main_t * vm,
523 unformat_input_t * input, vlib_cli_command_t * cmd)
525 ipsec_main_t *im = &ipsec_main;
526 u8 show_bindings = 0;
529 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
531 if (unformat (input, "%u", &spdi))
533 else if (unformat (input, "bindings"))
540 ipsec_spd_bindings_show_all (vm, im);
542 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
544 ipsec_spd_show_all (vm, im);
550 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
551 .path = "show ipsec spd",
552 .short_help = "show ipsec spd [index]",
553 .function = show_ipsec_spd_command_fn,
557 static clib_error_t *
558 show_ipsec_tunnel_command_fn (vlib_main_t * vm,
559 unformat_input_t * input,
560 vlib_cli_command_t * cmd)
562 ipsec_main_t *im = &ipsec_main;
565 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
567 if (unformat (input, "%u", &ti))
574 vlib_cli_output (vm, "%U", format_ipsec_tunnel, ti);
576 ipsec_tunnel_show_all (vm, im);
582 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
583 .path = "show ipsec tunnel",
584 .short_help = "show ipsec tunnel [index]",
585 .function = show_ipsec_tunnel_command_fn,
589 static clib_error_t *
590 ipsec_show_backends_command_fn (vlib_main_t * vm,
591 unformat_input_t * input,
592 vlib_cli_command_t * cmd)
594 ipsec_main_t *im = &ipsec_main;
597 (void) unformat (input, "verbose %u", &verbose);
599 vlib_cli_output (vm, "IPsec AH backends available:");
600 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
601 ipsec_ah_backend_t *ab;
603 pool_foreach (ab, im->ah_backends, {
604 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
605 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
608 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
609 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
610 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
611 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
612 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
613 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
614 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
615 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
619 vlib_cli_output (vm, "%v", s);
621 vlib_cli_output (vm, "IPsec ESP backends available:");
622 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
623 ipsec_esp_backend_t *eb;
625 pool_foreach (eb, im->esp_backends, {
626 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
627 eb - im->esp_backends == im->esp_current_backend ? "yes"
631 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
632 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
633 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
634 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
635 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
636 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
637 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
638 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
642 vlib_cli_output (vm, "%v", s);
649 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
650 .path = "show ipsec backends",
651 .short_help = "show ipsec backends",
652 .function = ipsec_show_backends_command_fn,
656 static clib_error_t *
657 ipsec_select_backend_command_fn (vlib_main_t * vm,
658 unformat_input_t * input,
659 vlib_cli_command_t * cmd)
662 ipsec_main_t *im = &ipsec_main;
664 if (pool_elts (im->sad) > 0)
666 return clib_error_return (0,
667 "Cannot change IPsec backend, while %u SA entries are configured",
668 pool_elts (im->sad));
671 unformat_input_t _line_input, *line_input = &_line_input;
672 /* Get a line of input. */
673 if (!unformat_user (input, unformat_line_input, line_input))
676 if (unformat (line_input, "ah"))
678 if (unformat (line_input, "%u", &backend_index))
680 if (ipsec_select_ah_backend (im, backend_index) < 0)
682 return clib_error_return (0, "Invalid AH backend index `%u'",
688 return clib_error_return (0, "Invalid backend index `%U'",
689 format_unformat_error, line_input);
692 else if (unformat (line_input, "esp"))
694 if (unformat (line_input, "%u", &backend_index))
696 if (ipsec_select_esp_backend (im, backend_index) < 0)
698 return clib_error_return (0, "Invalid ESP backend index `%u'",
704 return clib_error_return (0, "Invalid backend index `%U'",
705 format_unformat_error, line_input);
710 return clib_error_return (0, "Unknown input `%U'",
711 format_unformat_error, line_input);
718 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
719 .path = "ipsec select backend",
720 .short_help = "ipsec select backend <ah|esp> <backend index>",
721 .function = ipsec_select_backend_command_fn,
726 static clib_error_t *
727 clear_ipsec_counters_command_fn (vlib_main_t * vm,
728 unformat_input_t * input,
729 vlib_cli_command_t * cmd)
731 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
732 vlib_clear_combined_counters (&ipsec_sa_counters);
738 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
739 .path = "clear ipsec counters",
740 .short_help = "clear ipsec counters",
741 .function = clear_ipsec_counters_command_fn,
745 static clib_error_t *
746 create_ipsec_tunnel_command_fn (vlib_main_t * vm,
747 unformat_input_t * input,
748 vlib_cli_command_t * cmd)
750 unformat_input_t _line_input, *line_input = &_line_input;
751 ipsec_add_del_tunnel_args_t a;
756 clib_error_t *error = NULL;
757 ipsec_key_t rck = { 0 };
758 ipsec_key_t lck = { 0 };
759 ipsec_key_t lik = { 0 };
760 ipsec_key_t rik = { 0 };
762 clib_memset (&a, 0, sizeof (a));
765 /* Get a line of input. */
766 if (!unformat_user (input, unformat_line_input, line_input))
769 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
772 (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
775 ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
780 (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
783 ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
787 else if (unformat (line_input, "local-spi %u", &a.local_spi))
789 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
791 else if (unformat (line_input, "instance %u", &a.show_instance))
793 else if (unformat (line_input, "salt 0x%x", &a.salt))
795 else if (unformat (line_input, "udp-encap"))
797 else if (unformat (line_input, "use-esn"))
799 else if (unformat (line_input, "use-anti-replay"))
801 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
805 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
809 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
811 else if (unformat (line_input, "crypto-alg %U",
812 unformat_ipsec_crypto_alg, &a.crypto_alg))
816 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
820 (line_input, "rmote-integ-key %U", unformat_ipsec_key, &rik))
822 else if (unformat (line_input, "integ-alg %U",
823 unformat_ipsec_integ_alg, &a.integ_alg))
825 else if (unformat (line_input, "del"))
829 error = clib_error_return (0, "unknown input `%U'",
830 format_unformat_error, line_input);
837 error = clib_error_return (0, "mandatory argument(s) missing");
841 if (ipv4_set && ipv6_set)
842 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
846 clib_memcpy (a.local_crypto_key, lck.data, lck.len);
847 a.local_crypto_key_len = lck.len;
848 clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
849 a.remote_crypto_key_len = rck.len;
851 clib_memcpy (a.local_integ_key, lik.data, lik.len);
852 a.local_integ_key_len = lck.len;
853 clib_memcpy (a.remote_integ_key, rik.data, rik.len);
854 a.remote_integ_key_len = rck.len;
856 rv = ipsec_add_del_tunnel_if (&a);
862 case VNET_API_ERROR_INVALID_VALUE:
864 error = clib_error_return (0,
865 "IPSec tunnel interface already exists...");
867 error = clib_error_return (0, "IPSec tunnel interface not exists...");
870 error = clib_error_return (0, "ipsec_register_interface returned %d",
876 unformat_free (line_input);
882 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
883 .path = "create ipsec tunnel",
884 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
885 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
886 "[tx-table <table-id>]",
887 .function = create_ipsec_tunnel_command_fn,
891 static clib_error_t *
892 set_interface_key_command_fn (vlib_main_t * vm,
893 unformat_input_t * input,
894 vlib_cli_command_t * cmd)
896 unformat_input_t _line_input, *line_input = &_line_input;
897 ipsec_main_t *im = &ipsec_main;
898 ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
899 u32 hw_if_index = (u32) ~ 0;
902 clib_error_t *error = NULL;
904 if (!unformat_user (input, unformat_line_input, line_input))
907 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
909 if (unformat (line_input, "%U",
910 unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
914 (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
915 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
918 (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
919 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
922 (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
923 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
926 (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
927 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
928 else if (unformat (line_input, "%U", unformat_hex_string, &key))
932 error = clib_error_return (0, "parse error: '%U'",
933 format_unformat_error, line_input);
938 if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
940 error = clib_error_return (0, "unknown key type");
944 if (alg > 0 && vec_len (key) == 0)
946 error = clib_error_return (0, "key is not specified");
950 if (hw_if_index == (u32) ~ 0)
952 error = clib_error_return (0, "interface not specified");
956 ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
960 unformat_free (line_input);
966 VLIB_CLI_COMMAND (set_interface_key_command, static) = {
967 .path = "set interface ipsec key",
969 "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
970 .function = set_interface_key_command_fn,
975 ipsec_cli_init (vlib_main_t * vm)
980 VLIB_INIT_FUNCTION (ipsec_cli_init);
984 * fd.io coding-style-patch-verification: ON
987 * eval: (c-set-style "gnu")