misc: move to new pool_foreach macros
[vpp.git] / src / plugins / ikev2 / ikev2_api.c
1 /*
2  *------------------------------------------------------------------
3  * ipsec_api.c - ipsec api
4  *
5  * Copyright (c) 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 <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22 #include <vnet/api_errno.h>
23 #include <vpp/app/version.h>
24 #include <vnet/ip/ip_types_api.h>
25 #include <ikev2/ikev2.h>
26 #include <ikev2/ikev2_priv.h>
27
28 /* define message IDs */
29 #include <vnet/format_fns.h>
30 #include <plugins/ikev2/ikev2.api_enum.h>
31 #include <plugins/ikev2/ikev2.api_types.h>
32
33
34 #define vl_endianfun            /* define message structures */
35 #include <plugins/ikev2/ikev2.api.h>
36 #include <plugins/ikev2/ikev2_types.api.h>
37 #undef vl_endianfun
38
39 extern ikev2_main_t ikev2_main;
40
41 #define IKEV2_PLUGIN_VERSION_MAJOR 1
42 #define IKEV2_PLUGIN_VERSION_MINOR 0
43 #define REPLY_MSG_ID_BASE ikev2_main.msg_id_base
44 #include <vlibapi/api_helper_macros.h>
45
46 static u32
47 ikev2_encode_sa_index (u32 sai, u32 ti)
48 {
49   return (ti << 16) | sai;
50 }
51
52 static void
53 ikev2_decode_sa_index (u32 api_sai, u32 * sai, u32 * ti)
54 {
55   *sai = api_sai & 0xffff;
56   *ti = api_sai >> 16;
57 }
58
59 static void
60 cp_ike_transforms (vl_api_ikev2_ike_transforms_t * vl_api_ts,
61                    ikev2_transforms_set * ts)
62 {
63   vl_api_ts->crypto_alg = ts->crypto_alg;
64   vl_api_ts->integ_alg = ts->integ_alg;
65   vl_api_ts->dh_group = ts->dh_type;
66   vl_api_ts->crypto_key_size = ts->crypto_key_size;
67 }
68
69 static void
70 cp_esp_transforms (vl_api_ikev2_esp_transforms_t * vl_api_ts,
71                    ikev2_transforms_set * ts)
72 {
73   vl_api_ts->crypto_alg = ts->crypto_alg;
74   vl_api_ts->integ_alg = ts->integ_alg;
75   vl_api_ts->crypto_key_size = ts->crypto_key_size;
76 }
77
78 static void
79 cp_id (vl_api_ikev2_id_t * vl_api_id, ikev2_id_t * id)
80 {
81   if (!id->data)
82     return;
83
84   int size_data = 0;
85   vl_api_id->type = id->type;
86   size_data = sizeof (vl_api_id->data) - 1;     // size without zero ending character
87   if (vec_len (id->data) < size_data)
88     size_data = vec_len (id->data);
89
90   vl_api_id->data_len = size_data;
91   clib_memcpy (vl_api_id->data, id->data, size_data);
92 }
93
94 static void
95 cp_ts (vl_api_ikev2_ts_t * vl_api_ts, ikev2_ts_t * ts, u8 is_local)
96 {
97   vl_api_ts->is_local = is_local;
98   vl_api_ts->protocol_id = ts->protocol_id;
99   vl_api_ts->start_port = ts->start_port;
100   vl_api_ts->end_port = ts->end_port;
101   ip_address_encode2 (&ts->start_addr, &vl_api_ts->start_addr);
102   ip_address_encode2 (&ts->end_addr, &vl_api_ts->end_addr);
103 }
104
105 static void
106 cp_auth (vl_api_ikev2_auth_t * vl_api_auth, ikev2_auth_t * auth)
107 {
108   vl_api_auth->method = auth->method;
109   vl_api_auth->data_len = vec_len (auth->data);
110   vl_api_auth->hex = auth->hex;
111   clib_memcpy (&vl_api_auth->data, auth->data, vec_len (auth->data));
112 }
113
114 static void
115 cp_responder (vl_api_ikev2_responder_t * vl_api_responder,
116               ikev2_responder_t * responder)
117 {
118   vl_api_responder->sw_if_index = responder->sw_if_index;
119   ip_address_encode2 (&responder->addr, &vl_api_responder->addr);
120 }
121
122 void
123 cp_sa_transform (vl_api_ikev2_sa_transform_t * vl_tr,
124                  ikev2_sa_transform_t * tr)
125 {
126   vl_tr->transform_type = tr->type;
127   vl_tr->key_len = tr->key_len;
128   vl_tr->key_trunc = tr->key_trunc;
129   vl_tr->block_size = tr->block_size;
130   vl_tr->dh_group = tr->dh_group;
131   vl_tr->transform_id = tr->encr_type;
132 }
133
134 static void
135 send_profile (ikev2_profile_t * profile, vl_api_registration_t * reg,
136               u32 context)
137 {
138   vl_api_ikev2_profile_details_t *rmp = 0;
139
140   rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len (profile->auth.data));
141   clib_memset (rmp, 0, sizeof (*rmp) + vec_len (profile->auth.data));
142   ikev2_main_t *im = &ikev2_main;
143   rmp->_vl_msg_id = ntohs (VL_API_IKEV2_PROFILE_DETAILS + im->msg_id_base);
144   rmp->context = context;
145
146   int size_data = sizeof (rmp->profile.name) - 1;
147   if (vec_len (profile->name) < size_data)
148     size_data = vec_len (profile->name);
149   clib_memcpy (rmp->profile.name, profile->name, size_data);
150
151   cp_ike_transforms (&rmp->profile.ike_ts, &profile->ike_ts);
152   cp_esp_transforms (&rmp->profile.esp_ts, &profile->esp_ts);
153
154   cp_id (&rmp->profile.loc_id, &profile->loc_id);
155   cp_id (&rmp->profile.rem_id, &profile->rem_id);
156
157   cp_ts (&rmp->profile.rem_ts, &profile->rem_ts, 0 /* is_local */ );
158   cp_ts (&rmp->profile.loc_ts, &profile->loc_ts, 1 /* is_local */ );
159
160   cp_auth (&rmp->profile.auth, &profile->auth);
161
162   cp_responder (&rmp->profile.responder, &profile->responder);
163
164   rmp->profile.udp_encap = profile->udp_encap;
165   rmp->profile.tun_itf = profile->tun_itf;
166   rmp->profile.natt_disabled = profile->natt_disabled;
167   rmp->profile.ipsec_over_udp_port = profile->ipsec_over_udp_port;
168
169   rmp->profile.lifetime = profile->lifetime;
170   rmp->profile.lifetime_maxdata = profile->lifetime_maxdata;
171   rmp->profile.lifetime_jitter = profile->lifetime_jitter;
172   rmp->profile.handover = profile->handover;
173
174   vl_api_ikev2_profile_t_endian (&rmp->profile);
175
176   vl_api_send_msg (reg, (u8 *) rmp);
177 }
178
179 static void
180 vl_api_ikev2_profile_dump_t_handler (vl_api_ikev2_profile_dump_t * mp)
181 {
182   ikev2_main_t *im = &ikev2_main;
183   ikev2_profile_t *profile;
184   vl_api_registration_t *reg;
185   reg = vl_api_client_index_to_registration (mp->client_index);
186   if (!reg)
187     return;
188
189   /* *INDENT-OFF* */
190   pool_foreach (profile, im->profiles)
191    {
192     send_profile (profile, reg, mp->context);
193   }
194   /* *INDENT-ON* */
195 }
196
197 static void
198 send_sa (ikev2_sa_t * sa, vl_api_ikev2_sa_dump_t * mp, u32 api_sa_index)
199 {
200   vl_api_ikev2_sa_details_t *rmp = 0;
201   int rv = 0;
202   ikev2_sa_transform_t *tr;
203
204   /* *INDENT-OFF* */
205   REPLY_MACRO2_ZERO (VL_API_IKEV2_SA_DETAILS,
206   {
207     vl_api_ikev2_sa_t *rsa = &rmp->sa;
208     vl_api_ikev2_keys_t* k = &rsa->keys;
209     rsa->profile_index = rsa->profile_index;
210     rsa->sa_index = api_sa_index;
211     ip_address_encode2 (&sa->iaddr, &rsa->iaddr);
212     ip_address_encode2 (&sa->raddr, &rsa->raddr);
213     rsa->ispi = sa->ispi;
214     rsa->rspi = sa->rspi;
215     cp_id(&rsa->i_id, &sa->i_id);
216     cp_id(&rsa->r_id, &sa->r_id);
217
218     tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
219     if (tr)
220       cp_sa_transform (&rsa->encryption, tr);
221
222     tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
223     if (tr)
224       cp_sa_transform (&rsa->prf, tr);
225
226     tr = ikev2_sa_get_td_for_type (sa->r_proposals,
227                                    IKEV2_TRANSFORM_TYPE_INTEG);
228     if (tr)
229       cp_sa_transform (&rsa->integrity, tr);
230
231     tr = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_DH);
232     if (tr)
233       cp_sa_transform (&rsa->dh, tr);
234
235     k->sk_d_len = vec_len (sa->sk_d);
236     clib_memcpy (&k->sk_d, sa->sk_d, k->sk_d_len);
237
238     k->sk_ai_len = vec_len (sa->sk_ai);
239     clib_memcpy (&k->sk_ai, sa->sk_ai, k->sk_ai_len);
240
241     k->sk_ar_len = vec_len (sa->sk_ar);
242     clib_memcpy (&k->sk_ar, sa->sk_ar, k->sk_ar_len);
243
244     k->sk_ei_len = vec_len (sa->sk_ei);
245     clib_memcpy (&k->sk_ei, sa->sk_ei, k->sk_ei_len);
246
247     k->sk_er_len = vec_len (sa->sk_er);
248     clib_memcpy (&k->sk_er, sa->sk_er, k->sk_er_len);
249
250     k->sk_pi_len = vec_len (sa->sk_pi);
251     clib_memcpy (&k->sk_pi, sa->sk_pi, k->sk_pi_len);
252
253     k->sk_pr_len = vec_len (sa->sk_pr);
254     clib_memcpy (&k->sk_pr, sa->sk_pr, k->sk_pr_len);
255
256     vl_api_ikev2_sa_t_endian(rsa);
257   });
258   /* *INDENT-ON* */
259 }
260
261 static void
262 vl_api_ikev2_sa_dump_t_handler (vl_api_ikev2_sa_dump_t * mp)
263 {
264   ikev2_main_t *km = &ikev2_main;
265   ikev2_main_per_thread_data_t *tkm;
266   ikev2_sa_t *sa;
267
268   vec_foreach (tkm, km->per_thread_data)
269   {
270     /* *INDENT-OFF* */
271     pool_foreach (sa, tkm->sas)
272      {
273       u32 api_sa_index = ikev2_encode_sa_index (sa - tkm->sas,
274                                               tkm - km->per_thread_data);
275       send_sa (sa, mp, api_sa_index);
276     }
277     /* *INDENT-ON* */
278   }
279 }
280
281
282 static void
283 send_child_sa (ikev2_child_sa_t * child,
284                vl_api_ikev2_child_sa_dump_t * mp, u32 child_sa_index,
285                u32 sa_index)
286 {
287   vl_api_ikev2_child_sa_details_t *rmp = 0;
288   int rv = 0;
289   ikev2_sa_transform_t *tr;
290
291   /* *INDENT-OFF* */
292   REPLY_MACRO2_ZERO (VL_API_IKEV2_CHILD_SA_DETAILS,
293   {
294     vl_api_ikev2_keys_t *k = &rmp->child_sa.keys;
295     rmp->child_sa.child_sa_index = child_sa_index;
296     rmp->child_sa.sa_index = sa_index;
297     rmp->child_sa.i_spi =
298       child->i_proposals ? child->i_proposals[0].spi : 0;
299     rmp->child_sa.r_spi =
300       child->r_proposals ? child->r_proposals[0].spi : 0;
301
302     tr = ikev2_sa_get_td_for_type (child->r_proposals,
303                                    IKEV2_TRANSFORM_TYPE_ENCR);
304     if (tr)
305       cp_sa_transform (&rmp->child_sa.encryption, tr);
306
307     tr = ikev2_sa_get_td_for_type (child->r_proposals,
308                                    IKEV2_TRANSFORM_TYPE_INTEG);
309     if (tr)
310       cp_sa_transform (&rmp->child_sa.integrity, tr);
311
312     tr = ikev2_sa_get_td_for_type (child->r_proposals,
313                                    IKEV2_TRANSFORM_TYPE_ESN);
314     if (tr)
315       cp_sa_transform (&rmp->child_sa.esn, tr);
316
317     k->sk_ei_len = vec_len (child->sk_ei);
318     clib_memcpy (&k->sk_ei, child->sk_ei, k->sk_ei_len);
319
320     k->sk_er_len = vec_len (child->sk_er);
321     clib_memcpy (&k->sk_er, child->sk_er, k->sk_er_len);
322
323     if (vec_len (child->sk_ai))
324       {
325         k->sk_ai_len = vec_len (child->sk_ai);
326         clib_memcpy (&k->sk_ai, child->sk_ai,
327                      k->sk_ai_len);
328
329         k->sk_ar_len = vec_len (child->sk_ar);
330         clib_memcpy (&k->sk_ar, child->sk_ar,
331                      k->sk_ar_len);
332       }
333
334     vl_api_ikev2_child_sa_t_endian (&rmp->child_sa);
335   });
336   /* *INDENT-ON* */
337 }
338
339 static void
340 vl_api_ikev2_child_sa_dump_t_handler (vl_api_ikev2_child_sa_dump_t * mp)
341 {
342   ikev2_main_t *im = &ikev2_main;
343   ikev2_main_per_thread_data_t *tkm;
344   ikev2_sa_t *sa;
345   ikev2_child_sa_t *child;
346   u32 sai = ~0, ti = ~0;
347
348   ikev2_decode_sa_index (clib_net_to_host_u32 (mp->sa_index), &sai, &ti);
349
350   if (vec_len (im->per_thread_data) <= ti)
351     return;
352
353   tkm = vec_elt_at_index (im->per_thread_data, ti);
354
355   if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
356     return;
357
358   sa = pool_elt_at_index (tkm->sas, sai);
359
360   vec_foreach (child, sa->childs)
361   {
362     u32 child_sa_index = child - sa->childs;
363     send_child_sa (child, mp, child_sa_index, sai);
364   }
365 }
366
367 static void
368   vl_api_ikev2_traffic_selector_dump_t_handler
369   (vl_api_ikev2_traffic_selector_dump_t * mp)
370 {
371   ikev2_main_t *im = &ikev2_main;
372   ikev2_main_per_thread_data_t *tkm;
373   ikev2_sa_t *sa;
374   ikev2_child_sa_t *child;
375   ikev2_ts_t *ts;
376   u32 sai = ~0, ti = ~0;
377
378   u32 api_sa_index = clib_net_to_host_u32 (mp->sa_index);
379   u32 child_sa_index = clib_net_to_host_u32 (mp->child_sa_index);
380   ikev2_decode_sa_index (api_sa_index, &sai, &ti);
381
382   if (vec_len (im->per_thread_data) <= ti)
383     return;
384
385   tkm = vec_elt_at_index (im->per_thread_data, ti);
386
387   if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
388     return;
389
390   sa = pool_elt_at_index (tkm->sas, sai);
391
392   if (vec_len (sa->childs) <= child_sa_index)
393     return;
394
395   child = vec_elt_at_index (sa->childs, child_sa_index);
396
397   vec_foreach (ts, mp->is_initiator ? child->tsi : child->tsr)
398   {
399     vl_api_ikev2_traffic_selector_details_t *rmp = 0;
400     int rv = 0;
401
402     /* *INDENT-OFF* */
403     REPLY_MACRO2_ZERO (VL_API_IKEV2_TRAFFIC_SELECTOR_DETAILS,
404     {
405       rmp->ts.sa_index = api_sa_index;
406       rmp->ts.child_sa_index = child_sa_index;
407       cp_ts (&rmp->ts, ts, mp->is_initiator);
408       vl_api_ikev2_ts_t_endian (&rmp->ts);
409     });
410     /* *INDENT-ON* */
411   }
412 }
413
414 static void
415 vl_api_ikev2_nonce_get_t_handler (vl_api_ikev2_nonce_get_t * mp)
416 {
417   ikev2_main_t *im = &ikev2_main;
418   ikev2_main_per_thread_data_t *tkm;
419   ikev2_sa_t *sa;
420   u32 sai = ~0, ti = ~0;
421
422   ikev2_decode_sa_index (clib_net_to_host_u32 (mp->sa_index), &sai, &ti);
423
424   if (vec_len (im->per_thread_data) <= ti)
425     return;
426
427   tkm = vec_elt_at_index (im->per_thread_data, ti);
428
429   if (pool_len (tkm->sas) <= sai || pool_is_free_index (tkm->sas, sai))
430     return;
431
432   sa = pool_elt_at_index (tkm->sas, sai);
433
434   u8 *nonce = mp->is_initiator ? sa->i_nonce : sa->r_nonce;
435   vl_api_ikev2_nonce_get_reply_t *rmp = 0;
436   int data_len = vec_len (nonce);
437   int rv = 0;
438
439   /* *INDENT-OFF* */
440   REPLY_MACRO3_ZERO (VL_API_IKEV2_NONCE_GET_REPLY, data_len,
441   {
442     rmp->data_len = clib_host_to_net_u32 (data_len);
443     clib_memcpy (rmp->nonce, nonce, data_len);
444   });
445   /* *INDENT-ON* */
446 }
447
448 static void
449 vl_api_ikev2_plugin_get_version_t_handler (vl_api_ikev2_plugin_get_version_t *
450                                            mp)
451 {
452   ikev2_main_t *im = &ikev2_main;
453   vl_api_ikev2_plugin_get_version_reply_t *rmp;
454   int msg_size = sizeof (*rmp);
455   vl_api_registration_t *reg;
456
457   reg = vl_api_client_index_to_registration (mp->client_index);
458   if (!reg)
459     return;
460
461   rmp = vl_msg_api_alloc (msg_size);
462   clib_memset (rmp, 0, msg_size);
463   rmp->_vl_msg_id =
464     ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION_REPLY + im->msg_id_base);
465   rmp->context = mp->context;
466   rmp->major = htonl (IKEV2_PLUGIN_VERSION_MAJOR);
467   rmp->minor = htonl (IKEV2_PLUGIN_VERSION_MINOR);
468
469   vl_api_send_msg (reg, (u8 *) rmp);
470 }
471
472 static void
473   vl_api_ikev2_profile_set_liveness_t_handler
474   (vl_api_ikev2_profile_set_liveness_t * mp)
475 {
476   vl_api_ikev2_profile_set_liveness_reply_t *rmp;
477   int rv = 0;
478
479 #if WITH_LIBSSL > 0
480   clib_error_t *error;
481   error = ikev2_set_liveness_params (clib_net_to_host_u32 (mp->period),
482                                      clib_net_to_host_u32 (mp->max_retries));
483   if (error)
484     {
485       ikev2_log_error ("%U", format_clib_error, error);
486       clib_error_free (error);
487       rv = VNET_API_ERROR_UNSPECIFIED;
488     }
489 #else
490   rv = VNET_API_ERROR_UNIMPLEMENTED;
491 #endif
492
493   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_LIVENESS_REPLY);
494 }
495
496 static void
497 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
498 {
499   vl_api_ikev2_profile_add_del_reply_t *rmp;
500   int rv = 0;
501
502 #if WITH_LIBSSL > 0
503   vlib_main_t *vm = vlib_get_main ();
504   clib_error_t *error;
505   u8 *tmp = format (0, "%s", mp->name);
506   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
507   vec_free (tmp);
508   if (error)
509     {
510       ikev2_log_error ("%U", format_clib_error, error);
511       clib_error_free (error);
512       rv = VNET_API_ERROR_UNSPECIFIED;
513     }
514 #else
515   rv = VNET_API_ERROR_UNIMPLEMENTED;
516 #endif
517
518   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
519 }
520
521 static void
522   vl_api_ikev2_profile_set_auth_t_handler
523   (vl_api_ikev2_profile_set_auth_t * mp)
524 {
525   vl_api_ikev2_profile_set_auth_reply_t *rmp;
526   int rv = 0;
527
528 #if WITH_LIBSSL > 0
529   vlib_main_t *vm = vlib_get_main ();
530   clib_error_t *error;
531   int data_len = ntohl (mp->data_len);
532   u8 *tmp = format (0, "%s", mp->name);
533   u8 *data = vec_new (u8, data_len);
534   clib_memcpy (data, mp->data, data_len);
535   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
536   vec_free (tmp);
537   vec_free (data);
538   if (error)
539     {
540       ikev2_log_error ("%U", format_clib_error, error);
541       clib_error_free (error);
542       rv = VNET_API_ERROR_UNSPECIFIED;
543     }
544 #else
545   rv = VNET_API_ERROR_UNIMPLEMENTED;
546 #endif
547
548   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
549 }
550
551 static void
552 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
553 {
554   vl_api_ikev2_profile_set_id_reply_t *rmp;
555   int rv = 0;
556
557 #if WITH_LIBSSL > 0
558   vlib_main_t *vm = vlib_get_main ();
559   clib_error_t *error;
560   u8 *tmp = format (0, "%s", mp->name);
561   int data_len = ntohl (mp->data_len);
562   u8 *data = vec_new (u8, data_len);
563   clib_memcpy (data, mp->data, data_len);
564   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
565   vec_free (tmp);
566   vec_free (data);
567   if (error)
568     {
569       ikev2_log_error ("%U", format_clib_error, error);
570       clib_error_free (error);
571       rv = VNET_API_ERROR_UNSPECIFIED;
572     }
573 #else
574   rv = VNET_API_ERROR_UNIMPLEMENTED;
575 #endif
576
577   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
578 }
579
580 static void
581   vl_api_ikev2_profile_set_udp_encap_t_handler
582   (vl_api_ikev2_profile_set_udp_encap_t * mp)
583 {
584   vl_api_ikev2_profile_set_udp_encap_reply_t *rmp;
585   int rv = 0;
586
587 #if WITH_LIBSSL > 0
588   vlib_main_t *vm = vlib_get_main ();
589   clib_error_t *error;
590   u8 *tmp = format (0, "%s", mp->name);
591   error = ikev2_set_profile_udp_encap (vm, tmp);
592   vec_free (tmp);
593   if (error)
594     {
595       ikev2_log_error ("%U", format_clib_error, error);
596       clib_error_free (error);
597       rv = VNET_API_ERROR_UNSPECIFIED;
598     }
599 #else
600   rv = VNET_API_ERROR_UNIMPLEMENTED;
601 #endif
602
603   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_UDP_ENCAP_REPLY);
604 }
605
606 static void
607 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
608 {
609   vl_api_ikev2_profile_set_ts_reply_t *rmp;
610   int rv = 0;
611
612 #if WITH_LIBSSL > 0
613   vlib_main_t *vm = vlib_get_main ();
614   clib_error_t *error;
615   u8 *tmp = format (0, "%s", mp->name);
616   ip_address_t start_addr, end_addr;
617   ip_address_decode2 (&mp->ts.start_addr, &start_addr);
618   ip_address_decode2 (&mp->ts.end_addr, &end_addr);
619   error =
620     ikev2_set_profile_ts (vm, tmp, mp->ts.protocol_id,
621                           clib_net_to_host_u16 (mp->ts.start_port),
622                           clib_net_to_host_u16 (mp->ts.end_port),
623                           start_addr, end_addr, mp->ts.is_local);
624   vec_free (tmp);
625   if (error)
626     {
627       ikev2_log_error ("%U", format_clib_error, error);
628       clib_error_free (error);
629       rv = VNET_API_ERROR_UNSPECIFIED;
630     }
631 #else
632   rv = VNET_API_ERROR_UNIMPLEMENTED;
633 #endif
634
635   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
636 }
637
638 static void
639 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
640 {
641   vl_api_ikev2_set_local_key_reply_t *rmp;
642   int rv = 0;
643
644 #if WITH_LIBSSL > 0
645   vlib_main_t *vm = vlib_get_main ();
646   clib_error_t *error;
647
648   error = ikev2_set_local_key (vm, mp->key_file);
649   if (error)
650     {
651       ikev2_log_error ("%U", format_clib_error, error);
652       clib_error_free (error);
653       rv = VNET_API_ERROR_UNSPECIFIED;
654     }
655 #else
656   rv = VNET_API_ERROR_UNIMPLEMENTED;
657 #endif
658
659   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
660 }
661
662 static void
663 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
664 {
665   vl_api_ikev2_set_responder_reply_t *rmp;
666   int rv = 0;
667
668 #if WITH_LIBSSL > 0
669   vlib_main_t *vm = vlib_get_main ();
670   clib_error_t *error;
671
672   u8 *tmp = format (0, "%s", mp->name);
673   ip_address_t ip;
674   ip_address_decode2 (&mp->responder.addr, &ip);
675   u32 sw_if_index = clib_net_to_host_u32 (mp->responder.sw_if_index);
676
677   error = ikev2_set_profile_responder (vm, tmp, sw_if_index, ip);
678   vec_free (tmp);
679   if (error)
680     {
681       ikev2_log_error ("%U", format_clib_error, error);
682       clib_error_free (error);
683       rv = VNET_API_ERROR_UNSPECIFIED;
684     }
685 #else
686   rv = VNET_API_ERROR_UNIMPLEMENTED;
687 #endif
688
689   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
690 }
691
692 static void
693 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
694                                            mp)
695 {
696   vl_api_ikev2_set_ike_transforms_reply_t *rmp;
697   int rv = 0;
698
699 #if WITH_LIBSSL > 0
700   vlib_main_t *vm = vlib_get_main ();
701   clib_error_t *error;
702
703   u8 *tmp = format (0, "%s", mp->name);
704
705   error =
706     ikev2_set_profile_ike_transforms (vm, tmp, mp->tr.crypto_alg,
707                                       mp->tr.integ_alg,
708                                       mp->tr.dh_group,
709                                       ntohl (mp->tr.crypto_key_size));
710   vec_free (tmp);
711   if (error)
712     {
713       ikev2_log_error ("%U", format_clib_error, error);
714       clib_error_free (error);
715       rv = VNET_API_ERROR_UNSPECIFIED;
716     }
717 #else
718   rv = VNET_API_ERROR_UNIMPLEMENTED;
719 #endif
720
721   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
722 }
723
724 static void
725 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
726                                            mp)
727 {
728   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
729   int rv = 0;
730
731 #if WITH_LIBSSL > 0
732   vlib_main_t *vm = vlib_get_main ();
733   clib_error_t *error;
734
735   u8 *tmp = format (0, "%s", mp->name);
736
737   error =
738     ikev2_set_profile_esp_transforms (vm, tmp, mp->tr.crypto_alg,
739                                       mp->tr.integ_alg,
740                                       ntohl (mp->tr.crypto_key_size));
741   vec_free (tmp);
742   if (error)
743     {
744       ikev2_log_error ("%U", format_clib_error, error);
745       clib_error_free (error);
746       rv = VNET_API_ERROR_UNSPECIFIED;
747     }
748 #else
749   rv = VNET_API_ERROR_UNIMPLEMENTED;
750 #endif
751
752   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
753 }
754
755 static void
756 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
757 {
758   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
759   int rv = 0;
760
761 #if WITH_LIBSSL > 0
762   vlib_main_t *vm = vlib_get_main ();
763   clib_error_t *error;
764
765   u8 *tmp = format (0, "%s", mp->name);
766
767   error =
768     ikev2_set_profile_sa_lifetime (vm, tmp,
769                                    clib_net_to_host_u64 (mp->lifetime),
770                                    ntohl (mp->lifetime_jitter),
771                                    ntohl (mp->handover),
772                                    clib_net_to_host_u64
773                                    (mp->lifetime_maxdata));
774   vec_free (tmp);
775   if (error)
776     {
777       ikev2_log_error ("%U", format_clib_error, error);
778       clib_error_free (error);
779       rv = VNET_API_ERROR_UNSPECIFIED;
780     }
781 #else
782   rv = VNET_API_ERROR_UNIMPLEMENTED;
783 #endif
784
785   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
786 }
787
788 static void
789   vl_api_ikev2_profile_set_ipsec_udp_port_t_handler
790   (vl_api_ikev2_profile_set_ipsec_udp_port_t * mp)
791 {
792   vl_api_ikev2_profile_set_ipsec_udp_port_reply_t *rmp;
793   int rv = 0;
794
795 #if WITH_LIBSSL > 0
796   vlib_main_t *vm = vlib_get_main ();
797
798   u8 *tmp = format (0, "%s", mp->name);
799
800   rv =
801     ikev2_set_profile_ipsec_udp_port (vm, tmp,
802                                       clib_net_to_host_u16 (mp->port),
803                                       mp->is_set);
804   vec_free (tmp);
805 #else
806   rv = VNET_API_ERROR_UNIMPLEMENTED;
807 #endif
808
809   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_IPSEC_UDP_PORT_REPLY);
810 }
811
812 static void
813   vl_api_ikev2_set_tunnel_interface_t_handler
814   (vl_api_ikev2_set_tunnel_interface_t * mp)
815 {
816   vl_api_ikev2_set_tunnel_interface_reply_t *rmp;
817   int rv = 0;
818
819   VALIDATE_SW_IF_INDEX (mp);
820
821 #if WITH_LIBSSL > 0
822   u8 *tmp = format (0, "%s", mp->name);
823   clib_error_t *error;
824
825   error = ikev2_set_profile_tunnel_interface (vlib_get_main (), tmp,
826                                               ntohl (mp->sw_if_index));
827
828   if (error)
829     {
830       ikev2_log_error ("%U", format_clib_error, error);
831       clib_error_free (error);
832       rv = VNET_API_ERROR_UNSPECIFIED;
833     }
834   vec_free (tmp);
835 #else
836   rv = VNET_API_ERROR_UNIMPLEMENTED;
837 #endif
838
839   BAD_SW_IF_INDEX_LABEL;
840   REPLY_MACRO (VL_API_IKEV2_SET_TUNNEL_INTERFACE_REPLY);
841 }
842
843 static void
844 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
845 {
846   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
847   int rv = 0;
848
849 #if WITH_LIBSSL > 0
850   vlib_main_t *vm = vlib_get_main ();
851   clib_error_t *error;
852
853   u8 *tmp = format (0, "%s", mp->name);
854
855   error = ikev2_initiate_sa_init (vm, tmp);
856   vec_free (tmp);
857   if (error)
858     {
859       ikev2_log_error ("%U", format_clib_error, error);
860       clib_error_free (error);
861       rv = VNET_API_ERROR_UNSPECIFIED;
862     }
863 #else
864   rv = VNET_API_ERROR_UNIMPLEMENTED;
865 #endif
866
867   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
868 }
869
870 static void
871 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
872                                             * mp)
873 {
874   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
875   int rv = 0;
876
877 #if WITH_LIBSSL > 0
878   vlib_main_t *vm = vlib_get_main ();
879   clib_error_t *error;
880
881   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
882   if (error)
883     {
884       ikev2_log_error ("%U", format_clib_error, error);
885       clib_error_free (error);
886       rv = VNET_API_ERROR_UNSPECIFIED;
887     }
888 #else
889   rv = VNET_API_ERROR_UNIMPLEMENTED;
890 #endif
891
892   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
893 }
894
895 static void
896   vl_api_ikev2_initiate_del_child_sa_t_handler
897   (vl_api_ikev2_initiate_del_child_sa_t * mp)
898 {
899   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
900   int rv = 0;
901
902 #if WITH_LIBSSL > 0
903   vlib_main_t *vm = vlib_get_main ();
904   clib_error_t *error;
905
906   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
907   if (error)
908     {
909       ikev2_log_error ("%U", format_clib_error, error);
910       clib_error_free (error);
911       rv = VNET_API_ERROR_UNSPECIFIED;
912     }
913 #else
914   rv = VNET_API_ERROR_UNIMPLEMENTED;
915 #endif
916
917   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
918 }
919
920 static void
921   vl_api_ikev2_profile_disable_natt_t_handler
922   (vl_api_ikev2_profile_disable_natt_t * mp)
923 {
924   vl_api_ikev2_profile_disable_natt_reply_t *rmp;
925   int rv = 0;
926
927 #if WITH_LIBSSL > 0
928   clib_error_t *error;
929
930   u8 *tmp = format (0, "%s", mp->name);
931   error = ikev2_profile_natt_disable (tmp);
932   vec_free (tmp);
933   if (error)
934     {
935       ikev2_log_error ("%U", format_clib_error, error);
936       clib_error_free (error);
937       rv = VNET_API_ERROR_UNSPECIFIED;
938     }
939 #else
940   rv = VNET_API_ERROR_UNIMPLEMENTED;
941 #endif
942
943   REPLY_MACRO (VL_API_IKEV2_PROFILE_DISABLE_NATT_REPLY);
944 }
945
946 static void
947   vl_api_ikev2_initiate_rekey_child_sa_t_handler
948   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
949 {
950   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
951   int rv = 0;
952
953 #if WITH_LIBSSL > 0
954   vlib_main_t *vm = vlib_get_main ();
955   clib_error_t *error;
956
957   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
958   if (error)
959     {
960       ikev2_log_error ("%U", format_clib_error, error);
961       clib_error_free (error);
962       rv = VNET_API_ERROR_UNSPECIFIED;
963     }
964 #else
965   rv = VNET_API_ERROR_UNIMPLEMENTED;
966 #endif
967
968   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
969 }
970
971 #include <ikev2/ikev2.api.c>
972 static clib_error_t *
973 ikev2_api_init (vlib_main_t * vm)
974 {
975   ikev2_main_t *im = &ikev2_main;
976
977   /* Ask for a correctly-sized block of API message decode slots */
978   im->msg_id_base = setup_message_id_table ();
979
980   return 0;
981 }
982
983 VLIB_INIT_FUNCTION (ikev2_api_init);
984
985 /*
986  * fd.io coding-style-patch-verification: ON
987  *
988  * Local Variables:
989  * eval: (c-set-style "gnu")
990  * End:
991  */