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