ikev2: add SA 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 #define vl_endianfun            /* define message structures */
38 #include <plugins/ikev2/ikev2.api.h>
39 #undef vl_endianfun
40
41 typedef struct
42 {
43   /* API message ID base */
44   u16 msg_id_base;
45   u32 ping_id;
46   vat_main_t *vat_main;
47 } ikev2_test_main_t;
48
49 ikev2_test_main_t ikev2_test_main;
50
51 uword
52 unformat_ikev2_auth_method (unformat_input_t * input, va_list * args)
53 {
54   u32 *r = va_arg (*args, u32 *);
55
56   if (0);
57 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_AUTH_METHOD_##f;
58   foreach_ikev2_auth_method
59 #undef _
60     else
61     return 0;
62   return 1;
63 }
64
65
66 uword
67 unformat_ikev2_id_type (unformat_input_t * input, va_list * args)
68 {
69   u32 *r = va_arg (*args, u32 *);
70
71   if (0);
72 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_ID_TYPE_##f;
73   foreach_ikev2_id_type
74 #undef _
75     else
76     return 0;
77   return 1;
78 }
79
80 #define MACRO_FORMAT(lc)                                \
81 u8 * format_ikev2_##lc (u8 * s, va_list * args)         \
82 {                                                       \
83   u32 i = va_arg (*args, u32);                          \
84   char * t = 0;                                         \
85   switch (i) {                                          \
86         foreach_ikev2_##lc                              \
87       default:                                          \
88         return format (s, "unknown (%u)", i);           \
89     }                                                   \
90   s = format (s, "%s", t);                              \
91   return s;                                             \
92 }
93
94 #define _(v,f,str) case IKEV2_AUTH_METHOD_##f: t = str; break;
95 MACRO_FORMAT (auth_method)
96 #undef _
97 #define _(v,f,str) case IKEV2_ID_TYPE_##f: t = str; break;
98   MACRO_FORMAT (id_type)
99 #undef _
100 #define _(v,f,str) case IKEV2_TRANSFORM_TYPE_##f: t = str; break;
101   MACRO_FORMAT (transform_type)
102 #undef _
103 #define _(v,f,str) case IKEV2_TRANSFORM_ENCR_TYPE_##f: t = str; break;
104   MACRO_FORMAT (transform_encr_type)
105 #undef _
106 #define _(v,f,str) case IKEV2_TRANSFORM_PRF_TYPE_##f: t = str; break;
107   MACRO_FORMAT (transform_prf_type)
108 #undef _
109 #define _(v,f,str) case IKEV2_TRANSFORM_INTEG_TYPE_##f: t = str; break;
110   MACRO_FORMAT (transform_integ_type)
111 #undef _
112 #define _(v,f,str) case IKEV2_TRANSFORM_DH_TYPE_##f: t = str; break;
113   MACRO_FORMAT (transform_dh_type)
114 #undef _
115 #define _(v,f,str) case IKEV2_TRANSFORM_ESN_TYPE_##f: t = str; break;
116   MACRO_FORMAT (transform_esn_type)
117 #undef _
118      u8 *format_ikev2_id_type_and_data (u8 * s, va_list * args)
119 {
120   vl_api_ikev2_id_t *id = va_arg (*args, vl_api_ikev2_id_t *);
121
122   if (id->type == 0)
123     return format (s, "none");
124
125   s = format (s, "%U", format_ikev2_id_type, id->type);
126
127   switch (id->type)
128     {
129     case 0:
130       return format (s, "none");
131     case IKEV2_ID_TYPE_ID_FQDN:
132       s = format (s, " %s", id->data);
133       break;
134     case IKEV2_ID_TYPE_ID_RFC822_ADDR:
135       s = format (s, " %s", id->data);
136       break;
137     case IKEV2_ID_TYPE_ID_IPV4_ADDR:
138       s = format (s, " %U", format_ip4_address, id->data);
139       break;
140     case IKEV2_ID_TYPE_ID_KEY_ID:
141       s = format (s, " 0x%U", format_hex_bytes, id->data, id->data_len);
142       break;
143     default:
144       s = format (s, " %s", id->data);
145     }
146
147   return s;
148 }
149
150 u8 *
151 format_ikev2_sa_transform (u8 * s, va_list * args)
152 {
153   vl_api_ikev2_sa_transform_t *tr =
154     va_arg (*args, vl_api_ikev2_sa_transform_t *);
155
156   if (!tr)
157     return s;
158
159   if (tr->transform_type >= IKEV2_TRANSFORM_NUM_TYPES)
160     return s;
161
162   s = format (s, "%U:", format_ikev2_transform_type, tr->transform_type);
163
164   switch (tr->transform_type)
165     {
166     case IKEV2_TRANSFORM_TYPE_ENCR:
167       s =
168         format (s, "%U", format_ikev2_transform_encr_type, tr->transform_id);
169       break;
170     case IKEV2_TRANSFORM_TYPE_PRF:
171       s = format (s, "%U", format_ikev2_transform_prf_type, tr->transform_id);
172       break;
173     case IKEV2_TRANSFORM_TYPE_INTEG:
174       s =
175         format (s, "%U", format_ikev2_transform_integ_type, tr->transform_id);
176       break;
177     case IKEV2_TRANSFORM_TYPE_DH:
178       s = format (s, "%U", format_ikev2_transform_dh_type, tr->transform_id);
179       break;
180     case IKEV2_TRANSFORM_TYPE_ESN:
181       s = format (s, "%U", format_ikev2_transform_esn_type, tr->transform_id);
182       break;
183     default:
184       break;
185     }
186
187   if (tr->transform_type == IKEV2_TRANSFORM_TYPE_ENCR &&
188       tr->transform_id == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
189     s = format (s, "-%u", tr->key_len * 8);
190
191   return s;
192 }
193
194 static int
195 api_ikev2_profile_dump (vat_main_t * vam)
196 {
197   ikev2_test_main_t *ik = &ikev2_test_main;
198   vl_api_ikev2_profile_dump_t *mp;
199   vl_api_control_ping_t *mp_ping;
200   int ret;
201
202   /* Construct the API message */
203   M (IKEV2_PROFILE_DUMP, mp);
204
205   /* send it... */
206   S (mp);
207
208   /* Use a control ping for synchronization */
209   if (!ik->ping_id)
210     ik->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
211   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
212   mp_ping->_vl_msg_id = htons (ik->ping_id);
213   mp_ping->client_index = vam->my_client_index;
214
215   vam->result_ready = 0;
216   S (mp_ping);
217
218   /* Wait for a reply... */
219   W (ret);
220   return ret;
221 }
222
223 static void vl_api_ikev2_profile_details_t_handler
224   (vl_api_ikev2_profile_details_t * mp)
225 {
226   vat_main_t *vam = ikev2_test_main.vat_main;
227   vl_api_ikev2_profile_t *p = &mp->profile;
228   ip4_address_t start_addr, end_addr;
229
230   fformat (vam->ofp, "profile %s\n", p->name);
231
232   if (p->auth.method)
233     {
234       if (p->auth.hex)
235         fformat (vam->ofp, "  auth-method %U auth data 0x%U\n",
236                  format_ikev2_auth_method, p->auth.method,
237                  format_hex_bytes, p->auth.data,
238                  clib_net_to_host_u32 (p->auth.data_len));
239       else
240         fformat (vam->ofp, "  auth-method %U auth data %v\n",
241                  format_ikev2_auth_method, p->auth.method, format (0,
242                                                                    "%s",
243                                                                    p->
244                                                                    auth.data));
245     }
246
247   if (p->loc_id.type)
248     {
249       fformat (vam->ofp, "  local id-type data %U\n",
250                format_ikev2_id_type_and_data, &p->loc_id);
251     }
252
253   if (p->rem_id.type)
254     {
255       fformat (vam->ofp, "  remote id-type data %U\n",
256                format_ikev2_id_type_and_data, &p->rem_id);
257     }
258
259   ip4_address_decode (p->loc_ts.start_addr, &start_addr);
260   ip4_address_decode (p->loc_ts.end_addr, &end_addr);
261   fformat (vam->ofp, "  local traffic-selector addr %U - %U port %u - %u"
262            " protocol %u\n",
263            format_ip4_address, &start_addr,
264            format_ip4_address, &end_addr,
265            clib_net_to_host_u16 (p->loc_ts.start_port),
266            clib_net_to_host_u16 (p->loc_ts.end_port), p->loc_ts.protocol_id);
267
268   ip4_address_decode (p->rem_ts.start_addr, &start_addr);
269   ip4_address_decode (p->rem_ts.end_addr, &end_addr);
270   fformat (vam->ofp, "  remote traffic-selector addr %U - %U port %u - %u"
271            " protocol %u\n",
272            format_ip4_address, &start_addr,
273            format_ip4_address, &end_addr,
274            clib_net_to_host_u16 (p->rem_ts.start_port),
275            clib_net_to_host_u16 (p->rem_ts.end_port), p->rem_ts.protocol_id);
276   u32 tun_itf = clib_net_to_host_u32 (p->tun_itf);
277   if (~0 != tun_itf)
278     fformat (vam->ofp, "  protected tunnel idx %d\n", tun_itf);
279
280   u32 sw_if_index = clib_net_to_host_u32 (p->responder.sw_if_index);
281   if (~0 != sw_if_index)
282     fformat (vam->ofp, "  responder idx %d %U\n",
283              sw_if_index, format_ip4_address, &p->responder.ip4);
284
285   if (p->udp_encap)
286     fformat (vam->ofp, "  udp-encap\n");
287
288   u32 ipsec_over_udp_port = clib_net_to_host_u16 (p->ipsec_over_udp_port);
289   if (ipsec_over_udp_port != IPSEC_UDP_PORT_NONE)
290     fformat (vam->ofp, "  ipsec-over-udp port %d\n", ipsec_over_udp_port);
291
292   u32 crypto_key_size = clib_net_to_host_u32 (p->ike_ts.crypto_key_size);
293   if (p->ike_ts.crypto_alg || p->ike_ts.integ_alg || p->ike_ts.dh_group
294       || crypto_key_size)
295     fformat (vam->ofp, "  ike-crypto-alg %U %u ike-integ-alg %U ike-dh %U\n",
296              format_ikev2_transform_encr_type, p->ike_ts.crypto_alg,
297              crypto_key_size, format_ikev2_transform_integ_type,
298              p->ike_ts.integ_alg, format_ikev2_transform_dh_type,
299              p->ike_ts.dh_group);
300
301   crypto_key_size = clib_net_to_host_u32 (p->esp_ts.crypto_key_size);
302   if (p->esp_ts.crypto_alg || p->esp_ts.integ_alg)
303     fformat (vam->ofp, "  esp-crypto-alg %U %u esp-integ-alg %U\n",
304              format_ikev2_transform_encr_type, p->esp_ts.crypto_alg,
305              crypto_key_size,
306              format_ikev2_transform_integ_type, p->esp_ts.integ_alg);
307
308   fformat (vam->ofp, "  lifetime %d jitter %d handover %d maxdata %d\n",
309            clib_net_to_host_u64 (p->lifetime),
310            clib_net_to_host_u32 (p->lifetime_jitter),
311            clib_net_to_host_u32 (p->handover),
312            clib_net_to_host_u64 (p->lifetime_maxdata));
313
314   vam->result_ready = 1;
315 }
316
317 static int
318 api_ikev2_sa_dump (vat_main_t * vam)
319 {
320   ikev2_test_main_t *im = &ikev2_test_main;
321   vl_api_ikev2_sa_dump_t *mp;
322   vl_api_control_ping_t *mp_ping;
323   int ret;
324
325   /* Construct the API message */
326   M (IKEV2_SA_DUMP, mp);
327
328   /* send it... */
329   S (mp);
330
331   /* Use a control ping for synchronization */
332   if (!im->ping_id)
333     im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
334   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
335   mp_ping->_vl_msg_id = htons (im->ping_id);
336   mp_ping->client_index = vam->my_client_index;
337   vam->result_ready = 0;
338
339   S (mp_ping);
340
341   /* Wait for a reply... */
342   W (ret);
343   return ret;
344 }
345
346 static void
347 vl_api_ikev2_sa_details_t_handler (vl_api_ikev2_sa_details_t * mp)
348 {
349   vat_main_t *vam = ikev2_test_main.vat_main;
350   vl_api_ikev2_sa_t *sa = &mp->sa;
351   ip4_address_t iaddr;
352   ip4_address_t raddr;
353   vl_api_ikev2_keys_t *k = &sa->keys;
354   vl_api_ikev2_sa_t_endian (sa);
355
356   ip4_address_decode (sa->iaddr, &iaddr);
357   ip4_address_decode (sa->raddr, &raddr);
358
359   fformat (vam->ofp, "profile index %d sa index: %d\n",
360            mp->sa.profile_index, mp->sa.sa_index);
361   fformat (vam->ofp, " iip %U ispi %lx rip %U rspi %lx\n", format_ip4_address,
362            &iaddr, sa->ispi, format_ip4_address, &raddr, sa->rspi);
363   fformat (vam->ofp, " %U ", format_ikev2_sa_transform, &sa->encryption);
364   fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &sa->prf);
365   fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &sa->integrity);
366   fformat (vam->ofp, "%U \n", format_ikev2_sa_transform, &sa->dh);
367
368   fformat (vam->ofp, "  SK_d    %U\n",
369            format_hex_bytes, k->sk_d, k->sk_d_len);
370
371   fformat (vam->ofp, "  SK_a  i:%U\n        r:%U\n",
372            format_hex_bytes, k->sk_ai, k->sk_ai_len, format_hex_bytes,
373            k->sk_ar, k->sk_ar_len);
374
375   fformat (vam->ofp, "  SK_e  i:%U\n        r:%U\n", format_hex_bytes,
376            k->sk_ei, k->sk_ei_len, format_hex_bytes, k->sk_er, k->sk_er_len);
377
378   fformat (vam->ofp, "  SK_p  i:%U\n        r:%U\n", format_hex_bytes,
379            k->sk_pi, k->sk_pi_len, format_hex_bytes, k->sk_pr, k->sk_pr_len);
380
381   fformat (vam->ofp, "  identifier (i) %U\n",
382            format_ikev2_id_type_and_data, &sa->i_id);
383   fformat (vam->ofp, "  identifier (r) %U\n",
384            format_ikev2_id_type_and_data, &sa->r_id);
385
386   vam->result_ready = 1;
387 }
388
389 static int
390 api_ikev2_child_sa_dump (vat_main_t * vam)
391 {
392   unformat_input_t *i = vam->input;
393   ikev2_test_main_t *im = &ikev2_test_main;
394   vl_api_ikev2_child_sa_dump_t *mp;
395   vl_api_control_ping_t *mp_ping;
396   int ret;
397   u32 sa_index = ~0;
398
399   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
400     {
401       if (unformat (i, "sa_index %d", &sa_index))
402         ;
403       else
404         {
405           errmsg ("parse error '%U'", format_unformat_error, i);
406           return -99;
407         }
408     }
409
410   if (sa_index == ~0)
411     return -99;
412
413   /* Construct the API message */
414   M (IKEV2_CHILD_SA_DUMP, mp);
415
416   mp->sa_index = clib_net_to_host_u32 (sa_index);
417
418   /* send it... */
419   S (mp);
420
421   /* Use a control ping for synchronization */
422   if (!im->ping_id)
423     im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
424   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
425   mp_ping->_vl_msg_id = htons (im->ping_id);
426   mp_ping->client_index = vam->my_client_index;
427   vam->result_ready = 0;
428
429   S (mp_ping);
430
431   /* Wait for a reply... */
432   W (ret);
433   return ret;
434 }
435
436 static void
437 vl_api_ikev2_child_sa_details_t_handler (vl_api_ikev2_child_sa_details_t * mp)
438 {
439   vat_main_t *vam = ikev2_test_main.vat_main;
440   vl_api_ikev2_child_sa_t *child_sa = &mp->child_sa;
441   vl_api_ikev2_keys_t *k = &child_sa->keys;
442   vl_api_ikev2_child_sa_t_endian (child_sa);
443
444   fformat (vam->ofp, "  child sa %u:\n", child_sa->child_sa_index);
445
446   fformat (vam->ofp, "    %U ", format_ikev2_sa_transform,
447            &child_sa->encryption);
448   fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &child_sa->integrity);
449   fformat (vam->ofp, "%U \n", format_ikev2_sa_transform, &child_sa->esn);
450
451   fformat (vam->ofp, "    spi(i) %lx spi(r) %lx\n",
452            child_sa->i_spi, child_sa->r_spi);
453
454   fformat (vam->ofp, "    SK_e  i:%U\n          r:%U\n",
455            format_hex_bytes, k->sk_ei, k->sk_ei_len,
456            format_hex_bytes, k->sk_er, k->sk_er_len);
457   if (k->sk_ai_len)
458     {
459       fformat (vam->ofp, "    SK_a  i:%U\n          r:%U\n",
460                format_hex_bytes, k->sk_ai, k->sk_ai_len,
461                format_hex_bytes, k->sk_ar, k->sk_ar_len);
462     }
463   vam->result_ready = 1;
464 }
465
466 static int
467 api_ikev2_traffic_selector_dump (vat_main_t * vam)
468 {
469   unformat_input_t *i = vam->input;
470   ikev2_test_main_t *im = &ikev2_test_main;
471   vl_api_ikev2_traffic_selector_dump_t *mp;
472   vl_api_control_ping_t *mp_ping;
473   int is_initiator = ~0;
474   int sa_index = ~0;
475   int child_sa_index = ~0;
476   int ret;
477
478   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
479     {
480       if (unformat (i, "initiator"))
481         is_initiator = 1;
482       else if (unformat (i, "responder"))
483         is_initiator = 0;
484       else if (unformat (i, "sa_index %d", &sa_index))
485         ;
486       else if (unformat (i, "child_sa_index %d", &child_sa_index))
487         ;
488       else
489         {
490           errmsg ("parse error '%U'", format_unformat_error, i);
491           return -99;
492         }
493     }
494
495   if (child_sa_index == ~0 || sa_index == ~0 || is_initiator == ~0)
496     return -99;
497
498   /* Construct the API message */
499   M (IKEV2_TRAFFIC_SELECTOR_DUMP, mp);
500
501   mp->is_initiator = is_initiator;
502   mp->sa_index = clib_host_to_net_u32 (sa_index);
503   mp->child_sa_index = clib_host_to_net_u32 (child_sa_index);
504
505   /* send it... */
506   S (mp);
507
508   /* Use a control ping for synchronization */
509   if (!im->ping_id)
510     im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
511   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
512   mp_ping->_vl_msg_id = htons (im->ping_id);
513   mp_ping->client_index = vam->my_client_index;
514   vam->result_ready = 0;
515
516   S (mp_ping);
517
518   /* Wait for a reply... */
519   W (ret);
520   return ret;
521 }
522
523 static void
524   vl_api_ikev2_traffic_selector_details_t_handler
525   (vl_api_ikev2_traffic_selector_details_t * mp)
526 {
527   vat_main_t *vam = ikev2_test_main.vat_main;
528   vl_api_ikev2_ts_t *ts = &mp->ts;
529   ip4_address_t start_addr;
530   ip4_address_t end_addr;
531   vl_api_ikev2_ts_t_endian (ts);
532
533   ip4_address_decode (ts->start_addr, &start_addr);
534   ip4_address_decode (ts->end_addr, &end_addr);
535
536   fformat (vam->ofp, " %s protocol_id %u addr "
537            "%U - %U port %u - %u\n",
538            ts->is_local, ts->protocol_id,
539            format_ip4_address, &start_addr,
540            format_ip4_address, &end_addr, ts->start_port, ts->end_port);
541   vam->result_ready = 1;
542 }
543
544 static int
545 api_ikev2_nonce_get (vat_main_t * vam)
546 {
547   unformat_input_t *i = vam->input;
548   ikev2_test_main_t *im = &ikev2_test_main;
549   vl_api_ikev2_nonce_get_t *mp;
550   vl_api_control_ping_t *mp_ping;
551   u32 is_initiator = ~0;
552   u32 sa_index = ~0;
553   int ret;
554
555   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
556     {
557       if (unformat (i, "initiator"))
558         is_initiator = 1;
559       else if (unformat (i, "responder"))
560         is_initiator = 0;
561       else if (unformat (i, "sa_index %d", &sa_index))
562         ;
563       else
564         {
565           errmsg ("parse error '%U'", format_unformat_error, i);
566           return -99;
567         }
568     }
569
570   if (sa_index == ~0 || is_initiator == ~0)
571     return -99;
572
573   /* Construct the API message */
574   M (IKEV2_NONCE_GET, mp);
575
576   mp->is_initiator = is_initiator;
577   mp->sa_index = clib_host_to_net_u32 (sa_index);
578
579   /* send it... */
580   S (mp);
581
582   /* Use a control ping for synchronization */
583   if (!im->ping_id)
584     im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
585   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
586   mp_ping->_vl_msg_id = htons (im->ping_id);
587   mp_ping->client_index = vam->my_client_index;
588   vam->result_ready = 0;
589
590   S (mp_ping);
591
592   /* Wait for a reply... */
593   W (ret);
594   return ret;
595 }
596
597 static void
598 vl_api_ikev2_nonce_get_reply_t_handler (vl_api_ikev2_nonce_get_reply_t * mp)
599 {
600   vat_main_t *vam = ikev2_test_main.vat_main;
601   mp->data_len = clib_net_to_host_u32 (mp->data_len);
602
603   fformat (vam->ofp, "  nonce:%U\n",
604            format_hex_bytes, mp->nonce, mp->data_len);
605
606   vam->result_ready = 1;
607 }
608
609 static int
610 api_ikev2_plugin_get_version (vat_main_t * vam)
611 {
612   ikev2_test_main_t *sm = &ikev2_test_main;
613   vl_api_ikev2_plugin_get_version_t *mp;
614   u32 msg_size = sizeof (*mp);
615   int ret;
616
617   vam->result_ready = 0;
618   mp = vl_msg_api_alloc_as_if_client (msg_size);
619   clib_memset (mp, 0, msg_size);
620   mp->_vl_msg_id = ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION + sm->msg_id_base);
621   mp->client_index = vam->my_client_index;
622
623   /* send it... */
624   S (mp);
625
626   /* Wait for a reply... */
627   W (ret);
628   return ret;
629 }
630
631 static void vl_api_ikev2_plugin_get_version_reply_t_handler
632   (vl_api_ikev2_plugin_get_version_reply_t * mp)
633 {
634   vat_main_t *vam = ikev2_test_main.vat_main;
635   clib_warning ("IKEv2 plugin version: %d.%d", ntohl (mp->major),
636                 ntohl (mp->minor));
637   vam->result_ready = 1;
638 }
639
640 static int
641 api_ikev2_profile_set_ipsec_udp_port (vat_main_t * vam)
642 {
643   return 0;
644 }
645
646 static int
647 api_ikev2_profile_set_liveness (vat_main_t * vam)
648 {
649   unformat_input_t *i = vam->input;
650   vl_api_ikev2_profile_set_liveness_t *mp;
651   u32 period = 0, max_retries = 0;
652   int ret;
653
654   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
655     {
656       if (!unformat (i, "period %d max-retries %d", &period, &max_retries))
657         {
658           errmsg ("parse error '%U'", format_unformat_error, i);
659           return -99;
660         }
661     }
662
663   M (IKEV2_PROFILE_SET_LIVENESS, mp);
664
665   mp->period = clib_host_to_net_u32 (period);
666   mp->max_retries = clib_host_to_net_u32 (max_retries);
667
668   S (mp);
669   W (ret);
670
671   return ret;
672 }
673
674 static int
675 api_ikev2_profile_add_del (vat_main_t * vam)
676 {
677   unformat_input_t *i = vam->input;
678   vl_api_ikev2_profile_add_del_t *mp;
679   u8 is_add = 1;
680   u8 *name = 0;
681   int ret;
682
683   const char *valid_chars = "a-zA-Z0-9_";
684
685   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
686     {
687       if (unformat (i, "del"))
688         is_add = 0;
689       else if (unformat (i, "name %U", unformat_token, valid_chars, &name))
690         vec_add1 (name, 0);
691       else
692         {
693           errmsg ("parse error '%U'", format_unformat_error, i);
694           return -99;
695         }
696     }
697
698   if (!vec_len (name))
699     {
700       errmsg ("profile name must be specified");
701       return -99;
702     }
703
704   if (vec_len (name) > 64)
705     {
706       errmsg ("profile name too long");
707       return -99;
708     }
709
710   M (IKEV2_PROFILE_ADD_DEL, mp);
711
712   clib_memcpy (mp->name, name, vec_len (name));
713   mp->is_add = is_add;
714   vec_free (name);
715
716   S (mp);
717   W (ret);
718   return ret;
719 }
720
721 static int
722 api_ikev2_profile_set_auth (vat_main_t * vam)
723 {
724   unformat_input_t *i = vam->input;
725   vl_api_ikev2_profile_set_auth_t *mp;
726   u8 *name = 0;
727   u8 *data = 0;
728   u32 auth_method = 0;
729   u8 is_hex = 0;
730   int ret;
731
732   const char *valid_chars = "a-zA-Z0-9_";
733
734   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
735     {
736       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
737         vec_add1 (name, 0);
738       else if (unformat (i, "auth_method %U",
739                          unformat_ikev2_auth_method, &auth_method))
740         ;
741       else if (unformat (i, "auth_data 0x%U", unformat_hex_string, &data))
742         is_hex = 1;
743       else if (unformat (i, "auth_data %v", &data))
744         ;
745       else
746         {
747           errmsg ("parse error '%U'", format_unformat_error, i);
748           return -99;
749         }
750     }
751
752   if (!vec_len (name))
753     {
754       errmsg ("profile name must be specified");
755       return -99;
756     }
757
758   if (vec_len (name) > 64)
759     {
760       errmsg ("profile name too long");
761       return -99;
762     }
763
764   if (!vec_len (data))
765     {
766       errmsg ("auth_data must be specified");
767       return -99;
768     }
769
770   if (!auth_method)
771     {
772       errmsg ("auth_method must be specified");
773       return -99;
774     }
775
776   M (IKEV2_PROFILE_SET_AUTH, mp);
777
778   mp->is_hex = is_hex;
779   mp->auth_method = (u8) auth_method;
780   mp->data_len = vec_len (data);
781   clib_memcpy (mp->name, name, vec_len (name));
782   clib_memcpy (mp->data, data, vec_len (data));
783   vec_free (name);
784   vec_free (data);
785
786   S (mp);
787   W (ret);
788   return ret;
789 }
790
791 static int
792 api_ikev2_profile_set_id (vat_main_t * vam)
793 {
794   unformat_input_t *i = vam->input;
795   vl_api_ikev2_profile_set_id_t *mp;
796   u8 *name = 0;
797   u8 *data = 0;
798   u8 is_local = 0;
799   u32 id_type = 0;
800   ip4_address_t ip4;
801   int ret;
802
803   const char *valid_chars = "a-zA-Z0-9_";
804
805   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
806     {
807       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
808         vec_add1 (name, 0);
809       else if (unformat (i, "id_type %U", unformat_ikev2_id_type, &id_type))
810         ;
811       else if (unformat (i, "id_data %U", unformat_ip4_address, &ip4))
812         {
813           data = vec_new (u8, 4);
814           clib_memcpy (data, ip4.as_u8, 4);
815         }
816       else if (unformat (i, "id_data 0x%U", unformat_hex_string, &data))
817         ;
818       else if (unformat (i, "id_data %v", &data))
819         ;
820       else if (unformat (i, "local"))
821         is_local = 1;
822       else if (unformat (i, "remote"))
823         is_local = 0;
824       else
825         {
826           errmsg ("parse error '%U'", format_unformat_error, i);
827           return -99;
828         }
829     }
830
831   if (!vec_len (name))
832     {
833       errmsg ("profile name must be specified");
834       return -99;
835     }
836
837   if (vec_len (name) > 64)
838     {
839       errmsg ("profile name too long");
840       return -99;
841     }
842
843   if (!vec_len (data))
844     {
845       errmsg ("id_data must be specified");
846       return -99;
847     }
848
849   if (!id_type)
850     {
851       errmsg ("id_type must be specified");
852       return -99;
853     }
854
855   M (IKEV2_PROFILE_SET_ID, mp);
856
857   mp->is_local = is_local;
858   mp->id_type = (u8) id_type;
859   mp->data_len = vec_len (data);
860   clib_memcpy (mp->name, name, vec_len (name));
861   clib_memcpy (mp->data, data, vec_len (data));
862   vec_free (name);
863   vec_free (data);
864
865   S (mp);
866   W (ret);
867   return ret;
868 }
869
870 static int
871 api_ikev2_profile_set_ts (vat_main_t * vam)
872 {
873   unformat_input_t *i = vam->input;
874   vl_api_ikev2_profile_set_ts_t *mp;
875   u8 *name = 0;
876   u8 is_local = 0;
877   u32 proto = 0, start_port = 0, end_port = (u32) ~ 0;
878   ip4_address_t start_addr, end_addr;
879
880   const char *valid_chars = "a-zA-Z0-9_";
881   int ret;
882
883   start_addr.as_u32 = 0;
884   end_addr.as_u32 = (u32) ~ 0;
885
886   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
887     {
888       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
889         vec_add1 (name, 0);
890       else if (unformat (i, "protocol %d", &proto))
891         ;
892       else if (unformat (i, "start_port %d", &start_port))
893         ;
894       else if (unformat (i, "end_port %d", &end_port))
895         ;
896       else
897         if (unformat (i, "start_addr %U", unformat_ip4_address, &start_addr))
898         ;
899       else if (unformat (i, "end_addr %U", unformat_ip4_address, &end_addr))
900         ;
901       else if (unformat (i, "local"))
902         is_local = 1;
903       else if (unformat (i, "remote"))
904         is_local = 0;
905       else
906         {
907           errmsg ("parse error '%U'", format_unformat_error, i);
908           return -99;
909         }
910     }
911
912   if (!vec_len (name))
913     {
914       errmsg ("profile name must be specified");
915       return -99;
916     }
917
918   if (vec_len (name) > 64)
919     {
920       errmsg ("profile name too long");
921       return -99;
922     }
923
924   M (IKEV2_PROFILE_SET_TS, mp);
925
926   mp->ts.is_local = is_local;
927   mp->ts.protocol_id = (u8) proto;
928   mp->ts.start_port = clib_host_to_net_u16 ((u16) start_port);
929   mp->ts.end_port = clib_host_to_net_u16 ((u16) end_port);
930   ip4_address_encode (&start_addr, mp->ts.start_addr);
931   ip4_address_encode (&end_addr, mp->ts.end_addr);
932   clib_memcpy (mp->name, name, vec_len (name));
933   vec_free (name);
934
935   S (mp);
936   W (ret);
937   return ret;
938 }
939
940 static int
941 api_ikev2_set_local_key (vat_main_t * vam)
942 {
943   unformat_input_t *i = vam->input;
944   vl_api_ikev2_set_local_key_t *mp;
945   u8 *file = 0;
946   int ret;
947
948   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
949     {
950       if (unformat (i, "file %v", &file))
951         vec_add1 (file, 0);
952       else
953         {
954           errmsg ("parse error '%U'", format_unformat_error, i);
955           return -99;
956         }
957     }
958
959   if (!vec_len (file))
960     {
961       errmsg ("RSA key file must be specified");
962       return -99;
963     }
964
965   if (vec_len (file) > 256)
966     {
967       errmsg ("file name too long");
968       return -99;
969     }
970
971   M (IKEV2_SET_LOCAL_KEY, mp);
972
973   clib_memcpy (mp->key_file, file, vec_len (file));
974   vec_free (file);
975
976   S (mp);
977   W (ret);
978   return ret;
979 }
980
981 static int
982 api_ikev2_profile_set_udp_encap (vat_main_t * vam)
983 {
984   unformat_input_t *i = vam->input;
985   vl_api_ikev2_set_responder_t *mp;
986   int ret;
987   u8 *name = 0;
988
989   const char *valid_chars = "a-zA-Z0-9_";
990
991   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
992     {
993       if (unformat (i, "%U udp-encap", unformat_token, valid_chars, &name))
994         vec_add1 (name, 0);
995       else
996         {
997           errmsg ("parse error '%U'", format_unformat_error, i);
998           return -99;
999         }
1000     }
1001
1002   if (!vec_len (name))
1003     {
1004       errmsg ("profile name must be specified");
1005       return -99;
1006     }
1007
1008   if (vec_len (name) > 64)
1009     {
1010       errmsg ("profile name too long");
1011       return -99;
1012     }
1013
1014   M (IKEV2_PROFILE_SET_UDP_ENCAP, mp);
1015
1016   clib_memcpy (mp->name, name, vec_len (name));
1017   vec_free (name);
1018
1019   S (mp);
1020   W (ret);
1021   return ret;
1022 }
1023
1024 static int
1025 api_ikev2_set_tunnel_interface (vat_main_t * vam)
1026 {
1027   return (0);
1028 }
1029
1030 static int
1031 api_ikev2_set_responder (vat_main_t * vam)
1032 {
1033   unformat_input_t *i = vam->input;
1034   vl_api_ikev2_set_responder_t *mp;
1035   int ret;
1036   u8 *name = 0;
1037   u32 sw_if_index = ~0;
1038   ip4_address_t address;
1039
1040   const char *valid_chars = "a-zA-Z0-9_";
1041
1042   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1043     {
1044       if (unformat
1045           (i, "%U interface %d address %U", unformat_token, valid_chars,
1046            &name, &sw_if_index, unformat_ip4_address, &address))
1047         vec_add1 (name, 0);
1048       else
1049         {
1050           errmsg ("parse error '%U'", format_unformat_error, i);
1051           return -99;
1052         }
1053     }
1054
1055   if (!vec_len (name))
1056     {
1057       errmsg ("profile name must be specified");
1058       return -99;
1059     }
1060
1061   if (vec_len (name) > 64)
1062     {
1063       errmsg ("profile name too long");
1064       return -99;
1065     }
1066
1067   M (IKEV2_SET_RESPONDER, mp);
1068
1069   clib_memcpy (mp->name, name, vec_len (name));
1070   vec_free (name);
1071
1072   mp->responder.sw_if_index = clib_host_to_net_u32 (sw_if_index);
1073   ip4_address_encode (&address, mp->responder.ip4);
1074
1075   S (mp);
1076   W (ret);
1077   return ret;
1078 }
1079
1080 static int
1081 api_ikev2_set_ike_transforms (vat_main_t * vam)
1082 {
1083   unformat_input_t *i = vam->input;
1084   vl_api_ikev2_set_ike_transforms_t *mp;
1085   int ret;
1086   u8 *name = 0;
1087   u32 crypto_alg, crypto_key_size, integ_alg, dh_group;
1088
1089   const char *valid_chars = "a-zA-Z0-9_";
1090
1091   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1092     {
1093       if (unformat (i, "%U %d %d %d %d", unformat_token, valid_chars, &name,
1094                     &crypto_alg, &crypto_key_size, &integ_alg, &dh_group))
1095         vec_add1 (name, 0);
1096       else
1097         {
1098           errmsg ("parse error '%U'", format_unformat_error, i);
1099           return -99;
1100         }
1101     }
1102
1103   if (!vec_len (name))
1104     {
1105       errmsg ("profile name must be specified");
1106       return -99;
1107     }
1108
1109   if (vec_len (name) > 64)
1110     {
1111       errmsg ("profile name too long");
1112       return -99;
1113     }
1114
1115   M (IKEV2_SET_IKE_TRANSFORMS, mp);
1116
1117   clib_memcpy (mp->name, name, vec_len (name));
1118   vec_free (name);
1119   mp->tr.crypto_alg = crypto_alg;
1120   mp->tr.crypto_key_size = clib_host_to_net_u32 (crypto_key_size);
1121   mp->tr.integ_alg = integ_alg;
1122   mp->tr.dh_group = dh_group;
1123
1124   S (mp);
1125   W (ret);
1126   return ret;
1127 }
1128
1129
1130 static int
1131 api_ikev2_set_esp_transforms (vat_main_t * vam)
1132 {
1133   unformat_input_t *i = vam->input;
1134   vl_api_ikev2_set_esp_transforms_t *mp;
1135   int ret;
1136   u8 *name = 0;
1137   u32 crypto_alg, crypto_key_size, integ_alg;
1138
1139   const char *valid_chars = "a-zA-Z0-9_";
1140
1141   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1142     {
1143       if (unformat (i, "%U %d %d %d", unformat_token, valid_chars, &name,
1144                     &crypto_alg, &crypto_key_size, &integ_alg))
1145         vec_add1 (name, 0);
1146       else
1147         {
1148           errmsg ("parse error '%U'", format_unformat_error, i);
1149           return -99;
1150         }
1151     }
1152
1153   if (!vec_len (name))
1154     {
1155       errmsg ("profile name must be specified");
1156       return -99;
1157     }
1158
1159   if (vec_len (name) > 64)
1160     {
1161       errmsg ("profile name too long");
1162       return -99;
1163     }
1164
1165   M (IKEV2_SET_ESP_TRANSFORMS, mp);
1166
1167   clib_memcpy (mp->name, name, vec_len (name));
1168   vec_free (name);
1169   mp->tr.crypto_alg = crypto_alg;
1170   mp->tr.crypto_key_size = clib_host_to_net_u32 (crypto_key_size);
1171   mp->tr.integ_alg = integ_alg;
1172
1173   S (mp);
1174   W (ret);
1175   return ret;
1176 }
1177
1178 static int
1179 api_ikev2_set_sa_lifetime (vat_main_t * vam)
1180 {
1181   unformat_input_t *i = vam->input;
1182   vl_api_ikev2_set_sa_lifetime_t *mp;
1183   int ret;
1184   u8 *name = 0;
1185   u64 lifetime, lifetime_maxdata;
1186   u32 lifetime_jitter, handover;
1187
1188   const char *valid_chars = "a-zA-Z0-9_";
1189
1190   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1191     {
1192       if (unformat (i, "%U %lu %u %u %lu", unformat_token, valid_chars, &name,
1193                     &lifetime, &lifetime_jitter, &handover,
1194                     &lifetime_maxdata))
1195         vec_add1 (name, 0);
1196       else
1197         {
1198           errmsg ("parse error '%U'", format_unformat_error, i);
1199           return -99;
1200         }
1201     }
1202
1203   if (!vec_len (name))
1204     {
1205       errmsg ("profile name must be specified");
1206       return -99;
1207     }
1208
1209   if (vec_len (name) > 64)
1210     {
1211       errmsg ("profile name too long");
1212       return -99;
1213     }
1214
1215   M (IKEV2_SET_SA_LIFETIME, mp);
1216
1217   clib_memcpy (mp->name, name, vec_len (name));
1218   vec_free (name);
1219   mp->lifetime = lifetime;
1220   mp->lifetime_jitter = lifetime_jitter;
1221   mp->handover = handover;
1222   mp->lifetime_maxdata = lifetime_maxdata;
1223
1224   S (mp);
1225   W (ret);
1226   return ret;
1227 }
1228
1229 static int
1230 api_ikev2_initiate_sa_init (vat_main_t * vam)
1231 {
1232   unformat_input_t *i = vam->input;
1233   vl_api_ikev2_initiate_sa_init_t *mp;
1234   int ret;
1235   u8 *name = 0;
1236
1237   const char *valid_chars = "a-zA-Z0-9_";
1238
1239   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1240     {
1241       if (unformat (i, "%U", unformat_token, valid_chars, &name))
1242         vec_add1 (name, 0);
1243       else
1244         {
1245           errmsg ("parse error '%U'", format_unformat_error, i);
1246           return -99;
1247         }
1248     }
1249
1250   if (!vec_len (name))
1251     {
1252       errmsg ("profile name must be specified");
1253       return -99;
1254     }
1255
1256   if (vec_len (name) > 64)
1257     {
1258       errmsg ("profile name too long");
1259       return -99;
1260     }
1261
1262   M (IKEV2_INITIATE_SA_INIT, mp);
1263
1264   clib_memcpy (mp->name, name, vec_len (name));
1265   vec_free (name);
1266
1267   S (mp);
1268   W (ret);
1269   return ret;
1270 }
1271
1272 static int
1273 api_ikev2_initiate_del_ike_sa (vat_main_t * vam)
1274 {
1275   unformat_input_t *i = vam->input;
1276   vl_api_ikev2_initiate_del_ike_sa_t *mp;
1277   int ret;
1278   u64 ispi;
1279
1280
1281   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1282     {
1283       if (unformat (i, "%lx", &ispi))
1284         ;
1285       else
1286         {
1287           errmsg ("parse error '%U'", format_unformat_error, i);
1288           return -99;
1289         }
1290     }
1291
1292   M (IKEV2_INITIATE_DEL_IKE_SA, mp);
1293
1294   mp->ispi = ispi;
1295
1296   S (mp);
1297   W (ret);
1298   return ret;
1299 }
1300
1301 static int
1302 api_ikev2_initiate_del_child_sa (vat_main_t * vam)
1303 {
1304   unformat_input_t *i = vam->input;
1305   vl_api_ikev2_initiate_del_child_sa_t *mp;
1306   int ret;
1307   u32 ispi;
1308
1309
1310   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1311     {
1312       if (unformat (i, "%x", &ispi))
1313         ;
1314       else
1315         {
1316           errmsg ("parse error '%U'", format_unformat_error, i);
1317           return -99;
1318         }
1319     }
1320
1321   M (IKEV2_INITIATE_DEL_CHILD_SA, mp);
1322
1323   mp->ispi = ispi;
1324
1325   S (mp);
1326   W (ret);
1327   return ret;
1328 }
1329
1330 static int
1331 api_ikev2_initiate_rekey_child_sa (vat_main_t * vam)
1332 {
1333   unformat_input_t *i = vam->input;
1334   vl_api_ikev2_initiate_rekey_child_sa_t *mp;
1335   int ret;
1336   u32 ispi;
1337
1338
1339   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1340     {
1341       if (unformat (i, "%x", &ispi))
1342         ;
1343       else
1344         {
1345           errmsg ("parse error '%U'", format_unformat_error, i);
1346           return -99;
1347         }
1348     }
1349
1350   M (IKEV2_INITIATE_REKEY_CHILD_SA, mp);
1351
1352   mp->ispi = ispi;
1353
1354   S (mp);
1355   W (ret);
1356   return ret;
1357 }
1358
1359 #include <ikev2/ikev2.api_test.c>
1360
1361 /*
1362  * fd.io coding-style-patch-verification: ON
1363  *
1364  * Local Variables:
1365  * eval: (c-set-style "gnu")
1366  * End:
1367  */