ikev2: add option to disable NAT traversal
[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     rv = VNET_API_ERROR_UNSPECIFIED;
485 #else
486   rv = VNET_API_ERROR_UNIMPLEMENTED;
487 #endif
488
489   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_LIVENESS_REPLY);
490 }
491
492 static void
493 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
494 {
495   vl_api_ikev2_profile_add_del_reply_t *rmp;
496   int rv = 0;
497
498 #if WITH_LIBSSL > 0
499   vlib_main_t *vm = vlib_get_main ();
500   clib_error_t *error;
501   u8 *tmp = format (0, "%s", mp->name);
502   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
503   vec_free (tmp);
504   if (error)
505     rv = VNET_API_ERROR_UNSPECIFIED;
506 #else
507   rv = VNET_API_ERROR_UNIMPLEMENTED;
508 #endif
509
510   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
511 }
512
513 static void
514   vl_api_ikev2_profile_set_auth_t_handler
515   (vl_api_ikev2_profile_set_auth_t * mp)
516 {
517   vl_api_ikev2_profile_set_auth_reply_t *rmp;
518   int rv = 0;
519
520 #if WITH_LIBSSL > 0
521   vlib_main_t *vm = vlib_get_main ();
522   clib_error_t *error;
523   int data_len = ntohl (mp->data_len);
524   u8 *tmp = format (0, "%s", mp->name);
525   u8 *data = vec_new (u8, data_len);
526   clib_memcpy (data, mp->data, data_len);
527   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
528   vec_free (tmp);
529   vec_free (data);
530   if (error)
531     rv = VNET_API_ERROR_UNSPECIFIED;
532 #else
533   rv = VNET_API_ERROR_UNIMPLEMENTED;
534 #endif
535
536   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
537 }
538
539 static void
540 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
541 {
542   vl_api_ikev2_profile_set_id_reply_t *rmp;
543   int rv = 0;
544
545 #if WITH_LIBSSL > 0
546   vlib_main_t *vm = vlib_get_main ();
547   clib_error_t *error;
548   u8 *tmp = format (0, "%s", mp->name);
549   int data_len = ntohl (mp->data_len);
550   u8 *data = vec_new (u8, data_len);
551   clib_memcpy (data, mp->data, data_len);
552   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
553   vec_free (tmp);
554   vec_free (data);
555   if (error)
556     rv = VNET_API_ERROR_UNSPECIFIED;
557 #else
558   rv = VNET_API_ERROR_UNIMPLEMENTED;
559 #endif
560
561   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
562 }
563
564 static void
565   vl_api_ikev2_profile_set_udp_encap_t_handler
566   (vl_api_ikev2_profile_set_udp_encap_t * mp)
567 {
568   vl_api_ikev2_profile_set_udp_encap_reply_t *rmp;
569   int rv = 0;
570
571 #if WITH_LIBSSL > 0
572   vlib_main_t *vm = vlib_get_main ();
573   clib_error_t *error;
574   u8 *tmp = format (0, "%s", mp->name);
575   error = ikev2_set_profile_udp_encap (vm, tmp);
576   vec_free (tmp);
577   if (error)
578     rv = VNET_API_ERROR_UNSPECIFIED;
579 #else
580   rv = VNET_API_ERROR_UNIMPLEMENTED;
581 #endif
582
583   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_UDP_ENCAP_REPLY);
584 }
585
586 static void
587 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
588 {
589   vl_api_ikev2_profile_set_ts_reply_t *rmp;
590   int rv = 0;
591
592 #if WITH_LIBSSL > 0
593   vlib_main_t *vm = vlib_get_main ();
594   clib_error_t *error;
595   u8 *tmp = format (0, "%s", mp->name);
596   ip_address_t start_addr, end_addr;
597   ip_address_decode2 (&mp->ts.start_addr, &start_addr);
598   ip_address_decode2 (&mp->ts.end_addr, &end_addr);
599   error =
600     ikev2_set_profile_ts (vm, tmp, mp->ts.protocol_id,
601                           clib_net_to_host_u16 (mp->ts.start_port),
602                           clib_net_to_host_u16 (mp->ts.end_port),
603                           start_addr, end_addr, mp->ts.is_local);
604   vec_free (tmp);
605   if (error)
606     rv = VNET_API_ERROR_UNSPECIFIED;
607 #else
608   rv = VNET_API_ERROR_UNIMPLEMENTED;
609 #endif
610
611   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
612 }
613
614 static void
615 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
616 {
617   vl_api_ikev2_set_local_key_reply_t *rmp;
618   int rv = 0;
619
620 #if WITH_LIBSSL > 0
621   vlib_main_t *vm = vlib_get_main ();
622   clib_error_t *error;
623
624   error = ikev2_set_local_key (vm, mp->key_file);
625   if (error)
626     rv = VNET_API_ERROR_UNSPECIFIED;
627 #else
628   rv = VNET_API_ERROR_UNIMPLEMENTED;
629 #endif
630
631   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
632 }
633
634 static void
635 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
636 {
637   vl_api_ikev2_set_responder_reply_t *rmp;
638   int rv = 0;
639
640 #if WITH_LIBSSL > 0
641   vlib_main_t *vm = vlib_get_main ();
642   clib_error_t *error;
643
644   u8 *tmp = format (0, "%s", mp->name);
645   ip_address_t ip;
646   ip_address_decode2 (&mp->responder.addr, &ip);
647   u32 sw_if_index = clib_net_to_host_u32 (mp->responder.sw_if_index);
648
649   error = ikev2_set_profile_responder (vm, tmp, sw_if_index, ip);
650   vec_free (tmp);
651   if (error)
652     rv = VNET_API_ERROR_UNSPECIFIED;
653 #else
654   rv = VNET_API_ERROR_UNIMPLEMENTED;
655 #endif
656
657   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
658 }
659
660 static void
661 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
662                                            mp)
663 {
664   vl_api_ikev2_set_ike_transforms_reply_t *rmp;
665   int rv = 0;
666
667 #if WITH_LIBSSL > 0
668   vlib_main_t *vm = vlib_get_main ();
669   clib_error_t *error;
670
671   u8 *tmp = format (0, "%s", mp->name);
672
673   error =
674     ikev2_set_profile_ike_transforms (vm, tmp, mp->tr.crypto_alg,
675                                       mp->tr.integ_alg,
676                                       mp->tr.dh_group,
677                                       ntohl (mp->tr.crypto_key_size));
678   vec_free (tmp);
679   if (error)
680     rv = VNET_API_ERROR_UNSPECIFIED;
681 #else
682   rv = VNET_API_ERROR_UNIMPLEMENTED;
683 #endif
684
685   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
686 }
687
688 static void
689 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
690                                            mp)
691 {
692   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
693   int rv = 0;
694
695 #if WITH_LIBSSL > 0
696   vlib_main_t *vm = vlib_get_main ();
697   clib_error_t *error;
698
699   u8 *tmp = format (0, "%s", mp->name);
700
701   error =
702     ikev2_set_profile_esp_transforms (vm, tmp, mp->tr.crypto_alg,
703                                       mp->tr.integ_alg,
704                                       ntohl (mp->tr.crypto_key_size));
705   vec_free (tmp);
706   if (error)
707     rv = VNET_API_ERROR_UNSPECIFIED;
708 #else
709   rv = VNET_API_ERROR_UNIMPLEMENTED;
710 #endif
711
712   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
713 }
714
715 static void
716 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
717 {
718   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
719   int rv = 0;
720
721 #if WITH_LIBSSL > 0
722   vlib_main_t *vm = vlib_get_main ();
723   clib_error_t *error;
724
725   u8 *tmp = format (0, "%s", mp->name);
726
727   error =
728     ikev2_set_profile_sa_lifetime (vm, tmp,
729                                    clib_net_to_host_u64 (mp->lifetime),
730                                    ntohl (mp->lifetime_jitter),
731                                    ntohl (mp->handover),
732                                    clib_net_to_host_u64
733                                    (mp->lifetime_maxdata));
734   vec_free (tmp);
735   if (error)
736     rv = VNET_API_ERROR_UNSPECIFIED;
737 #else
738   rv = VNET_API_ERROR_UNIMPLEMENTED;
739 #endif
740
741   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
742 }
743
744 static void
745   vl_api_ikev2_profile_set_ipsec_udp_port_t_handler
746   (vl_api_ikev2_profile_set_ipsec_udp_port_t * mp)
747 {
748   vl_api_ikev2_profile_set_ipsec_udp_port_reply_t *rmp;
749   int rv = 0;
750
751 #if WITH_LIBSSL > 0
752   vlib_main_t *vm = vlib_get_main ();
753
754   u8 *tmp = format (0, "%s", mp->name);
755
756   rv =
757     ikev2_set_profile_ipsec_udp_port (vm, tmp,
758                                       clib_net_to_host_u16 (mp->port),
759                                       mp->is_set);
760   vec_free (tmp);
761 #else
762   rv = VNET_API_ERROR_UNIMPLEMENTED;
763 #endif
764
765   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_IPSEC_UDP_PORT_REPLY);
766 }
767
768 static void
769   vl_api_ikev2_set_tunnel_interface_t_handler
770   (vl_api_ikev2_set_tunnel_interface_t * mp)
771 {
772   vl_api_ikev2_set_tunnel_interface_reply_t *rmp;
773   int rv = 0;
774
775   VALIDATE_SW_IF_INDEX (mp);
776
777 #if WITH_LIBSSL > 0
778   u8 *tmp = format (0, "%s", mp->name);
779   clib_error_t *error;
780
781   error = ikev2_set_profile_tunnel_interface (vlib_get_main (), tmp,
782                                               ntohl (mp->sw_if_index));
783
784   if (error)
785     rv = VNET_API_ERROR_UNSPECIFIED;
786   vec_free (tmp);
787 #else
788   rv = VNET_API_ERROR_UNIMPLEMENTED;
789 #endif
790
791   BAD_SW_IF_INDEX_LABEL;
792   REPLY_MACRO (VL_API_IKEV2_SET_TUNNEL_INTERFACE_REPLY);
793 }
794
795 static void
796 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
797 {
798   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
799   int rv = 0;
800
801 #if WITH_LIBSSL > 0
802   vlib_main_t *vm = vlib_get_main ();
803   clib_error_t *error;
804
805   u8 *tmp = format (0, "%s", mp->name);
806
807   error = ikev2_initiate_sa_init (vm, tmp);
808   vec_free (tmp);
809   if (error)
810     rv = VNET_API_ERROR_UNSPECIFIED;
811 #else
812   rv = VNET_API_ERROR_UNIMPLEMENTED;
813 #endif
814
815   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
816 }
817
818 static void
819 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
820                                             * mp)
821 {
822   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
823   int rv = 0;
824
825 #if WITH_LIBSSL > 0
826   vlib_main_t *vm = vlib_get_main ();
827   clib_error_t *error;
828
829   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
830   if (error)
831     rv = VNET_API_ERROR_UNSPECIFIED;
832 #else
833   rv = VNET_API_ERROR_UNIMPLEMENTED;
834 #endif
835
836   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
837 }
838
839 static void
840   vl_api_ikev2_initiate_del_child_sa_t_handler
841   (vl_api_ikev2_initiate_del_child_sa_t * mp)
842 {
843   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
844   int rv = 0;
845
846 #if WITH_LIBSSL > 0
847   vlib_main_t *vm = vlib_get_main ();
848   clib_error_t *error;
849
850   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
851   if (error)
852     rv = VNET_API_ERROR_UNSPECIFIED;
853 #else
854   rv = VNET_API_ERROR_UNIMPLEMENTED;
855 #endif
856
857   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
858 }
859
860 static void
861   vl_api_ikev2_profile_disable_natt_t_handler
862   (vl_api_ikev2_profile_disable_natt_t * mp)
863 {
864   vl_api_ikev2_profile_disable_natt_reply_t *rmp;
865   int rv = 0;
866
867 #if WITH_LIBSSL > 0
868   clib_error_t *error;
869
870   u8 *tmp = format (0, "%s", mp->name);
871   error = ikev2_profile_natt_disable (tmp);
872   vec_free (tmp);
873   if (error)
874     rv = VNET_API_ERROR_UNSPECIFIED;
875 #else
876   rv = VNET_API_ERROR_UNIMPLEMENTED;
877 #endif
878
879   REPLY_MACRO (VL_API_IKEV2_PROFILE_DISABLE_NATT_REPLY);
880 }
881
882 static void
883   vl_api_ikev2_initiate_rekey_child_sa_t_handler
884   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
885 {
886   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
887   int rv = 0;
888
889 #if WITH_LIBSSL > 0
890   vlib_main_t *vm = vlib_get_main ();
891   clib_error_t *error;
892
893   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
894   if (error)
895     rv = VNET_API_ERROR_UNSPECIFIED;
896 #else
897   rv = VNET_API_ERROR_UNIMPLEMENTED;
898 #endif
899
900   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
901 }
902
903 #include <ikev2/ikev2.api.c>
904 static clib_error_t *
905 ikev2_api_init (vlib_main_t * vm)
906 {
907   ikev2_main_t *im = &ikev2_main;
908
909   /* Ask for a correctly-sized block of API message decode slots */
910   im->msg_id_base = setup_message_id_table ();
911
912   return 0;
913 }
914
915 VLIB_INIT_FUNCTION (ikev2_api_init);
916
917 /*
918  * fd.io coding-style-patch-verification: ON
919  *
920  * Local Variables:
921  * eval: (c-set-style "gnu")
922  * End:
923  */