ikev2: remove api boilerplate
[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 <plugins/ikev2/ikev2.h>
25
26 #define __plugin_msg_base ikev2_test_main.msg_id_base
27 #include <vlibapi/vat_helper_macros.h>
28
29 /* Declare message IDs */
30 #include <ikev2/ikev2.api_enum.h>
31 #include <ikev2/ikev2.api_types.h>
32
33 typedef struct
34 {
35   /* API message ID base */
36   u16 msg_id_base;
37   vat_main_t *vat_main;
38 } ikev2_test_main_t;
39
40 ikev2_test_main_t ikev2_test_main;
41
42 uword
43 unformat_ikev2_auth_method (unformat_input_t * input, va_list * args)
44 {
45   u32 *r = va_arg (*args, u32 *);
46
47   if (0);
48 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_AUTH_METHOD_##f;
49   foreach_ikev2_auth_method
50 #undef _
51     else
52     return 0;
53   return 1;
54 }
55
56 uword
57 unformat_ikev2_id_type (unformat_input_t * input, va_list * args)
58 {
59   u32 *r = va_arg (*args, u32 *);
60
61   if (0);
62 #define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_ID_TYPE_##f;
63   foreach_ikev2_id_type
64 #undef _
65     else
66     return 0;
67   return 1;
68 }
69
70 static int
71 api_ikev2_plugin_get_version (vat_main_t * vam)
72 {
73   ikev2_test_main_t *sm = &ikev2_test_main;
74   vl_api_ikev2_plugin_get_version_t *mp;
75   u32 msg_size = sizeof (*mp);
76   int ret;
77
78   vam->result_ready = 0;
79   mp = vl_msg_api_alloc_as_if_client (msg_size);
80   clib_memset (mp, 0, msg_size);
81   mp->_vl_msg_id = ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION + sm->msg_id_base);
82   mp->client_index = vam->my_client_index;
83
84   /* send it... */
85   S (mp);
86
87   /* Wait for a reply... */
88   W (ret);
89   return ret;
90 }
91
92 static void vl_api_ikev2_plugin_get_version_reply_t_handler
93   (vl_api_ikev2_plugin_get_version_reply_t * mp)
94 {
95   vat_main_t *vam = ikev2_test_main.vat_main;
96   clib_warning ("IKEv2 plugin version: %d.%d", ntohl (mp->major),
97                 ntohl (mp->minor));
98   vam->result_ready = 1;
99 }
100
101 static int
102 api_ikev2_profile_add_del (vat_main_t * vam)
103 {
104   unformat_input_t *i = vam->input;
105   vl_api_ikev2_profile_add_del_t *mp;
106   u8 is_add = 1;
107   u8 *name = 0;
108   int ret;
109
110   const char *valid_chars = "a-zA-Z0-9_";
111
112   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
113     {
114       if (unformat (i, "del"))
115         is_add = 0;
116       else if (unformat (i, "name %U", unformat_token, valid_chars, &name))
117         vec_add1 (name, 0);
118       else
119         {
120           errmsg ("parse error '%U'", format_unformat_error, i);
121           return -99;
122         }
123     }
124
125   if (!vec_len (name))
126     {
127       errmsg ("profile name must be specified");
128       return -99;
129     }
130
131   if (vec_len (name) > 64)
132     {
133       errmsg ("profile name too long");
134       return -99;
135     }
136
137   M (IKEV2_PROFILE_ADD_DEL, mp);
138
139   clib_memcpy (mp->name, name, vec_len (name));
140   mp->is_add = is_add;
141   vec_free (name);
142
143   S (mp);
144   W (ret);
145   return ret;
146 }
147
148 static int
149 api_ikev2_profile_set_auth (vat_main_t * vam)
150 {
151   unformat_input_t *i = vam->input;
152   vl_api_ikev2_profile_set_auth_t *mp;
153   u8 *name = 0;
154   u8 *data = 0;
155   u32 auth_method = 0;
156   u8 is_hex = 0;
157   int ret;
158
159   const char *valid_chars = "a-zA-Z0-9_";
160
161   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
162     {
163       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
164         vec_add1 (name, 0);
165       else if (unformat (i, "auth_method %U",
166                          unformat_ikev2_auth_method, &auth_method))
167         ;
168       else if (unformat (i, "auth_data 0x%U", unformat_hex_string, &data))
169         is_hex = 1;
170       else if (unformat (i, "auth_data %v", &data))
171         ;
172       else
173         {
174           errmsg ("parse error '%U'", format_unformat_error, i);
175           return -99;
176         }
177     }
178
179   if (!vec_len (name))
180     {
181       errmsg ("profile name must be specified");
182       return -99;
183     }
184
185   if (vec_len (name) > 64)
186     {
187       errmsg ("profile name too long");
188       return -99;
189     }
190
191   if (!vec_len (data))
192     {
193       errmsg ("auth_data must be specified");
194       return -99;
195     }
196
197   if (!auth_method)
198     {
199       errmsg ("auth_method must be specified");
200       return -99;
201     }
202
203   M (IKEV2_PROFILE_SET_AUTH, mp);
204
205   mp->is_hex = is_hex;
206   mp->auth_method = (u8) auth_method;
207   mp->data_len = vec_len (data);
208   clib_memcpy (mp->name, name, vec_len (name));
209   clib_memcpy (mp->data, data, vec_len (data));
210   vec_free (name);
211   vec_free (data);
212
213   S (mp);
214   W (ret);
215   return ret;
216 }
217
218 static int
219 api_ikev2_profile_set_id (vat_main_t * vam)
220 {
221   unformat_input_t *i = vam->input;
222   vl_api_ikev2_profile_set_id_t *mp;
223   u8 *name = 0;
224   u8 *data = 0;
225   u8 is_local = 0;
226   u32 id_type = 0;
227   ip4_address_t ip4;
228   int ret;
229
230   const char *valid_chars = "a-zA-Z0-9_";
231
232   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
233     {
234       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
235         vec_add1 (name, 0);
236       else if (unformat (i, "id_type %U", unformat_ikev2_id_type, &id_type))
237         ;
238       else if (unformat (i, "id_data %U", unformat_ip4_address, &ip4))
239         {
240           data = vec_new (u8, 4);
241           clib_memcpy (data, ip4.as_u8, 4);
242         }
243       else if (unformat (i, "id_data 0x%U", unformat_hex_string, &data))
244         ;
245       else if (unformat (i, "id_data %v", &data))
246         ;
247       else if (unformat (i, "local"))
248         is_local = 1;
249       else if (unformat (i, "remote"))
250         is_local = 0;
251       else
252         {
253           errmsg ("parse error '%U'", format_unformat_error, i);
254           return -99;
255         }
256     }
257
258   if (!vec_len (name))
259     {
260       errmsg ("profile name must be specified");
261       return -99;
262     }
263
264   if (vec_len (name) > 64)
265     {
266       errmsg ("profile name too long");
267       return -99;
268     }
269
270   if (!vec_len (data))
271     {
272       errmsg ("id_data must be specified");
273       return -99;
274     }
275
276   if (!id_type)
277     {
278       errmsg ("id_type must be specified");
279       return -99;
280     }
281
282   M (IKEV2_PROFILE_SET_ID, mp);
283
284   mp->is_local = is_local;
285   mp->id_type = (u8) id_type;
286   mp->data_len = vec_len (data);
287   clib_memcpy (mp->name, name, vec_len (name));
288   clib_memcpy (mp->data, data, vec_len (data));
289   vec_free (name);
290   vec_free (data);
291
292   S (mp);
293   W (ret);
294   return ret;
295 }
296
297 static int
298 api_ikev2_profile_set_ts (vat_main_t * vam)
299 {
300   unformat_input_t *i = vam->input;
301   vl_api_ikev2_profile_set_ts_t *mp;
302   u8 *name = 0;
303   u8 is_local = 0;
304   u32 proto = 0, start_port = 0, end_port = (u32) ~ 0;
305   ip4_address_t start_addr, end_addr;
306
307   const char *valid_chars = "a-zA-Z0-9_";
308   int ret;
309
310   start_addr.as_u32 = 0;
311   end_addr.as_u32 = (u32) ~ 0;
312
313   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
314     {
315       if (unformat (i, "name %U", unformat_token, valid_chars, &name))
316         vec_add1 (name, 0);
317       else if (unformat (i, "protocol %d", &proto))
318         ;
319       else if (unformat (i, "start_port %d", &start_port))
320         ;
321       else if (unformat (i, "end_port %d", &end_port))
322         ;
323       else
324         if (unformat (i, "start_addr %U", unformat_ip4_address, &start_addr))
325         ;
326       else if (unformat (i, "end_addr %U", unformat_ip4_address, &end_addr))
327         ;
328       else if (unformat (i, "local"))
329         is_local = 1;
330       else if (unformat (i, "remote"))
331         is_local = 0;
332       else
333         {
334           errmsg ("parse error '%U'", format_unformat_error, i);
335           return -99;
336         }
337     }
338
339   if (!vec_len (name))
340     {
341       errmsg ("profile name must be specified");
342       return -99;
343     }
344
345   if (vec_len (name) > 64)
346     {
347       errmsg ("profile name too long");
348       return -99;
349     }
350
351   M (IKEV2_PROFILE_SET_TS, mp);
352
353   mp->is_local = is_local;
354   mp->proto = (u8) proto;
355   mp->start_port = (u16) start_port;
356   mp->end_port = (u16) end_port;
357   mp->start_addr = start_addr.as_u32;
358   mp->end_addr = end_addr.as_u32;
359   clib_memcpy (mp->name, name, vec_len (name));
360   vec_free (name);
361
362   S (mp);
363   W (ret);
364   return ret;
365 }
366
367 static int
368 api_ikev2_set_local_key (vat_main_t * vam)
369 {
370   unformat_input_t *i = vam->input;
371   vl_api_ikev2_set_local_key_t *mp;
372   u8 *file = 0;
373   int ret;
374
375   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
376     {
377       if (unformat (i, "file %v", &file))
378         vec_add1 (file, 0);
379       else
380         {
381           errmsg ("parse error '%U'", format_unformat_error, i);
382           return -99;
383         }
384     }
385
386   if (!vec_len (file))
387     {
388       errmsg ("RSA key file must be specified");
389       return -99;
390     }
391
392   if (vec_len (file) > 256)
393     {
394       errmsg ("file name too long");
395       return -99;
396     }
397
398   M (IKEV2_SET_LOCAL_KEY, mp);
399
400   clib_memcpy (mp->key_file, file, vec_len (file));
401   vec_free (file);
402
403   S (mp);
404   W (ret);
405   return ret;
406 }
407
408 static int
409 api_ikev2_set_responder (vat_main_t * vam)
410 {
411   unformat_input_t *i = vam->input;
412   vl_api_ikev2_set_responder_t *mp;
413   int ret;
414   u8 *name = 0;
415   u32 sw_if_index = ~0;
416   ip4_address_t address;
417
418   const char *valid_chars = "a-zA-Z0-9_";
419
420   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
421     {
422       if (unformat
423           (i, "%U interface %d address %U", unformat_token, valid_chars,
424            &name, &sw_if_index, unformat_ip4_address, &address))
425         vec_add1 (name, 0);
426       else
427         {
428           errmsg ("parse error '%U'", format_unformat_error, i);
429           return -99;
430         }
431     }
432
433   if (!vec_len (name))
434     {
435       errmsg ("profile name must be specified");
436       return -99;
437     }
438
439   if (vec_len (name) > 64)
440     {
441       errmsg ("profile name too long");
442       return -99;
443     }
444
445   M (IKEV2_SET_RESPONDER, mp);
446
447   clib_memcpy (mp->name, name, vec_len (name));
448   vec_free (name);
449
450   mp->sw_if_index = sw_if_index;
451   clib_memcpy (mp->address, &address, sizeof (address));
452
453   S (mp);
454   W (ret);
455   return ret;
456 }
457
458 static int
459 api_ikev2_set_ike_transforms (vat_main_t * vam)
460 {
461   unformat_input_t *i = vam->input;
462   vl_api_ikev2_set_ike_transforms_t *mp;
463   int ret;
464   u8 *name = 0;
465   u32 crypto_alg, crypto_key_size, integ_alg, dh_group;
466
467   const char *valid_chars = "a-zA-Z0-9_";
468
469   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
470     {
471       if (unformat (i, "%U %d %d %d %d", unformat_token, valid_chars, &name,
472                     &crypto_alg, &crypto_key_size, &integ_alg, &dh_group))
473         vec_add1 (name, 0);
474       else
475         {
476           errmsg ("parse error '%U'", format_unformat_error, i);
477           return -99;
478         }
479     }
480
481   if (!vec_len (name))
482     {
483       errmsg ("profile name must be specified");
484       return -99;
485     }
486
487   if (vec_len (name) > 64)
488     {
489       errmsg ("profile name too long");
490       return -99;
491     }
492
493   M (IKEV2_SET_IKE_TRANSFORMS, mp);
494
495   clib_memcpy (mp->name, name, vec_len (name));
496   vec_free (name);
497   mp->crypto_alg = crypto_alg;
498   mp->crypto_key_size = crypto_key_size;
499   mp->integ_alg = integ_alg;
500   mp->dh_group = dh_group;
501
502   S (mp);
503   W (ret);
504   return ret;
505 }
506
507
508 static int
509 api_ikev2_set_esp_transforms (vat_main_t * vam)
510 {
511   unformat_input_t *i = vam->input;
512   vl_api_ikev2_set_esp_transforms_t *mp;
513   int ret;
514   u8 *name = 0;
515   u32 crypto_alg, crypto_key_size, integ_alg, dh_group;
516
517   const char *valid_chars = "a-zA-Z0-9_";
518
519   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
520     {
521       if (unformat (i, "%U %d %d %d %d", unformat_token, valid_chars, &name,
522                     &crypto_alg, &crypto_key_size, &integ_alg, &dh_group))
523         vec_add1 (name, 0);
524       else
525         {
526           errmsg ("parse error '%U'", format_unformat_error, i);
527           return -99;
528         }
529     }
530
531   if (!vec_len (name))
532     {
533       errmsg ("profile name must be specified");
534       return -99;
535     }
536
537   if (vec_len (name) > 64)
538     {
539       errmsg ("profile name too long");
540       return -99;
541     }
542
543   M (IKEV2_SET_ESP_TRANSFORMS, mp);
544
545   clib_memcpy (mp->name, name, vec_len (name));
546   vec_free (name);
547   mp->crypto_alg = crypto_alg;
548   mp->crypto_key_size = crypto_key_size;
549   mp->integ_alg = integ_alg;
550   mp->dh_group = dh_group;
551
552   S (mp);
553   W (ret);
554   return ret;
555 }
556
557 static int
558 api_ikev2_set_sa_lifetime (vat_main_t * vam)
559 {
560   unformat_input_t *i = vam->input;
561   vl_api_ikev2_set_sa_lifetime_t *mp;
562   int ret;
563   u8 *name = 0;
564   u64 lifetime, lifetime_maxdata;
565   u32 lifetime_jitter, handover;
566
567   const char *valid_chars = "a-zA-Z0-9_";
568
569   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
570     {
571       if (unformat (i, "%U %lu %u %u %lu", unformat_token, valid_chars, &name,
572                     &lifetime, &lifetime_jitter, &handover,
573                     &lifetime_maxdata))
574         vec_add1 (name, 0);
575       else
576         {
577           errmsg ("parse error '%U'", format_unformat_error, i);
578           return -99;
579         }
580     }
581
582   if (!vec_len (name))
583     {
584       errmsg ("profile name must be specified");
585       return -99;
586     }
587
588   if (vec_len (name) > 64)
589     {
590       errmsg ("profile name too long");
591       return -99;
592     }
593
594   M (IKEV2_SET_SA_LIFETIME, mp);
595
596   clib_memcpy (mp->name, name, vec_len (name));
597   vec_free (name);
598   mp->lifetime = lifetime;
599   mp->lifetime_jitter = lifetime_jitter;
600   mp->handover = handover;
601   mp->lifetime_maxdata = lifetime_maxdata;
602
603   S (mp);
604   W (ret);
605   return ret;
606 }
607
608 static int
609 api_ikev2_initiate_sa_init (vat_main_t * vam)
610 {
611   unformat_input_t *i = vam->input;
612   vl_api_ikev2_initiate_sa_init_t *mp;
613   int ret;
614   u8 *name = 0;
615
616   const char *valid_chars = "a-zA-Z0-9_";
617
618   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
619     {
620       if (unformat (i, "%U", unformat_token, valid_chars, &name))
621         vec_add1 (name, 0);
622       else
623         {
624           errmsg ("parse error '%U'", format_unformat_error, i);
625           return -99;
626         }
627     }
628
629   if (!vec_len (name))
630     {
631       errmsg ("profile name must be specified");
632       return -99;
633     }
634
635   if (vec_len (name) > 64)
636     {
637       errmsg ("profile name too long");
638       return -99;
639     }
640
641   M (IKEV2_INITIATE_SA_INIT, mp);
642
643   clib_memcpy (mp->name, name, vec_len (name));
644   vec_free (name);
645
646   S (mp);
647   W (ret);
648   return ret;
649 }
650
651 static int
652 api_ikev2_initiate_del_ike_sa (vat_main_t * vam)
653 {
654   unformat_input_t *i = vam->input;
655   vl_api_ikev2_initiate_del_ike_sa_t *mp;
656   int ret;
657   u64 ispi;
658
659
660   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
661     {
662       if (unformat (i, "%lx", &ispi))
663         ;
664       else
665         {
666           errmsg ("parse error '%U'", format_unformat_error, i);
667           return -99;
668         }
669     }
670
671   M (IKEV2_INITIATE_DEL_IKE_SA, mp);
672
673   mp->ispi = ispi;
674
675   S (mp);
676   W (ret);
677   return ret;
678 }
679
680 static int
681 api_ikev2_initiate_del_child_sa (vat_main_t * vam)
682 {
683   unformat_input_t *i = vam->input;
684   vl_api_ikev2_initiate_del_child_sa_t *mp;
685   int ret;
686   u32 ispi;
687
688
689   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
690     {
691       if (unformat (i, "%x", &ispi))
692         ;
693       else
694         {
695           errmsg ("parse error '%U'", format_unformat_error, i);
696           return -99;
697         }
698     }
699
700   M (IKEV2_INITIATE_DEL_CHILD_SA, mp);
701
702   mp->ispi = ispi;
703
704   S (mp);
705   W (ret);
706   return ret;
707 }
708
709 static int
710 api_ikev2_initiate_rekey_child_sa (vat_main_t * vam)
711 {
712   unformat_input_t *i = vam->input;
713   vl_api_ikev2_initiate_rekey_child_sa_t *mp;
714   int ret;
715   u32 ispi;
716
717
718   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
719     {
720       if (unformat (i, "%x", &ispi))
721         ;
722       else
723         {
724           errmsg ("parse error '%U'", format_unformat_error, i);
725           return -99;
726         }
727     }
728
729   M (IKEV2_INITIATE_REKEY_CHILD_SA, mp);
730
731   mp->ispi = ispi;
732
733   S (mp);
734   W (ret);
735   return ret;
736 }
737
738 #include <ikev2/ikev2.api_test.c>
739
740 /*
741  * fd.io coding-style-patch-verification: ON
742  *
743  * Local Variables:
744  * eval: (c-set-style "gnu")
745  * End:
746  */