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