Add API call to set keys on IPsec tunnel intf
[vpp.git] / src / vnet / ipsec / ipsec_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
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ip/ip.h>
26
27 #include <vnet/vnet_msg_enum.h>
28
29 #if WITH_LIBSSL > 0
30 #include <vnet/ipsec/ipsec.h>
31 #include <vnet/ipsec/ikev2.h>
32 #endif /* IPSEC */
33
34 #define vl_typedefs             /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_typedefs
37
38 #define vl_endianfun            /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <vnet/vnet_all_api_h.h>
46 #undef vl_printfun
47
48 #include <vlibapi/api_helper_macros.h>
49
50 #define foreach_vpe_api_msg                                             \
51 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                                 \
52 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)             \
53 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry)                     \
54 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry)                     \
55 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                                   \
56 _(IPSEC_SA_DUMP, ipsec_sa_dump)                                         \
57 _(IPSEC_SPD_DUMP, ipsec_spd_dump)                                       \
58 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del)                     \
59 _(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key)                     \
60 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
61 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
62 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
63 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
64 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
65 _(IKEV2_SET_RESPONDER, ikev2_set_responder)                             \
66 _(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms)                   \
67 _(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms)                   \
68 _(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime)                         \
69 _(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init)                       \
70 _(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa)                 \
71 _(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa)             \
72 _(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa)
73
74 static void vl_api_ipsec_spd_add_del_t_handler
75   (vl_api_ipsec_spd_add_del_t * mp)
76 {
77 #if WITH_LIBSSL == 0
78   clib_warning ("unimplemented");
79 #else
80
81   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
82   vl_api_ipsec_spd_add_del_reply_t *rmp;
83   int rv;
84
85   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
86
87   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
88 #endif
89 }
90
91 static void vl_api_ipsec_interface_add_del_spd_t_handler
92   (vl_api_ipsec_interface_add_del_spd_t * mp)
93 {
94   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
95   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
96   int rv;
97   u32 sw_if_index __attribute__ ((unused));
98   u32 spd_id __attribute__ ((unused));
99
100   sw_if_index = ntohl (mp->sw_if_index);
101   spd_id = ntohl (mp->spd_id);
102
103   VALIDATE_SW_IF_INDEX (mp);
104
105 #if WITH_LIBSSL > 0
106   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
107 #else
108   rv = VNET_API_ERROR_UNIMPLEMENTED;
109 #endif
110
111   BAD_SW_IF_INDEX_LABEL;
112
113   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
114 }
115
116 static void vl_api_ipsec_spd_add_del_entry_t_handler
117   (vl_api_ipsec_spd_add_del_entry_t * mp)
118 {
119   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
120   vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
121   int rv;
122
123 #if WITH_LIBSSL > 0
124   ipsec_policy_t p;
125
126   memset (&p, 0, sizeof (p));
127
128   p.id = ntohl (mp->spd_id);
129   p.priority = ntohl (mp->priority);
130   p.is_outbound = mp->is_outbound;
131   p.is_ipv6 = mp->is_ipv6;
132
133   if (mp->is_ipv6 || mp->is_ip_any)
134     {
135       clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
136       clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
137       clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
138       clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
139     }
140   else
141     {
142       clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
143       clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
144       clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
145       clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
146     }
147   p.protocol = mp->protocol;
148   p.rport.start = ntohs (mp->remote_port_start);
149   p.rport.stop = ntohs (mp->remote_port_stop);
150   p.lport.start = ntohs (mp->local_port_start);
151   p.lport.stop = ntohs (mp->local_port_stop);
152   /* policy action resolve unsupported */
153   if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
154     {
155       clib_warning ("unsupported action: 'resolve'");
156       rv = VNET_API_ERROR_UNIMPLEMENTED;
157       goto out;
158     }
159   p.policy = mp->policy;
160   p.sa_id = ntohl (mp->sa_id);
161
162   rv = ipsec_add_del_policy (vm, &p, mp->is_add);
163   if (rv)
164     goto out;
165
166   if (mp->is_ip_any)
167     {
168       p.is_ipv6 = 1;
169       rv = ipsec_add_del_policy (vm, &p, mp->is_add);
170     }
171 #else
172   rv = VNET_API_ERROR_UNIMPLEMENTED;
173   goto out;
174 #endif
175
176 out:
177   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
178 }
179
180 static void vl_api_ipsec_sad_add_del_entry_t_handler
181   (vl_api_ipsec_sad_add_del_entry_t * mp)
182 {
183   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
184   vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
185   int rv;
186 #if WITH_LIBSSL > 0
187   ipsec_main_t *im = &ipsec_main;
188   ipsec_sa_t sa;
189
190   memset (&sa, 0, sizeof (sa));
191
192   sa.id = ntohl (mp->sad_id);
193   sa.spi = ntohl (mp->spi);
194   /* security protocol AH unsupported */
195   if (mp->protocol == IPSEC_PROTOCOL_AH)
196     {
197       clib_warning ("unsupported security protocol 'AH'");
198       rv = VNET_API_ERROR_UNIMPLEMENTED;
199       goto out;
200     }
201   sa.protocol = mp->protocol;
202   /* check for unsupported crypto-alg */
203   if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
204       mp->crypto_algorithm >= IPSEC_CRYPTO_N_ALG)
205     {
206       clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
207                     mp->crypto_algorithm);
208       rv = VNET_API_ERROR_UNIMPLEMENTED;
209       goto out;
210     }
211   sa.crypto_alg = mp->crypto_algorithm;
212   sa.crypto_key_len = mp->crypto_key_length;
213   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
214   /* check for unsupported integ-alg */
215   if (mp->integrity_algorithm >= IPSEC_INTEG_N_ALG)
216     {
217       clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
218                     mp->integrity_algorithm);
219       rv = VNET_API_ERROR_UNIMPLEMENTED;
220       goto out;
221     }
222
223   sa.integ_alg = mp->integrity_algorithm;
224   sa.integ_key_len = mp->integrity_key_length;
225   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
226   sa.use_esn = mp->use_extended_sequence_number;
227   sa.is_tunnel = mp->is_tunnel;
228   sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
229   if (sa.is_tunnel_ip6)
230     {
231       clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
232       clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
233     }
234   else
235     {
236       clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
237       clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
238     }
239
240   ASSERT (im->cb.check_support_cb);
241   clib_error_t *err = im->cb.check_support_cb (&sa);
242   if (err)
243     {
244       clib_warning ("%s", err->what);
245       rv = VNET_API_ERROR_UNIMPLEMENTED;
246       goto out;
247     }
248
249   rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
250 #else
251   rv = VNET_API_ERROR_UNIMPLEMENTED;
252   goto out;
253 #endif
254
255 out:
256   REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
257 }
258
259 static void
260 send_ipsec_spd_details (ipsec_policy_t * p, unix_shared_memory_queue_t * q,
261                         u32 context)
262 {
263   vl_api_ipsec_spd_details_t *mp;
264
265   mp = vl_msg_api_alloc (sizeof (*mp));
266   memset (mp, 0, sizeof (*mp));
267   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
268   mp->context = context;
269
270   mp->spd_id = htonl (p->id);
271   mp->priority = htonl (p->priority);
272   mp->is_outbound = p->is_outbound;
273   mp->is_ipv6 = p->is_ipv6;
274   if (p->is_ipv6)
275     {
276       memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
277       memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
278       memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
279       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
280     }
281   else
282     {
283       memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
284       memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
285       memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
286       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
287     }
288   mp->local_start_port = htons (p->lport.start);
289   mp->local_stop_port = htons (p->lport.stop);
290   mp->remote_start_port = htons (p->rport.start);
291   mp->remote_stop_port = htons (p->rport.stop);
292   mp->protocol = p->protocol;
293   mp->policy = p->policy;
294   mp->sa_id = htonl (p->sa_id);
295   mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
296   mp->packets = clib_host_to_net_u64 (p->counter.packets);
297
298   vl_msg_api_send_shmem (q, (u8 *) & mp);
299 }
300
301 static void
302 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
303 {
304   unix_shared_memory_queue_t *q;
305   ipsec_main_t *im = &ipsec_main;
306   ipsec_policy_t *policy;
307   ipsec_spd_t *spd;
308   uword *p;
309   u32 spd_index;
310 #if WITH_LIBSSL > 0
311   q = vl_api_client_index_to_input_queue (mp->client_index);
312   if (q == 0)
313     return;
314
315   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
316   if (!p)
317     return;
318
319   spd_index = p[0];
320   spd = pool_elt_at_index (im->spds, spd_index);
321
322   /* *INDENT-OFF* */
323   pool_foreach (policy, spd->policies,
324   ({
325     if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
326       send_ipsec_spd_details (policy, q,
327                               mp->context);}
328     ));
329   /* *INDENT-ON* */
330 #else
331   clib_warning ("unimplemented");
332 #endif
333 }
334
335 static void
336 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
337 {
338   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
339   vl_api_ipsec_sa_set_key_reply_t *rmp;
340   int rv;
341 #if WITH_LIBSSL > 0
342   ipsec_sa_t sa;
343   sa.id = ntohl (mp->sa_id);
344   sa.crypto_key_len = mp->crypto_key_length;
345   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
346   sa.integ_key_len = mp->integrity_key_length;
347   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
348
349   rv = ipsec_set_sa_key (vm, &sa);
350 #else
351   rv = VNET_API_ERROR_UNIMPLEMENTED;
352 #endif
353
354   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
355 }
356
357 static void
358 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
359                                           mp)
360 {
361   vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
362   ipsec_main_t *im = &ipsec_main;
363   vnet_main_t *vnm = im->vnet_main;
364   u32 sw_if_index = ~0;
365   int rv;
366
367 #if WITH_LIBSSL > 0
368   ipsec_add_del_tunnel_args_t tun;
369
370   memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
371
372   tun.is_add = mp->is_add;
373   tun.esn = mp->esn;
374   tun.anti_replay = mp->anti_replay;
375   tun.local_spi = ntohl (mp->local_spi);
376   tun.remote_spi = ntohl (mp->remote_spi);
377   tun.crypto_alg = mp->crypto_alg;
378   tun.local_crypto_key_len = mp->local_crypto_key_len;
379   tun.remote_crypto_key_len = mp->remote_crypto_key_len;
380   tun.integ_alg = mp->integ_alg;
381   tun.local_integ_key_len = mp->local_integ_key_len;
382   tun.remote_integ_key_len = mp->remote_integ_key_len;
383   memcpy (&tun.local_ip, mp->local_ip, 4);
384   memcpy (&tun.remote_ip, mp->remote_ip, 4);
385   memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
386           mp->local_crypto_key_len);
387   memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
388           mp->remote_crypto_key_len);
389   memcpy (&tun.local_integ_key, &mp->local_integ_key,
390           mp->local_integ_key_len);
391   memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
392           mp->remote_integ_key_len);
393
394   rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
395
396 #else
397   rv = VNET_API_ERROR_UNIMPLEMENTED;
398 #endif
399
400   REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY, (
401                                                         {
402                                                         rmp->sw_if_index =
403                                                         htonl (sw_if_index);
404                                                         }));
405 }
406
407 static void
408 send_ipsec_sa_details (ipsec_sa_t * sa, unix_shared_memory_queue_t * q,
409                        u32 context, u32 sw_if_index)
410 {
411   vl_api_ipsec_sa_details_t *mp;
412
413   mp = vl_msg_api_alloc (sizeof (*mp));
414   memset (mp, 0, sizeof (*mp));
415   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
416   mp->context = context;
417
418   mp->sa_id = htonl (sa->id);
419   mp->sw_if_index = htonl (sw_if_index);
420
421   mp->spi = htonl (sa->spi);
422   mp->protocol = sa->protocol;
423
424   mp->crypto_alg = sa->crypto_alg;
425   mp->crypto_key_len = sa->crypto_key_len;
426   memcpy (mp->crypto_key, sa->crypto_key, sa->crypto_key_len);
427
428   mp->integ_alg = sa->integ_alg;
429   mp->integ_key_len = sa->integ_key_len;
430   memcpy (mp->integ_key, sa->integ_key, sa->integ_key_len);
431
432   mp->use_esn = sa->use_esn;
433   mp->use_anti_replay = sa->use_anti_replay;
434
435   mp->is_tunnel = sa->is_tunnel;
436   mp->is_tunnel_ip6 = sa->is_tunnel_ip6;
437
438   if (sa->is_tunnel)
439     {
440       if (sa->is_tunnel_ip6)
441         {
442           memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip6, 16);
443           memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip6, 16);
444         }
445       else
446         {
447           memcpy (mp->tunnel_src_addr, &sa->tunnel_src_addr.ip4, 4);
448           memcpy (mp->tunnel_dst_addr, &sa->tunnel_dst_addr.ip4, 4);
449         }
450     }
451
452   mp->salt = clib_host_to_net_u32 (sa->salt);
453   mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
454   mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
455   if (sa->use_esn)
456     {
457       mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
458       mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
459     }
460   if (sa->use_anti_replay)
461     mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
462   mp->total_data_size = clib_host_to_net_u64 (sa->total_data_size);
463
464   vl_msg_api_send_shmem (q, (u8 *) & mp);
465 }
466
467
468 static void
469 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
470 {
471   unix_shared_memory_queue_t *q;
472   ipsec_main_t *im = &ipsec_main;
473   vnet_main_t *vnm = im->vnet_main;
474   ipsec_sa_t *sa;
475   ipsec_tunnel_if_t *t;
476   u32 *sa_index_to_tun_if_index = 0;
477
478 #if WITH_LIBSSL > 0
479   q = vl_api_client_index_to_input_queue (mp->client_index);
480   if (q == 0 || pool_elts (im->sad) == 0)
481     return;
482
483   vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
484                            ~0);
485
486   /* *INDENT-OFF* */
487   pool_foreach (t, im->tunnel_interfaces,
488   ({
489     vnet_hw_interface_t *hi;
490     u32 sw_if_index = ~0;
491
492     hi = vnet_get_hw_interface (vnm, t->hw_if_index);
493     sw_if_index = hi->sw_if_index;
494     sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
495     sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
496   }));
497
498   pool_foreach (sa, im->sad,
499   ({
500     if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
501       send_ipsec_sa_details (sa, q, mp->context,
502                              sa_index_to_tun_if_index[sa - im->sad]);
503   }));
504   /* *INDENT-ON* */
505
506   vec_free (sa_index_to_tun_if_index);
507 #else
508   clib_warning ("unimplemented");
509 #endif
510 }
511
512
513 static void
514 vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
515                                           mp)
516 {
517   vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
518   ipsec_main_t *im = &ipsec_main;
519   vnet_main_t *vnm = im->vnet_main;
520   vnet_sw_interface_t *sw;
521   u8 *key = 0;
522   int rv;
523
524 #if WITH_LIBSSL > 0
525   sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
526
527   switch (mp->key_type)
528     {
529     case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
530     case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
531       if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
532           mp->alg > IPSEC_CRYPTO_N_ALG)
533         {
534           rv = VNET_API_ERROR_UNIMPLEMENTED;
535           goto out;
536         }
537       break;
538     case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
539     case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
540       if (mp->alg > IPSEC_INTEG_N_ALG)
541         {
542           rv = VNET_API_ERROR_UNIMPLEMENTED;
543           goto out;
544         }
545       break;
546     case IPSEC_IF_SET_KEY_TYPE_NONE:
547     default:
548       rv = VNET_API_ERROR_UNIMPLEMENTED;
549       goto out;
550       break;
551     }
552
553   key = vec_new (u8, mp->key_len);
554   clib_memcpy (key, mp->key, mp->key_len);
555
556   rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
557                                 key);
558   vec_free (key);
559 #else
560   clib_warning ("unimplemented");
561 #endif
562
563 out:
564   REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
565 }
566
567
568 static void
569 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
570 {
571   vl_api_ikev2_profile_add_del_reply_t *rmp;
572   int rv = 0;
573
574 #if WITH_LIBSSL > 0
575   vlib_main_t *vm = vlib_get_main ();
576   clib_error_t *error;
577   u8 *tmp = format (0, "%s", mp->name);
578   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
579   vec_free (tmp);
580   if (error)
581     rv = VNET_API_ERROR_UNSPECIFIED;
582 #else
583   rv = VNET_API_ERROR_UNIMPLEMENTED;
584 #endif
585
586   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
587 }
588
589 static void
590   vl_api_ikev2_profile_set_auth_t_handler
591   (vl_api_ikev2_profile_set_auth_t * mp)
592 {
593   vl_api_ikev2_profile_set_auth_reply_t *rmp;
594   int rv = 0;
595
596 #if WITH_LIBSSL > 0
597   vlib_main_t *vm = vlib_get_main ();
598   clib_error_t *error;
599   u8 *tmp = format (0, "%s", mp->name);
600   u8 *data = vec_new (u8, mp->data_len);
601   clib_memcpy (data, mp->data, mp->data_len);
602   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
603   vec_free (tmp);
604   vec_free (data);
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_AUTH_REPLY);
612 }
613
614 static void
615 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
616 {
617   vl_api_ikev2_profile_add_del_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   u8 *tmp = format (0, "%s", mp->name);
624   u8 *data = vec_new (u8, mp->data_len);
625   clib_memcpy (data, mp->data, mp->data_len);
626   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
627   vec_free (tmp);
628   vec_free (data);
629   if (error)
630     rv = VNET_API_ERROR_UNSPECIFIED;
631 #else
632   rv = VNET_API_ERROR_UNIMPLEMENTED;
633 #endif
634
635   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
636 }
637
638 static void
639 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
640 {
641   vl_api_ikev2_profile_set_ts_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   u8 *tmp = format (0, "%s", mp->name);
648   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
649                                 mp->end_port, (ip4_address_t) mp->start_addr,
650                                 (ip4_address_t) mp->end_addr, mp->is_local);
651   vec_free (tmp);
652   if (error)
653     rv = VNET_API_ERROR_UNSPECIFIED;
654 #else
655   rv = VNET_API_ERROR_UNIMPLEMENTED;
656 #endif
657
658   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
659 }
660
661 static void
662 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
663 {
664   vl_api_ikev2_profile_set_ts_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   error = ikev2_set_local_key (vm, mp->key_file);
672   if (error)
673     rv = VNET_API_ERROR_UNSPECIFIED;
674 #else
675   rv = VNET_API_ERROR_UNIMPLEMENTED;
676 #endif
677
678   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
679 }
680
681 static void
682 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
683 {
684   vl_api_ikev2_set_responder_reply_t *rmp;
685   int rv = 0;
686
687 #if WITH_LIBSSL > 0
688   vlib_main_t *vm = vlib_get_main ();
689   clib_error_t *error;
690
691   u8 *tmp = format (0, "%s", mp->name);
692   ip4_address_t ip4;
693   clib_memcpy (&ip4, mp->address, sizeof (ip4));
694
695   error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
696   vec_free (tmp);
697   if (error)
698     rv = VNET_API_ERROR_UNSPECIFIED;
699 #else
700   rv = VNET_API_ERROR_UNIMPLEMENTED;
701 #endif
702
703   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
704 }
705
706 static void
707 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
708                                            mp)
709 {
710   vl_api_ikev2_set_ike_transforms_reply_t *rmp;
711   int rv = 0;
712
713 #if WITH_LIBSSL > 0
714   vlib_main_t *vm = vlib_get_main ();
715   clib_error_t *error;
716
717   u8 *tmp = format (0, "%s", mp->name);
718
719   error =
720     ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
721                                       mp->dh_group, mp->crypto_key_size);
722   vec_free (tmp);
723   if (error)
724     rv = VNET_API_ERROR_UNSPECIFIED;
725 #else
726   rv = VNET_API_ERROR_UNIMPLEMENTED;
727 #endif
728
729   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
730 }
731
732 static void
733 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
734                                            mp)
735 {
736   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
737   int rv = 0;
738
739 #if WITH_LIBSSL > 0
740   vlib_main_t *vm = vlib_get_main ();
741   clib_error_t *error;
742
743   u8 *tmp = format (0, "%s", mp->name);
744
745   error =
746     ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
747                                       mp->dh_group, mp->crypto_key_size);
748   vec_free (tmp);
749   if (error)
750     rv = VNET_API_ERROR_UNSPECIFIED;
751 #else
752   rv = VNET_API_ERROR_UNIMPLEMENTED;
753 #endif
754
755   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
756 }
757
758 static void
759 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
760 {
761   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
762   int rv = 0;
763
764 #if WITH_LIBSSL > 0
765   vlib_main_t *vm = vlib_get_main ();
766   clib_error_t *error;
767
768   u8 *tmp = format (0, "%s", mp->name);
769
770   error =
771     ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
772                                    mp->handover, mp->lifetime_maxdata);
773   vec_free (tmp);
774   if (error)
775     rv = VNET_API_ERROR_UNSPECIFIED;
776 #else
777   rv = VNET_API_ERROR_UNIMPLEMENTED;
778 #endif
779
780   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
781 }
782
783 static void
784 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
785 {
786   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
787   int rv = 0;
788
789 #if WITH_LIBSSL > 0
790   vlib_main_t *vm = vlib_get_main ();
791   clib_error_t *error;
792
793   u8 *tmp = format (0, "%s", mp->name);
794
795   error = ikev2_initiate_sa_init (vm, tmp);
796   vec_free (tmp);
797   if (error)
798     rv = VNET_API_ERROR_UNSPECIFIED;
799 #else
800   rv = VNET_API_ERROR_UNIMPLEMENTED;
801 #endif
802
803   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
804 }
805
806 static void
807 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
808                                             * mp)
809 {
810   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
811   int rv = 0;
812
813 #if WITH_LIBSSL > 0
814   vlib_main_t *vm = vlib_get_main ();
815   clib_error_t *error;
816
817   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
818   if (error)
819     rv = VNET_API_ERROR_UNSPECIFIED;
820 #else
821   rv = VNET_API_ERROR_UNIMPLEMENTED;
822 #endif
823
824   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
825 }
826
827 static void
828   vl_api_ikev2_initiate_del_child_sa_t_handler
829   (vl_api_ikev2_initiate_del_child_sa_t * mp)
830 {
831   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
832   int rv = 0;
833
834 #if WITH_LIBSSL > 0
835   vlib_main_t *vm = vlib_get_main ();
836   clib_error_t *error;
837
838   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
839   if (error)
840     rv = VNET_API_ERROR_UNSPECIFIED;
841 #else
842   rv = VNET_API_ERROR_UNIMPLEMENTED;
843 #endif
844
845   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
846 }
847
848 static void
849   vl_api_ikev2_initiate_rekey_child_sa_t_handler
850   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
851 {
852   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
853   int rv = 0;
854
855 #if WITH_LIBSSL > 0
856   vlib_main_t *vm = vlib_get_main ();
857   clib_error_t *error;
858
859   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
860   if (error)
861     rv = VNET_API_ERROR_UNSPECIFIED;
862 #else
863   rv = VNET_API_ERROR_UNIMPLEMENTED;
864 #endif
865
866   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
867 }
868
869 /*
870  * ipsec_api_hookup
871  * Add vpe's API message handlers to the table.
872  * vlib has alread mapped shared memory and
873  * added the client registration handlers.
874  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
875  */
876 #define vl_msg_name_crc_list
877 #include <vnet/vnet_all_api_h.h>
878 #undef vl_msg_name_crc_list
879
880 static void
881 setup_message_id_table (api_main_t * am)
882 {
883 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
884   foreach_vl_msg_name_crc_ipsec;
885 #undef _
886 }
887
888 static clib_error_t *
889 ipsec_api_hookup (vlib_main_t * vm)
890 {
891   api_main_t *am = &api_main;
892
893 #define _(N,n)                                                  \
894     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
895                            vl_api_##n##_t_handler,              \
896                            vl_noop_handler,                     \
897                            vl_api_##n##_t_endian,               \
898                            vl_api_##n##_t_print,                \
899                            sizeof(vl_api_##n##_t), 1);
900   foreach_vpe_api_msg;
901 #undef _
902
903   /*
904    * Set up the (msg_name, crc, message-id) table
905    */
906   setup_message_id_table (am);
907
908   return 0;
909 }
910
911 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
912
913 /*
914  * fd.io coding-style-patch-verification: ON
915  *
916  * Local Variables:
917  * eval: (c-set-style "gnu")
918  * End:
919  */