ikev2: refactor and test profile dump API
[vpp.git] / src / plugins / ikev2 / ikev2_test.c
1 /*
2  *------------------------------------------------------------------
3  * api_format.c
4  *
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19
20 #include <vat/vat.h>
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>
27
28 #define __plugin_msg_base ikev2_test_main.msg_id_base
29 #include <vlibapi/vat_helper_macros.h>
30
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>
36
37 typedef struct
38 {
39   /* API message ID base */
40   u16 msg_id_base;
41   u32 ping_id;
42   vat_main_t *vat_main;
43 } ikev2_test_main_t;
44
45 ikev2_test_main_t ikev2_test_main;
46
47 uword
48 unformat_ikev2_auth_method (unformat_input_t * input, va_list * args)
49 {
50   u32 *r = va_arg (*args, u32 *);
51
52   if (0);
53 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_AUTH_METHOD_##f;
54   foreach_ikev2_auth_method
55 #undef _
56     else
57     return 0;
58   return 1;
59 }
60
61
62 uword
63 unformat_ikev2_id_type (unformat_input_t * input, va_list * args)
64 {
65   u32 *r = va_arg (*args, u32 *);
66
67   if (0);
68 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_ID_TYPE_##f;
69   foreach_ikev2_id_type
70 #undef _
71     else
72     return 0;
73   return 1;
74 }
75
76 #define MACRO_FORMAT(lc)                                \
77 u8 * format_ikev2_##lc (u8 * s, va_list * args)         \
78 {                                                       \
79   u32 i = va_arg (*args, u32);                          \
80   char * t = 0;                                         \
81   switch (i) {                                          \
82         foreach_ikev2_##lc                              \
83       default:                                          \
84         return format (s, "unknown (%u)", i);           \
85     }                                                   \
86   s = format (s, "%s", t);                              \
87   return s;                                             \
88 }
89
90 #define _(v,f,str) case IKEV2_AUTH_METHOD_##f: t = str; break;
91 MACRO_FORMAT (auth_method)
92 #undef _
93 #define _(v,f,str) case IKEV2_ID_TYPE_##f: t = str; break;
94   MACRO_FORMAT (id_type)
95 #undef _
96 #define _(v,f,str) case IKEV2_TRANSFORM_ENCR_TYPE_##f: t = str; break;
97   MACRO_FORMAT (transform_encr_type)
98 #undef _
99 #define _(v,f,str) case IKEV2_TRANSFORM_INTEG_TYPE_##f: t = str; break;
100   MACRO_FORMAT (transform_integ_type)
101 #undef _
102 #define _(v,f,str) case IKEV2_TRANSFORM_DH_TYPE_##f: t = str; break;
103   MACRO_FORMAT (transform_dh_type)
104 #undef _
105      u8 *format_ikev2_id_type_and_data (u8 * s, va_list * args)
106 {
107   vl_api_ikev2_id_t *id = va_arg (*args, vl_api_ikev2_id_t *);
108
109   if (id->type == 0)
110     return format (s, "none");
111
112   s = format (s, "%U", format_ikev2_id_type, id->type);
113
114   switch (id->type)
115     {
116     case 0:
117       return format (s, "none");
118     case IKEV2_ID_TYPE_ID_FQDN:
119       s = format (s, " %s", id->data);
120       break;
121     case IKEV2_ID_TYPE_ID_RFC822_ADDR:
122       s = format (s, " %s", id->data);
123       break;
124     case IKEV2_ID_TYPE_ID_IPV4_ADDR:
125       s = format (s, " %U", format_ip4_address, id->data);
126       break;
127     case IKEV2_ID_TYPE_ID_KEY_ID:
128       s = format (s, " 0x%U", format_hex_bytes, id->data, id->data_len);
129       break;
130     default:
131       s = format (s, " %s", id->data);
132     }
133
134   return s;
135 }
136
137 static int
138 api_ikev2_profile_dump (vat_main_t * vam)
139 {
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;
143   int ret;
144
145   /* Construct the API message */
146   M (IKEV2_PROFILE_DUMP, mp);
147
148   /* send it... */
149   S (mp);
150
151   /* Use a control ping for synchronization */
152   if (!ik->ping_id)
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;
157
158   fformat (vam->ofp, "Sending ping id=%d\n", ik->ping_id);
159
160   vam->result_ready = 0;
161   S (mp_ping);
162
163   /* Wait for a reply... */
164   W (ret);
165   return ret;
166 }
167
168 static void vl_api_ikev2_profile_details_t_handler
169   (vl_api_ikev2_profile_details_t * mp)
170 {
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;
174
175   fformat (vam->ofp, "profile %s\n", p->name);
176
177   if (p->auth.method)
178     {
179       if (p->auth.hex)
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));
184       else
185         fformat (vam->ofp, "  auth-method %U auth data %v\n",
186                  format_ikev2_auth_method, p->auth.method, format (0,
187                                                                    "%s",
188                                                                    p->
189                                                                    auth.data));
190     }
191
192   if (p->loc_id.type)
193     {
194       fformat (vam->ofp, "  local id-type data %U\n",
195                format_ikev2_id_type_and_data, &p->loc_id);
196     }
197
198   if (p->rem_id.type)
199     {
200       fformat (vam->ofp, "  remote id-type data %U\n",
201                format_ikev2_id_type_and_data, &p->rem_id);
202     }
203
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"
207            " protocol %u\n",
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);
212
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"
216            " protocol %u\n",
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);
222   if (~0 != tun_itf)
223     fformat (vam->ofp, "  protected tunnel idx %d\n", tun_itf);
224
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);
229
230   if (p->udp_encap)
231     fformat (vam->ofp, "  udp-encap\n");
232
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);
236
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
239       || crypto_key_size)
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,
244              p->ike_ts.dh_group);
245
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,
250              crypto_key_size,
251              format_ikev2_transform_integ_type, p->esp_ts.integ_alg);
252
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));
258
259   vam->result_ready = 1;
260 }
261
262 static int
263 api_ikev2_plugin_get_version (vat_main_t * vam)
264 {
265   ikev2_test_main_t *sm = &ikev2_test_main;
266   vl_api_ikev2_plugin_get_version_t *mp;
267   u32 msg_size = sizeof (*mp);
268   int ret;
269
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;
275
276   /* send it... */
277   S (mp);
278
279   /* Wait for a reply... */
280   W (ret);
281   return ret;
282 }
283
284 static void vl_api_ikev2_plugin_get_version_reply_t_handler
285   (vl_api_ikev2_plugin_get_version_reply_t * mp)
286 {
287   vat_main_t *vam = ikev2_test_main.vat_main;
288   clib_warning ("IKEv2 plugin version: %d.%d", ntohl (mp->major),
289                 ntohl (mp->minor));
290   vam->result_ready = 1;
291 }
292
293 static int
294 api_ikev2_profile_set_ipsec_udp_port (vat_main_t * vam)
295 {
296   return 0;
297 }
298
299 static int
300 api_ikev2_profile_set_liveness (vat_main_t * vam)
301 {
302   unformat_input_t *i = vam->input;
303   vl_api_ikev2_profile_set_liveness_t *mp;
304   u32 period = 0, max_retries = 0;
305   int ret;
306
307   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
308     {
309       if (!unformat (i, "period %d max-retries %d", &period, &max_retries))
310         {
311           errmsg ("parse error '%U'", format_unformat_error, i);
312           return -99;
313         }
314     }
315
316   M (IKEV2_PROFILE_SET_LIVENESS, mp);
317
318   mp->period = clib_host_to_net_u32 (period);
319   mp->max_retries = clib_host_to_net_u32 (max_retries);
320
321   S (mp);
322   W (ret);
323
324   return ret;
325 }
326
327 static int
328 api_ikev2_profile_add_del (vat_main_t * vam)
329 {
330   unformat_input_t *i = vam->input;
331   vl_api_ikev2_profile_add_del_t *mp;
332   u8 is_add = 1;
333   u8 *name = 0;
334   int ret;
335
336   const char *valid_chars = "a-zA-Z0-9_";
337
338   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
339     {
340       if (unformat (i, "del"))
341         is_add = 0;
342       else if (unformat (i, "name %U", unformat_token, valid_chars, &name))
343         vec_add1 (name, 0);
344       else
345         {
346           errmsg ("parse error '%U'", format_unformat_error, i);
347           return -99;
348         }
349     }
350
351   if (!vec_len (name))
352     {
353       errmsg ("profile name must be specified");
354       return -99;
355     }
356
357   if (vec_len (name) > 64)
358     {
359       errmsg ("profile name too long");
360       return -99;
361     }
362
363   M (IKEV2_PROFILE_ADD_DEL, mp);
364
365   clib_memcpy (mp->name, name, vec_len (name));
366   mp->is_add = is_add;
367   vec_free (name);
368
369   S (mp);
370   W (ret);
371   return ret;
372 }
373
374 static int
375 api_ikev2_profile_set_auth (vat_main_t * vam)
376 {
377   unformat_input_t *i = vam->input;
378   vl_api_ikev2_profile_set_auth_t *mp;
379   u8 *name = 0;
380   u8 *data = 0;
381   u32 auth_method = 0;
382   u8 is_hex = 0;
383   int ret;
384
385   const char *valid_chars = "a-zA-Z0-9_";
386
387   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
388     {
389       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
390         vec_add1 (name, 0);
391       else if (unformat (i, "auth_method %U",
392                          unformat_ikev2_auth_method, &auth_method))
393         ;
394       else if (unformat (i, "auth_data 0x%U", unformat_hex_string, &data))
395         is_hex = 1;
396       else if (unformat (i, "auth_data %v", &data))
397         ;
398       else
399         {
400           errmsg ("parse error '%U'", format_unformat_error, i);
401           return -99;
402         }
403     }
404
405   if (!vec_len (name))
406     {
407       errmsg ("profile name must be specified");
408       return -99;
409     }
410
411   if (vec_len (name) > 64)
412     {
413       errmsg ("profile name too long");
414       return -99;
415     }
416
417   if (!vec_len (data))
418     {
419       errmsg ("auth_data must be specified");
420       return -99;
421     }
422
423   if (!auth_method)
424     {
425       errmsg ("auth_method must be specified");
426       return -99;
427     }
428
429   M (IKEV2_PROFILE_SET_AUTH, mp);
430
431   mp->is_hex = is_hex;
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));
436   vec_free (name);
437   vec_free (data);
438
439   S (mp);
440   W (ret);
441   return ret;
442 }
443
444 static int
445 api_ikev2_profile_set_id (vat_main_t * vam)
446 {
447   unformat_input_t *i = vam->input;
448   vl_api_ikev2_profile_set_id_t *mp;
449   u8 *name = 0;
450   u8 *data = 0;
451   u8 is_local = 0;
452   u32 id_type = 0;
453   ip4_address_t ip4;
454   int ret;
455
456   const char *valid_chars = "a-zA-Z0-9_";
457
458   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
459     {
460       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
461         vec_add1 (name, 0);
462       else if (unformat (i, "id_type %U", unformat_ikev2_id_type, &id_type))
463         ;
464       else if (unformat (i, "id_data %U", unformat_ip4_address, &ip4))
465         {
466           data = vec_new (u8, 4);
467           clib_memcpy (data, ip4.as_u8, 4);
468         }
469       else if (unformat (i, "id_data 0x%U", unformat_hex_string, &data))
470         ;
471       else if (unformat (i, "id_data %v", &data))
472         ;
473       else if (unformat (i, "local"))
474         is_local = 1;
475       else if (unformat (i, "remote"))
476         is_local = 0;
477       else
478         {
479           errmsg ("parse error '%U'", format_unformat_error, i);
480           return -99;
481         }
482     }
483
484   if (!vec_len (name))
485     {
486       errmsg ("profile name must be specified");
487       return -99;
488     }
489
490   if (vec_len (name) > 64)
491     {
492       errmsg ("profile name too long");
493       return -99;
494     }
495
496   if (!vec_len (data))
497     {
498       errmsg ("id_data must be specified");
499       return -99;
500     }
501
502   if (!id_type)
503     {
504       errmsg ("id_type must be specified");
505       return -99;
506     }
507
508   M (IKEV2_PROFILE_SET_ID, mp);
509
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));
515   vec_free (name);
516   vec_free (data);
517
518   S (mp);
519   W (ret);
520   return ret;
521 }
522
523 static int
524 api_ikev2_profile_set_ts (vat_main_t * vam)
525 {
526   unformat_input_t *i = vam->input;
527   vl_api_ikev2_profile_set_ts_t *mp;
528   u8 *name = 0;
529   u8 is_local = 0;
530   u32 proto = 0, start_port = 0, end_port = (u32) ~ 0;
531   ip4_address_t start_addr, end_addr;
532
533   const char *valid_chars = "a-zA-Z0-9_";
534   int ret;
535
536   start_addr.as_u32 = 0;
537   end_addr.as_u32 = (u32) ~ 0;
538
539   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
540     {
541       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
542         vec_add1 (name, 0);
543       else if (unformat (i, "protocol %d", &proto))
544         ;
545       else if (unformat (i, "start_port %d", &start_port))
546         ;
547       else if (unformat (i, "end_port %d", &end_port))
548         ;
549       else
550         if (unformat (i, "start_addr %U", unformat_ip4_address, &start_addr))
551         ;
552       else if (unformat (i, "end_addr %U", unformat_ip4_address, &end_addr))
553         ;
554       else if (unformat (i, "local"))
555         is_local = 1;
556       else if (unformat (i, "remote"))
557         is_local = 0;
558       else
559         {
560           errmsg ("parse error '%U'", format_unformat_error, i);
561           return -99;
562         }
563     }
564
565   if (!vec_len (name))
566     {
567       errmsg ("profile name must be specified");
568       return -99;
569     }
570
571   if (vec_len (name) > 64)
572     {
573       errmsg ("profile name too long");
574       return -99;
575     }
576
577   M (IKEV2_PROFILE_SET_TS, mp);
578
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));
586   vec_free (name);
587
588   S (mp);
589   W (ret);
590   return ret;
591 }
592
593 static int
594 api_ikev2_set_local_key (vat_main_t * vam)
595 {
596   unformat_input_t *i = vam->input;
597   vl_api_ikev2_set_local_key_t *mp;
598   u8 *file = 0;
599   int ret;
600
601   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
602     {
603       if (unformat (i, "file %v", &file))
604         vec_add1 (file, 0);
605       else
606         {
607           errmsg ("parse error '%U'", format_unformat_error, i);
608           return -99;
609         }
610     }
611
612   if (!vec_len (file))
613     {
614       errmsg ("RSA key file must be specified");
615       return -99;
616     }
617
618   if (vec_len (file) > 256)
619     {
620       errmsg ("file name too long");
621       return -99;
622     }
623
624   M (IKEV2_SET_LOCAL_KEY, mp);
625
626   clib_memcpy (mp->key_file, file, vec_len (file));
627   vec_free (file);
628
629   S (mp);
630   W (ret);
631   return ret;
632 }
633
634 static int
635 api_ikev2_profile_set_udp_encap (vat_main_t * vam)
636 {
637   unformat_input_t *i = vam->input;
638   vl_api_ikev2_set_responder_t *mp;
639   int ret;
640   u8 *name = 0;
641
642   const char *valid_chars = "a-zA-Z0-9_";
643
644   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
645     {
646       if (unformat (i, "%U udp-encap", unformat_token, valid_chars, &name))
647         vec_add1 (name, 0);
648       else
649         {
650           errmsg ("parse error '%U'", format_unformat_error, i);
651           return -99;
652         }
653     }
654
655   if (!vec_len (name))
656     {
657       errmsg ("profile name must be specified");
658       return -99;
659     }
660
661   if (vec_len (name) > 64)
662     {
663       errmsg ("profile name too long");
664       return -99;
665     }
666
667   M (IKEV2_PROFILE_SET_UDP_ENCAP, mp);
668
669   clib_memcpy (mp->name, name, vec_len (name));
670   vec_free (name);
671
672   S (mp);
673   W (ret);
674   return ret;
675 }
676
677 static int
678 api_ikev2_set_tunnel_interface (vat_main_t * vam)
679 {
680   return (0);
681 }
682
683 static int
684 api_ikev2_set_responder (vat_main_t * vam)
685 {
686   unformat_input_t *i = vam->input;
687   vl_api_ikev2_set_responder_t *mp;
688   int ret;
689   u8 *name = 0;
690   u32 sw_if_index = ~0;
691   ip4_address_t address;
692
693   const char *valid_chars = "a-zA-Z0-9_";
694
695   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
696     {
697       if (unformat
698           (i, "%U interface %d address %U", unformat_token, valid_chars,
699            &name, &sw_if_index, unformat_ip4_address, &address))
700         vec_add1 (name, 0);
701       else
702         {
703           errmsg ("parse error '%U'", format_unformat_error, i);
704           return -99;
705         }
706     }
707
708   if (!vec_len (name))
709     {
710       errmsg ("profile name must be specified");
711       return -99;
712     }
713
714   if (vec_len (name) > 64)
715     {
716       errmsg ("profile name too long");
717       return -99;
718     }
719
720   M (IKEV2_SET_RESPONDER, mp);
721
722   clib_memcpy (mp->name, name, vec_len (name));
723   vec_free (name);
724
725   mp->responder.sw_if_index = clib_host_to_net_u32 (sw_if_index);
726   ip4_address_encode (&address, mp->responder.ip4);
727
728   S (mp);
729   W (ret);
730   return ret;
731 }
732
733 static int
734 api_ikev2_set_ike_transforms (vat_main_t * vam)
735 {
736   unformat_input_t *i = vam->input;
737   vl_api_ikev2_set_ike_transforms_t *mp;
738   int ret;
739   u8 *name = 0;
740   u32 crypto_alg, crypto_key_size, integ_alg, dh_group;
741
742   const char *valid_chars = "a-zA-Z0-9_";
743
744   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
745     {
746       if (unformat (i, "%U %d %d %d %d", unformat_token, valid_chars, &name,
747                     &crypto_alg, &crypto_key_size, &integ_alg, &dh_group))
748         vec_add1 (name, 0);
749       else
750         {
751           errmsg ("parse error '%U'", format_unformat_error, i);
752           return -99;
753         }
754     }
755
756   if (!vec_len (name))
757     {
758       errmsg ("profile name must be specified");
759       return -99;
760     }
761
762   if (vec_len (name) > 64)
763     {
764       errmsg ("profile name too long");
765       return -99;
766     }
767
768   M (IKEV2_SET_IKE_TRANSFORMS, mp);
769
770   clib_memcpy (mp->name, name, vec_len (name));
771   vec_free (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;
776
777   S (mp);
778   W (ret);
779   return ret;
780 }
781
782
783 static int
784 api_ikev2_set_esp_transforms (vat_main_t * vam)
785 {
786   unformat_input_t *i = vam->input;
787   vl_api_ikev2_set_esp_transforms_t *mp;
788   int ret;
789   u8 *name = 0;
790   u32 crypto_alg, crypto_key_size, integ_alg;
791
792   const char *valid_chars = "a-zA-Z0-9_";
793
794   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
795     {
796       if (unformat (i, "%U %d %d %d", unformat_token, valid_chars, &name,
797                     &crypto_alg, &crypto_key_size, &integ_alg))
798         vec_add1 (name, 0);
799       else
800         {
801           errmsg ("parse error '%U'", format_unformat_error, i);
802           return -99;
803         }
804     }
805
806   if (!vec_len (name))
807     {
808       errmsg ("profile name must be specified");
809       return -99;
810     }
811
812   if (vec_len (name) > 64)
813     {
814       errmsg ("profile name too long");
815       return -99;
816     }
817
818   M (IKEV2_SET_ESP_TRANSFORMS, mp);
819
820   clib_memcpy (mp->name, name, vec_len (name));
821   vec_free (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;
825
826   S (mp);
827   W (ret);
828   return ret;
829 }
830
831 static int
832 api_ikev2_set_sa_lifetime (vat_main_t * vam)
833 {
834   unformat_input_t *i = vam->input;
835   vl_api_ikev2_set_sa_lifetime_t *mp;
836   int ret;
837   u8 *name = 0;
838   u64 lifetime, lifetime_maxdata;
839   u32 lifetime_jitter, handover;
840
841   const char *valid_chars = "a-zA-Z0-9_";
842
843   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
844     {
845       if (unformat (i, "%U %lu %u %u %lu", unformat_token, valid_chars, &name,
846                     &lifetime, &lifetime_jitter, &handover,
847                     &lifetime_maxdata))
848         vec_add1 (name, 0);
849       else
850         {
851           errmsg ("parse error '%U'", format_unformat_error, i);
852           return -99;
853         }
854     }
855
856   if (!vec_len (name))
857     {
858       errmsg ("profile name must be specified");
859       return -99;
860     }
861
862   if (vec_len (name) > 64)
863     {
864       errmsg ("profile name too long");
865       return -99;
866     }
867
868   M (IKEV2_SET_SA_LIFETIME, mp);
869
870   clib_memcpy (mp->name, name, vec_len (name));
871   vec_free (name);
872   mp->lifetime = lifetime;
873   mp->lifetime_jitter = lifetime_jitter;
874   mp->handover = handover;
875   mp->lifetime_maxdata = lifetime_maxdata;
876
877   S (mp);
878   W (ret);
879   return ret;
880 }
881
882 static int
883 api_ikev2_initiate_sa_init (vat_main_t * vam)
884 {
885   unformat_input_t *i = vam->input;
886   vl_api_ikev2_initiate_sa_init_t *mp;
887   int ret;
888   u8 *name = 0;
889
890   const char *valid_chars = "a-zA-Z0-9_";
891
892   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
893     {
894       if (unformat (i, "%U", unformat_token, valid_chars, &name))
895         vec_add1 (name, 0);
896       else
897         {
898           errmsg ("parse error '%U'", format_unformat_error, i);
899           return -99;
900         }
901     }
902
903   if (!vec_len (name))
904     {
905       errmsg ("profile name must be specified");
906       return -99;
907     }
908
909   if (vec_len (name) > 64)
910     {
911       errmsg ("profile name too long");
912       return -99;
913     }
914
915   M (IKEV2_INITIATE_SA_INIT, mp);
916
917   clib_memcpy (mp->name, name, vec_len (name));
918   vec_free (name);
919
920   S (mp);
921   W (ret);
922   return ret;
923 }
924
925 static int
926 api_ikev2_initiate_del_ike_sa (vat_main_t * vam)
927 {
928   unformat_input_t *i = vam->input;
929   vl_api_ikev2_initiate_del_ike_sa_t *mp;
930   int ret;
931   u64 ispi;
932
933
934   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
935     {
936       if (unformat (i, "%lx", &ispi))
937         ;
938       else
939         {
940           errmsg ("parse error '%U'", format_unformat_error, i);
941           return -99;
942         }
943     }
944
945   M (IKEV2_INITIATE_DEL_IKE_SA, mp);
946
947   mp->ispi = ispi;
948
949   S (mp);
950   W (ret);
951   return ret;
952 }
953
954 static int
955 api_ikev2_initiate_del_child_sa (vat_main_t * vam)
956 {
957   unformat_input_t *i = vam->input;
958   vl_api_ikev2_initiate_del_child_sa_t *mp;
959   int ret;
960   u32 ispi;
961
962
963   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
964     {
965       if (unformat (i, "%x", &ispi))
966         ;
967       else
968         {
969           errmsg ("parse error '%U'", format_unformat_error, i);
970           return -99;
971         }
972     }
973
974   M (IKEV2_INITIATE_DEL_CHILD_SA, mp);
975
976   mp->ispi = ispi;
977
978   S (mp);
979   W (ret);
980   return ret;
981 }
982
983 static int
984 api_ikev2_initiate_rekey_child_sa (vat_main_t * vam)
985 {
986   unformat_input_t *i = vam->input;
987   vl_api_ikev2_initiate_rekey_child_sa_t *mp;
988   int ret;
989   u32 ispi;
990
991
992   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
993     {
994       if (unformat (i, "%x", &ispi))
995         ;
996       else
997         {
998           errmsg ("parse error '%U'", format_unformat_error, i);
999           return -99;
1000         }
1001     }
1002
1003   M (IKEV2_INITIATE_REKEY_CHILD_SA, mp);
1004
1005   mp->ispi = ispi;
1006
1007   S (mp);
1008   W (ret);
1009   return ret;
1010 }
1011
1012 #include <ikev2/ikev2.api_test.c>
1013
1014 /*
1015  * fd.io coding-style-patch-verification: ON
1016  *
1017  * Local Variables:
1018  * eval: (c-set-style "gnu")
1019  * End:
1020  */