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