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