2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
19 #include <vnet/udp/udp.h>
20 #include <vnet/ipsec/ipsec_sa.h>
21 #include <plugins/ikev2/ikev2.h>
22 #include <plugins/ikev2/ikev2_priv.h>
25 format_ikev2_id_type_and_data (u8 * s, va_list * args)
27 ikev2_id_t *id = va_arg (*args, ikev2_id_t *);
29 if (id->type == 0 || vec_len (id->data) == 0)
30 return format (s, "none");
32 s = format (s, "%U", format_ikev2_id_type, id->type);
34 if (id->type == IKEV2_ID_TYPE_ID_FQDN ||
35 id->type == IKEV2_ID_TYPE_ID_RFC822_ADDR)
37 s = format (s, " %v", id->data);
42 format (s, " %U", format_hex_bytes, &id->data,
43 (uword) (vec_len (id->data)));
51 show_ikev2_sa_command_fn (vlib_main_t * vm,
52 unformat_input_t * input, vlib_cli_command_t * cmd)
54 ikev2_main_t *km = &ikev2_main;
55 ikev2_main_per_thread_data_t *tkm;
58 ikev2_child_sa_t *child;
59 ikev2_sa_transform_t *tr;
61 vec_foreach (tkm, km->per_thread_data)
64 pool_foreach (sa, tkm->sas, ({
66 vlib_cli_output(vm, " iip %U ispi %lx rip %U rspi %lx",
67 format_ip4_address, &sa->iaddr, sa->ispi,
68 format_ip4_address, &sa->raddr, sa->rspi);
70 tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
71 s = format(s, "%U ", format_ikev2_sa_transform, tr);
73 tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
74 s = format(s, "%U ", format_ikev2_sa_transform, tr);
76 tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
77 s = format(s, "%U ", format_ikev2_sa_transform, tr);
79 tr = ikev2_sa_get_td_for_type(sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH);
80 s = format(s, "%U ", format_ikev2_sa_transform, tr);
82 vlib_cli_output(vm, " %v", s);
85 vlib_cli_output(vm, " nonce i:%U\n r:%U",
86 format_hex_bytes, sa->i_nonce, vec_len(sa->i_nonce),
87 format_hex_bytes, sa->r_nonce, vec_len(sa->r_nonce));
89 vlib_cli_output(vm, " SK_d %U",
90 format_hex_bytes, sa->sk_d, vec_len(sa->sk_d));
91 vlib_cli_output(vm, " SK_a i:%U\n r:%U",
92 format_hex_bytes, sa->sk_ai, vec_len(sa->sk_ai),
93 format_hex_bytes, sa->sk_ar, vec_len(sa->sk_ar));
94 vlib_cli_output(vm, " SK_e i:%U\n r:%U",
95 format_hex_bytes, sa->sk_ei, vec_len(sa->sk_ei),
96 format_hex_bytes, sa->sk_er, vec_len(sa->sk_er));
97 vlib_cli_output(vm, " SK_p i:%U\n r:%U",
98 format_hex_bytes, sa->sk_pi, vec_len(sa->sk_pi),
99 format_hex_bytes, sa->sk_pr, vec_len(sa->sk_pr));
101 vlib_cli_output(vm, " identifier (i) %U",
102 format_ikev2_id_type_and_data, &sa->i_id);
103 vlib_cli_output(vm, " identifier (r) %U",
104 format_ikev2_id_type_and_data, &sa->r_id);
106 vec_foreach(child, sa->childs)
108 vlib_cli_output(vm, " child sa %u:", child - sa->childs);
110 tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
111 s = format(s, "%U ", format_ikev2_sa_transform, tr);
113 tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
114 s = format(s, "%U ", format_ikev2_sa_transform, tr);
116 tr = ikev2_sa_get_td_for_type(child->r_proposals, IKEV2_TRANSFORM_TYPE_ESN);
117 s = format(s, "%U ", format_ikev2_sa_transform, tr);
119 vlib_cli_output(vm, " %v", s);
122 vlib_cli_output(vm, " spi(i) %lx spi(r) %lx",
123 child->i_proposals ? child->i_proposals[0].spi : 0,
124 child->r_proposals ? child->r_proposals[0].spi : 0);
126 vlib_cli_output(vm, " SK_e i:%U\n r:%U",
127 format_hex_bytes, child->sk_ei, vec_len(child->sk_ei),
128 format_hex_bytes, child->sk_er, vec_len(child->sk_er));
131 vlib_cli_output(vm, " SK_a i:%U\n r:%U",
132 format_hex_bytes, child->sk_ai, vec_len(child->sk_ai),
133 format_hex_bytes, child->sk_ar, vec_len(child->sk_ar));
134 vlib_cli_output(vm, " traffic selectors (i):");
136 vec_foreach(ts, child->tsi)
138 vlib_cli_output(vm, " %u type %u protocol_id %u addr "
139 "%U - %U port %u - %u",
141 ts->ts_type, ts->protocol_id,
142 format_ip4_address, &ts->start_addr,
143 format_ip4_address, &ts->end_addr,
144 clib_net_to_host_u16( ts->start_port),
145 clib_net_to_host_u16( ts->end_port));
147 vlib_cli_output(vm, " traffic selectors (r):");
148 vec_foreach(ts, child->tsr)
150 vlib_cli_output(vm, " %u type %u protocol_id %u addr "
151 "%U - %U port %u - %u",
153 ts->ts_type, ts->protocol_id,
154 format_ip4_address, &ts->start_addr,
155 format_ip4_address, &ts->end_addr,
156 clib_net_to_host_u16( ts->start_port),
157 clib_net_to_host_u16( ts->end_port));
160 vlib_cli_output(vm, "");
168 VLIB_CLI_COMMAND (show_ikev2_sa_command, static) = {
169 .path = "show ikev2 sa",
170 .short_help = "show ikev2 sa",
171 .function = show_ikev2_sa_command_fn,
175 static clib_error_t *
176 ikev2_profile_add_del_command_fn (vlib_main_t * vm,
177 unformat_input_t * input,
178 vlib_cli_command_t * cmd)
180 vnet_main_t *vnm = vnet_get_main ();
181 unformat_input_t _line_input, *line_input = &_line_input;
186 u32 tmp1, tmp2, tmp3;
189 ip4_address_t end_addr;
190 u32 responder_sw_if_index = (u32) ~ 0;
191 u32 tun_sw_if_index = (u32) ~ 0;
192 ip4_address_t responder_ip4;
193 ikev2_transform_encr_type_t crypto_alg;
194 ikev2_transform_integ_type_t integ_alg;
195 ikev2_transform_dh_type_t dh_type;
197 const char *valid_chars = "a-zA-Z0-9_";
199 if (!unformat_user (input, unformat_line_input, line_input))
202 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
204 if (unformat (line_input, "add %U", unformat_token, valid_chars, &name))
206 r = ikev2_add_del_profile (vm, name, 1);
211 (line_input, "del %U", unformat_token, valid_chars, &name))
213 r = ikev2_add_del_profile (vm, name, 0);
216 else if (unformat (line_input, "set %U auth shared-key-mic string %v",
217 unformat_token, valid_chars, &name, &data))
220 ikev2_set_profile_auth (vm, name,
221 IKEV2_AUTH_METHOD_SHARED_KEY_MIC, data,
225 else if (unformat (line_input, "set %U auth shared-key-mic hex %U",
226 unformat_token, valid_chars, &name,
227 unformat_hex_string, &data))
230 ikev2_set_profile_auth (vm, name,
231 IKEV2_AUTH_METHOD_SHARED_KEY_MIC, data,
235 else if (unformat (line_input, "set %U auth rsa-sig cert-file %v",
236 unformat_token, valid_chars, &name, &data))
239 ikev2_set_profile_auth (vm, name, IKEV2_AUTH_METHOD_RSA_SIG, data,
243 else if (unformat (line_input, "set %U id local %U %U",
244 unformat_token, valid_chars, &name,
245 unformat_ikev2_id_type, &id_type,
246 unformat_ip4_address, &ip4))
248 data = vec_new (u8, 4);
249 clib_memcpy (data, ip4.as_u8, 4);
251 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
254 else if (unformat (line_input, "set %U id local %U 0x%U",
255 unformat_token, valid_chars, &name,
256 unformat_ikev2_id_type, &id_type,
257 unformat_hex_string, &data))
260 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
263 else if (unformat (line_input, "set %U id local %U %v",
264 unformat_token, valid_chars, &name,
265 unformat_ikev2_id_type, &id_type, &data))
268 ikev2_set_profile_id (vm, name, (u8) id_type, data, /*local */ 1);
271 else if (unformat (line_input, "set %U id remote %U %U",
272 unformat_token, valid_chars, &name,
273 unformat_ikev2_id_type, &id_type,
274 unformat_ip4_address, &ip4))
276 data = vec_new (u8, 4);
277 clib_memcpy (data, ip4.as_u8, 4);
278 r = ikev2_set_profile_id (vm, name, (u8) id_type, data, /*remote */
282 else if (unformat (line_input, "set %U id remote %U 0x%U",
283 unformat_token, valid_chars, &name,
284 unformat_ikev2_id_type, &id_type,
285 unformat_hex_string, &data))
287 r = ikev2_set_profile_id (vm, name, (u8) id_type, data, /*remote */
291 else if (unformat (line_input, "set %U id remote %U %v",
292 unformat_token, valid_chars, &name,
293 unformat_ikev2_id_type, &id_type, &data))
295 r = ikev2_set_profile_id (vm, name, (u8) id_type, data, /*remote */
299 else if (unformat (line_input, "set %U traffic-selector local "
300 "ip-range %U - %U port-range %u - %u protocol %u",
301 unformat_token, valid_chars, &name,
302 unformat_ip4_address, &ip4,
303 unformat_ip4_address, &end_addr,
304 &tmp1, &tmp2, &tmp3))
307 ikev2_set_profile_ts (vm, name, (u8) tmp3, (u16) tmp1, (u16) tmp2,
308 ip4, end_addr, /*local */ 1);
311 else if (unformat (line_input, "set %U traffic-selector remote "
312 "ip-range %U - %U port-range %u - %u protocol %u",
313 unformat_token, valid_chars, &name,
314 unformat_ip4_address, &ip4,
315 unformat_ip4_address, &end_addr,
316 &tmp1, &tmp2, &tmp3))
319 ikev2_set_profile_ts (vm, name, (u8) tmp3, (u16) tmp1, (u16) tmp2,
320 ip4, end_addr, /*remote */ 0);
323 else if (unformat (line_input, "set %U responder %U %U",
324 unformat_token, valid_chars, &name,
325 unformat_vnet_sw_interface, vnm,
326 &responder_sw_if_index, unformat_ip4_address,
330 ikev2_set_profile_responder (vm, name, responder_sw_if_index,
334 else if (unformat (line_input, "set %U tunnel %U",
335 unformat_token, valid_chars, &name,
336 unformat_vnet_sw_interface, vnm, &tun_sw_if_index))
338 r = ikev2_set_profile_tunnel_interface (vm, name, tun_sw_if_index);
344 "set %U ike-crypto-alg %U %u ike-integ-alg %U ike-dh %U",
345 unformat_token, valid_chars, &name,
346 unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
347 unformat_ikev2_transform_integ_type, &integ_alg,
348 unformat_ikev2_transform_dh_type, &dh_type))
351 ikev2_set_profile_ike_transforms (vm, name, crypto_alg, integ_alg,
358 "set %U esp-crypto-alg %U %u esp-integ-alg %U esp-dh %U",
359 unformat_token, valid_chars, &name,
360 unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
361 unformat_ikev2_transform_integ_type, &integ_alg,
362 unformat_ikev2_transform_dh_type, &dh_type))
365 ikev2_set_profile_esp_transforms (vm, name, crypto_alg, integ_alg,
371 "set %U esp-crypto-alg %U %u esp-dh %U",
372 unformat_token, valid_chars, &name,
373 unformat_ikev2_transform_encr_type, &crypto_alg, &tmp1,
374 unformat_ikev2_transform_dh_type, &dh_type))
377 ikev2_set_profile_esp_transforms (vm, name, crypto_alg, 0,
381 else if (unformat (line_input, "set %U sa-lifetime %lu %u %u %lu",
382 unformat_token, valid_chars, &name,
383 &tmp4, &tmp1, &tmp2, &tmp5))
386 ikev2_set_profile_sa_lifetime (vm, name, tmp4, tmp1, tmp2, tmp5);
389 else if (unformat (line_input, "set %U udp-encap",
390 unformat_token, valid_chars, &name))
392 r = ikev2_set_profile_udp_encap (vm, name);
395 else if (unformat (line_input, "set %U ipsec-over-udp port %u",
396 unformat_token, valid_chars, &name, &tmp1))
398 int rv = ikev2_set_profile_ipsec_udp_port (vm, name, tmp1, 1);
400 r = clib_error_return (0, "Error: %U", format_vnet_api_errno, rv);
407 r = clib_error_return (0, "parse error: '%U'",
408 format_unformat_error, line_input);
413 unformat_free (line_input);
418 VLIB_CLI_COMMAND (ikev2_profile_add_del_command, static) = {
419 .path = "ikev2 profile",
421 "ikev2 profile [add|del] <id>\n"
422 "ikev2 profile set <id> auth [rsa-sig|shared-key-mic] [cert-file|string|hex]"
424 "ikev2 profile set <id> id <local|remote> <type> <data>\n"
425 "ikev2 profile set <id> tunnel <interface>\n"
426 "ikev2 profile set <id> udp-encap\n"
427 "ikev2 profile set <id> traffic-selector <local|remote> ip-range "
428 "<start-addr> - <end-addr> port-range <start-port> - <end-port> "
429 "protocol <protocol-number>\n"
430 "ikev2 profile set <id> responder <interface> <addr>\n"
431 "ikev2 profile set <id> ike-crypto-alg <crypto alg> <key size> ike-integ-alg <integ alg> ike-dh <dh type>\n"
432 "ikev2 profile set <id> esp-crypto-alg <crypto alg> <key size> "
433 "[esp-integ-alg <integ alg>] esp-dh <dh type>\n"
434 "ikev2 profile set <id> sa-lifetime <seconds> <jitter> <handover> <max bytes>",
435 .function = ikev2_profile_add_del_command_fn,
439 static clib_error_t *
440 show_ikev2_profile_command_fn (vlib_main_t * vm,
441 unformat_input_t * input,
442 vlib_cli_command_t * cmd)
444 ikev2_main_t *km = &ikev2_main;
448 pool_foreach (p, km->profiles, ({
449 vlib_cli_output(vm, "profile %v", p->name);
454 vlib_cli_output(vm, " auth-method %U auth data 0x%U",
455 format_ikev2_auth_method, p->auth.method,
456 format_hex_bytes, p->auth.data, vec_len(p->auth.data));
458 vlib_cli_output(vm, " auth-method %U auth data %v",
459 format_ikev2_auth_method, p->auth.method, p->auth.data);
464 if (p->loc_id.type == IKEV2_ID_TYPE_ID_IPV4_ADDR)
465 vlib_cli_output(vm, " local id-type %U data %U",
466 format_ikev2_id_type, p->loc_id.type,
467 format_ip4_address, p->loc_id.data);
468 else if (p->loc_id.type == IKEV2_ID_TYPE_ID_KEY_ID)
469 vlib_cli_output(vm, " local id-type %U data 0x%U",
470 format_ikev2_id_type, p->loc_id.type,
471 format_hex_bytes, p->loc_id.data,
472 vec_len(p->loc_id.data));
474 vlib_cli_output(vm, " local id-type %U data %v",
475 format_ikev2_id_type, p->loc_id.type, p->loc_id.data);
480 if (p->rem_id.type == IKEV2_ID_TYPE_ID_IPV4_ADDR)
481 vlib_cli_output(vm, " remote id-type %U data %U",
482 format_ikev2_id_type, p->rem_id.type,
483 format_ip4_address, p->rem_id.data);
484 else if (p->rem_id.type == IKEV2_ID_TYPE_ID_KEY_ID)
485 vlib_cli_output(vm, " remote id-type %U data 0x%U",
486 format_ikev2_id_type, p->rem_id.type,
487 format_hex_bytes, p->rem_id.data,
488 vec_len(p->rem_id.data));
490 vlib_cli_output(vm, " remote id-type %U data %v",
491 format_ikev2_id_type, p->rem_id.type, p->rem_id.data);
494 if (p->loc_ts.end_addr.as_u32)
495 vlib_cli_output(vm, " local traffic-selector addr %U - %U port %u - %u"
497 format_ip4_address, &p->loc_ts.start_addr,
498 format_ip4_address, &p->loc_ts.end_addr,
499 p->loc_ts.start_port, p->loc_ts.end_port,
500 p->loc_ts.protocol_id);
502 if (p->rem_ts.end_addr.as_u32)
503 vlib_cli_output(vm, " remote traffic-selector addr %U - %U port %u - %u"
505 format_ip4_address, &p->rem_ts.start_addr,
506 format_ip4_address, &p->rem_ts.end_addr,
507 p->rem_ts.start_port, p->rem_ts.end_port,
508 p->rem_ts.protocol_id);
509 if (~0 != p->tun_itf)
510 vlib_cli_output(vm, " protected tunnel %U",
511 format_vnet_sw_if_index_name, vnet_get_main(), p->tun_itf);
513 vlib_cli_output(vm, " udp-encap");
515 if (p->ipsec_over_udp_port != IPSEC_UDP_PORT_NONE)
516 vlib_cli_output(vm, " ipsec-over-udp port %d", p->ipsec_over_udp_port);
524 VLIB_CLI_COMMAND (show_ikev2_profile_command, static) = {
525 .path = "show ikev2 profile",
526 .short_help = "show ikev2 profile",
527 .function = show_ikev2_profile_command_fn,
531 static clib_error_t *
532 set_ikev2_liveness_period_fn (vlib_main_t * vm,
533 unformat_input_t * input,
534 vlib_cli_command_t * cmd)
536 unformat_input_t _line_input, *line_input = &_line_input;
538 u32 period = 0, max_retries = 0;
540 if (!unformat_user (input, unformat_line_input, line_input))
543 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
545 if (unformat (line_input, "%d %d", &period, &max_retries))
547 r = ikev2_set_liveness_params (period, max_retries);
554 r = clib_error_return (0, "parse error: '%U'",
555 format_unformat_error, line_input);
558 unformat_free (line_input);
563 VLIB_CLI_COMMAND (set_ikev2_liveness_command, static) = {
564 .path = "ikev2 set liveness",
565 .short_help = "ikev2 set liveness <period> <max-retires>",
566 .function = set_ikev2_liveness_period_fn,
570 static clib_error_t *
571 set_ikev2_local_key_command_fn (vlib_main_t * vm,
572 unformat_input_t * input,
573 vlib_cli_command_t * cmd)
575 unformat_input_t _line_input, *line_input = &_line_input;
579 if (!unformat_user (input, unformat_line_input, line_input))
582 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
584 if (unformat (line_input, "%s", &data))
586 r = ikev2_set_local_key (vm, data);
593 r = clib_error_return (0, "parse error: '%U'",
594 format_unformat_error, line_input);
598 unformat_free (line_input);
603 VLIB_CLI_COMMAND (set_ikev2_local_key_command, static) = {
604 .path = "set ikev2 local key",
606 "set ikev2 local key <file>",
607 .function = set_ikev2_local_key_command_fn,
612 static clib_error_t *
613 ikev2_initiate_command_fn (vlib_main_t * vm,
614 unformat_input_t * input, vlib_cli_command_t * cmd)
616 unformat_input_t _line_input, *line_input = &_line_input;
622 const char *valid_chars = "a-zA-Z0-9_";
624 if (!unformat_user (input, unformat_line_input, line_input))
627 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
630 (line_input, "sa-init %U", unformat_token, valid_chars, &name))
632 r = ikev2_initiate_sa_init (vm, name);
635 else if (unformat (line_input, "del-child-sa %x", &tmp1))
637 r = ikev2_initiate_delete_child_sa (vm, tmp1);
640 else if (unformat (line_input, "del-sa %lx", &tmp2))
642 r = ikev2_initiate_delete_ike_sa (vm, tmp2);
645 else if (unformat (line_input, "rekey-child-sa %x", &tmp1))
647 r = ikev2_initiate_rekey_child_sa (vm, tmp1);
654 r = clib_error_return (0, "parse error: '%U'",
655 format_unformat_error, line_input);
659 unformat_free (line_input);
664 VLIB_CLI_COMMAND (ikev2_initiate_command, static) = {
665 .path = "ikev2 initiate",
667 "ikev2 initiate sa-init <profile id>\n"
668 "ikev2 initiate del-child-sa <child sa ispi>\n"
669 "ikev2 initiate del-sa <sa ispi>\n"
670 "ikev2 initiate rekey-child-sa <profile id> <child sa ispi>\n",
671 .function = ikev2_initiate_command_fn,
676 ikev2_cli_reference (void)
680 static clib_error_t *
681 ikev2_set_log_level_command_fn (vlib_main_t * vm,
682 unformat_input_t * input,
683 vlib_cli_command_t * cmd)
685 unformat_input_t _line_input, *line_input = &_line_input;
686 u32 log_level = IKEV2_LOG_NONE;
687 clib_error_t *error = 0;
689 /* Get a line of input. */
690 if (!unformat_user (input, unformat_line_input, line_input))
693 if (!unformat (line_input, "%d", &log_level))
695 error = clib_error_return (0, "unknown input '%U'",
696 format_unformat_error, line_input);
699 int rc = ikev2_set_log_level (log_level);
701 error = clib_error_return (0, "setting log level failed!");
704 unformat_free (line_input);
709 VLIB_CLI_COMMAND (ikev2_set_log_level_command, static) = {
710 .path = "ikev2 set logging level",
711 .function = ikev2_set_log_level_command_fn,
712 .short_help = "ikev2 set logging level <0-5>",
717 * fd.io coding-style-patch-verification: ON
720 * eval: (c-set-style "gnu")