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