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