2 *------------------------------------------------------------------
5 * Copyright (c) 2014-2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23 #include <vppinfra/error.h>
24 #include <vnet/ipsec/ipsec_sa.h>
25 #include <plugins/ikev2/ikev2.h>
26 #include <vnet/ip/ip_types_api.h>
28 #define __plugin_msg_base ikev2_test_main.msg_id_base
29 #include <vlibapi/vat_helper_macros.h>
31 /* Declare message IDs */
32 #include <vnet/format_fns.h>
33 #include <ikev2/ikev2.api_enum.h>
34 #include <ikev2/ikev2.api_types.h>
35 #include <vpp/api/vpe.api_types.h>
39 /* API message ID base */
45 ikev2_test_main_t ikev2_test_main;
48 unformat_ikev2_auth_method (unformat_input_t * input, va_list * args)
50 u32 *r = va_arg (*args, u32 *);
53 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_AUTH_METHOD_##f;
54 foreach_ikev2_auth_method
63 unformat_ikev2_id_type (unformat_input_t * input, va_list * args)
65 u32 *r = va_arg (*args, u32 *);
68 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_ID_TYPE_##f;
76 #define MACRO_FORMAT(lc) \
77 u8 * format_ikev2_##lc (u8 * s, va_list * args) \
79 u32 i = va_arg (*args, u32); \
84 return format (s, "unknown (%u)", i); \
86 s = format (s, "%s", t); \
90 #define _(v,f,str) case IKEV2_AUTH_METHOD_##f: t = str; break;
91 MACRO_FORMAT (auth_method)
93 #define _(v,f,str) case IKEV2_ID_TYPE_##f: t = str; break;
94 MACRO_FORMAT (id_type)
96 #define _(v,f,str) case IKEV2_TRANSFORM_ENCR_TYPE_##f: t = str; break;
97 MACRO_FORMAT (transform_encr_type)
99 #define _(v,f,str) case IKEV2_TRANSFORM_INTEG_TYPE_##f: t = str; break;
100 MACRO_FORMAT (transform_integ_type)
102 #define _(v,f,str) case IKEV2_TRANSFORM_DH_TYPE_##f: t = str; break;
103 MACRO_FORMAT (transform_dh_type)
105 u8 *format_ikev2_id_type_and_data (u8 * s, va_list * args)
107 vl_api_ikev2_id_t *id = va_arg (*args, vl_api_ikev2_id_t *);
110 return format (s, "none");
112 s = format (s, "%U", format_ikev2_id_type, id->type);
117 return format (s, "none");
118 case IKEV2_ID_TYPE_ID_FQDN:
119 s = format (s, " %s", id->data);
121 case IKEV2_ID_TYPE_ID_RFC822_ADDR:
122 s = format (s, " %s", id->data);
124 case IKEV2_ID_TYPE_ID_IPV4_ADDR:
125 s = format (s, " %U", format_ip4_address, id->data);
127 case IKEV2_ID_TYPE_ID_KEY_ID:
128 s = format (s, " 0x%U", format_hex_bytes, id->data, id->data_len);
131 s = format (s, " %s", id->data);
138 api_ikev2_profile_dump (vat_main_t * vam)
140 ikev2_test_main_t *ik = &ikev2_test_main;
141 vl_api_ikev2_profile_dump_t *mp;
142 vl_api_control_ping_t *mp_ping;
145 /* Construct the API message */
146 M (IKEV2_PROFILE_DUMP, mp);
151 /* Use a control ping for synchronization */
153 ik->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
154 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
155 mp_ping->_vl_msg_id = htons (ik->ping_id);
156 mp_ping->client_index = vam->my_client_index;
158 fformat (vam->ofp, "Sending ping id=%d\n", ik->ping_id);
160 vam->result_ready = 0;
163 /* Wait for a reply... */
168 static void vl_api_ikev2_profile_details_t_handler
169 (vl_api_ikev2_profile_details_t * mp)
171 vat_main_t *vam = ikev2_test_main.vat_main;
172 vl_api_ikev2_profile_t *p = &mp->profile;
173 ip4_address_t start_addr, end_addr;
175 fformat (vam->ofp, "profile %s\n", p->name);
180 fformat (vam->ofp, " auth-method %U auth data 0x%U\n",
181 format_ikev2_auth_method, p->auth.method,
182 format_hex_bytes, p->auth.data,
183 clib_net_to_host_u32 (p->auth.data_len));
185 fformat (vam->ofp, " auth-method %U auth data %v\n",
186 format_ikev2_auth_method, p->auth.method, format (0,
194 fformat (vam->ofp, " local id-type data %U\n",
195 format_ikev2_id_type_and_data, &p->loc_id);
200 fformat (vam->ofp, " remote id-type data %U\n",
201 format_ikev2_id_type_and_data, &p->rem_id);
204 ip4_address_decode (p->loc_ts.start_addr, &start_addr);
205 ip4_address_decode (p->loc_ts.end_addr, &end_addr);
206 fformat (vam->ofp, " local traffic-selector addr %U - %U port %u - %u"
208 format_ip4_address, &start_addr,
209 format_ip4_address, &end_addr,
210 clib_net_to_host_u16 (p->loc_ts.start_port),
211 clib_net_to_host_u16 (p->loc_ts.end_port), p->loc_ts.protocol_id);
213 ip4_address_decode (p->rem_ts.start_addr, &start_addr);
214 ip4_address_decode (p->rem_ts.end_addr, &end_addr);
215 fformat (vam->ofp, " remote traffic-selector addr %U - %U port %u - %u"
217 format_ip4_address, &start_addr,
218 format_ip4_address, &end_addr,
219 clib_net_to_host_u16 (p->rem_ts.start_port),
220 clib_net_to_host_u16 (p->rem_ts.end_port), p->rem_ts.protocol_id);
221 u32 tun_itf = clib_net_to_host_u32 (p->tun_itf);
223 fformat (vam->ofp, " protected tunnel idx %d\n", tun_itf);
225 u32 sw_if_index = clib_net_to_host_u32 (p->responder.sw_if_index);
226 if (~0 != sw_if_index)
227 fformat (vam->ofp, " responder idx %d %U\n",
228 sw_if_index, format_ip4_address, &p->responder.ip4);
231 fformat (vam->ofp, " udp-encap\n");
233 u32 ipsec_over_udp_port = clib_net_to_host_u16 (p->ipsec_over_udp_port);
234 if (ipsec_over_udp_port != IPSEC_UDP_PORT_NONE)
235 fformat (vam->ofp, " ipsec-over-udp port %d\n", ipsec_over_udp_port);
237 u32 crypto_key_size = clib_net_to_host_u32 (p->ike_ts.crypto_key_size);
238 if (p->ike_ts.crypto_alg || p->ike_ts.integ_alg || p->ike_ts.dh_group
240 fformat (vam->ofp, " ike-crypto-alg %U %u ike-integ-alg %U ike-dh %U\n",
241 format_ikev2_transform_encr_type, p->ike_ts.crypto_alg,
242 crypto_key_size, format_ikev2_transform_integ_type,
243 p->ike_ts.integ_alg, format_ikev2_transform_dh_type,
246 crypto_key_size = clib_net_to_host_u32 (p->esp_ts.crypto_key_size);
247 if (p->esp_ts.crypto_alg || p->esp_ts.integ_alg)
248 fformat (vam->ofp, " esp-crypto-alg %U %u esp-integ-alg %U\n",
249 format_ikev2_transform_encr_type, p->esp_ts.crypto_alg,
251 format_ikev2_transform_integ_type, p->esp_ts.integ_alg);
253 fformat (vam->ofp, " lifetime %d jitter %d handover %d maxdata %d\n",
254 clib_net_to_host_u64 (p->lifetime),
255 clib_net_to_host_u32 (p->lifetime_jitter),
256 clib_net_to_host_u32 (p->handover),
257 clib_net_to_host_u64 (p->lifetime_maxdata));
259 vam->result_ready = 1;
263 api_ikev2_plugin_get_version (vat_main_t * vam)
265 ikev2_test_main_t *sm = &ikev2_test_main;
266 vl_api_ikev2_plugin_get_version_t *mp;
267 u32 msg_size = sizeof (*mp);
270 vam->result_ready = 0;
271 mp = vl_msg_api_alloc_as_if_client (msg_size);
272 clib_memset (mp, 0, msg_size);
273 mp->_vl_msg_id = ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION + sm->msg_id_base);
274 mp->client_index = vam->my_client_index;
279 /* Wait for a reply... */
284 static void vl_api_ikev2_plugin_get_version_reply_t_handler
285 (vl_api_ikev2_plugin_get_version_reply_t * mp)
287 vat_main_t *vam = ikev2_test_main.vat_main;
288 clib_warning ("IKEv2 plugin version: %d.%d", ntohl (mp->major),
290 vam->result_ready = 1;
294 api_ikev2_profile_set_ipsec_udp_port (vat_main_t * vam)
300 api_ikev2_profile_set_liveness (vat_main_t * vam)
302 unformat_input_t *i = vam->input;
303 vl_api_ikev2_profile_set_liveness_t *mp;
304 u32 period = 0, max_retries = 0;
307 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
309 if (!unformat (i, "period %d max-retries %d", &period, &max_retries))
311 errmsg ("parse error '%U'", format_unformat_error, i);
316 M (IKEV2_PROFILE_SET_LIVENESS, mp);
318 mp->period = clib_host_to_net_u32 (period);
319 mp->max_retries = clib_host_to_net_u32 (max_retries);
328 api_ikev2_profile_add_del (vat_main_t * vam)
330 unformat_input_t *i = vam->input;
331 vl_api_ikev2_profile_add_del_t *mp;
336 const char *valid_chars = "a-zA-Z0-9_";
338 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
340 if (unformat (i, "del"))
342 else if (unformat (i, "name %U", unformat_token, valid_chars, &name))
346 errmsg ("parse error '%U'", format_unformat_error, i);
353 errmsg ("profile name must be specified");
357 if (vec_len (name) > 64)
359 errmsg ("profile name too long");
363 M (IKEV2_PROFILE_ADD_DEL, mp);
365 clib_memcpy (mp->name, name, vec_len (name));
375 api_ikev2_profile_set_auth (vat_main_t * vam)
377 unformat_input_t *i = vam->input;
378 vl_api_ikev2_profile_set_auth_t *mp;
385 const char *valid_chars = "a-zA-Z0-9_";
387 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
389 if (unformat (i, "name %U", unformat_token, valid_chars, &name))
391 else if (unformat (i, "auth_method %U",
392 unformat_ikev2_auth_method, &auth_method))
394 else if (unformat (i, "auth_data 0x%U", unformat_hex_string, &data))
396 else if (unformat (i, "auth_data %v", &data))
400 errmsg ("parse error '%U'", format_unformat_error, i);
407 errmsg ("profile name must be specified");
411 if (vec_len (name) > 64)
413 errmsg ("profile name too long");
419 errmsg ("auth_data must be specified");
425 errmsg ("auth_method must be specified");
429 M (IKEV2_PROFILE_SET_AUTH, mp);
432 mp->auth_method = (u8) auth_method;
433 mp->data_len = vec_len (data);
434 clib_memcpy (mp->name, name, vec_len (name));
435 clib_memcpy (mp->data, data, vec_len (data));
445 api_ikev2_profile_set_id (vat_main_t * vam)
447 unformat_input_t *i = vam->input;
448 vl_api_ikev2_profile_set_id_t *mp;
456 const char *valid_chars = "a-zA-Z0-9_";
458 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
460 if (unformat (i, "name %U", unformat_token, valid_chars, &name))
462 else if (unformat (i, "id_type %U", unformat_ikev2_id_type, &id_type))
464 else if (unformat (i, "id_data %U", unformat_ip4_address, &ip4))
466 data = vec_new (u8, 4);
467 clib_memcpy (data, ip4.as_u8, 4);
469 else if (unformat (i, "id_data 0x%U", unformat_hex_string, &data))
471 else if (unformat (i, "id_data %v", &data))
473 else if (unformat (i, "local"))
475 else if (unformat (i, "remote"))
479 errmsg ("parse error '%U'", format_unformat_error, i);
486 errmsg ("profile name must be specified");
490 if (vec_len (name) > 64)
492 errmsg ("profile name too long");
498 errmsg ("id_data must be specified");
504 errmsg ("id_type must be specified");
508 M (IKEV2_PROFILE_SET_ID, mp);
510 mp->is_local = is_local;
511 mp->id_type = (u8) id_type;
512 mp->data_len = vec_len (data);
513 clib_memcpy (mp->name, name, vec_len (name));
514 clib_memcpy (mp->data, data, vec_len (data));
524 api_ikev2_profile_set_ts (vat_main_t * vam)
526 unformat_input_t *i = vam->input;
527 vl_api_ikev2_profile_set_ts_t *mp;
530 u32 proto = 0, start_port = 0, end_port = (u32) ~ 0;
531 ip4_address_t start_addr, end_addr;
533 const char *valid_chars = "a-zA-Z0-9_";
536 start_addr.as_u32 = 0;
537 end_addr.as_u32 = (u32) ~ 0;
539 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
541 if (unformat (i, "name %U", unformat_token, valid_chars, &name))
543 else if (unformat (i, "protocol %d", &proto))
545 else if (unformat (i, "start_port %d", &start_port))
547 else if (unformat (i, "end_port %d", &end_port))
550 if (unformat (i, "start_addr %U", unformat_ip4_address, &start_addr))
552 else if (unformat (i, "end_addr %U", unformat_ip4_address, &end_addr))
554 else if (unformat (i, "local"))
556 else if (unformat (i, "remote"))
560 errmsg ("parse error '%U'", format_unformat_error, i);
567 errmsg ("profile name must be specified");
571 if (vec_len (name) > 64)
573 errmsg ("profile name too long");
577 M (IKEV2_PROFILE_SET_TS, mp);
579 mp->ts.is_local = is_local;
580 mp->ts.protocol_id = (u8) proto;
581 mp->ts.start_port = clib_host_to_net_u16 ((u16) start_port);
582 mp->ts.end_port = clib_host_to_net_u16 ((u16) end_port);
583 ip4_address_encode (&start_addr, mp->ts.start_addr);
584 ip4_address_encode (&end_addr, mp->ts.end_addr);
585 clib_memcpy (mp->name, name, vec_len (name));
594 api_ikev2_set_local_key (vat_main_t * vam)
596 unformat_input_t *i = vam->input;
597 vl_api_ikev2_set_local_key_t *mp;
601 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
603 if (unformat (i, "file %v", &file))
607 errmsg ("parse error '%U'", format_unformat_error, i);
614 errmsg ("RSA key file must be specified");
618 if (vec_len (file) > 256)
620 errmsg ("file name too long");
624 M (IKEV2_SET_LOCAL_KEY, mp);
626 clib_memcpy (mp->key_file, file, vec_len (file));
635 api_ikev2_profile_set_udp_encap (vat_main_t * vam)
637 unformat_input_t *i = vam->input;
638 vl_api_ikev2_set_responder_t *mp;
642 const char *valid_chars = "a-zA-Z0-9_";
644 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
646 if (unformat (i, "%U udp-encap", unformat_token, valid_chars, &name))
650 errmsg ("parse error '%U'", format_unformat_error, i);
657 errmsg ("profile name must be specified");
661 if (vec_len (name) > 64)
663 errmsg ("profile name too long");
667 M (IKEV2_PROFILE_SET_UDP_ENCAP, mp);
669 clib_memcpy (mp->name, name, vec_len (name));
678 api_ikev2_set_tunnel_interface (vat_main_t * vam)
684 api_ikev2_set_responder (vat_main_t * vam)
686 unformat_input_t *i = vam->input;
687 vl_api_ikev2_set_responder_t *mp;
690 u32 sw_if_index = ~0;
691 ip4_address_t address;
693 const char *valid_chars = "a-zA-Z0-9_";
695 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
698 (i, "%U interface %d address %U", unformat_token, valid_chars,
699 &name, &sw_if_index, unformat_ip4_address, &address))
703 errmsg ("parse error '%U'", format_unformat_error, i);
710 errmsg ("profile name must be specified");
714 if (vec_len (name) > 64)
716 errmsg ("profile name too long");
720 M (IKEV2_SET_RESPONDER, mp);
722 clib_memcpy (mp->name, name, vec_len (name));
725 mp->responder.sw_if_index = clib_host_to_net_u32 (sw_if_index);
726 ip4_address_encode (&address, mp->responder.ip4);
734 api_ikev2_set_ike_transforms (vat_main_t * vam)
736 unformat_input_t *i = vam->input;
737 vl_api_ikev2_set_ike_transforms_t *mp;
740 u32 crypto_alg, crypto_key_size, integ_alg, dh_group;
742 const char *valid_chars = "a-zA-Z0-9_";
744 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
746 if (unformat (i, "%U %d %d %d %d", unformat_token, valid_chars, &name,
747 &crypto_alg, &crypto_key_size, &integ_alg, &dh_group))
751 errmsg ("parse error '%U'", format_unformat_error, i);
758 errmsg ("profile name must be specified");
762 if (vec_len (name) > 64)
764 errmsg ("profile name too long");
768 M (IKEV2_SET_IKE_TRANSFORMS, mp);
770 clib_memcpy (mp->name, name, vec_len (name));
772 mp->tr.crypto_alg = crypto_alg;
773 mp->tr.crypto_key_size = clib_host_to_net_u32 (crypto_key_size);
774 mp->tr.integ_alg = integ_alg;
775 mp->tr.dh_group = dh_group;
784 api_ikev2_set_esp_transforms (vat_main_t * vam)
786 unformat_input_t *i = vam->input;
787 vl_api_ikev2_set_esp_transforms_t *mp;
790 u32 crypto_alg, crypto_key_size, integ_alg;
792 const char *valid_chars = "a-zA-Z0-9_";
794 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
796 if (unformat (i, "%U %d %d %d", unformat_token, valid_chars, &name,
797 &crypto_alg, &crypto_key_size, &integ_alg))
801 errmsg ("parse error '%U'", format_unformat_error, i);
808 errmsg ("profile name must be specified");
812 if (vec_len (name) > 64)
814 errmsg ("profile name too long");
818 M (IKEV2_SET_ESP_TRANSFORMS, mp);
820 clib_memcpy (mp->name, name, vec_len (name));
822 mp->tr.crypto_alg = crypto_alg;
823 mp->tr.crypto_key_size = clib_host_to_net_u32 (crypto_key_size);
824 mp->tr.integ_alg = integ_alg;
832 api_ikev2_set_sa_lifetime (vat_main_t * vam)
834 unformat_input_t *i = vam->input;
835 vl_api_ikev2_set_sa_lifetime_t *mp;
838 u64 lifetime, lifetime_maxdata;
839 u32 lifetime_jitter, handover;
841 const char *valid_chars = "a-zA-Z0-9_";
843 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
845 if (unformat (i, "%U %lu %u %u %lu", unformat_token, valid_chars, &name,
846 &lifetime, &lifetime_jitter, &handover,
851 errmsg ("parse error '%U'", format_unformat_error, i);
858 errmsg ("profile name must be specified");
862 if (vec_len (name) > 64)
864 errmsg ("profile name too long");
868 M (IKEV2_SET_SA_LIFETIME, mp);
870 clib_memcpy (mp->name, name, vec_len (name));
872 mp->lifetime = lifetime;
873 mp->lifetime_jitter = lifetime_jitter;
874 mp->handover = handover;
875 mp->lifetime_maxdata = lifetime_maxdata;
883 api_ikev2_initiate_sa_init (vat_main_t * vam)
885 unformat_input_t *i = vam->input;
886 vl_api_ikev2_initiate_sa_init_t *mp;
890 const char *valid_chars = "a-zA-Z0-9_";
892 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
894 if (unformat (i, "%U", unformat_token, valid_chars, &name))
898 errmsg ("parse error '%U'", format_unformat_error, i);
905 errmsg ("profile name must be specified");
909 if (vec_len (name) > 64)
911 errmsg ("profile name too long");
915 M (IKEV2_INITIATE_SA_INIT, mp);
917 clib_memcpy (mp->name, name, vec_len (name));
926 api_ikev2_initiate_del_ike_sa (vat_main_t * vam)
928 unformat_input_t *i = vam->input;
929 vl_api_ikev2_initiate_del_ike_sa_t *mp;
934 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
936 if (unformat (i, "%lx", &ispi))
940 errmsg ("parse error '%U'", format_unformat_error, i);
945 M (IKEV2_INITIATE_DEL_IKE_SA, mp);
955 api_ikev2_initiate_del_child_sa (vat_main_t * vam)
957 unformat_input_t *i = vam->input;
958 vl_api_ikev2_initiate_del_child_sa_t *mp;
963 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
965 if (unformat (i, "%x", &ispi))
969 errmsg ("parse error '%U'", format_unformat_error, i);
974 M (IKEV2_INITIATE_DEL_CHILD_SA, mp);
984 api_ikev2_initiate_rekey_child_sa (vat_main_t * vam)
986 unformat_input_t *i = vam->input;
987 vl_api_ikev2_initiate_rekey_child_sa_t *mp;
992 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
994 if (unformat (i, "%x", &ispi))
998 errmsg ("parse error '%U'", format_unformat_error, i);
1003 M (IKEV2_INITIATE_REKEY_CHILD_SA, mp);
1012 #include <ikev2/ikev2.api_test.c>
1015 * fd.io coding-style-patch-verification: ON
1018 * eval: (c-set-style "gnu")