IPSEC: move SA counters into the stats segment
[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 #include <vnet/ip/ip_types_api.h>
27 #include <vnet/fib/fib.h>
28
29 #include <vnet/vnet_msg_enum.h>
30
31 #if WITH_LIBSSL > 0
32 #include <vnet/ipsec/ipsec.h>
33 #include <vnet/ipsec/ikev2.h>
34 #endif /* IPSEC */
35
36 #define vl_typedefs             /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_typedefs
39
40 #define vl_endianfun            /* define message structures */
41 #include <vnet/vnet_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #define vl_printfun
47 #include <vnet/vnet_all_api_h.h>
48 #undef vl_printfun
49
50 #include <vlibapi/api_helper_macros.h>
51
52 #define foreach_vpe_api_msg                                     \
53 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                         \
54 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)     \
55 _(IPSEC_SPD_ENTRY_ADD_DEL, ipsec_spd_entry_add_del)             \
56 _(IPSEC_SAD_ENTRY_ADD_DEL, ipsec_sad_entry_add_del)             \
57 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                           \
58 _(IPSEC_SA_DUMP, ipsec_sa_dump)                                 \
59 _(IPSEC_SPDS_DUMP, ipsec_spds_dump)                             \
60 _(IPSEC_SPD_DUMP, ipsec_spd_dump)                               \
61 _(IPSEC_SPD_INTERFACE_DUMP, ipsec_spd_interface_dump)           \
62 _(IPSEC_TUNNEL_IF_ADD_DEL, ipsec_tunnel_if_add_del)             \
63 _(IPSEC_TUNNEL_IF_SET_KEY, ipsec_tunnel_if_set_key)             \
64 _(IPSEC_TUNNEL_IF_SET_SA, ipsec_tunnel_if_set_sa)               \
65 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                 \
66 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)               \
67 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                   \
68 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                   \
69 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                     \
70 _(IKEV2_SET_RESPONDER, ikev2_set_responder)                     \
71 _(IKEV2_SET_IKE_TRANSFORMS, ikev2_set_ike_transforms)           \
72 _(IKEV2_SET_ESP_TRANSFORMS, ikev2_set_esp_transforms)           \
73 _(IKEV2_SET_SA_LIFETIME, ikev2_set_sa_lifetime)                 \
74 _(IKEV2_INITIATE_SA_INIT, ikev2_initiate_sa_init)               \
75 _(IKEV2_INITIATE_DEL_IKE_SA, ikev2_initiate_del_ike_sa)         \
76 _(IKEV2_INITIATE_DEL_CHILD_SA, ikev2_initiate_del_child_sa)     \
77 _(IKEV2_INITIATE_REKEY_CHILD_SA, ikev2_initiate_rekey_child_sa) \
78 _(IPSEC_SELECT_BACKEND, ipsec_select_backend)                   \
79 _(IPSEC_BACKEND_DUMP, ipsec_backend_dump)
80
81 static void
82 vl_api_ipsec_spd_add_del_t_handler (vl_api_ipsec_spd_add_del_t * mp)
83 {
84 #if WITH_LIBSSL == 0
85   clib_warning ("unimplemented");
86 #else
87
88   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
89   vl_api_ipsec_spd_add_del_reply_t *rmp;
90   int rv;
91
92   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
93
94   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
95 #endif
96 }
97
98 static void vl_api_ipsec_interface_add_del_spd_t_handler
99   (vl_api_ipsec_interface_add_del_spd_t * mp)
100 {
101   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
102   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
103   int rv;
104   u32 sw_if_index __attribute__ ((unused));
105   u32 spd_id __attribute__ ((unused));
106
107   sw_if_index = ntohl (mp->sw_if_index);
108   spd_id = ntohl (mp->spd_id);
109
110   VALIDATE_SW_IF_INDEX (mp);
111
112 #if WITH_LIBSSL > 0
113   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
114 #else
115   rv = VNET_API_ERROR_UNIMPLEMENTED;
116 #endif
117
118   BAD_SW_IF_INDEX_LABEL;
119
120   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
121 }
122
123 static int
124 ipsec_spd_action_decode (vl_api_ipsec_spd_action_t in,
125                          ipsec_policy_action_t * out)
126 {
127   in = clib_net_to_host_u32 (in);
128
129   switch (in)
130     {
131 #define _(v,f,s) case IPSEC_API_SPD_ACTION_##f: \
132       *out = IPSEC_POLICY_ACTION_##f;              \
133       return (0);
134       foreach_ipsec_policy_action
135 #undef _
136     }
137   return (VNET_API_ERROR_UNIMPLEMENTED);
138 }
139
140 static void vl_api_ipsec_spd_entry_add_del_t_handler
141   (vl_api_ipsec_spd_entry_add_del_t * mp)
142 {
143   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
144   vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
145   ip46_type_t itype;
146   u32 stat_index;
147   int rv;
148
149 #if WITH_LIBSSL > 0
150   ipsec_policy_t p;
151
152   clib_memset (&p, 0, sizeof (p));
153
154   p.id = ntohl (mp->entry.spd_id);
155   p.priority = ntohl (mp->entry.priority);
156   p.is_outbound = mp->entry.is_outbound;
157
158   itype = ip_address_decode (&mp->entry.remote_address_start, &p.raddr.start);
159   ip_address_decode (&mp->entry.remote_address_stop, &p.raddr.stop);
160   ip_address_decode (&mp->entry.local_address_start, &p.laddr.start);
161   ip_address_decode (&mp->entry.local_address_stop, &p.laddr.stop);
162
163   p.is_ipv6 = (itype == IP46_TYPE_IP6);
164
165   p.protocol = mp->entry.protocol;
166   p.rport.start = ntohs (mp->entry.remote_port_start);
167   p.rport.stop = ntohs (mp->entry.remote_port_stop);
168   p.lport.start = ntohs (mp->entry.local_port_start);
169   p.lport.stop = ntohs (mp->entry.local_port_stop);
170
171   rv = ipsec_spd_action_decode (mp->entry.policy, &p.policy);
172
173   if (rv)
174     goto out;
175
176   /* policy action resolve unsupported */
177   if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
178     {
179       clib_warning ("unsupported action: 'resolve'");
180       rv = VNET_API_ERROR_UNIMPLEMENTED;
181       goto out;
182     }
183   p.sa_id = ntohl (mp->entry.sa_id);
184
185   rv = ipsec_add_del_policy (vm, &p, mp->is_add, &stat_index);
186   if (rv)
187     goto out;
188
189 #else
190   rv = VNET_API_ERROR_UNIMPLEMENTED;
191   goto out;
192 #endif
193
194 out:
195   /* *INDENT-OFF* */
196   REPLY_MACRO2 (VL_API_IPSEC_SPD_ENTRY_ADD_DEL_REPLY,
197   ({
198     rmp->stat_index = ntohl(stat_index);
199   }));
200   /* *INDENT-ON* */
201 }
202
203 static int
204 ipsec_proto_decode (vl_api_ipsec_proto_t in, ipsec_protocol_t * out)
205 {
206   in = clib_net_to_host_u32 (in);
207
208   switch (in)
209     {
210     case IPSEC_API_PROTO_ESP:
211       *out = IPSEC_PROTOCOL_ESP;
212       return (0);
213     case IPSEC_API_PROTO_AH:
214       *out = IPSEC_PROTOCOL_AH;
215       return (0);
216     }
217   return (VNET_API_ERROR_UNIMPLEMENTED);
218 }
219
220 static vl_api_ipsec_proto_t
221 ipsec_proto_encode (ipsec_protocol_t p)
222 {
223   switch (p)
224     {
225     case IPSEC_PROTOCOL_ESP:
226       return clib_host_to_net_u32 (IPSEC_API_PROTO_ESP);
227     case IPSEC_PROTOCOL_AH:
228       return clib_host_to_net_u32 (IPSEC_API_PROTO_AH);
229     }
230   return (VNET_API_ERROR_UNIMPLEMENTED);
231 }
232
233 static int
234 ipsec_crypto_algo_decode (vl_api_ipsec_crypto_alg_t in,
235                           ipsec_crypto_alg_t * out)
236 {
237   in = clib_net_to_host_u32 (in);
238
239   switch (in)
240     {
241 #define _(v,f,s) case IPSEC_API_CRYPTO_ALG_##f: \
242       *out = IPSEC_CRYPTO_ALG_##f;              \
243       return (0);
244       foreach_ipsec_crypto_alg
245 #undef _
246     }
247   return (VNET_API_ERROR_UNIMPLEMENTED);
248 }
249
250 static vl_api_ipsec_crypto_alg_t
251 ipsec_crypto_algo_encode (ipsec_crypto_alg_t c)
252 {
253   switch (c)
254     {
255 #define _(v,f,s) case IPSEC_CRYPTO_ALG_##f:                     \
256       return clib_host_to_net_u32(IPSEC_API_CRYPTO_ALG_##f);
257       foreach_ipsec_crypto_alg
258 #undef _
259     case IPSEC_CRYPTO_N_ALG:
260       break;
261     }
262   ASSERT (0);
263   return (VNET_API_ERROR_UNIMPLEMENTED);
264 }
265
266
267 static int
268 ipsec_integ_algo_decode (vl_api_ipsec_integ_alg_t in, ipsec_integ_alg_t * out)
269 {
270   in = clib_net_to_host_u32 (in);
271
272   switch (in)
273     {
274 #define _(v,f,s) case IPSEC_API_INTEG_ALG_##f:  \
275       *out = IPSEC_INTEG_ALG_##f;               \
276       return (0);
277       foreach_ipsec_integ_alg
278 #undef _
279     }
280   return (VNET_API_ERROR_UNIMPLEMENTED);
281 }
282
283 static vl_api_ipsec_integ_alg_t
284 ipsec_integ_algo_encode (ipsec_integ_alg_t i)
285 {
286   switch (i)
287     {
288 #define _(v,f,s) case IPSEC_INTEG_ALG_##f:                      \
289       return (clib_host_to_net_u32(IPSEC_API_INTEG_ALG_##f));
290       foreach_ipsec_integ_alg
291 #undef _
292     case IPSEC_INTEG_N_ALG:
293       break;
294     }
295   ASSERT (0);
296   return (VNET_API_ERROR_UNIMPLEMENTED);
297 }
298
299 static void
300 ipsec_key_decode (const vl_api_key_t * key, ipsec_key_t * out)
301 {
302   ipsec_mk_key (out, key->data, key->length);
303 }
304
305 static void
306 ipsec_key_encode (const ipsec_key_t * in, vl_api_key_t * out)
307 {
308   out->length = in->len;
309   clib_memcpy (out->data, in->data, out->length);
310 }
311
312 static ipsec_sa_flags_t
313 ipsec_sa_flags_decode (vl_api_ipsec_sad_flags_t in)
314 {
315   ipsec_sa_flags_t flags = IPSEC_SA_FLAG_NONE;
316   in = clib_net_to_host_u32 (in);
317
318 #define _(v,f,s) if (in & IPSEC_API_SAD_FLAG_##f)    \
319     flags |= IPSEC_SA_FLAG_##f;
320   foreach_ipsec_sa_flags
321 #undef _
322     return (flags);
323 }
324
325 static vl_api_ipsec_sad_flags_t
326 ipsec_sad_flags_encode (const ipsec_sa_t * sa)
327 {
328   vl_api_ipsec_sad_flags_t flags = IPSEC_API_SAD_FLAG_NONE;
329
330   if (sa->use_esn)
331     flags |= IPSEC_API_SAD_FLAG_USE_EXTENDED_SEQ_NUM;
332   if (sa->use_anti_replay)
333     flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
334   if (sa->is_tunnel)
335     flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
336   if (sa->is_tunnel_ip6)
337     flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
338   if (sa->udp_encap)
339     flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
340
341   return clib_host_to_net_u32 (flags);
342 }
343
344 static void vl_api_ipsec_sad_entry_add_del_t_handler
345   (vl_api_ipsec_sad_entry_add_del_t * mp)
346 {
347   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
348   vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
349   ip46_address_t tun_src = { }, tun_dst =
350   {
351   };
352   ipsec_key_t crypto_key, integ_key;
353   ipsec_crypto_alg_t crypto_alg;
354   ipsec_integ_alg_t integ_alg;
355   ipsec_protocol_t proto;
356   ipsec_sa_flags_t flags;
357   u32 id, spi, sa_index;
358   int rv;
359
360 #if WITH_LIBSSL > 0
361
362   id = ntohl (mp->entry.sad_id);
363   spi = ntohl (mp->entry.spi);
364
365   rv = ipsec_proto_decode (mp->entry.protocol, &proto);
366
367   if (rv)
368     goto out;
369
370   rv = ipsec_crypto_algo_decode (mp->entry.crypto_algorithm, &crypto_alg);
371
372   if (rv)
373     goto out;
374
375   rv = ipsec_integ_algo_decode (mp->entry.integrity_algorithm, &integ_alg);
376
377   if (rv)
378     goto out;
379
380   ipsec_key_decode (&mp->entry.crypto_key, &crypto_key);
381   ipsec_key_decode (&mp->entry.integrity_key, &integ_key);
382
383   flags = ipsec_sa_flags_decode (mp->entry.flags);
384
385   ip_address_decode (&mp->entry.tunnel_src, &tun_src);
386   ip_address_decode (&mp->entry.tunnel_dst, &tun_dst);
387
388
389   if (mp->is_add)
390     rv = ipsec_sa_add (id, spi, proto,
391                        crypto_alg, &crypto_key,
392                        integ_alg, &integ_key, flags,
393                        0, &tun_src, &tun_dst, &sa_index);
394   else
395     rv = ipsec_sa_del (id);
396
397 #else
398   rv = VNET_API_ERROR_UNIMPLEMENTED;
399 #endif
400
401 out:
402   /* *INDENT-OFF* */
403   REPLY_MACRO2 (VL_API_IPSEC_SAD_ENTRY_ADD_DEL_REPLY,
404   {
405     rmp->stat_index = htonl (sa_index);
406   });
407   /* *INDENT-ON* */
408 }
409
410 static void
411 send_ipsec_spds_details (ipsec_spd_t * spd, vl_api_registration_t * reg,
412                          u32 context)
413 {
414   vl_api_ipsec_spds_details_t *mp;
415   u32 n_policies = 0;
416
417   mp = vl_msg_api_alloc (sizeof (*mp));
418   clib_memset (mp, 0, sizeof (*mp));
419   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPDS_DETAILS);
420   mp->context = context;
421
422   mp->spd_id = htonl (spd->id);
423 #define _(s, n) n_policies += vec_len (spd->policies[IPSEC_SPD_POLICY_##s]);
424   foreach_ipsec_spd_policy_type
425 #undef _
426     mp->npolicies = htonl (n_policies);
427
428   vl_api_send_msg (reg, (u8 *) mp);
429 }
430
431 static void
432 vl_api_ipsec_spds_dump_t_handler (vl_api_ipsec_spds_dump_t * mp)
433 {
434   vl_api_registration_t *reg;
435   ipsec_main_t *im = &ipsec_main;
436   ipsec_spd_t *spd;
437 #if WITH_LIBSSL > 0
438   reg = vl_api_client_index_to_registration (mp->client_index);
439   if (!reg)
440     return;
441
442   /* *INDENT-OFF* */
443   pool_foreach (spd, im->spds, ({
444     send_ipsec_spds_details (spd, reg, mp->context);
445   }));
446   /* *INDENT-ON* */
447 #else
448   clib_warning ("unimplemented");
449 #endif
450 }
451
452 vl_api_ipsec_spd_action_t
453 ipsec_spd_action_encode (ipsec_policy_action_t in)
454 {
455   vl_api_ipsec_spd_action_t out = IPSEC_API_SPD_ACTION_BYPASS;
456
457   switch (in)
458     {
459 #define _(v,f,s) case IPSEC_POLICY_ACTION_##f: \
460       out = IPSEC_API_SPD_ACTION_##f;          \
461       break;
462       foreach_ipsec_policy_action
463 #undef _
464     }
465   return (clib_host_to_net_u32 (out));
466 }
467
468 static void
469 send_ipsec_spd_details (ipsec_policy_t * p, vl_api_registration_t * reg,
470                         u32 context)
471 {
472   vl_api_ipsec_spd_details_t *mp;
473
474   mp = vl_msg_api_alloc (sizeof (*mp));
475   clib_memset (mp, 0, sizeof (*mp));
476   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
477   mp->context = context;
478
479   mp->entry.spd_id = htonl (p->id);
480   mp->entry.priority = htonl (p->priority);
481   mp->entry.is_outbound = p->is_outbound;
482
483   ip_address_encode (&p->laddr.start, IP46_TYPE_ANY,
484                      &mp->entry.local_address_start);
485   ip_address_encode (&p->laddr.stop, IP46_TYPE_ANY,
486                      &mp->entry.local_address_stop);
487   ip_address_encode (&p->raddr.start, IP46_TYPE_ANY,
488                      &mp->entry.remote_address_start);
489   ip_address_encode (&p->raddr.stop, IP46_TYPE_ANY,
490                      &mp->entry.remote_address_stop);
491   mp->entry.local_port_start = htons (p->lport.start);
492   mp->entry.local_port_stop = htons (p->lport.stop);
493   mp->entry.remote_port_start = htons (p->rport.start);
494   mp->entry.remote_port_stop = htons (p->rport.stop);
495   mp->entry.protocol = p->protocol;
496   mp->entry.policy = ipsec_spd_action_encode (p->policy);
497   mp->entry.sa_id = htonl (p->sa_id);
498
499   vl_api_send_msg (reg, (u8 *) mp);
500 }
501
502 static void
503 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
504 {
505   vl_api_registration_t *reg;
506   ipsec_main_t *im = &ipsec_main;
507   ipsec_spd_policy_t ptype;
508   ipsec_policy_t *policy;
509   ipsec_spd_t *spd;
510   uword *p;
511   u32 spd_index, *ii;
512 #if WITH_LIBSSL > 0
513   reg = vl_api_client_index_to_registration (mp->client_index);
514   if (!reg)
515     return;
516
517   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
518   if (!p)
519     return;
520
521   spd_index = p[0];
522   spd = pool_elt_at_index (im->spds, spd_index);
523
524   /* *INDENT-OFF* */
525   FOR_EACH_IPSEC_SPD_POLICY_TYPE(ptype) {
526     vec_foreach(ii, spd->policies[ptype])
527       {
528         policy = pool_elt_at_index(im->policies, *ii);
529
530         if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == policy->sa_id)
531           send_ipsec_spd_details (policy, reg, mp->context);
532       }
533   }
534   /* *INDENT-ON* */
535 #else
536   clib_warning ("unimplemented");
537 #endif
538 }
539
540 static void
541 send_ipsec_spd_interface_details (vl_api_registration_t * reg, u32 spd_index,
542                                   u32 sw_if_index, u32 context)
543 {
544   vl_api_ipsec_spd_interface_details_t *mp;
545
546   mp = vl_msg_api_alloc (sizeof (*mp));
547   clib_memset (mp, 0, sizeof (*mp));
548   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_INTERFACE_DETAILS);
549   mp->context = context;
550
551   mp->spd_index = htonl (spd_index);
552   mp->sw_if_index = htonl (sw_if_index);
553
554   vl_api_send_msg (reg, (u8 *) mp);
555 }
556
557 static void
558 vl_api_ipsec_spd_interface_dump_t_handler (vl_api_ipsec_spd_interface_dump_t *
559                                            mp)
560 {
561   ipsec_main_t *im = &ipsec_main;
562   vl_api_registration_t *reg;
563   u32 k, v, spd_index;
564
565 #if WITH_LIBSSL > 0
566   reg = vl_api_client_index_to_registration (mp->client_index);
567   if (!reg)
568     return;
569
570   if (mp->spd_index_valid)
571     {
572       spd_index = ntohl (mp->spd_index);
573       /* *INDENT-OFF* */
574       hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
575         if (v == spd_index)
576           send_ipsec_spd_interface_details(reg, v, k, mp->context);
577       }));
578       /* *INDENT-ON* */
579     }
580   else
581     {
582       /* *INDENT-OFF* */
583       hash_foreach(k, v, im->spd_index_by_sw_if_index, ({
584         send_ipsec_spd_interface_details(reg, v, k, mp->context);
585       }));
586       /* *INDENT-ON* */
587     }
588
589 #else
590   clib_warning ("unimplemented");
591 #endif
592 }
593
594 static void
595 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
596 {
597   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
598   vl_api_ipsec_sa_set_key_reply_t *rmp;
599   ipsec_key_t ck, ik;
600   u32 id;
601   int rv;
602 #if WITH_LIBSSL > 0
603
604   id = ntohl (mp->sa_id);
605
606   ipsec_key_decode (&mp->crypto_key, &ck);
607   ipsec_key_decode (&mp->integrity_key, &ik);
608
609   rv = ipsec_set_sa_key (id, &ck, &ik);
610 #else
611   rv = VNET_API_ERROR_UNIMPLEMENTED;
612 #endif
613
614   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
615 }
616
617 static void
618 vl_api_ipsec_tunnel_if_add_del_t_handler (vl_api_ipsec_tunnel_if_add_del_t *
619                                           mp)
620 {
621   vl_api_ipsec_tunnel_if_add_del_reply_t *rmp;
622   ipsec_main_t *im = &ipsec_main;
623   vnet_main_t *vnm = im->vnet_main;
624   u32 sw_if_index = ~0;
625   int rv;
626
627 #if WITH_LIBSSL > 0
628   ipsec_add_del_tunnel_args_t tun;
629
630   clib_memset (&tun, 0, sizeof (ipsec_add_del_tunnel_args_t));
631
632   tun.is_add = mp->is_add;
633   tun.esn = mp->esn;
634   tun.anti_replay = mp->anti_replay;
635   tun.local_spi = ntohl (mp->local_spi);
636   tun.remote_spi = ntohl (mp->remote_spi);
637   tun.crypto_alg = mp->crypto_alg;
638   tun.local_crypto_key_len = mp->local_crypto_key_len;
639   tun.remote_crypto_key_len = mp->remote_crypto_key_len;
640   tun.integ_alg = mp->integ_alg;
641   tun.local_integ_key_len = mp->local_integ_key_len;
642   tun.remote_integ_key_len = mp->remote_integ_key_len;
643   tun.udp_encap = mp->udp_encap;
644   tun.tx_table_id = ntohl (mp->tx_table_id);
645   memcpy (&tun.local_ip.ip4, mp->local_ip, 4);
646   memcpy (&tun.remote_ip.ip4, mp->remote_ip, 4);
647   memcpy (&tun.local_crypto_key, &mp->local_crypto_key,
648           mp->local_crypto_key_len);
649   memcpy (&tun.remote_crypto_key, &mp->remote_crypto_key,
650           mp->remote_crypto_key_len);
651   memcpy (&tun.local_integ_key, &mp->local_integ_key,
652           mp->local_integ_key_len);
653   memcpy (&tun.remote_integ_key, &mp->remote_integ_key,
654           mp->remote_integ_key_len);
655   tun.renumber = mp->renumber;
656   tun.show_instance = ntohl (mp->show_instance);
657
658   rv = ipsec_add_del_tunnel_if_internal (vnm, &tun, &sw_if_index);
659
660 #else
661   rv = VNET_API_ERROR_UNIMPLEMENTED;
662 #endif
663
664   /* *INDENT-OFF* */
665   REPLY_MACRO2 (VL_API_IPSEC_TUNNEL_IF_ADD_DEL_REPLY,
666   ({
667     rmp->sw_if_index = htonl (sw_if_index);
668   }));
669   /* *INDENT-ON* */
670 }
671
672 static void
673 send_ipsec_sa_details (ipsec_sa_t * sa, vl_api_registration_t * reg,
674                        u32 context, u32 sw_if_index)
675 {
676   vl_api_ipsec_sa_details_t *mp;
677
678   mp = vl_msg_api_alloc (sizeof (*mp));
679   clib_memset (mp, 0, sizeof (*mp));
680   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SA_DETAILS);
681   mp->context = context;
682
683   mp->entry.sad_id = htonl (sa->id);
684   mp->entry.spi = htonl (sa->spi);
685   mp->entry.protocol = ipsec_proto_encode (sa->protocol);
686   mp->entry.tx_table_id =
687     htonl (fib_table_get_table_id (sa->tx_fib_index, FIB_PROTOCOL_IP4));
688
689   mp->entry.crypto_algorithm = ipsec_crypto_algo_encode (sa->crypto_alg);
690   ipsec_key_encode (&sa->crypto_key, &mp->entry.crypto_key);
691
692   mp->entry.integrity_algorithm = ipsec_integ_algo_encode (sa->integ_alg);
693   ipsec_key_encode (&sa->integ_key, &mp->entry.integrity_key);
694
695   mp->entry.flags = ipsec_sad_flags_encode (sa);
696
697   if (sa->is_tunnel)
698     {
699       ip_address_encode (&sa->tunnel_src_addr, IP46_TYPE_ANY,
700                          &mp->entry.tunnel_src);
701       ip_address_encode (&sa->tunnel_dst_addr, IP46_TYPE_ANY,
702                          &mp->entry.tunnel_dst);
703     }
704
705   mp->sw_if_index = htonl (sw_if_index);
706   mp->salt = clib_host_to_net_u32 (sa->salt);
707   mp->seq_outbound = clib_host_to_net_u64 (((u64) sa->seq));
708   mp->last_seq_inbound = clib_host_to_net_u64 (((u64) sa->last_seq));
709   if (sa->use_esn)
710     {
711       mp->seq_outbound |= (u64) (clib_host_to_net_u32 (sa->seq_hi));
712       mp->last_seq_inbound |= (u64) (clib_host_to_net_u32 (sa->last_seq_hi));
713     }
714   if (sa->use_anti_replay)
715     mp->replay_window = clib_host_to_net_u64 (sa->replay_window);
716
717   vl_api_send_msg (reg, (u8 *) mp);
718 }
719
720
721 static void
722 vl_api_ipsec_sa_dump_t_handler (vl_api_ipsec_sa_dump_t * mp)
723 {
724   vl_api_registration_t *reg;
725   ipsec_main_t *im = &ipsec_main;
726   vnet_main_t *vnm = im->vnet_main;
727   ipsec_sa_t *sa;
728   ipsec_tunnel_if_t *t;
729   u32 *sa_index_to_tun_if_index = 0;
730
731 #if WITH_LIBSSL > 0
732   reg = vl_api_client_index_to_registration (mp->client_index);
733   if (!reg || pool_elts (im->sad) == 0)
734     return;
735
736   vec_validate_init_empty (sa_index_to_tun_if_index, vec_len (im->sad) - 1,
737                            ~0);
738
739   /* *INDENT-OFF* */
740   pool_foreach (t, im->tunnel_interfaces,
741   ({
742     vnet_hw_interface_t *hi;
743     u32 sw_if_index = ~0;
744
745     hi = vnet_get_hw_interface (vnm, t->hw_if_index);
746     sw_if_index = hi->sw_if_index;
747     sa_index_to_tun_if_index[t->input_sa_index] = sw_if_index;
748     sa_index_to_tun_if_index[t->output_sa_index] = sw_if_index;
749   }));
750
751   pool_foreach (sa, im->sad,
752   ({
753     if (mp->sa_id == ~(0) || ntohl (mp->sa_id) == sa->id)
754       send_ipsec_sa_details (sa, reg, mp->context,
755                              sa_index_to_tun_if_index[sa - im->sad]);
756   }));
757   /* *INDENT-ON* */
758
759   vec_free (sa_index_to_tun_if_index);
760 #else
761   clib_warning ("unimplemented");
762 #endif
763 }
764
765
766 static void
767 vl_api_ipsec_tunnel_if_set_key_t_handler (vl_api_ipsec_tunnel_if_set_key_t *
768                                           mp)
769 {
770   vl_api_ipsec_tunnel_if_set_key_reply_t *rmp;
771   ipsec_main_t *im = &ipsec_main;
772   vnet_main_t *vnm = im->vnet_main;
773   vnet_sw_interface_t *sw;
774   u8 *key = 0;
775   int rv;
776
777 #if WITH_LIBSSL > 0
778   sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
779
780   switch (mp->key_type)
781     {
782     case IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO:
783     case IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO:
784       if (mp->alg < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
785           mp->alg >= IPSEC_CRYPTO_N_ALG)
786         {
787           rv = VNET_API_ERROR_UNIMPLEMENTED;
788           goto out;
789         }
790       break;
791     case IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG:
792     case IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG:
793       if (mp->alg >= IPSEC_INTEG_N_ALG)
794         {
795           rv = VNET_API_ERROR_UNIMPLEMENTED;
796           goto out;
797         }
798       break;
799     case IPSEC_IF_SET_KEY_TYPE_NONE:
800     default:
801       rv = VNET_API_ERROR_UNIMPLEMENTED;
802       goto out;
803       break;
804     }
805
806   key = vec_new (u8, mp->key_len);
807   clib_memcpy (key, mp->key, mp->key_len);
808
809   rv = ipsec_set_interface_key (vnm, sw->hw_if_index, mp->key_type, mp->alg,
810                                 key);
811   vec_free (key);
812 #else
813   clib_warning ("unimplemented");
814 #endif
815
816 out:
817   REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_KEY_REPLY);
818 }
819
820
821 static void
822 vl_api_ipsec_tunnel_if_set_sa_t_handler (vl_api_ipsec_tunnel_if_set_sa_t * mp)
823 {
824   vl_api_ipsec_tunnel_if_set_sa_reply_t *rmp;
825   ipsec_main_t *im = &ipsec_main;
826   vnet_main_t *vnm = im->vnet_main;
827   vnet_sw_interface_t *sw;
828   int rv;
829
830 #if WITH_LIBSSL > 0
831   sw = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
832
833   rv = ipsec_set_interface_sa (vnm, sw->hw_if_index, ntohl (mp->sa_id),
834                                mp->is_outbound);
835 #else
836   clib_warning ("unimplemented");
837 #endif
838
839   REPLY_MACRO (VL_API_IPSEC_TUNNEL_IF_SET_SA_REPLY);
840 }
841
842
843 static void
844 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
845 {
846   vl_api_ikev2_profile_add_del_reply_t *rmp;
847   int rv = 0;
848
849 #if WITH_LIBSSL > 0
850   vlib_main_t *vm = vlib_get_main ();
851   clib_error_t *error;
852   u8 *tmp = format (0, "%s", mp->name);
853   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
854   vec_free (tmp);
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_PROFILE_ADD_DEL_REPLY);
862 }
863
864 static void
865   vl_api_ikev2_profile_set_auth_t_handler
866   (vl_api_ikev2_profile_set_auth_t * mp)
867 {
868   vl_api_ikev2_profile_set_auth_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   int data_len = ntohl (mp->data_len);
875   u8 *tmp = format (0, "%s", mp->name);
876   u8 *data = vec_new (u8, data_len);
877   clib_memcpy (data, mp->data, data_len);
878   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
879   vec_free (tmp);
880   vec_free (data);
881   if (error)
882     rv = VNET_API_ERROR_UNSPECIFIED;
883 #else
884   rv = VNET_API_ERROR_UNIMPLEMENTED;
885 #endif
886
887   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
888 }
889
890 static void
891 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
892 {
893   vl_api_ikev2_profile_add_del_reply_t *rmp;
894   int rv = 0;
895
896 #if WITH_LIBSSL > 0
897   vlib_main_t *vm = vlib_get_main ();
898   clib_error_t *error;
899   u8 *tmp = format (0, "%s", mp->name);
900   int data_len = ntohl (mp->data_len);
901   u8 *data = vec_new (u8, data_len);
902   clib_memcpy (data, mp->data, data_len);
903   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
904   vec_free (tmp);
905   vec_free (data);
906   if (error)
907     rv = VNET_API_ERROR_UNSPECIFIED;
908 #else
909   rv = VNET_API_ERROR_UNIMPLEMENTED;
910 #endif
911
912   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
913 }
914
915 static void
916 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
917 {
918   vl_api_ikev2_profile_set_ts_reply_t *rmp;
919   int rv = 0;
920
921 #if WITH_LIBSSL > 0
922   vlib_main_t *vm = vlib_get_main ();
923   clib_error_t *error;
924   u8 *tmp = format (0, "%s", mp->name);
925   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
926                                 mp->end_port, (ip4_address_t) mp->start_addr,
927                                 (ip4_address_t) mp->end_addr, mp->is_local);
928   vec_free (tmp);
929   if (error)
930     rv = VNET_API_ERROR_UNSPECIFIED;
931 #else
932   rv = VNET_API_ERROR_UNIMPLEMENTED;
933 #endif
934
935   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
936 }
937
938 static void
939 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
940 {
941   vl_api_ikev2_profile_set_ts_reply_t *rmp;
942   int rv = 0;
943
944 #if WITH_LIBSSL > 0
945   vlib_main_t *vm = vlib_get_main ();
946   clib_error_t *error;
947
948   error = ikev2_set_local_key (vm, mp->key_file);
949   if (error)
950     rv = VNET_API_ERROR_UNSPECIFIED;
951 #else
952   rv = VNET_API_ERROR_UNIMPLEMENTED;
953 #endif
954
955   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
956 }
957
958 static void
959 vl_api_ikev2_set_responder_t_handler (vl_api_ikev2_set_responder_t * mp)
960 {
961   vl_api_ikev2_set_responder_reply_t *rmp;
962   int rv = 0;
963
964 #if WITH_LIBSSL > 0
965   vlib_main_t *vm = vlib_get_main ();
966   clib_error_t *error;
967
968   u8 *tmp = format (0, "%s", mp->name);
969   ip4_address_t ip4;
970   clib_memcpy (&ip4, mp->address, sizeof (ip4));
971
972   error = ikev2_set_profile_responder (vm, tmp, mp->sw_if_index, ip4);
973   vec_free (tmp);
974   if (error)
975     rv = VNET_API_ERROR_UNSPECIFIED;
976 #else
977   rv = VNET_API_ERROR_UNIMPLEMENTED;
978 #endif
979
980   REPLY_MACRO (VL_API_IKEV2_SET_RESPONDER_REPLY);
981 }
982
983 static void
984 vl_api_ikev2_set_ike_transforms_t_handler (vl_api_ikev2_set_ike_transforms_t *
985                                            mp)
986 {
987   vl_api_ikev2_set_ike_transforms_reply_t *rmp;
988   int rv = 0;
989
990 #if WITH_LIBSSL > 0
991   vlib_main_t *vm = vlib_get_main ();
992   clib_error_t *error;
993
994   u8 *tmp = format (0, "%s", mp->name);
995
996   error =
997     ikev2_set_profile_ike_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
998                                       mp->dh_group, mp->crypto_key_size);
999   vec_free (tmp);
1000   if (error)
1001     rv = VNET_API_ERROR_UNSPECIFIED;
1002 #else
1003   rv = VNET_API_ERROR_UNIMPLEMENTED;
1004 #endif
1005
1006   REPLY_MACRO (VL_API_IKEV2_SET_IKE_TRANSFORMS_REPLY);
1007 }
1008
1009 static void
1010 vl_api_ikev2_set_esp_transforms_t_handler (vl_api_ikev2_set_esp_transforms_t *
1011                                            mp)
1012 {
1013   vl_api_ikev2_set_esp_transforms_reply_t *rmp;
1014   int rv = 0;
1015
1016 #if WITH_LIBSSL > 0
1017   vlib_main_t *vm = vlib_get_main ();
1018   clib_error_t *error;
1019
1020   u8 *tmp = format (0, "%s", mp->name);
1021
1022   error =
1023     ikev2_set_profile_esp_transforms (vm, tmp, mp->crypto_alg, mp->integ_alg,
1024                                       mp->dh_group, mp->crypto_key_size);
1025   vec_free (tmp);
1026   if (error)
1027     rv = VNET_API_ERROR_UNSPECIFIED;
1028 #else
1029   rv = VNET_API_ERROR_UNIMPLEMENTED;
1030 #endif
1031
1032   REPLY_MACRO (VL_API_IKEV2_SET_ESP_TRANSFORMS_REPLY);
1033 }
1034
1035 static void
1036 vl_api_ikev2_set_sa_lifetime_t_handler (vl_api_ikev2_set_sa_lifetime_t * mp)
1037 {
1038   vl_api_ikev2_set_sa_lifetime_reply_t *rmp;
1039   int rv = 0;
1040
1041 #if WITH_LIBSSL > 0
1042   vlib_main_t *vm = vlib_get_main ();
1043   clib_error_t *error;
1044
1045   u8 *tmp = format (0, "%s", mp->name);
1046
1047   error =
1048     ikev2_set_profile_sa_lifetime (vm, tmp, mp->lifetime, mp->lifetime_jitter,
1049                                    mp->handover, mp->lifetime_maxdata);
1050   vec_free (tmp);
1051   if (error)
1052     rv = VNET_API_ERROR_UNSPECIFIED;
1053 #else
1054   rv = VNET_API_ERROR_UNIMPLEMENTED;
1055 #endif
1056
1057   REPLY_MACRO (VL_API_IKEV2_SET_SA_LIFETIME_REPLY);
1058 }
1059
1060 static void
1061 vl_api_ikev2_initiate_sa_init_t_handler (vl_api_ikev2_initiate_sa_init_t * mp)
1062 {
1063   vl_api_ikev2_initiate_sa_init_reply_t *rmp;
1064   int rv = 0;
1065
1066 #if WITH_LIBSSL > 0
1067   vlib_main_t *vm = vlib_get_main ();
1068   clib_error_t *error;
1069
1070   u8 *tmp = format (0, "%s", mp->name);
1071
1072   error = ikev2_initiate_sa_init (vm, tmp);
1073   vec_free (tmp);
1074   if (error)
1075     rv = VNET_API_ERROR_UNSPECIFIED;
1076 #else
1077   rv = VNET_API_ERROR_UNIMPLEMENTED;
1078 #endif
1079
1080   REPLY_MACRO (VL_API_IKEV2_INITIATE_SA_INIT_REPLY);
1081 }
1082
1083 static void
1084 vl_api_ikev2_initiate_del_ike_sa_t_handler (vl_api_ikev2_initiate_del_ike_sa_t
1085                                             * mp)
1086 {
1087   vl_api_ikev2_initiate_del_ike_sa_reply_t *rmp;
1088   int rv = 0;
1089
1090 #if WITH_LIBSSL > 0
1091   vlib_main_t *vm = vlib_get_main ();
1092   clib_error_t *error;
1093
1094   error = ikev2_initiate_delete_ike_sa (vm, mp->ispi);
1095   if (error)
1096     rv = VNET_API_ERROR_UNSPECIFIED;
1097 #else
1098   rv = VNET_API_ERROR_UNIMPLEMENTED;
1099 #endif
1100
1101   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_IKE_SA_REPLY);
1102 }
1103
1104 static void
1105   vl_api_ikev2_initiate_del_child_sa_t_handler
1106   (vl_api_ikev2_initiate_del_child_sa_t * mp)
1107 {
1108   vl_api_ikev2_initiate_del_child_sa_reply_t *rmp;
1109   int rv = 0;
1110
1111 #if WITH_LIBSSL > 0
1112   vlib_main_t *vm = vlib_get_main ();
1113   clib_error_t *error;
1114
1115   error = ikev2_initiate_delete_child_sa (vm, mp->ispi);
1116   if (error)
1117     rv = VNET_API_ERROR_UNSPECIFIED;
1118 #else
1119   rv = VNET_API_ERROR_UNIMPLEMENTED;
1120 #endif
1121
1122   REPLY_MACRO (VL_API_IKEV2_INITIATE_DEL_CHILD_SA_REPLY);
1123 }
1124
1125 static void
1126   vl_api_ikev2_initiate_rekey_child_sa_t_handler
1127   (vl_api_ikev2_initiate_rekey_child_sa_t * mp)
1128 {
1129   vl_api_ikev2_initiate_rekey_child_sa_reply_t *rmp;
1130   int rv = 0;
1131
1132 #if WITH_LIBSSL > 0
1133   vlib_main_t *vm = vlib_get_main ();
1134   clib_error_t *error;
1135
1136   error = ikev2_initiate_rekey_child_sa (vm, mp->ispi);
1137   if (error)
1138     rv = VNET_API_ERROR_UNSPECIFIED;
1139 #else
1140   rv = VNET_API_ERROR_UNIMPLEMENTED;
1141 #endif
1142
1143   REPLY_MACRO (VL_API_IKEV2_INITIATE_REKEY_CHILD_SA_REPLY);
1144 }
1145
1146 /*
1147  * ipsec_api_hookup
1148  * Add vpe's API message handlers to the table.
1149  * vlib has already mapped shared memory and
1150  * added the client registration handlers.
1151  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1152  */
1153 #define vl_msg_name_crc_list
1154 #include <vnet/vnet_all_api_h.h>
1155 #undef vl_msg_name_crc_list
1156
1157 static void
1158 setup_message_id_table (api_main_t * am)
1159 {
1160 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1161   foreach_vl_msg_name_crc_ipsec;
1162 #undef _
1163 }
1164
1165 static void
1166 vl_api_ipsec_backend_dump_t_handler (vl_api_ipsec_backend_dump_t * mp)
1167 {
1168   vl_api_registration_t *rp;
1169   ipsec_main_t *im = &ipsec_main;
1170   u32 context = mp->context;
1171
1172   rp = vl_api_client_index_to_registration (mp->client_index);
1173
1174   if (rp == 0)
1175     {
1176       clib_warning ("Client %d AWOL", mp->client_index);
1177       return;
1178     }
1179
1180   ipsec_ah_backend_t *ab;
1181   ipsec_esp_backend_t *eb;
1182   /* *INDENT-OFF* */
1183   pool_foreach (ab, im->ah_backends, {
1184     vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1185     clib_memset (mp, 0, sizeof (*mp));
1186     mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1187     mp->context = context;
1188     snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (ab->name),
1189               ab->name);
1190     mp->protocol = ntohl (IPSEC_API_PROTO_AH);
1191     mp->index = ab - im->ah_backends;
1192     mp->active = mp->index == im->ah_current_backend ? 1 : 0;
1193     vl_api_send_msg (rp, (u8 *)mp);
1194   });
1195   pool_foreach (eb, im->esp_backends, {
1196     vl_api_ipsec_backend_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
1197     clib_memset (mp, 0, sizeof (*mp));
1198     mp->_vl_msg_id = ntohs (VL_API_IPSEC_BACKEND_DETAILS);
1199     mp->context = context;
1200     snprintf ((char *)mp->name, sizeof (mp->name), "%.*s", vec_len (eb->name),
1201               eb->name);
1202     mp->protocol = ntohl (IPSEC_API_PROTO_ESP);
1203     mp->index = eb - im->esp_backends;
1204     mp->active = mp->index == im->esp_current_backend ? 1 : 0;
1205     vl_api_send_msg (rp, (u8 *)mp);
1206   });
1207   /* *INDENT-ON* */
1208 }
1209
1210 static void
1211 vl_api_ipsec_select_backend_t_handler (vl_api_ipsec_select_backend_t * mp)
1212 {
1213   ipsec_main_t *im = &ipsec_main;
1214   vl_api_ipsec_select_backend_reply_t *rmp;
1215   ipsec_protocol_t protocol;
1216   int rv = 0;
1217   if (pool_elts (im->sad) > 0)
1218     {
1219       rv = VNET_API_ERROR_INSTANCE_IN_USE;
1220       goto done;
1221     }
1222
1223   rv = ipsec_proto_decode (mp->protocol, &protocol);
1224
1225   if (rv)
1226     goto done;
1227
1228 #if WITH_LIBSSL > 0
1229   switch (protocol)
1230     {
1231     case IPSEC_PROTOCOL_ESP:
1232       if (pool_is_free_index (im->esp_backends, mp->index))
1233         {
1234           rv = VNET_API_ERROR_INVALID_VALUE;
1235           break;
1236         }
1237       ipsec_select_esp_backend (im, mp->index);
1238       break;
1239     case IPSEC_PROTOCOL_AH:
1240       if (pool_is_free_index (im->ah_backends, mp->index))
1241         {
1242           rv = VNET_API_ERROR_INVALID_VALUE;
1243           break;
1244         }
1245       ipsec_select_ah_backend (im, mp->index);
1246       break;
1247     default:
1248       rv = VNET_API_ERROR_INVALID_VALUE;
1249       break;
1250     }
1251 #else
1252   clib_warning ("unimplemented");       /* FIXME */
1253 #endif
1254 done:
1255   REPLY_MACRO (VL_API_IPSEC_SELECT_BACKEND_REPLY);
1256 }
1257
1258 static clib_error_t *
1259 ipsec_api_hookup (vlib_main_t * vm)
1260 {
1261   api_main_t *am = &api_main;
1262
1263 #define _(N,n)                                                  \
1264     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1265                            vl_api_##n##_t_handler,              \
1266                            vl_noop_handler,                     \
1267                            vl_api_##n##_t_endian,               \
1268                            vl_api_##n##_t_print,                \
1269                            sizeof(vl_api_##n##_t), 1);
1270   foreach_vpe_api_msg;
1271 #undef _
1272
1273   /*
1274    * Set up the (msg_name, crc, message-id) table
1275    */
1276   setup_message_id_table (am);
1277
1278   return 0;
1279 }
1280
1281 VLIB_API_INIT_FUNCTION (ipsec_api_hookup);
1282
1283 /*
1284  * fd.io coding-style-patch-verification: ON
1285  *
1286  * Local Variables:
1287  * eval: (c-set-style "gnu")
1288  * End:
1289  */