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