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